1 21 22 27 28 package javax.activation; 29 30 import java.io.ObjectOutput ; 31 import java.io.ObjectInput ; 32 import java.io.IOException ; 33 import java.io.*; 34 35 39 public class MimeType implements Externalizable { 40 41 private String primaryType; 42 private String subType; 43 private MimeTypeParameterList parameters; 44 45 48 private static final String TSPECIALS = "()<>@,;:/[]?=\\\""; 49 50 53 public MimeType() { 54 primaryType = "application"; 55 subType = "*"; 56 parameters = new MimeTypeParameterList (); 57 } 58 59 64 public MimeType(String rawdata) throws MimeTypeParseException { 65 parse(rawdata); 66 } 67 68 77 public MimeType(String primary, String sub) throws MimeTypeParseException { 78 if (isValidToken(primary)) { 80 primaryType = primary.toLowerCase(); 81 } else { 82 throw new MimeTypeParseException ("Primary type is invalid."); 83 } 84 85 if (isValidToken(sub)) { 87 subType = sub.toLowerCase(); 88 } else { 89 throw new MimeTypeParseException ("Sub type is invalid."); 90 } 91 92 parameters = new MimeTypeParameterList (); 93 } 94 95 98 private void parse(String rawdata) throws MimeTypeParseException { 99 int slashIndex = rawdata.indexOf('/'); 100 int semIndex = rawdata.indexOf(';'); 101 if ((slashIndex < 0) && (semIndex < 0)) { 102 throw new MimeTypeParseException ("Unable to find a sub type."); 105 } else if ((slashIndex < 0) && (semIndex >= 0)) { 106 throw new MimeTypeParseException ("Unable to find a sub type."); 109 } else if ((slashIndex >= 0) && (semIndex < 0)) { 110 primaryType = rawdata.substring(0, slashIndex).trim().toLowerCase(); 112 subType = rawdata.substring(slashIndex + 1).trim().toLowerCase(); 113 parameters = new MimeTypeParameterList (); 114 } else if (slashIndex < semIndex) { 115 primaryType = rawdata.substring(0, slashIndex).trim().toLowerCase(); 117 subType = rawdata.substring(slashIndex + 1, 118 semIndex).trim().toLowerCase(); 119 parameters = new MimeTypeParameterList (rawdata.substring(semIndex)); 120 } else { 121 throw new MimeTypeParseException ("Unable to find a sub type."); 124 } 125 126 128 if (!isValidToken(primaryType)) 130 throw new MimeTypeParseException ("Primary type is invalid."); 131 132 if (!isValidToken(subType)) 134 throw new MimeTypeParseException ("Sub type is invalid."); 135 } 136 137 142 public String getPrimaryType() { 143 return primaryType; 144 } 145 146 153 public void setPrimaryType(String primary) throws MimeTypeParseException { 154 if (!isValidToken(primaryType)) 156 throw new MimeTypeParseException ("Primary type is invalid."); 157 primaryType = primary.toLowerCase(); 158 } 159 160 165 public String getSubType() { 166 return subType; 167 } 168 169 176 public void setSubType(String sub) throws MimeTypeParseException { 177 if (!isValidToken(subType)) 179 throw new MimeTypeParseException ("Sub type is invalid."); 180 subType = sub.toLowerCase(); 181 } 182 183 188 public MimeTypeParameterList getParameters() { 189 return parameters; 190 } 191 192 199 public String getParameter(String name) { 200 return parameters.get(name); 201 } 202 203 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 236 public String getBaseType() { 237 return primaryType + "/" + subType; 238 } 239 240 247 public boolean match(MimeType type) { 248 return primaryType.equals(type.getPrimaryType()) 249 && (subType.equals("*") 250 || type.getSubType().equals("*") 251 || (subType.equals(type.getSubType()))); 252 } 253 254 261 public boolean match(String rawdata) throws MimeTypeParseException { 262 return match(new MimeType (rawdata)); 263 } 264 265 274 public void writeExternal(ObjectOutput out) throws IOException { 275 out.writeUTF(toString()); 276 out.flush(); 277 } 278 279 290 public void readExternal(ObjectInput in) 291 throws IOException , ClassNotFoundException { 292 try { 293 parse(in.readUTF()); 294 } catch (MimeTypeParseException e) { 295 throw new IOException (e.toString()); 296 } 297 } 298 299 301 304 private static boolean isTokenChar(char c) { 305 return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0); 306 } 307 308 311 private boolean isValidToken(String s) { 312 int len = s.length(); 313 if (len > 0) { 314 for (int i = 0; i < len; ++i) { 315 char c = s.charAt(i); 316 if (!isTokenChar(c)) { 317 return false; 318 } 319 } 320 return true; 321 } else { 322 return false; 323 } 324 } 325 326 343 } 344 | Popular Tags |