KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > ContentType


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  * @(#)ContentType.java 1.9 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.internet;
29
30 import javax.mail.*;
31 import java.util.*;
32 import java.io.*;
33
34 /**
35  * This class represents a MIME ContentType value. It provides
36  * methods to parse a ContentType string into individual components
37  * and to generate a MIME style ContentType string.
38  *
39  * @version 1.9, 05/08/29
40  * @author John Mani
41  */

42
43 public class ContentType {
44
45     private String JavaDoc primaryType; // primary type
46
private String JavaDoc subType; // subtype
47
private ParameterList JavaDoc list; // parameter list
48

49     /**
50      * No-arg Constructor.
51      */

52     public ContentType() { }
53
54     /**
55      * Constructor.
56      *
57      * @param primaryType primary type
58      * @param subType subType
59      * @param list ParameterList
60      */

61     public ContentType(String JavaDoc primaryType, String JavaDoc subType,
62             ParameterList JavaDoc list) {
63     this.primaryType = primaryType;
64     this.subType = subType;
65     this.list = list;
66     }
67
68     /**
69      * Constructor that takes a Content-Type string. The String
70      * is parsed into its constituents: primaryType, subType
71      * and parameters. A ParseException is thrown if the parse fails.
72      *
73      * @param s the Content-Type string.
74      * @exception ParseException if the parse fails.
75      */

76     public ContentType(String JavaDoc s) throws ParseException JavaDoc {
77     HeaderTokenizer JavaDoc h = new HeaderTokenizer JavaDoc(s, HeaderTokenizer.MIME);
78     HeaderTokenizer.Token JavaDoc tk;
79
80     // First "type" ..
81
tk = h.next();
82     if (tk.getType() != HeaderTokenizer.Token.ATOM)
83         throw new ParseException JavaDoc();
84     primaryType = tk.getValue();
85
86     // The '/' separator ..
87
tk = h.next();
88     if ((char)tk.getType() != '/')
89         throw new ParseException JavaDoc();
90
91     // Then "subType" ..
92
tk = h.next();
93     if (tk.getType() != HeaderTokenizer.Token.ATOM)
94         throw new ParseException JavaDoc();
95     subType = tk.getValue();
96
97     // Finally parameters ..
98
String JavaDoc rem = h.getRemainder();
99     if (rem != null)
100         list = new ParameterList JavaDoc(rem);
101     }
102
103     /**
104      * Return the primary type.
105      * @return the primary type
106      */

107     public String JavaDoc getPrimaryType() {
108     return primaryType;
109     }
110
111     /**
112      * Return the subType.
113      * @return the subType
114      */

115     public String JavaDoc getSubType() {
116     return subType;
117     }
118
119     /**
120      * Return the MIME type string, without the parameters.
121      * The returned value is basically the concatenation of
122      * the primaryType, the '/' character and the secondaryType.
123      *
124      * @return the type
125      */

126     public String JavaDoc getBaseType() {
127     return primaryType + '/' + subType;
128     }
129
130     /**
131      * Return the specified parameter value. Returns <code>null</code>
132      * if this parameter is absent.
133      * @return parameter value
134      */

135     public String JavaDoc getParameter(String JavaDoc name) {
136     if (list == null)
137         return null;
138
139     return list.get(name);
140     }
141
142     /**
143      * Return a ParameterList object that holds all the available
144      * parameters. Returns null if no parameters are available.
145      *
146      * @return ParameterList
147      */

148     public ParameterList JavaDoc getParameterList() {
149     return list;
150     }
151
152     /**
153      * Set the primary type. Overrides existing primary type.
154      * @param primaryType primary type
155      */

156     public void setPrimaryType(String JavaDoc primaryType) {
157     this.primaryType = primaryType;
158     }
159
160     /**
161      * Set the subType. Replaces the existing subType.
162      * @param subType the subType
163      */

164     public void setSubType(String JavaDoc subType) {
165     this.subType = subType;
166     }
167
168     /**
169      * Set the specified parameter. If this parameter already exists,
170      * it is replaced by this new value.
171      *
172      * @param name parameter name
173      * @param value parameter value
174      */

175     public void setParameter(String JavaDoc name, String JavaDoc value) {
176     if (list == null)
177         list = new ParameterList JavaDoc();
178
179     list.set(name, value);
180     }
181
182     /**
183      * Set a new ParameterList.
184      * @param list ParameterList
185      */

186     public void setParameterList(ParameterList JavaDoc list) {
187     this.list = list;
188     }
189
190     /**
191      * Retrieve a RFC2045 style string representation of
192      * this Content-Type. Returns <code>null</code> if
193      * the conversion failed.
194      *
195      * @return RFC2045 style string
196      */

197     public String JavaDoc toString() {
198     if (primaryType == null || subType == null) // need both
199
return null;
200
201     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
202     sb.append(primaryType).append('/').append(subType);
203     if (list != null)
204             // append the parameter list
205
// use the length of the string buffer + the length of
206
// the header name formatted as follows "Content-Type: "
207
sb.append(list.toString(sb.length() + 14));
208     
209     return sb.toString();
210     }
211
212     /**
213      * Match with the specified ContentType object. This method
214      * compares <strong>only the <code>primaryType</code> and
215      * <code>subType</code> </strong>. The parameters of both operands
216      * are ignored. <p>
217      *
218      * For example, this method will return <code>true</code> when
219      * comparing the ContentTypes for <strong>"text/plain"</strong>
220      * and <strong>"text/plain; charset=foobar"</strong>.
221      *
222      * If the <code>subType</code> of either operand is the special
223      * character '*', then the subtype is ignored during the match.
224      * For example, this method will return <code>true</code> when
225      * comparing the ContentTypes for <strong>"text/plain"</strong>
226      * and <strong>"text/*" </strong>
227      *
228      * @param cType ContentType to compare this against
229      */

230     public boolean match(ContentType JavaDoc cType) {
231     // Match primaryType
232
if (!primaryType.equalsIgnoreCase(cType.getPrimaryType()))
233         return false;
234     
235     String JavaDoc sType = cType.getSubType();
236
237     // If either one of the subTypes is wildcarded, return true
238
if ((subType.charAt(0) == '*') || (sType.charAt(0) == '*'))
239         return true;
240     
241     // Match subType
242
if (!subType.equalsIgnoreCase(sType))
243         return false;
244
245     return true;
246     }
247
248     /**
249      * Match with the specified content-type string. This method
250      * compares <strong>only the <code>primaryType</code> and
251      * <code>subType</code> </strong>.
252      * The parameters of both operands are ignored. <p>
253      *
254      * For example, this method will return <code>true</code> when
255      * comparing the ContentType for <strong>"text/plain"</strong>
256      * with <strong>"text/plain; charset=foobar"</strong>.
257      *
258      * If the <code>subType</code> of either operand is the special
259      * character '*', then the subtype is ignored during the match.
260      * For example, this method will return <code>true</code> when
261      * comparing the ContentType for <strong>"text/plain"</strong>
262      * with <strong>"text/*" </strong>
263      */

264     public boolean match(String JavaDoc s) {
265     try {
266         return match(new ContentType JavaDoc(s));
267     } catch (ParseException JavaDoc pex) {
268         return false;
269     }
270     }
271 }
272
Popular Tags