1 21 22 27 28 package com.sun.activation.registries; 29 30 import java.io.*; 31 import java.util.*; 32 33 public class MailcapFile { 34 35 41 private Map type_hash = new HashMap(); 42 43 46 private Map fallback_hash = new HashMap(); 47 48 52 private Map native_commands = new HashMap(); 53 54 private static boolean addReverse = false; 55 56 static { 57 try { 58 addReverse = Boolean.getBoolean("javax.activation.addreverse"); 59 } catch (Throwable t) { 60 } 62 } 63 64 69 public MailcapFile(String new_fname) throws IOException { 70 if (LogSupport.isLoggable()) 71 LogSupport.log("new MailcapFile: file " + new_fname); 72 FileReader reader = null; 73 try { 74 reader = new FileReader(new_fname); 75 parse(new BufferedReader(reader)); 76 } finally { 77 if (reader != null) { 78 try { 79 reader.close(); 80 } catch (IOException ex) { } 81 } 82 } 83 } 84 85 90 public MailcapFile(InputStream is) throws IOException { 91 if (LogSupport.isLoggable()) 92 LogSupport.log("new MailcapFile: InputStream"); 93 parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1"))); 94 } 95 96 99 public MailcapFile() { 100 if (LogSupport.isLoggable()) 101 LogSupport.log("new MailcapFile: default"); 102 } 103 104 112 public Map getMailcapList(String mime_type) { 113 Map search_result = null; 114 Map wildcard_result = null; 115 116 search_result = (Map)type_hash.get(mime_type); 118 119 int separator = mime_type.indexOf('/'); 121 String subtype = mime_type.substring(separator + 1); 122 if (!subtype.equals("*")) { 123 String type = mime_type.substring(0, separator + 1) + "*"; 124 wildcard_result = (Map)type_hash.get(type); 125 126 if (wildcard_result != null) { if (search_result != null) 128 search_result = 129 mergeResults(search_result, wildcard_result); 130 else 131 search_result = wildcard_result; 132 } 133 } 134 return search_result; 135 } 136 137 145 public Map getMailcapFallbackList(String mime_type) { 146 Map search_result = null; 147 Map wildcard_result = null; 148 149 search_result = (Map)fallback_hash.get(mime_type); 151 152 int separator = mime_type.indexOf('/'); 154 String subtype = mime_type.substring(separator + 1); 155 if (!subtype.equals("*")) { 156 String type = mime_type.substring(0, separator + 1) + "*"; 157 wildcard_result = (Map)fallback_hash.get(type); 158 159 if (wildcard_result != null) { if (search_result != null) 161 search_result = 162 mergeResults(search_result, wildcard_result); 163 else 164 search_result = wildcard_result; 165 } 166 } 167 return search_result; 168 } 169 170 173 public String [] getMimeTypes() { 174 Set types = new HashSet(type_hash.keySet()); 175 types.addAll(fallback_hash.keySet()); 176 types.addAll(native_commands.keySet()); 177 String [] mts = new String [types.size()]; 178 mts = (String [])types.toArray(mts); 179 return mts; 180 } 181 182 185 public String [] getNativeCommands(String mime_type) { 186 String [] cmds = null; 187 List v = (List)native_commands.get(mime_type.toLowerCase()); 188 if (v != null) { 189 cmds = new String [v.size()]; 190 cmds = (String [])v.toArray(cmds); 191 } 192 return cmds; 193 } 194 195 201 private Map mergeResults(Map first, Map second) { 202 Iterator verb_enum = second.keySet().iterator(); 203 Map clonedHash = new HashMap(first); 204 205 while (verb_enum.hasNext()) { 207 String verb = (String )verb_enum.next(); 208 List cmdVector = (List)clonedHash.get(verb); 209 if (cmdVector == null) { 210 clonedHash.put(verb, second.get(verb)); 211 } else { 212 List oldV = (List)second.get(verb); 214 cmdVector = new ArrayList(cmdVector); 215 cmdVector.addAll(oldV); 216 clonedHash.put(verb, cmdVector); 217 } 218 } 219 return clonedHash; 220 } 221 222 232 public void appendToMailcap(String mail_cap) { 233 if (LogSupport.isLoggable()) 234 LogSupport.log("appendToMailcap: " + mail_cap); 235 try { 236 parse(new StringReader(mail_cap)); 237 } catch (IOException ex) { 238 } 240 } 241 242 245 private void parse(Reader reader) throws IOException { 246 BufferedReader buf_reader = new BufferedReader(reader); 247 String line = null; 248 String continued = null; 249 250 while ((line = buf_reader.readLine()) != null) { 251 253 line = line.trim(); 254 255 try { 256 if (line.charAt(0) == '#') 257 continue; 258 if (line.charAt(line.length() - 1) == '\\') { 259 if (continued != null) 260 continued += line.substring(0, line.length() - 1); 261 else 262 continued = line.substring(0, line.length() - 1); 263 } else if (continued != null) { 264 continued = continued + line; 266 try { 268 parseLine(continued); 269 } catch (MailcapParseException e) { 270 } 272 continued = null; 273 } 274 else { 275 try { 277 parseLine(line); 278 } catch (MailcapParseException e) { 280 } 282 } 283 } catch (StringIndexOutOfBoundsException e) {} 284 } 285 } 286 287 293 protected void parseLine(String mailcapEntry) 294 throws MailcapParseException, IOException { 295 MailcapTokenizer tokenizer = new MailcapTokenizer(mailcapEntry); 296 tokenizer.setIsAutoquoting(false); 297 298 if (LogSupport.isLoggable()) 299 LogSupport.log("parse: " + mailcapEntry); 300 int currentToken = tokenizer.nextToken(); 302 if (currentToken != MailcapTokenizer.STRING_TOKEN) { 303 reportParseError(MailcapTokenizer.STRING_TOKEN, currentToken, 304 tokenizer.getCurrentTokenValue()); 305 } 306 String primaryType = tokenizer.getCurrentTokenValue().toLowerCase(); 307 String subType = "*"; 308 309 currentToken = tokenizer.nextToken(); 312 if ((currentToken != MailcapTokenizer.SLASH_TOKEN) && 313 (currentToken != MailcapTokenizer.SEMICOLON_TOKEN)) { 314 reportParseError(MailcapTokenizer.SLASH_TOKEN, 315 MailcapTokenizer.SEMICOLON_TOKEN, currentToken, 316 tokenizer.getCurrentTokenValue()); 317 } 318 319 if (currentToken == MailcapTokenizer.SLASH_TOKEN) { 321 currentToken = tokenizer.nextToken(); 323 if (currentToken != MailcapTokenizer.STRING_TOKEN) { 324 reportParseError(MailcapTokenizer.STRING_TOKEN, 325 currentToken, tokenizer.getCurrentTokenValue()); 326 } 327 subType = tokenizer.getCurrentTokenValue().toLowerCase(); 328 329 currentToken = tokenizer.nextToken(); 331 } 332 333 String mimeType = primaryType + "/" + subType; 334 335 if (LogSupport.isLoggable()) 336 LogSupport.log(" Type: " + mimeType); 337 338 Map commands = new LinkedHashMap(); 341 if (currentToken != MailcapTokenizer.SEMICOLON_TOKEN) { 343 reportParseError(MailcapTokenizer.SEMICOLON_TOKEN, 344 currentToken, tokenizer.getCurrentTokenValue()); 345 } 346 348 tokenizer.setIsAutoquoting(true); 350 currentToken = tokenizer.nextToken(); 351 tokenizer.setIsAutoquoting(false); 352 if ((currentToken != MailcapTokenizer.STRING_TOKEN) && 353 (currentToken != MailcapTokenizer.SEMICOLON_TOKEN)) { 354 reportParseError(MailcapTokenizer.STRING_TOKEN, 355 MailcapTokenizer.SEMICOLON_TOKEN, currentToken, 356 tokenizer.getCurrentTokenValue()); 357 } 358 359 if (currentToken == MailcapTokenizer.STRING_TOKEN) { 360 List v = (List)native_commands.get(mimeType); 363 if (v == null) { 364 v = new ArrayList(); 365 v.add(mailcapEntry); 366 native_commands.put(mimeType, v); 367 } else { 368 v.add(mailcapEntry); 370 } 371 } 372 373 if (currentToken != MailcapTokenizer.SEMICOLON_TOKEN) { 375 currentToken = tokenizer.nextToken(); 376 } 377 378 if (currentToken == MailcapTokenizer.SEMICOLON_TOKEN) { 381 boolean isFallback = false; 382 do { 383 385 currentToken = tokenizer.nextToken(); 387 if (currentToken != MailcapTokenizer.STRING_TOKEN) { 388 reportParseError(MailcapTokenizer.STRING_TOKEN, 389 currentToken, tokenizer.getCurrentTokenValue()); 390 } 391 String paramName = 392 tokenizer.getCurrentTokenValue().toLowerCase(); 393 394 currentToken = tokenizer.nextToken(); 396 if ((currentToken != MailcapTokenizer.EQUALS_TOKEN) && 397 (currentToken != MailcapTokenizer.SEMICOLON_TOKEN) && 398 (currentToken != MailcapTokenizer.EOI_TOKEN)) { 399 reportParseError(MailcapTokenizer.EQUALS_TOKEN, 400 MailcapTokenizer.SEMICOLON_TOKEN, 401 MailcapTokenizer.EOI_TOKEN, 402 currentToken, tokenizer.getCurrentTokenValue()); 403 } 404 405 if (currentToken == MailcapTokenizer.EQUALS_TOKEN) { 407 409 tokenizer.setIsAutoquoting(true); 411 currentToken = tokenizer.nextToken(); 412 tokenizer.setIsAutoquoting(false); 413 if (currentToken != MailcapTokenizer.STRING_TOKEN) { 414 reportParseError(MailcapTokenizer.STRING_TOKEN, 415 currentToken, tokenizer.getCurrentTokenValue()); 416 } 417 String paramValue = 418 tokenizer.getCurrentTokenValue(); 419 420 if (paramName.startsWith("x-java-")) { 422 String commandName = paramName.substring(7); 423 425 if (commandName.equals("fallback-entry") && 426 paramValue.equalsIgnoreCase("true")) { 427 isFallback = true; 428 } else { 429 430 if (LogSupport.isLoggable()) 432 LogSupport.log(" Command: " + commandName + 433 ", Class: " + paramValue); 434 List classes = (List)commands.get(commandName); 435 if (classes == null) { 436 classes = new ArrayList(); 437 commands.put(commandName, classes); 438 } 439 if (addReverse) 440 classes.add(0, paramValue); 441 else 442 classes.add(paramValue); 443 } 444 } 445 446 currentToken = tokenizer.nextToken(); 448 } 449 } while (currentToken == MailcapTokenizer.SEMICOLON_TOKEN); 450 451 Map masterHash = isFallback ? fallback_hash : type_hash; 452 Map curcommands = 453 (Map)masterHash.get(mimeType); 454 if (curcommands == null) { 455 masterHash.put(mimeType, commands); 456 } else { 457 if (LogSupport.isLoggable()) 458 LogSupport.log("Merging commands for type " + mimeType); 459 Iterator cn = curcommands.keySet().iterator(); 462 while (cn.hasNext()) { 463 String cmdName = (String )cn.next(); 464 List ccv = (List)curcommands.get(cmdName); 465 List cv = (List)commands.get(cmdName); 466 if (cv == null) 467 continue; 468 Iterator cvn = cv.iterator(); 470 while (cvn.hasNext()) { 471 String clazz = (String )cvn.next(); 472 if (!ccv.contains(clazz)) 473 if (addReverse) 474 ccv.add(0, clazz); 475 else 476 ccv.add(clazz); 477 } 478 } 479 cn = commands.keySet().iterator(); 481 while (cn.hasNext()) { 482 String cmdName = (String )cn.next(); 483 if (curcommands.containsKey(cmdName)) 484 continue; 485 List cv = (List)commands.get(cmdName); 486 curcommands.put(cmdName, cv); 487 } 488 } 489 } else if (currentToken != MailcapTokenizer.EOI_TOKEN) { 490 reportParseError(MailcapTokenizer.EOI_TOKEN, 491 MailcapTokenizer.SEMICOLON_TOKEN, 492 currentToken, tokenizer.getCurrentTokenValue()); 493 } 494 } 495 496 protected static void reportParseError(int expectedToken, int actualToken, 497 String actualTokenValue) throws MailcapParseException { 498 throw new MailcapParseException("Encountered a " + 499 MailcapTokenizer.nameForToken(actualToken) + " token (" + 500 actualTokenValue + ") while expecting a " + 501 MailcapTokenizer.nameForToken(expectedToken) + " token."); 502 } 503 504 protected static void reportParseError(int expectedToken, 505 int otherExpectedToken, int actualToken, String actualTokenValue) 506 throws MailcapParseException { 507 throw new MailcapParseException("Encountered a " + 508 MailcapTokenizer.nameForToken(actualToken) + " token (" + 509 actualTokenValue + ") while expecting a " + 510 MailcapTokenizer.nameForToken(expectedToken) + " or a " + 511 MailcapTokenizer.nameForToken(otherExpectedToken) + " token."); 512 } 513 514 protected static void reportParseError(int expectedToken, 515 int otherExpectedToken, int anotherExpectedToken, int actualToken, 516 String actualTokenValue) throws MailcapParseException { 517 if (LogSupport.isLoggable()) 518 LogSupport.log("PARSE ERROR: " + "Encountered a " + 519 MailcapTokenizer.nameForToken(actualToken) + " token (" + 520 actualTokenValue + ") while expecting a " + 521 MailcapTokenizer.nameForToken(expectedToken) + ", a " + 522 MailcapTokenizer.nameForToken(otherExpectedToken) + ", or a " + 523 MailcapTokenizer.nameForToken(anotherExpectedToken) + " token."); 524 throw new MailcapParseException("Encountered a " + 525 MailcapTokenizer.nameForToken(actualToken) + " token (" + 526 actualTokenValue + ") while expecting a " + 527 MailcapTokenizer.nameForToken(expectedToken) + ", a " + 528 MailcapTokenizer.nameForToken(otherExpectedToken) + ", or a " + 529 MailcapTokenizer.nameForToken(anotherExpectedToken) + " token."); 530 } 531 532 562 } 563 | Popular Tags |