KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > activation > MimeTypeParameterList


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  * @(#)MimeTypeParameterList.java 1.11 05/11/16
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.activation;
29
30 import java.util.Hashtable JavaDoc;
31 import java.util.Enumeration JavaDoc;
32
33 /**
34  * A parameter list of a MimeType
35  * as defined in RFC 2045 and 2046. The Primary type of the
36  * object must already be stripped off.
37  *
38  * @see javax.activation.MimeType
39  */

40 public class MimeTypeParameterList {
41     private Hashtable JavaDoc parameters;
42
43     /**
44      * A string that holds all the special chars.
45      */

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

52     public MimeTypeParameterList() {
53         parameters = new Hashtable JavaDoc();
54     }
55
56     /**
57      * Constructs a new MimeTypeParameterList with the passed in data.
58      *
59      * @param parameterList an RFC 2045, 2046 compliant parameter list.
60      */

61     public MimeTypeParameterList(String JavaDoc parameterList)
62                     throws MimeTypeParseException JavaDoc {
63         parameters = new Hashtable JavaDoc();
64
65         // now parse rawdata
66
parse(parameterList);
67     }
68
69     /**
70      * A routine for parsing the parameter list out of a String.
71      *
72      * @param parameterList an RFC 2045, 2046 compliant parameter list.
73      */

74     protected void parse(String JavaDoc parameterList) throws MimeTypeParseException JavaDoc {
75     if (parameterList == null)
76         return;
77
78         int length = parameterList.length();
79         if (length <= 0)
80         return;
81
82     int i;
83     char c;
84     for (i = skipWhiteSpace(parameterList, 0);
85         i < length && (c = parameterList.charAt(i)) == ';';
86         i = skipWhiteSpace(parameterList, i)) {
87         int lastIndex;
88         String JavaDoc name;
89         String JavaDoc value;
90
91         // eat the ';'
92
i++;
93
94         // now parse the parameter name
95

96         // skip whitespace
97
i = skipWhiteSpace(parameterList, i);
98
99         // tolerate trailing semicolon, even though it violates the spec
100
if (i >= length)
101         return;
102
103         // find the end of the token char run
104
lastIndex = i;
105         while ((i < length) && isTokenChar(parameterList.charAt(i)))
106         i++;
107
108         name = parameterList.substring(lastIndex, i).toLowerCase();
109
110         // now parse the '=' that separates the name from the value
111
i = skipWhiteSpace(parameterList, i);
112
113         if (i >= length || parameterList.charAt(i) != '=')
114         throw new MimeTypeParseException JavaDoc(
115             "Couldn't find the '=' that separates a " +
116             "parameter name from its value.");
117
118         // eat it and parse the parameter value
119
i++;
120         i = skipWhiteSpace(parameterList, i);
121
122         if (i >= length)
123         throw new MimeTypeParseException JavaDoc(
124             "Couldn't find a value for parameter named " + name);
125
126         // now find out whether or not we have a quoted value
127
c = parameterList.charAt(i);
128         if (c == '"') {
129         // yup it's quoted so eat it and capture the quoted string
130
i++;
131         if (i >= length)
132             throw new MimeTypeParseException JavaDoc(
133                 "Encountered unterminated quoted parameter value.");
134
135         lastIndex = i;
136
137         // find the next unescaped quote
138
while (i < length) {
139             c = parameterList.charAt(i);
140             if (c == '"')
141             break;
142             if (c == '\\') {
143             // found an escape sequence
144
// so skip this and the
145
// next character
146
i++;
147             }
148             i++;
149         }
150         if (c != '"')
151             throw new MimeTypeParseException JavaDoc(
152             "Encountered unterminated quoted parameter value.");
153
154         value = unquote(parameterList.substring(lastIndex, i));
155         // eat the quote
156
i++;
157         } else if (isTokenChar(c)) {
158         // nope it's an ordinary token so it
159
// ends with a non-token char
160
lastIndex = i;
161         while (i < length && isTokenChar(parameterList.charAt(i)))
162             i++;
163         value = parameterList.substring(lastIndex, i);
164         } else {
165         // it ain't a value
166
throw new MimeTypeParseException JavaDoc(
167             "Unexpected character encountered at index " + i);
168         }
169
170         // now put the data into the hashtable
171
parameters.put(name, value);
172     }
173     if (i < length) {
174         throw new MimeTypeParseException JavaDoc(
175         "More characters encountered in input than expected.");
176     }
177     }
178
179     /**
180      * Return the number of name-value pairs in this list.
181      *
182      * @return the number of parameters
183      */

184     public int size() {
185         return parameters.size();
186     }
187
188     /**
189      * Determine whether or not this list is empty.
190      *
191      * @return true if there are no parameters
192      */

193     public boolean isEmpty() {
194         return parameters.isEmpty();
195     }
196
197     /**
198      * Retrieve the value associated with the given name, or null if there
199      * is no current association.
200      *
201      * @param name the parameter name
202      * @return the parameter's value
203      */

204     public String JavaDoc get(String JavaDoc name) {
205         return (String JavaDoc)parameters.get(name.trim().toLowerCase());
206     }
207
208     /**
209      * Set the value to be associated with the given name, replacing
210      * any previous association.
211      *
212      * @param name the parameter name
213      * @param value the parameter's value
214      */

215     public void set(String JavaDoc name, String JavaDoc value) {
216         parameters.put(name.trim().toLowerCase(), value);
217     }
218
219     /**
220      * Remove any value associated with the given name.
221      *
222      * @param name the parameter name
223      */

224     public void remove(String JavaDoc name) {
225         parameters.remove(name.trim().toLowerCase());
226     }
227
228     /**
229      * Retrieve an enumeration of all the names in this list.
230      *
231      * @return an enumeration of all parameter names
232      */

233     public Enumeration JavaDoc getNames() {
234         return parameters.keys();
235     }
236
237     /**
238      * Return a string representation of this object.
239      */

240     public String JavaDoc toString() {
241         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
242         buffer.ensureCapacity(parameters.size() * 16);
243             // heuristic: 8 characters per field
244

245         Enumeration JavaDoc keys = parameters.keys();
246         while (keys.hasMoreElements()) {
247             String JavaDoc key = (String JavaDoc)keys.nextElement();
248             buffer.append("; ");
249             buffer.append(key);
250             buffer.append('=');
251         buffer.append(quote((String JavaDoc)parameters.get(key)));
252         }
253
254         return buffer.toString();
255     }
256
257     // below here be scary parsing related things
258

259     /**
260      * Determine whether or not a given character belongs to a legal token.
261      */

262     private static boolean isTokenChar(char c) {
263         return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
264     }
265
266     /**
267      * return the index of the first non white space character in
268      * rawdata at or after index i.
269      */

270     private static int skipWhiteSpace(String JavaDoc rawdata, int i) {
271         int length = rawdata.length();
272     while ((i < length) && Character.isWhitespace(rawdata.charAt(i)))
273         i++;
274         return i;
275     }
276
277     /**
278      * A routine that knows how and when to quote and escape the given value.
279      */

280     private static String JavaDoc quote(String JavaDoc value) {
281         boolean needsQuotes = false;
282
283         // check to see if we actually have to quote this thing
284
int length = value.length();
285         for (int i = 0; (i < length) && !needsQuotes; i++) {
286             needsQuotes = !isTokenChar(value.charAt(i));
287         }
288
289         if (needsQuotes) {
290             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
291             buffer.ensureCapacity((int)(length * 1.5));
292
293             // add the initial quote
294
buffer.append('"');
295
296             // add the properly escaped text
297
for (int i = 0; i < length; ++i) {
298                 char c = value.charAt(i);
299                 if ((c == '\\') || (c == '"'))
300                     buffer.append('\\');
301                 buffer.append(c);
302             }
303
304             // add the closing quote
305
buffer.append('"');
306
307             return buffer.toString();
308         } else {
309             return value;
310         }
311     }
312
313     /**
314      * A routine that knows how to strip the quotes and
315      * escape sequences from the given value.
316      */

317     private static String JavaDoc unquote(String JavaDoc value) {
318         int valueLength = value.length();
319         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
320         buffer.ensureCapacity(valueLength);
321
322         boolean escaped = false;
323         for (int i = 0; i < valueLength; ++i) {
324             char currentChar = value.charAt(i);
325             if (!escaped && (currentChar != '\\')) {
326                 buffer.append(currentChar);
327             } else if (escaped) {
328                 buffer.append(currentChar);
329                 escaped = false;
330             } else {
331                 escaped = true;
332             }
333         }
334
335         return buffer.toString();
336     }
337 }
338
Popular Tags