KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > MimeTree


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message;
37
38 import java.lang.reflect.Array JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * The BodyStrcuture of a MIME message.
44  *
45  * @author Timo Stich <tstich@users.sourceforge.net>
46  */

47 public class MimeTree {
48     MimePart rootMimeNode;
49
50     /**
51      * Constructs the MimeTree.
52      *
53      * @param root
54      */

55     public MimeTree(MimePart root) {
56         rootMimeNode = root;
57     }
58
59     /**
60      * @param number
61      * @return the MimePart
62      */

63     public MimePart get(int number) {
64         List JavaDoc leafs = getAllLeafs();
65
66         return (MimePart) leafs.get(number);
67     }
68
69     /**
70      *
71      *
72      * @return the number of MimeParts.
73      */

74     public int count() {
75         if (rootMimeNode == null)
76             return 0;
77         return rootMimeNode.count();
78     }
79
80     /**
81      * Clear the MimeTree.
82      */

83     public void clear() {
84         rootMimeNode = null;
85     }
86
87     /**
88      * @return all leaf MimeParts in one list
89      */

90     public List JavaDoc getAllLeafs() {
91         return getLeafs(rootMimeNode);
92     }
93
94     /**
95      * Gets the MimePart with the specified address.
96      * The address is compatible with the address defined
97      * in IMAP (RFC3501).
98      *
99      * @param address
100      * @return the MimePart
101      */

102     public MimePart getFromAddress(Integer JavaDoc[] address) {
103         // If Root-Address and Root has no nodes return rootNode
104
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     /**
117      * Gets the first MimePart that is of content type Text.
118      * If a prefered Subtype is specified first this is searched
119      * for. If none is found the first Text part is returned.
120      *
121      * @param preferedSubtype a preferred Subtype or <code>null</code>
122      * @return the first found Text MIME part.
123      */

124     public MimePart getFirstTextPart(String JavaDoc preferedSubtype) {
125         MimePart textPart = getFirstLeafWithContentType(rootMimeNode, "text");
126
127         // Have we found anything ?
128
if (textPart == null)
129             return null;
130
131         // If nothing prefered return found
132
if( preferedSubtype == null )
133             return textPart;
134
135         MimeType type = textPart.getHeader().getMimeType();
136         // Is it of prefered Subtype?
137
if (type.getSubtype().equals(preferedSubtype))
138             return textPart;
139
140         // Try to find better TextPart!
141

142         // Check if part of Multipart/Alternative
143
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 JavaDoc alternatives =
151                     getLeafsWithContentType(parent, "text");
152
153                 
154                 
155                 // We can leave the first one out because we checked earlier
156
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                 // Nothing better found -> return first found!
169
}
170         }
171
172         return textPart;
173     }
174
175     /**
176      * Gets the first Leaf MimePart with the specified
177      * ContentType.
178      *
179      * @param root
180      * @param contentType
181      * @return the MimePart
182      */

183     public MimePart getFirstLeafWithContentType(
184         MimePart root,
185         String JavaDoc 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     /**
208      * Collects all leaf MIME parts with the specified ContentType.
209      *
210      * @param root
211      * @param contentType
212      * @return the List of MIME parts.
213      */

214     public List JavaDoc getLeafsWithContentType(
215         MimePart root,
216         String JavaDoc contentType) {
217
218
219         LinkedList JavaDoc result = new LinkedList JavaDoc();
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     /**
238      * Get all Leafs.
239      *
240      * @param root
241      * @return all Leafs in one List
242      */

243     public List JavaDoc getLeafs(MimePart root) {
244         LinkedList JavaDoc result = new LinkedList JavaDoc();
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     /**
260      * Returns the rootMimeNode.
261      * @return MimePart
262      */

263     public MimePart getRootMimeNode() {
264         return rootMimeNode;
265     }
266
267     /**
268      * Sets the rootMimeNode.
269      * @param rootMimeNode The rootMimeNode to set
270      */

271     public void setRootMimeNode(MimePart rootMimeNode) {
272         this.rootMimeNode = rootMimeNode;
273     }
274
275 }
276
Popular Tags