1 package org.apache.turbine.services.mimetype.util; 2 3 18 19 import java.util.ArrayList ; 20 21 29 public class MimeType 30 implements Cloneable 31 { 32 35 public static MimeType TEXT_HTML; 36 public static MimeType TEXT_WML; 37 public static MimeType TEXT_HDML; 38 public static MimeType TEXT_CHTML; 39 public static MimeType TEXT_PLAIN; 40 public static MimeType MULTIPART; 41 public static MimeType MULTIPART_FORM_DATA; 42 public static MimeType APPLICATION_POSTSCRIPT; 43 public static MimeType APPLICATION_OCTET_STREAM; 44 public static MimeType APPLICATION_X_JAVA_AGENT; 45 public static MimeType APPLICATION_X_WWW_FORM_URLENCODED; 46 public static MimeType MESSAGE_HTTP; 47 public static MimeType TEXT_CSS; 48 public static MimeType TEXT; 49 public static MimeType IMAGE_GIF; 50 public static MimeType IMAGE_JPEG; 51 public static MimeType IMAGE_WBMP; 52 53 static 54 { 55 TEXT_HTML = 56 new MimeType("text/html"); 57 TEXT_WML = 58 new MimeType("text/vnd.wap.wml"); 59 TEXT_HDML = 60 new MimeType("text/x-hdml"); 61 TEXT_CHTML = 62 new MimeType("text/x-chtml"); 63 TEXT_PLAIN = 64 new MimeType("text/plain"); 65 MULTIPART = 66 new MimeType("multipart/*"); 67 MULTIPART_FORM_DATA = 68 new MimeType("multipart/form-data"); 69 APPLICATION_POSTSCRIPT = 70 new MimeType("application/postscript"); 71 APPLICATION_OCTET_STREAM = 72 new MimeType("application/octet-stream"); 73 APPLICATION_X_JAVA_AGENT = 74 new MimeType("application/x-java-agent"); 75 APPLICATION_X_WWW_FORM_URLENCODED = 76 new MimeType("application/x-www-form-urlencoded"); 77 MESSAGE_HTTP = 78 new MimeType("message/http"); 79 TEXT_CSS = 80 new MimeType("text/css"); 81 TEXT = 82 new MimeType("text/*"); 83 IMAGE_GIF = 84 new MimeType("image/gif"); 85 IMAGE_JPEG = 86 new MimeType("image/jpeg"); 87 IMAGE_WBMP = 88 new MimeType("image/vnd.wap.wbmp"); 89 } 90 91 94 public static final int NO_MATCH = 0; 95 public static final int MATCH_TYPE = 1; 96 public static final int MATCH_SUBTYPE = 2; 97 public static final int MATCH_SPECIFIC_SUBTYPE = 3; 98 99 102 private String mimeType; 103 104 107 private String mimeSubtype; 108 109 112 private String parameterNames[]; 113 114 117 private String parameterValues[]; 118 119 122 private String mimeTypeString; 123 124 129 public MimeType(String spec) 130 { 131 this(spec, true); 132 } 133 134 141 public MimeType(String spec, 142 boolean parsep) 143 { 144 int start = 0; 145 char look = '\0'; 146 int length = spec.length(); 147 148 while ((start < length) && 150 Character.isWhitespace(spec.charAt(start))) 151 { 152 start++; 153 } 154 while ((length > start) && 155 Character.isWhitespace(spec.charAt(length - 1))) 156 { 157 length--; 158 } 159 160 StringBuffer sb = new StringBuffer (); 162 while ((start < length) && 163 ((look = spec.charAt(start)) != '/')) 164 { 165 sb.append((char) look); 166 start++; 167 } 168 if (look != '/') 169 { 170 throw new IllegalArgumentException ( 171 "Syntax error in MIME type " + spec); 172 } 173 mimeType = sb.toString(); 174 175 start++; 177 sb.setLength(0); 178 while ((start < length) && 179 ((look = spec.charAt(start)) != ';') && 180 !Character.isWhitespace(look)) 181 { 182 sb.append((char) look); 183 start++; 184 } 185 mimeSubtype = sb.toString(); 186 187 if (parsep) 188 { 189 while ((start < length) && 191 Character.isWhitespace(spec.charAt(start))) 192 { 193 start++; 194 } 195 if (start < length) 196 { 197 if (spec.charAt(start) != ';') 198 { 199 throw new IllegalArgumentException ( 200 "Syntax error in MIME type parameters " + spec); 201 } 202 start++; 203 ArrayList na = new ArrayList (4); 204 ArrayList va = new ArrayList (4); 205 while (start < length) 206 { 207 while ((start < length) && 209 Character.isWhitespace(spec.charAt(start))) 210 { 211 start++; 212 } 213 sb.setLength(0); 214 while ((start < length) && 215 ((look = spec.charAt(start)) != '=') && 216 !Character.isWhitespace(look)) 217 { 218 sb.append(Character.toLowerCase((char) look)); 219 start++; 220 } 221 String name = sb.toString(); 222 223 while ((start < length) && 225 Character.isWhitespace(spec.charAt(start))) 226 { 227 start++; 228 } 229 if (spec.charAt(start) != '=') 230 { 231 throw new IllegalArgumentException ( 232 "Syntax error in MIME type parameters " + spec); 233 } 234 start++; 235 while ((start < length) && 236 Character.isWhitespace(spec.charAt(start))) 237 { 238 start++; 239 } 240 sb.setLength(0); 241 char delim = ';'; 242 if (spec.charAt(start) == '"') 243 { 244 start++; 245 delim = '"'; 246 } 247 while ((start < length) && 248 ((look = spec.charAt(start)) != delim) && 249 ((delim == '"') || 250 !Character.isWhitespace(look))) 251 { 252 sb.append((char) look); 253 start++; 254 } 255 while ((start < length) && 256 (spec.charAt(start) != ';')) 257 { 258 start++; 259 } 260 start++; 261 String value = sb.toString(); 262 263 na.add(name); 264 va.add(value); 265 } 266 parameterNames = (String []) na.toArray(new String [na.size()]); 267 parameterValues = (String []) va.toArray(new String [va.size()]); 268 } 269 } 270 } 271 272 279 public MimeType(String type, 280 String subtype) 281 { 282 this(type, subtype, null, null); 283 } 284 285 294 public MimeType(String type, 295 String subtype, 296 String names[], 297 String values[]) 298 { 299 if ((type == null) || 300 (subtype == null)) 301 { 302 throw new NullPointerException ("MIME type or subtype missing"); 303 } 304 mimeType = type.trim(); 305 mimeSubtype = subtype.trim(); 306 parameterNames = names; 307 parameterValues = values; 308 } 309 310 322 public int match(MimeType other) 323 { 324 if (mimeType.equals("*") || 325 other.mimeType.equals("*")) 326 { 327 return MATCH_TYPE; 328 } 329 else if (!mimeType.equalsIgnoreCase(other.mimeType)) 330 { 331 return NO_MATCH; 332 } 333 else if (mimeSubtype.equals("*") || 334 other.mimeSubtype.equals("*")) 335 { 336 return MATCH_SUBTYPE; 337 } 338 else if (!mimeSubtype.equalsIgnoreCase(other.mimeSubtype)) 339 { 340 return NO_MATCH; 341 } 342 else 343 { 344 return MATCH_SPECIFIC_SUBTYPE; 345 } 346 } 347 348 353 public String getType() 354 { 355 return mimeType; 356 } 357 358 363 public String getSubtype() 364 { 365 return mimeSubtype; 366 } 367 368 373 public String getTypes() 374 { 375 return mimeType + '/' + mimeSubtype; 376 } 377 378 384 public boolean hasParameter(String param) 385 { 386 String [] na = parameterNames; 387 if (na != null) 388 { 389 for (int i = 0; i < na.length; i++) 390 { 391 if (na[i].equalsIgnoreCase(param)) 392 { 393 return true; 394 } 395 } 396 } 397 return false; 398 } 399 400 407 public String getParameter(String param) 408 { 409 String [] na = parameterNames; 410 if (na != null) 411 { 412 String [] va = parameterValues; 413 for (int i = 0; i < na.length; i++) 414 { 415 if (na[i].equalsIgnoreCase(param)) 416 { 417 return va[i]; 418 } 419 } 420 } 421 return null; 422 } 423 424 430 public synchronized void setParameter(String param, 431 String value) 432 { 433 if (parameterNames != null) 434 { 435 for (int i = 0; i < parameterNames.length; i++) 436 { 437 if (parameterNames[i].equalsIgnoreCase(param)) 438 { 439 parameterValues[i] = value; 440 mimeTypeString = null; 441 return; 442 } 443 } 444 } 445 addParameter(param, value); 446 } 447 448 454 public void addParameter(String param, 455 String value) 456 { 457 addParameters(new String []{param}, new String []{value}); 458 } 459 460 467 public synchronized void addParameters(String [] params, 468 String [] values) 469 { 470 if ((params == null) || 471 (values == null) || 472 (params.length != values.length)) 473 throw new IllegalArgumentException ("Incorrect MIME type parameters"); 474 475 if (parameterNames != null) 476 { 477 String [] na = new String [parameterNames.length + params.length]; 478 String [] va = new String [parameterValues.length + values.length]; 479 System.arraycopy(parameterNames, 0, na, 0, parameterNames.length); 480 System.arraycopy(params, 0, na, parameterNames.length, params.length); 481 System.arraycopy(parameterValues, 0, va, 0, parameterValues.length); 482 System.arraycopy(values, 0, va, parameterValues.length, values.length); 483 parameterNames = na; 484 parameterValues = va; 485 } 486 else 487 { 488 parameterNames = params; 489 parameterValues = values; 490 } 491 mimeTypeString = null; 492 } 493 494 499 public String toString() 500 { 501 if (mimeTypeString == null) 502 { 503 StringBuffer sb = new StringBuffer (mimeType); 504 sb.append('/'); 505 sb.append(mimeSubtype); 506 String [] na = parameterNames; 507 if (na != null) 508 { 509 String [] va = parameterValues; 510 for (int i = 0; i < va.length; i++) 511 { 512 sb.append(';'); 513 sb.append(na[i]); 514 if (va[i] != null) 515 { 516 sb.append('='); 517 sb.append(va[i]); 518 } 519 } 520 } 521 mimeTypeString = sb.toString(); 522 } 523 return mimeTypeString; 524 } 525 } 526 | Popular Tags |