1 7 8 package java.awt.datatransfer; 9 10 import java.io.Externalizable ; 11 import java.io.ObjectOutput ; 12 import java.io.ObjectInput ; 13 import java.io.IOException ; 14 import java.util.Enumeration ; 15 16 17 25 class MimeType implements Externalizable , Cloneable { 26 27 30 31 static final long serialVersionUID = -6568722458793895906L; 32 33 38 public MimeType() { 39 } 40 41 47 public MimeType(String rawdata) throws MimeTypeParseException { 48 parse(rawdata); 49 } 50 51 60 public MimeType(String primary, String sub) throws MimeTypeParseException { 61 this(primary, sub, new MimeTypeParameterList ()); 62 } 63 64 74 public MimeType(String primary, String sub, MimeTypeParameterList mtpl) throws 75 MimeTypeParseException { 76 if(isValidToken(primary)) { 78 primaryType = primary.toLowerCase(); 79 } else { 80 throw new MimeTypeParseException ("Primary type is invalid."); 81 } 82 83 if(isValidToken(sub)) { 85 subType = sub.toLowerCase(); 86 } else { 87 throw new MimeTypeParseException ("Sub type is invalid."); 88 } 89 90 parameters = (MimeTypeParameterList )mtpl.clone(); 91 } 92 93 public int hashCode() { 94 95 int code = 0; 98 code += primaryType.hashCode(); 99 code += subType.hashCode(); 100 code += parameters.hashCode(); 101 return code; 102 } 104 113 public boolean equals(Object thatObject) { 114 if (!(thatObject instanceof MimeType )) { 115 return false; 116 } 117 MimeType that = (MimeType )thatObject; 118 boolean isIt = 119 ((this.primaryType.equals(that.primaryType)) && 120 (this.subType.equals(that.subType)) && 121 (this.parameters.equals(that.parameters))); 122 return isIt; 123 } 125 130 private void parse(String rawdata) throws MimeTypeParseException { 131 int slashIndex = rawdata.indexOf('/'); 132 int semIndex = rawdata.indexOf(';'); 133 if((slashIndex < 0) && (semIndex < 0)) { 134 throw new MimeTypeParseException ("Unable to find a sub type."); 137 } else if((slashIndex < 0) && (semIndex >= 0)) { 138 throw new MimeTypeParseException ("Unable to find a sub type."); 141 } else if((slashIndex >= 0) && (semIndex < 0)) { 142 primaryType = rawdata.substring(0, 144 slashIndex).trim().toLowerCase(); 145 subType = rawdata.substring(slashIndex + 146 1).trim().toLowerCase(); 147 parameters = new MimeTypeParameterList (); 148 } else if (slashIndex < semIndex) { 149 primaryType = rawdata.substring(0, 151 slashIndex).trim().toLowerCase(); 152 subType = rawdata.substring(slashIndex + 1, 153 semIndex).trim().toLowerCase(); 154 parameters = new 155 MimeTypeParameterList (rawdata.substring(semIndex)); 156 } else { 157 throw new MimeTypeParseException ("Unable to find a sub type."); 160 } 161 162 164 if(!isValidToken(primaryType)) { 166 throw new MimeTypeParseException ("Primary type is invalid."); 167 } 168 169 if(!isValidToken(subType)) { 171 throw new MimeTypeParseException ("Sub type is invalid."); 172 } 173 } 174 175 178 public String getPrimaryType() { 179 return primaryType; 180 } 181 182 185 public String getSubType() { 186 return subType; 187 } 188 189 192 public MimeTypeParameterList getParameters() { 193 return (MimeTypeParameterList )parameters.clone(); 194 } 195 196 200 public String getParameter(String name) { 201 return parameters.get(name); 202 } 203 204 210 public void setParameter(String name, String value) { 211 parameters.set(name, value); 212 } 213 214 219 public void removeParameter(String name) { 220 parameters.remove(name); 221 } 222 223 226 public String toString() { 227 return getBaseType() + parameters.toString(); 228 } 229 230 234 public String getBaseType() { 235 return primaryType + "/" + subType; 236 } 237 238 249 public boolean match(MimeType type) { 250 if (type == null) 251 return false; 252 return primaryType.equals(type.getPrimaryType()) 253 && (subType.equals("*") 254 || type.getSubType().equals("*") 255 || (subType.equals(type.getSubType()))); 256 } 257 258 271 public boolean match(String rawdata) throws MimeTypeParseException { 272 if (rawdata == null) 273 return false; 274 return match(new MimeType (rawdata)); 275 } 276 277 284 public void writeExternal(ObjectOutput out) throws IOException { 285 String s = toString(); if (s.length() <= 65535) { out.writeUTF(s); 289 } else { 290 out.writeByte(0); 291 out.writeByte(0); 292 out.writeInt(s.length()); 293 out.write(s.getBytes()); 294 } 295 } 296 297 306 public void readExternal(ObjectInput in) throws IOException , 307 ClassNotFoundException { 308 String s = in.readUTF(); 309 if (s == null || s.length() == 0) { byte[] ba = new byte[in.readInt()]; 311 in.readFully(ba); 312 s = new String (ba); 313 } 314 try { 315 parse(s); 316 } catch(MimeTypeParseException e) { 317 throw new IOException (e.toString()); 318 } 319 } 320 321 325 326 public Object clone() { 327 MimeType newObj = null; 328 try { 329 newObj = (MimeType )super.clone(); 330 } catch (CloneNotSupportedException cannotHappen) { 331 } 332 newObj.parameters = (MimeTypeParameterList )parameters.clone(); 333 return newObj; 334 } 335 336 private String primaryType; 337 private String subType; 338 private MimeTypeParameterList parameters; 339 340 342 345 private static boolean isTokenChar(char c) { 346 return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0); 347 } 348 349 354 private boolean isValidToken(String s) { 355 int len = s.length(); 356 if(len > 0) { 357 for (int i = 0; i < len; ++i) { 358 char c = s.charAt(i); 359 if (!isTokenChar(c)) { 360 return false; 361 } 362 } 363 return true; 364 } else { 365 return false; 366 } 367 } 368 369 372 373 private static final String TSPECIALS = "()<>@,;:\\\"/[]?="; 374 375 } | Popular Tags |