1 package org.apache.turbine.services.mimetype.util;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.ArrayList;
58
59 /***
60 * This class is used to represent parsed MIME types.
61 * The representation is parsed from a string based
62 * representation of the MIME type, as defined in the RFC1345.
63 *
64 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
65 * @version $Id: MimeType.java,v 1.1.1.1 2001/08/16 05:09:08 jvanzyl Exp $
66 */
67 public class MimeType
68 implements Cloneable
69 {
70 /***
71 * A list of well known MIME types.
72 */
73 public static MimeType TEXT_HTML;
74 public static MimeType TEXT_WML;
75 public static MimeType TEXT_HDML;
76 public static MimeType TEXT_CHTML;
77 public static MimeType TEXT_PLAIN;
78 public static MimeType MULTIPART;
79 public static MimeType MULTIPART_FORM_DATA;
80 public static MimeType APPLICATION_POSTSCRIPT;
81 public static MimeType APPLICATION_OCTET_STREAM;
82 public static MimeType APPLICATION_X_JAVA_AGENT;
83 public static MimeType APPLICATION_X_WWW_FORM_URLENCODED;
84 public static MimeType MESSAGE_HTTP;
85 public static MimeType TEXT_CSS;
86 public static MimeType TEXT;
87 public static MimeType IMAGE_GIF;
88 public static MimeType IMAGE_JPEG;
89 public static MimeType IMAGE_WBMP;
90 static
91 {
92 TEXT_HTML =
93 new MimeType("text/html");
94 TEXT_WML =
95 new MimeType("text/vnd.wap.wml");
96 TEXT_HDML =
97 new MimeType("text/x-hdml");
98 TEXT_CHTML =
99 new MimeType("text/x-chtml");
100 TEXT_PLAIN =
101 new MimeType("text/plain");
102 MULTIPART =
103 new MimeType("multipart/*");
104 MULTIPART_FORM_DATA =
105 new MimeType("multipart/form-data");
106 APPLICATION_POSTSCRIPT =
107 new MimeType("application/postscript");
108 APPLICATION_OCTET_STREAM =
109 new MimeType("application/octet-stream");
110 APPLICATION_X_JAVA_AGENT =
111 new MimeType("application/x-java-agent");
112 APPLICATION_X_WWW_FORM_URLENCODED =
113 new MimeType("application/x-www-form-urlencoded");
114 MESSAGE_HTTP =
115 new MimeType("message/http");
116 TEXT_CSS =
117 new MimeType("text/css");
118 TEXT =
119 new MimeType("text/*");
120 IMAGE_GIF =
121 new MimeType("image/gif");
122 IMAGE_JPEG =
123 new MimeType("image/jpeg");
124 IMAGE_WBMP =
125 new MimeType("image/vnd.wap.wbmp");
126 }
127
128 /***
129 * MIME type matching constants.
130 */
131 public static final int NO_MATCH = 0;
132 public static final int MATCH_TYPE = 1;
133 public static final int MATCH_SUBTYPE = 2;
134 public static final int MATCH_SPECIFIC_SUBTYPE = 3;
135
136 /***
137 * A string representation of the main type.
138 */
139 private String mimeType;
140
141 /***
142 * A string representation of the subtype.
143 */
144 private String mimeSubtype;
145
146 /***
147 * Parameter names.
148 */
149 private String parameterNames[];
150
151 /***
152 * Parameter values.
153 */
154 private String parameterValues[];
155
156 /***
157 * A string representation of the MIME type.
158 */
159 private String mimeTypeString;
160
161 /***
162 * Constructs a new MIME type by parsing a specification string.
163 *
164 * @parameter spec a string representing a MIME type.
165 * @throws IllegalArgument for parsing errors.
166 */
167 public MimeType(String spec)
168 {
169 this(spec,true);
170 }
171
172 /***
173 * Constructs a new MIME type by parsing a specification string.
174 *
175 * @param spec a string representing a MIME type.
176 * @param parsep a flag for parsing parameters also.
177 * @throws IllegalArgumentException for parsing errors.
178 */
179 public MimeType(String spec,
180 boolean parsep)
181 {
182 int start = 0;
183 char look = '\0';
184 int length = spec.length();
185
186 // Skip leading/trailing blanks.
187 while ((start < length) &&
188 Character.isWhitespace(spec.charAt(start)))
189 {
190 start++;
191 }
192 while ((length > start) &&
193 Character.isWhitespace(spec.charAt(length - 1)))
194 {
195 length--;
196 }
197
198 // Get the type.
199 StringBuffer sb = new StringBuffer();
200 while ((start < length) &&
201 ((look = spec.charAt(start)) != '/'))
202 {
203 sb.append((char) look);
204 start++;
205 }
206 if (look != '/')
207 throw new IllegalArgumentException(
208 "Syntax error in MIME type " + spec);
209
210 mimeType = sb.toString();
211
212 // Get the subtype.
213 start++;
214 sb.setLength(0);
215 while ((start < length) &&
216 ((look = spec.charAt(start)) != ';') &&
217 !Character.isWhitespace(look))
218 {
219 sb.append((char) look);
220 start++;
221 }
222 mimeSubtype = sb.toString();
223
224 if (parsep)
225 {
226 // Get parameters, if any.
227 while ((start < length) &&
228 Character.isWhitespace(spec.charAt(start)))
229 {
230 start++;
231 }
232 if (start < length)
233 {
234 if (spec.charAt(start) != ';')
235 throw new IllegalArgumentException(
236 "Syntax error in MIME type parameters " + spec);
237
238 start++;
239 ArrayList na = new ArrayList(4);
240 ArrayList va = new ArrayList(4);
241 while (start < length)
242 {
243 // Get the name.
244 while ((start < length) &&
245 Character.isWhitespace(spec.charAt(start)))
246 {
247 start++;
248 }
249 sb.setLength(0);
250 while ((start < length) &&
251 ((look=spec.charAt(start)) != '=') &&
252 !Character.isWhitespace(look))
253 {
254 sb.append(Character.toLowerCase((char) look));
255 start++ ;
256 }
257 String name = sb.toString();
258
259 // Get the value.
260 while ((start < length) &&
261 Character.isWhitespace(spec.charAt(start)))
262 {
263 start++;
264 }
265 if (spec.charAt(start) != '=')
266 throw new IllegalArgumentException(
267 "Syntax error in MIME type parameters " + spec);
268
269 start++ ;
270 while ((start < length) &&
271 Character.isWhitespace(spec.charAt(start)))
272 {
273 start++;
274 }
275 sb.setLength(0);
276 char delim = ';';
277 if (spec.charAt(start) == '"')
278 {
279 start++;
280 delim = '"';
281 }
282 while ((start < length) &&
283 ((look = spec.charAt(start)) != delim) &&
284 ((delim == '"') ||
285 !Character.isWhitespace(look)))
286 {
287 sb.append((char) look);
288 start++;
289 }
290 while ((start < length) &&
291 (spec.charAt(start) != ';'))
292 {
293 start++;
294 }
295 start++;
296 String value = sb.toString();
297
298 na.add(name);
299 va.add(value);
300 }
301 parameterNames = (String[]) na.toArray(new String[na.size()]);
302 parameterValues = (String[]) va.toArray(new String[va.size()]);
303 }
304 }
305 }
306
307 /***
308 * Contructs a new MIME type from specified types.
309 *
310 * @param type a type.
311 * @param subtype a subtype.
312 * @throws NullPointerException if type or subtype are nulls.
313 */
314 public MimeType(String type,
315 String subtype)
316 {
317 this(type,subtype,null,null);
318 }
319
320 /***
321 * Contructs a new MIME type from specified parameters.
322 *
323 * @param type a type.
324 * @param subtype a subtype.
325 * @param names parameters names.
326 * @param values parameter values.
327 * @throws NullPointerException if type or subtype are nulls.
328 */
329 public MimeType(String type,
330 String subtype,
331 String names[],
332 String values[])
333 {
334 if ((type == null) ||
335 (subtype == null))
336 throw new NullPointerException("MIME type or subtype missing");
337
338 mimeType = type.trim();
339 mimeSubtype = subtype.trim();
340 parameterNames = names;
341 parameterValues = values;
342 }
343
344 /***
345 * Compares the specified MIME type to this one
346 * and returns a matching level:
347 * NO_MATCH=types do not match,
348 * MATCH_TYPE=types match,
349 * MATCH_SPECIFIC_TYPE=types match exactly,
350 * MATCH_SUBTYPE=types match, subtypes match too,
351 * MATCH_SPECIFIC_SUBTYPE=types match, subtypes match exactly.
352 *
353 * @param other the MimeType to compare.
354 * @return the matching level.
355 */
356 public int match(MimeType other)
357 {
358 if (mimeType.equals("*") ||
359 other.mimeType.equals("*"))
360 {
361 return MATCH_TYPE;
362 }
363 else if (!mimeType.equalsIgnoreCase(other.mimeType))
364 {
365 return NO_MATCH;
366 }
367 else if (mimeSubtype.equals("*") ||
368 other.mimeSubtype.equals("*"))
369 {
370 return MATCH_SUBTYPE;
371 }
372 else if (!mimeSubtype.equalsIgnoreCase(other.mimeSubtype))
373 {
374 return NO_MATCH;
375 }
376 else
377 {
378 return MATCH_SPECIFIC_SUBTYPE;
379 }
380 }
381
382 /***
383 * Gets the main type of the MIME type.
384 *
385 * @return the main type as a string.
386 */
387 public String getType()
388 {
389 return mimeType;
390 }
391
392 /***
393 * Gets the subtype of the MIME type.
394 *
395 * @return the subtype as a string.
396 */
397 public String getSubtype()
398 {
399 return mimeSubtype;
400 }
401
402 /***
403 * Gets the type and the subtype of the MIME type.
404 *
405 * @return the types as a string.
406 */
407 public String getTypes()
408 {
409 return mimeType + '/' + mimeSubtype;
410 }
411
412 /***
413 * Checks whether the MIME type contains the specified parameter.
414 *
415 * @param param the name opf the parameter.
416 * @return true if the parameter found, otherwise false.
417 */
418 public boolean hasParameter(String param)
419 {
420 String[] na = parameterNames;
421 if (na != null)
422 {
423 for (int i = 0; i < na.length; i++)
424 {
425 if (na[i].equalsIgnoreCase(param))
426 {
427 return true;
428 }
429 }
430 }
431 return false;
432 }
433
434 /***
435 * Gets the value of a MIME type parameter.
436 * The first parameter with the specifed name will be returned.
437 *
438 * @param param the name of the parameter.
439 * @return the value of the parameter, or null.
440 */
441 public String getParameter(String param)
442 {
443 String[] na = parameterNames;
444 if (na != null)
445 {
446 String[] va = parameterValues;
447 for (int i = 0; i < na.length; i++)
448 {
449 if (na[i].equalsIgnoreCase(param))
450 {
451 return va[i];
452 }
453 }
454 }
455 return null ;
456 }
457
458 /***
459 * Sets the value of a MIME type parameter replacing the old one.
460 *
461 * @param param the name of the parameter.
462 * @param value the value of the parameter.
463 */
464 public synchronized void setParameter(String param,
465 String value)
466 {
467 if (parameterNames != null)
468 {
469 for (int i = 0; i < parameterNames.length; i++)
470 {
471 if (parameterNames[i].equalsIgnoreCase(param))
472 {
473 parameterValues[i] = value;
474 mimeTypeString = null;
475 return;
476 }
477 }
478 }
479 addParameter(param,value);
480 }
481
482 /***
483 * Adds a parameter to the MIME type.
484 *
485 * @param param the name of the parameter.
486 * @param value the value of the parameter.
487 */
488 public void addParameter(String param,
489 String value)
490 {
491 addParameters(new String[]{ param },new String[]{ value });
492 }
493
494 /***
495 * Adds parameters to the MIME type.
496 *
497 * @param params an array of parameter names.
498 * @param values an array of parameter values.
499 * @throw IllegalArgumentException for incorrect parameters.
500 */
501 public synchronized void addParameters(String[] params,
502 String[] values)
503 {
504 if ((params == null) ||
505 (values == null) ||
506 (params.length != values.length))
507 throw new IllegalArgumentException("Incorrect MIME type parameters");
508
509 if (parameterNames != null)
510 {
511 String[] na = new String[parameterNames.length + params.length];
512 String[] va = new String[parameterValues.length + values.length];
513 System.arraycopy(parameterNames,0,na,0,parameterNames.length);
514 System.arraycopy(params,0,na,parameterNames.length,params.length);
515 System.arraycopy(parameterValues,0,va,0,parameterValues.length);
516 System.arraycopy(values,0,va,parameterValues.length,values.length);
517 parameterNames = na;
518 parameterValues = va;
519 }
520 else
521 {
522 parameterNames = params;
523 parameterValues = values;
524 }
525 mimeTypeString = null;
526 }
527
528 /***
529 * Converts the MIME type into a string.
530 *
531 * @return the string representation of the MIME type.
532 */
533 public String toString()
534 {
535 if (mimeTypeString == null)
536 {
537 StringBuffer sb = new StringBuffer(mimeType);
538 sb.append('/');
539 sb.append(mimeSubtype);
540 String[] na = parameterNames;
541 if (na != null)
542 {
543 String[] va = parameterValues;
544 for (int i = 0; i < va.length; i++)
545 {
546 sb.append(';');
547 sb.append(na[i]);
548 if (va[i] != null)
549 {
550 sb.append('=');
551 sb.append(va[i]);
552 }
553 }
554 }
555 mimeTypeString = sb.toString();
556 }
557 return mimeTypeString;
558 }
559 }
This page was automatically generated by Maven