1 21 22 27 28 package javax.activation; 29 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 33 40 public class MimeTypeParameterList { 41 private Hashtable parameters; 42 43 46 private static final String TSPECIALS = "()<>@,;:/[]?=\\\""; 47 48 49 52 public MimeTypeParameterList() { 53 parameters = new Hashtable (); 54 } 55 56 61 public MimeTypeParameterList(String parameterList) 62 throws MimeTypeParseException { 63 parameters = new Hashtable (); 64 65 parse(parameterList); 67 } 68 69 74 protected void parse(String parameterList) throws MimeTypeParseException { 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 name; 89 String value; 90 91 i++; 93 94 96 i = skipWhiteSpace(parameterList, i); 98 99 if (i >= length) 101 return; 102 103 lastIndex = i; 105 while ((i < length) && isTokenChar(parameterList.charAt(i))) 106 i++; 107 108 name = parameterList.substring(lastIndex, i).toLowerCase(); 109 110 i = skipWhiteSpace(parameterList, i); 112 113 if (i >= length || parameterList.charAt(i) != '=') 114 throw new MimeTypeParseException ( 115 "Couldn't find the '=' that separates a " + 116 "parameter name from its value."); 117 118 i++; 120 i = skipWhiteSpace(parameterList, i); 121 122 if (i >= length) 123 throw new MimeTypeParseException ( 124 "Couldn't find a value for parameter named " + name); 125 126 c = parameterList.charAt(i); 128 if (c == '"') { 129 i++; 131 if (i >= length) 132 throw new MimeTypeParseException ( 133 "Encountered unterminated quoted parameter value."); 134 135 lastIndex = i; 136 137 while (i < length) { 139 c = parameterList.charAt(i); 140 if (c == '"') 141 break; 142 if (c == '\\') { 143 i++; 147 } 148 i++; 149 } 150 if (c != '"') 151 throw new MimeTypeParseException ( 152 "Encountered unterminated quoted parameter value."); 153 154 value = unquote(parameterList.substring(lastIndex, i)); 155 i++; 157 } else if (isTokenChar(c)) { 158 lastIndex = i; 161 while (i < length && isTokenChar(parameterList.charAt(i))) 162 i++; 163 value = parameterList.substring(lastIndex, i); 164 } else { 165 throw new MimeTypeParseException ( 167 "Unexpected character encountered at index " + i); 168 } 169 170 parameters.put(name, value); 172 } 173 if (i < length) { 174 throw new MimeTypeParseException ( 175 "More characters encountered in input than expected."); 176 } 177 } 178 179 184 public int size() { 185 return parameters.size(); 186 } 187 188 193 public boolean isEmpty() { 194 return parameters.isEmpty(); 195 } 196 197 204 public String get(String name) { 205 return (String )parameters.get(name.trim().toLowerCase()); 206 } 207 208 215 public void set(String name, String value) { 216 parameters.put(name.trim().toLowerCase(), value); 217 } 218 219 224 public void remove(String name) { 225 parameters.remove(name.trim().toLowerCase()); 226 } 227 228 233 public Enumeration getNames() { 234 return parameters.keys(); 235 } 236 237 240 public String toString() { 241 StringBuffer buffer = new StringBuffer (); 242 buffer.ensureCapacity(parameters.size() * 16); 243 245 Enumeration keys = parameters.keys(); 246 while (keys.hasMoreElements()) { 247 String key = (String )keys.nextElement(); 248 buffer.append("; "); 249 buffer.append(key); 250 buffer.append('='); 251 buffer.append(quote((String )parameters.get(key))); 252 } 253 254 return buffer.toString(); 255 } 256 257 259 262 private static boolean isTokenChar(char c) { 263 return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0); 264 } 265 266 270 private static int skipWhiteSpace(String 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 280 private static String quote(String value) { 281 boolean needsQuotes = false; 282 283 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 buffer = new StringBuffer (); 291 buffer.ensureCapacity((int)(length * 1.5)); 292 293 buffer.append('"'); 295 296 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 buffer.append('"'); 306 307 return buffer.toString(); 308 } else { 309 return value; 310 } 311 } 312 313 317 private static String unquote(String value) { 318 int valueLength = value.length(); 319 StringBuffer buffer = new StringBuffer (); 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 |