1 7 8 package java.awt.datatransfer; 9 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.Set ; 15 16 17 24 class MimeTypeParameterList implements Cloneable { 25 26 29 public MimeTypeParameterList() { 30 parameters = new Hashtable (); 31 } 32 33 public MimeTypeParameterList(String rawdata) 34 throws MimeTypeParseException 35 { 36 parameters = new Hashtable (); 37 38 parse(rawdata); 40 } 41 42 public int hashCode() { 43 int code = Integer.MAX_VALUE/45; String paramName = null; 45 Enumeration enum_ = this.getNames(); 46 47 while (enum_.hasMoreElements()) { 48 paramName = (String )enum_.nextElement(); 49 code += paramName.hashCode(); 50 code += this.get(paramName).hashCode(); 51 } 52 53 return code; 54 } 56 61 public boolean equals(Object thatObject) { 62 if (!(thatObject instanceof MimeTypeParameterList )) { 64 return false; 65 } 66 MimeTypeParameterList that = (MimeTypeParameterList )thatObject; 67 if (this.size() != that.size()) { 68 return false; 69 } 70 String name = null; 71 String thisValue = null; 72 String thatValue = null; 73 Set entries = parameters.entrySet(); 74 Iterator iterator = entries.iterator(); 75 Map.Entry entry = null; 76 while (iterator.hasNext()) { 77 entry = (Map.Entry )iterator.next(); 78 name = (String )entry.getKey(); 79 thisValue = (String )entry.getValue(); 80 thatValue = (String )that.parameters.get(name); 81 if ((thisValue == null) || (thatValue == null)) { 82 if (thisValue != thatValue) { 84 return false; 85 } 86 } else if (!thisValue.equals(thatValue)) { 87 return false; 88 } 89 } 91 return true; 92 } 94 97 protected void parse(String rawdata) throws MimeTypeParseException { 98 int length = rawdata.length(); 99 if(length > 0) { 100 int currentIndex = skipWhiteSpace(rawdata, 0); 101 int lastIndex = 0; 102 103 if(currentIndex < length) { 104 char currentChar = rawdata.charAt(currentIndex); 105 while ((currentIndex < length) && (currentChar == ';')) { 106 String name; 107 String value; 108 boolean foundit; 109 110 ++currentIndex; 112 113 115 currentIndex = skipWhiteSpace(rawdata, currentIndex); 117 118 if(currentIndex < length) { 119 lastIndex = currentIndex; 121 currentChar = rawdata.charAt(currentIndex); 122 while((currentIndex < length) && isTokenChar(currentChar)) { 123 ++currentIndex; 124 currentChar = rawdata.charAt(currentIndex); 125 } 126 name = rawdata.substring(lastIndex, currentIndex).toLowerCase(); 127 128 130 currentIndex = skipWhiteSpace(rawdata, currentIndex); 132 133 if((currentIndex < length) && (rawdata.charAt(currentIndex) == '=')) { 134 ++currentIndex; 136 137 currentIndex = skipWhiteSpace(rawdata, currentIndex); 139 140 if(currentIndex < length) { 141 currentChar = rawdata.charAt(currentIndex); 143 if(currentChar == '"') { 144 ++currentIndex; 146 lastIndex = currentIndex; 147 148 if(currentIndex < length) { 149 foundit = false; 151 while((currentIndex < length) && !foundit) { 152 currentChar = rawdata.charAt(currentIndex); 153 if(currentChar == '\\') { 154 currentIndex += 2; 156 } else if(currentChar == '"') { 157 foundit = true; 159 } else { 160 ++currentIndex; 161 } 162 } 163 if(currentChar == '"') { 164 value = unquote(rawdata.substring(lastIndex, currentIndex)); 165 ++currentIndex; 167 } else { 168 throw new MimeTypeParseException ("Encountered unterminated quoted parameter value."); 169 } 170 } else { 171 throw new MimeTypeParseException ("Encountered unterminated quoted parameter value."); 172 } 173 } else if(isTokenChar(currentChar)) { 174 lastIndex = currentIndex; 176 foundit = false; 177 while((currentIndex < length) && !foundit) { 178 currentChar = rawdata.charAt(currentIndex); 179 180 if(isTokenChar(currentChar)) { 181 ++currentIndex; 182 } else { 183 foundit = true; 184 } 185 } 186 value = rawdata.substring(lastIndex, currentIndex); 187 } else { 188 throw new MimeTypeParseException ("Unexpected character encountered at index " + currentIndex); 190 } 191 192 parameters.put(name, value); 194 } else { 195 throw new MimeTypeParseException ("Couldn't find a value for parameter named " + name); 196 } 197 } else { 198 throw new MimeTypeParseException ("Couldn't find the '=' that separates a parameter name from its value."); 199 } 200 } else { 201 throw new MimeTypeParseException ("Couldn't find parameter name"); 202 } 203 204 currentIndex = skipWhiteSpace(rawdata, currentIndex); 206 if(currentIndex < length) { 207 currentChar = rawdata.charAt(currentIndex); 208 } 209 } 210 if(currentIndex < length) { 211 throw new MimeTypeParseException ("More characters encountered in input than expected."); 212 } 213 } 214 } 215 } 216 217 220 public int size() { 221 return parameters.size(); 222 } 223 224 227 public boolean isEmpty() { 228 return parameters.isEmpty(); 229 } 230 231 235 public String get(String name) { 236 return (String )parameters.get(name.trim().toLowerCase()); 237 } 238 239 243 public void set(String name, String value) { 244 parameters.put(name.trim().toLowerCase(), value); 245 } 246 247 250 public void remove(String name) { 251 parameters.remove(name.trim().toLowerCase()); 252 } 253 254 257 public Enumeration getNames() { 258 return parameters.keys(); 259 } 260 261 public String toString() { 262 StringBuffer buffer = new StringBuffer (); 263 buffer.ensureCapacity(parameters.size() * 16); 265 Enumeration keys = parameters.keys(); 266 while(keys.hasMoreElements()) 267 { 268 buffer.append("; "); 269 270 String key = (String )keys.nextElement(); 271 buffer.append(key); 272 buffer.append('='); 273 buffer.append(quote((String )parameters.get(key))); 274 } 275 276 return buffer.toString(); 277 } 278 279 282 283 public Object clone() { 284 MimeTypeParameterList newObj = null; 285 try { 286 newObj = (MimeTypeParameterList )super.clone(); 287 } catch (CloneNotSupportedException cannotHappen) { 288 } 289 newObj.parameters = (Hashtable )parameters.clone(); 290 return newObj; 291 } 292 293 private Hashtable parameters; 294 295 297 300 private static boolean isTokenChar(char c) { 301 return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0); 302 } 303 304 308 private static int skipWhiteSpace(String rawdata, int i) { 309 int length = rawdata.length(); 310 if (i < length) { 311 char c = rawdata.charAt(i); 312 while ((i < length) && Character.isWhitespace(c)) { 313 ++i; 314 c = rawdata.charAt(i); 315 } 316 } 317 318 return i; 319 } 320 321 324 private static String quote(String value) { 325 boolean needsQuotes = false; 326 327 int length = value.length(); 329 for(int i = 0; (i < length) && !needsQuotes; ++i) { 330 needsQuotes = !isTokenChar(value.charAt(i)); 331 } 332 333 if(needsQuotes) { 334 StringBuffer buffer = new StringBuffer (); 335 buffer.ensureCapacity((int)(length * 1.5)); 336 337 buffer.append('"'); 339 340 for(int i = 0; i < length; ++i) { 342 char c = value.charAt(i); 343 if((c == '\\') || (c == '"')) { 344 buffer.append('\\'); 345 } 346 buffer.append(c); 347 } 348 349 buffer.append('"'); 351 352 return buffer.toString(); 353 } 354 else 355 { 356 return value; 357 } 358 } 359 360 363 private static String unquote(String value) { 364 int valueLength = value.length(); 365 StringBuffer buffer = new StringBuffer (); 366 buffer.ensureCapacity(valueLength); 367 368 boolean escaped = false; 369 for(int i = 0; i < valueLength; ++i) { 370 char currentChar = value.charAt(i); 371 if(!escaped && (currentChar != '\\')) { 372 buffer.append(currentChar); 373 } else if(escaped) { 374 buffer.append(currentChar); 375 escaped = false; 376 } else { 377 escaped = true; 378 } 379 } 380 381 return buffer.toString(); 382 } 383 384 387 private static final String TSPECIALS = "()<>@,;:\\\"/[]?="; 388 389 } 390 | Popular Tags |