1 36 package org.columba.ristretto.message; 37 38 import java.lang.reflect.Array ; 39 import java.util.LinkedList ; 40 import java.util.List ; 41 42 47 public class MimeTree { 48 MimePart rootMimeNode; 49 50 55 public MimeTree(MimePart root) { 56 rootMimeNode = root; 57 } 58 59 63 public MimePart get(int number) { 64 List leafs = getAllLeafs(); 65 66 return (MimePart) leafs.get(number); 67 } 68 69 74 public int count() { 75 if (rootMimeNode == null) 76 return 0; 77 return rootMimeNode.count(); 78 } 79 80 83 public void clear() { 84 rootMimeNode = null; 85 } 86 87 90 public List getAllLeafs() { 91 return getLeafs(rootMimeNode); 92 } 93 94 102 public MimePart getFromAddress(Integer [] address) { 103 if(( Array.getLength( address ) == 1 ) && ( address[0].intValue() == 0 ) && (rootMimeNode.countChilds() == 0) ) 105 return rootMimeNode; 106 107 MimePart actPart = rootMimeNode; 108 109 for (int i = 0; i < Array.getLength(address); i++) { 110 actPart = actPart.getChild(address[i].intValue()); 111 } 112 113 return actPart; 114 } 115 116 124 public MimePart getFirstTextPart(String preferedSubtype) { 125 MimePart textPart = getFirstLeafWithContentType(rootMimeNode, "text"); 126 127 if (textPart == null) 129 return null; 130 131 if( preferedSubtype == null ) 133 return textPart; 134 135 MimeType type = textPart.getHeader().getMimeType(); 136 if (type.getSubtype().equals(preferedSubtype)) 138 return textPart; 139 140 142 MimePart parent = (MimePart) textPart.getParent(); 144 145 if (parent != null) { 146 if (parent.getHeader().getMimeType().getSubtype().equals("alternative")) { 147 148 149 MimePart nextTextPart; 150 List alternatives = 151 getLeafsWithContentType(parent, "text"); 152 153 154 155 for (int i = 1; i < alternatives.size(); i++) { 157 158 nextTextPart = (MimePart) alternatives.get(i); 159 160 161 if (nextTextPart 162 .getHeader() 163 .getMimeType().getSubtype() 164 .equals(preferedSubtype)) 165 return nextTextPart; 166 } 167 168 } 170 } 171 172 return textPart; 173 } 174 175 183 public MimePart getFirstLeafWithContentType( 184 MimePart root, 185 String contentType) { 186 MimePart result = null; 187 188 if (root.countChilds() > 0) { 189 190 for (int i = 0; i < root.countChilds(); i++) { 191 result = 192 getFirstLeafWithContentType( 193 (MimePart) root.getChild(i), 194 contentType); 195 if (result != null) 196 return result; 197 } 198 199 } else { 200 if (root.getHeader().getMimeType().getType().equals(contentType)) 201 return root; 202 } 203 204 return null; 205 } 206 207 214 public List getLeafsWithContentType( 215 MimePart root, 216 String contentType) { 217 218 219 LinkedList result = new LinkedList (); 220 221 if (root.countChilds() > 0) { 222 for (int i = 0; i < root.countChilds(); i++) { 223 result.addAll( 224 getLeafsWithContentType( 225 (MimePart) root.getChild(i), 226 contentType)); 227 } 228 229 } else { 230 if (root.getHeader().getMimeType().getType().equals(contentType)) 231 result.add(root); 232 } 233 234 return result; 235 } 236 237 243 public List getLeafs(MimePart root) { 244 LinkedList result = new LinkedList (); 245 246 if (root.countChilds() > 0) { 247 248 for (int i = 0; i < root.countChilds(); i++) { 249 result.addAll(getLeafs((MimePart) root.getChild(i))); 250 } 251 252 } else { 253 result.add(root); 254 } 255 256 return result; 257 } 258 259 263 public MimePart getRootMimeNode() { 264 return rootMimeNode; 265 } 266 267 271 public void setRootMimeNode(MimePart rootMimeNode) { 272 this.rootMimeNode = rootMimeNode; 273 } 274 275 } 276 | Popular Tags |