KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > MimeType


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)MimeType.java 1.19 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.io.ObjectOutput JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.*;
34
35 /**
36  * A Multipurpose Internet Mail Extension (MIME) type, as defined
37  * in RFC 2045 and 2046.
38  */

39 public class MimeType implements Externalizable {
40
41     private String JavaDoc primaryType;
42     private String JavaDoc subType;
43     private MimeTypeParameterList JavaDoc parameters;
44
45     /**
46      * A string that holds all the special chars.
47      */

48     private static final String JavaDoc TSPECIALS = "()<>@,;:/[]?=\\\"";
49
50     /**
51      * Default constructor.
52      */

53     public MimeType() {
54         primaryType = "application";
55         subType = "*";
56         parameters = new MimeTypeParameterList JavaDoc();
57     }
58
59     /**
60      * Constructor that builds a MimeType from a String.
61      *
62      * @param rawdata the MIME type string
63      */

64     public MimeType(String JavaDoc rawdata) throws MimeTypeParseException JavaDoc {
65         parse(rawdata);
66     }
67
68     /**
69      * Constructor that builds a MimeType with the given primary and sub type
70      * but has an empty parameter list.
71      *
72      * @param primary the primary MIME type
73      * @param sub the MIME sub-type
74      * @exception MimeTypeParseException if the primary type or subtype
75      * is not a valid token
76      */

77     public MimeType(String JavaDoc primary, String JavaDoc sub) throws MimeTypeParseException JavaDoc {
78         // check to see if primary is valid
79
if (isValidToken(primary)) {
80             primaryType = primary.toLowerCase();
81         } else {
82             throw new MimeTypeParseException JavaDoc("Primary type is invalid.");
83         }
84
85         // check to see if sub is valid
86
if (isValidToken(sub)) {
87             subType = sub.toLowerCase();
88         } else {
89             throw new MimeTypeParseException JavaDoc("Sub type is invalid.");
90         }
91
92         parameters = new MimeTypeParameterList JavaDoc();
93     }
94
95     /**
96      * A routine for parsing the MIME type out of a String.
97      */

98     private void parse(String JavaDoc rawdata) throws MimeTypeParseException JavaDoc {
99         int slashIndex = rawdata.indexOf('/');
100         int semIndex = rawdata.indexOf(';');
101         if ((slashIndex < 0) && (semIndex < 0)) {
102             // neither character is present, so treat it
103
// as an error
104
throw new MimeTypeParseException JavaDoc("Unable to find a sub type.");
105         } else if ((slashIndex < 0) && (semIndex >= 0)) {
106             // we have a ';' (and therefore a parameter list),
107
// but no '/' indicating a sub type is present
108
throw new MimeTypeParseException JavaDoc("Unable to find a sub type.");
109         } else if ((slashIndex >= 0) && (semIndex < 0)) {
110             // we have a primary and sub type but no parameter list
111
primaryType = rawdata.substring(0, slashIndex).trim().toLowerCase();
112             subType = rawdata.substring(slashIndex + 1).trim().toLowerCase();
113             parameters = new MimeTypeParameterList JavaDoc();
114         } else if (slashIndex < semIndex) {
115             // we have all three items in the proper sequence
116
primaryType = rawdata.substring(0, slashIndex).trim().toLowerCase();
117             subType = rawdata.substring(slashIndex + 1,
118                         semIndex).trim().toLowerCase();
119             parameters = new MimeTypeParameterList JavaDoc(rawdata.substring(semIndex));
120         } else {
121             // we have a ';' lexically before a '/' which means we
122
// have a primary type and a parameter list but no sub type
123
throw new MimeTypeParseException JavaDoc("Unable to find a sub type.");
124         }
125
126         // now validate the primary and sub types
127

128         // check to see if primary is valid
129
if (!isValidToken(primaryType))
130             throw new MimeTypeParseException JavaDoc("Primary type is invalid.");
131
132         // check to see if sub is valid
133
if (!isValidToken(subType))
134             throw new MimeTypeParseException JavaDoc("Sub type is invalid.");
135     }
136
137     /**
138      * Retrieve the primary type of this object.
139      *
140      * @return the primary MIME type
141      */

142     public String JavaDoc getPrimaryType() {
143         return primaryType;
144     }
145
146     /**
147      * Set the primary type for this object to the given String.
148      *
149      * @param primary the primary MIME type
150      * @exception MimeTypeParseException if the primary type
151      * is not a valid token
152      */

153     public void setPrimaryType(String JavaDoc primary) throws MimeTypeParseException JavaDoc {
154         // check to see if primary is valid
155
if (!isValidToken(primaryType))
156             throw new MimeTypeParseException JavaDoc("Primary type is invalid.");
157         primaryType = primary.toLowerCase();
158     }
159
160     /**
161      * Retrieve the subtype of this object.
162      *
163      * @return the MIME subtype
164      */

165     public String JavaDoc getSubType() {
166         return subType;
167     }
168
169     /**
170      * Set the subtype for this object to the given String.
171      *
172      * @param sub the MIME subtype
173      * @exception MimeTypeParseException if the subtype
174      * is not a valid token
175      */

176     public void setSubType(String JavaDoc sub) throws MimeTypeParseException JavaDoc {
177         // check to see if sub is valid
178
if (!isValidToken(subType))
179             throw new MimeTypeParseException JavaDoc("Sub type is invalid.");
180         subType = sub.toLowerCase();
181     }
182
183     /**
184      * Retrieve this object's parameter list.
185      *
186      * @return a MimeTypeParameterList object representing the parameters
187      */

188     public MimeTypeParameterList JavaDoc getParameters() {
189         return parameters;
190     }
191
192     /**
193      * Retrieve the value associated with the given name, or null if there
194      * is no current association.
195      *
196      * @param name the parameter name
197      * @return the paramter's value
198      */

199     public String JavaDoc getParameter(String JavaDoc name) {
200         return parameters.get(name);
201     }
202
203     /**
204      * Set the value to be associated with the given name, replacing
205      * any previous association.
206      *
207      * @param name the parameter name
208      * @param value the paramter's value
209      */

210     public void setParameter(String JavaDoc name, String JavaDoc value) {
211         parameters.set(name, value);
212     }
213
214     /**
215      * Remove any value associated with the given name.
216      *
217      * @param name the parameter name
218      */

219     public void removeParameter(String JavaDoc name) {
220         parameters.remove(name);
221     }
222
223     /**
224      * Return the String representation of this object.
225      */

226     public String JavaDoc toString() {
227         return getBaseType() + parameters.toString();
228     }
229
230     /**
231      * Return a String representation of this object
232      * without the parameter list.
233      *
234      * @return the MIME type and sub-type
235      */

236     public String JavaDoc getBaseType() {
237         return primaryType + "/" + subType;
238     }
239
240     /**
241      * Determine if the primary and sub type of this object is
242      * the same as what is in the given type.
243      *
244      * @param type the MimeType object to compare with
245      * @return true if they match
246      */

247     public boolean match(MimeType JavaDoc type) {
248         return primaryType.equals(type.getPrimaryType())
249                     && (subType.equals("*")
250                             || type.getSubType().equals("*")
251                             || (subType.equals(type.getSubType())));
252     }
253
254     /**
255      * Determine if the primary and sub type of this object is
256      * the same as the content type described in rawdata.
257      *
258      * @param rawdata the MIME type string to compare with
259      * @return true if they match
260      */

261     public boolean match(String JavaDoc rawdata) throws MimeTypeParseException JavaDoc {
262         return match(new MimeType JavaDoc(rawdata));
263     }
264
265     /**
266      * The object implements the writeExternal method to save its contents
267      * by calling the methods of DataOutput for its primitive values or
268      * calling the writeObject method of ObjectOutput for objects, strings
269      * and arrays.
270      *
271      * @param out the ObjectOutput object to write to
272      * @exception IOException Includes any I/O exceptions that may occur
273      */

274     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
275         out.writeUTF(toString());
276     out.flush();
277     }
278
279     /**
280      * The object implements the readExternal method to restore its
281      * contents by calling the methods of DataInput for primitive
282      * types and readObject for objects, strings and arrays. The
283      * readExternal method must read the values in the same sequence
284      * and with the same types as were written by writeExternal.
285      *
286      * @param in the ObjectInput object to read from
287      * @exception ClassNotFoundException If the class for an object being
288      * restored cannot be found.
289      */

290     public void readExternal(ObjectInput JavaDoc in)
291                 throws IOException JavaDoc, ClassNotFoundException JavaDoc {
292         try {
293             parse(in.readUTF());
294         } catch (MimeTypeParseException JavaDoc e) {
295             throw new IOException JavaDoc(e.toString());
296         }
297     }
298
299     // below here be scary parsing related things
300

301     /**
302      * Determine whether or not a given character belongs to a legal token.
303      */

304     private static boolean isTokenChar(char c) {
305         return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
306     }
307
308     /**
309      * Determine whether or not a given string is a legal token.
310      */

311     private boolean isValidToken(String JavaDoc s) {
312         int len = s.length();
313         if (len > 0) {
314             for (int i = 0; i < len; ++i) {
315                 char c = s.charAt(i);
316                 if (!isTokenChar(c)) {
317                     return false;
318                 }
319             }
320             return true;
321         } else {
322             return false;
323         }
324     }
325
326     /**
327      * A simple parser test,
328      * for debugging...
329      *
330     public static void main(String[] args)
331                 throws MimeTypeParseException, IOException {
332         for (int i = 0; i < args.length; ++i) {
333             System.out.println("Original: " + args[i]);
334
335             MimeType type = new MimeType(args[i]);
336
337             System.out.println("Short: " + type.getBaseType());
338             System.out.println("Parsed: " + type.toString());
339             System.out.println();
340         }
341     }
342     */

343 }
344
Popular Tags