KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
39 import java.util.Vector JavaDoc;
40
41
42 /**
43  * A mimepart including a MimeHeader.
44  *
45  *
46  * @author Timo Stich <tstich@users.sourceforge.net>
47  */

48 public class MimePart {
49     
50     private List JavaDoc childs;
51     private MimePart parent;
52     protected MimeHeader header;
53     protected int size;
54
55     /**
56      * Constructs the MimePart.
57      */

58     public MimePart() {
59         this( new MimeHeader() );
60     }
61     
62     /**
63      * Constructs the MimePart.
64      *
65      * @param header
66      */

67     public MimePart(MimeHeader header ) {
68         this.header = header;
69         childs = new Vector JavaDoc();
70     }
71     
72     /**
73      * Gets the address.
74      * @return Returns a Integer[]
75      */

76     public Integer JavaDoc[] getAddress() {
77         List JavaDoc result = new Vector JavaDoc();
78
79         if (parent == null)
80             result.add(new Integer JavaDoc(0));
81         else {
82             MimePart nextParent = parent;
83             MimePart nextChild = this;
84
85             while (nextParent != null) {
86                 result.add(0, new Integer JavaDoc( nextParent.getNumber(nextChild) ));
87
88                 nextChild = nextParent;
89                 nextParent = nextParent.getParent();
90             }
91         }
92
93         Integer JavaDoc[] returnValue = new Integer JavaDoc[result.size()];
94             
95         for( int i=0; i<result.size(); i++ )
96             returnValue[i] = (Integer JavaDoc) result.get(i);
97
98         return returnValue;
99     }
100
101     /**
102      * Returns the parent.
103      * @return MimeTreeNode
104      */

105     public MimePart getParent() {
106         return parent;
107     }
108
109     /**
110      * Sets the parent.
111      * @param parent The parent to set
112      */

113     public void setParent(MimePart parent) {
114         this.parent = parent;
115     }
116
117     /**
118      * @return the number of child MimeParts.
119      */

120     public int countChilds() {
121         return childs.size();
122     }
123
124     /**
125      * @param nr
126      * @return the Child
127      */

128     public MimePart getChild(int nr) {
129         return (MimePart) childs.get(nr);
130     }
131
132     /**
133      * Add a Child MimePart to this MimePart.
134      *
135      * @param child
136      */

137     public void addChild(MimePart child) {
138         if( child == null ) return;
139         childs.add(child);
140         child.setParent(this);
141     }
142
143     /**
144      * Counts the number of unique MimeParts. Multipart/Alternative
145      * MimeParts count as one.
146      *
147      * @return count the number of unique MimeParts.
148      */

149     public int count() {
150         // If this is a Multipart/Alternative then return also only 1
151
if (header.getMimeType().getSubtype().equals("alternative"))
152             return 1;
153
154         if (countChilds() == 0)
155             return 1;
156
157         int result = 0;
158
159         for (int i = 0; i < countChilds(); i++) {
160             result += getChild(i).count();
161         }
162
163         return result;
164     }
165
166     /**
167      * @param child
168      * @return the number of the Child MimePart
169      */

170     public int getNumber(MimePart child) {
171         return childs.indexOf(child);
172     }
173
174     /**
175      * @return all Child MimeParts in one List
176      */

177     public List JavaDoc getChilds() {
178         return childs;
179     }
180
181     /**
182      * @return the Header
183      */

184     public MimeHeader getHeader() {
185         return header;
186     }
187
188     /**
189      * Method setHeader.
190      * @param h
191      */

192     public void setHeader(MimeHeader h) {
193         header = h;
194     }
195     
196     /**
197      * Set the Size in bytes.
198      *
199      * @param size
200      */

201     public void setSize(int size) {
202         this.size = size;
203     }
204
205     /**
206      * @return the size in bytes
207      */

208     public int getSize() {
209         return size;
210     }
211
212 }
213
Popular Tags