1 3 19 20 package com.sun.org.apache.xml.internal.resolver; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.FileNotFoundException ; 25 import java.util.Enumeration ; 26 import java.util.Vector ; 27 import java.net.URL ; 28 import java.net.URLConnection ; 29 import java.net.MalformedURLException ; 30 import com.sun.org.apache.xml.internal.resolver.readers.SAXCatalogReader; 31 import com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader; 32 import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader; 33 import javax.xml.parsers.SAXParserFactory ; 34 35 46 public class Resolver extends Catalog { 47 52 public static final int URISUFFIX = CatalogEntry.addEntryType("URISUFFIX", 2); 53 54 60 public static final int SYSTEMSUFFIX = CatalogEntry.addEntryType("SYSTEMSUFFIX", 2); 61 62 67 public static final int RESOLVER = CatalogEntry.addEntryType("RESOLVER", 1); 68 69 77 public static final int SYSTEMREVERSE 78 = CatalogEntry.addEntryType("SYSTEMREVERSE", 1); 79 80 83 public void setupReaders() { 84 SAXParserFactory spf = SAXParserFactory.newInstance(); 85 spf.setNamespaceAware(true); 86 spf.setValidating(false); 87 88 SAXCatalogReader saxReader = new SAXCatalogReader(spf); 89 90 saxReader.setCatalogParser(null, "XMLCatalog", 91 "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader"); 92 93 saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, 94 "catalog", 95 "com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader"); 96 97 addReader("application/xml", saxReader); 98 99 TR9401CatalogReader textReader = new TR9401CatalogReader(); 100 addReader("text/plain", textReader); 101 } 102 103 113 public void addEntry(CatalogEntry entry) { 114 int type = entry.getEntryType(); 115 116 if (type == URISUFFIX) { 117 String suffix = normalizeURI(entry.getEntryArg(0)); 118 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 119 120 entry.setEntryArg(1, fsi); 121 122 catalogManager.debug.message(4, "URISUFFIX", suffix, fsi); 123 } else if (type == SYSTEMSUFFIX) { 124 String suffix = normalizeURI(entry.getEntryArg(0)); 125 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 126 127 entry.setEntryArg(1, fsi); 128 129 catalogManager.debug.message(4, "SYSTEMSUFFIX", suffix, fsi); 130 } 131 132 super.addEntry(entry); 133 } 134 135 155 public String resolveURI(String uri) 156 throws MalformedURLException , IOException { 157 158 String resolved = super.resolveURI(uri); 159 if (resolved != null) { 160 return resolved; 161 } 162 163 Enumeration en = catalogEntries.elements(); 164 while (en.hasMoreElements()) { 165 CatalogEntry e = (CatalogEntry) en.nextElement(); 166 if (e.getEntryType() == RESOLVER) { 167 resolved = resolveExternalSystem(uri, e.getEntryArg(0)); 168 if (resolved != null) { 169 return resolved; 170 } 171 } else if (e.getEntryType() == URISUFFIX) { 172 String suffix = e.getEntryArg(0); 173 String result = e.getEntryArg(1); 174 175 if (suffix.length() <= uri.length() 176 && uri.substring(uri.length()-suffix.length()).equals(suffix)) { 177 return result; 178 } 179 } 180 } 181 182 return resolveSubordinateCatalogs(Catalog.URI, 184 null, 185 null, 186 uri); 187 } 188 189 212 public String resolveSystem(String systemId) 213 throws MalformedURLException , IOException { 214 215 String resolved = super.resolveSystem(systemId); 216 if (resolved != null) { 217 return resolved; 218 } 219 220 Enumeration en = catalogEntries.elements(); 221 while (en.hasMoreElements()) { 222 CatalogEntry e = (CatalogEntry) en.nextElement(); 223 if (e.getEntryType() == RESOLVER) { 224 resolved = resolveExternalSystem(systemId, e.getEntryArg(0)); 225 if (resolved != null) { 226 return resolved; 227 } 228 } else if (e.getEntryType() == SYSTEMSUFFIX) { 229 String suffix = e.getEntryArg(0); 230 String result = e.getEntryArg(1); 231 232 if (suffix.length() <= systemId.length() 233 && systemId.substring(systemId.length()-suffix.length()).equals(suffix)) { 234 return result; 235 } 236 } 237 } 238 239 return resolveSubordinateCatalogs(Catalog.SYSTEM, 240 null, 241 null, 242 systemId); 243 } 244 245 274 public String resolvePublic(String publicId, String systemId) 275 throws MalformedURLException , IOException { 276 277 String resolved = super.resolvePublic(publicId, systemId); 278 if (resolved != null) { 279 return resolved; 280 } 281 282 Enumeration en = catalogEntries.elements(); 283 while (en.hasMoreElements()) { 284 CatalogEntry e = (CatalogEntry) en.nextElement(); 285 if (e.getEntryType() == RESOLVER) { 286 if (systemId != null) { 287 resolved = resolveExternalSystem(systemId, 288 e.getEntryArg(0)); 289 if (resolved != null) { 290 return resolved; 291 } 292 } 293 resolved = resolveExternalPublic(publicId, e.getEntryArg(0)); 294 if (resolved != null) { 295 return resolved; 296 } 297 } 298 } 299 300 return resolveSubordinateCatalogs(Catalog.PUBLIC, 301 null, 302 publicId, 303 systemId); 304 } 305 306 314 protected String resolveExternalSystem(String systemId, String resolver) 315 throws MalformedURLException , IOException { 316 Resolver r = queryResolver(resolver, "i2l", systemId, null); 317 if (r != null) { 318 return r.resolveSystem(systemId); 319 } else { 320 return null; 321 } 322 } 323 324 332 protected String resolveExternalPublic(String publicId, String resolver) 333 throws MalformedURLException , IOException { 334 Resolver r = queryResolver(resolver, "fpi2l", publicId, null); 335 if (r != null) { 336 return r.resolvePublic(publicId, null); 337 } else { 338 return null; 339 } 340 } 341 342 352 protected Resolver queryResolver(String resolver, 353 String command, 354 String arg1, 355 String arg2) { 356 InputStream iStream = null; 357 String RFC2483 = resolver + "?command=" + command 358 + "&format=tr9401&uri=" + arg1 359 + "&uri2=" + arg2; 360 String line = null; 361 362 try { 363 URL url = new URL (RFC2483); 364 365 URLConnection urlCon = url.openConnection(); 366 367 urlCon.setUseCaches(false); 368 369 Resolver r = (Resolver) newCatalog(); 370 371 String cType = urlCon.getContentType(); 372 373 if (cType.indexOf(";") > 0) { 375 cType = cType.substring(0, cType.indexOf(";")); 376 } 377 378 r.parseCatalog(cType, urlCon.getInputStream()); 379 380 return r; 381 } catch (CatalogException cex) { 382 if (cex.getExceptionType() == CatalogException.UNPARSEABLE) { 383 catalogManager.debug.message(1, "Unparseable catalog: " + RFC2483); 384 } else if (cex.getExceptionType() 385 == CatalogException.UNKNOWN_FORMAT) { 386 catalogManager.debug.message(1, "Unknown catalog format: " + RFC2483); 387 } 388 return null; 389 } catch (MalformedURLException mue) { 390 catalogManager.debug.message(1, "Malformed resolver URL: " + RFC2483); 391 return null; 392 } catch (IOException ie) { 393 catalogManager.debug.message(1, "I/O Exception opening resolver: " + RFC2483); 394 return null; 395 } 396 } 397 398 405 private Vector appendVector(Vector vec, Vector appvec) { 406 if (appvec != null) { 407 for (int count = 0; count < appvec.size(); count++) { 408 vec.addElement(appvec.elementAt(count)); 409 } 410 } 411 return vec; 412 } 413 414 421 public Vector resolveAllSystemReverse(String systemId) 422 throws MalformedURLException , IOException { 423 Vector resolved = new Vector (); 424 425 if (systemId != null) { 427 Vector localResolved = resolveLocalSystemReverse(systemId); 428 resolved = appendVector(resolved, localResolved); 429 } 430 431 Vector subResolved = resolveAllSubordinateCatalogs(SYSTEMREVERSE, 433 null, 434 null, 435 systemId); 436 437 return appendVector(resolved, subResolved); 438 } 439 440 447 public String resolveSystemReverse(String systemId) 448 throws MalformedURLException , IOException { 449 Vector resolved = resolveAllSystemReverse(systemId); 450 if (resolved != null && resolved.size() > 0) { 451 return (String ) resolved.elementAt(0); 452 } else { 453 return null; 454 } 455 } 456 457 484 public Vector resolveAllSystem(String systemId) 485 throws MalformedURLException , IOException { 486 Vector resolutions = new Vector (); 487 488 if (systemId != null) { 490 Vector localResolutions = resolveAllLocalSystem(systemId); 491 resolutions = appendVector(resolutions, localResolutions); 492 } 493 494 Vector subResolutions = resolveAllSubordinateCatalogs(SYSTEM, 496 null, 497 null, 498 systemId); 499 resolutions = appendVector(resolutions, subResolutions); 500 501 if (resolutions.size() > 0) { 502 return resolutions; 503 } else { 504 return null; 505 } 506 } 507 508 519 private Vector resolveAllLocalSystem(String systemId) { 520 Vector map = new Vector (); 521 String osname = System.getProperty("os.name"); 522 boolean windows = (osname.indexOf("Windows") >= 0); 523 Enumeration en = catalogEntries.elements(); 524 while (en.hasMoreElements()) { 525 CatalogEntry e = (CatalogEntry) en.nextElement(); 526 if (e.getEntryType() == SYSTEM 527 && (e.getEntryArg(0).equals(systemId) 528 || (windows 529 && e.getEntryArg(0).equalsIgnoreCase(systemId)))) { 530 map.addElement(e.getEntryArg(1)); 531 } 532 } 533 if (map.size() == 0) { 534 return null; 535 } else { 536 return map; 537 } 538 } 539 540 547 private Vector resolveLocalSystemReverse(String systemId) { 548 Vector map = new Vector (); 549 String osname = System.getProperty("os.name"); 550 boolean windows = (osname.indexOf("Windows") >= 0); 551 Enumeration en = catalogEntries.elements(); 552 while (en.hasMoreElements()) { 553 CatalogEntry e = (CatalogEntry) en.nextElement(); 554 if (e.getEntryType() == SYSTEM 555 && (e.getEntryArg(1).equals(systemId) 556 || (windows 557 && e.getEntryArg(1).equalsIgnoreCase(systemId)))) { 558 map.addElement(e.getEntryArg(0)); 559 } 560 } 561 if (map.size() == 0) { 562 return null; 563 } else { 564 return map; 565 } 566 } 567 568 596 private synchronized Vector resolveAllSubordinateCatalogs(int entityType, 597 String entityName, 598 String publicId, 599 String systemId) 600 throws MalformedURLException , IOException { 601 602 Vector resolutions = new Vector (); 603 604 for (int catPos = 0; catPos < catalogs.size(); catPos++) { 605 Resolver c = null; 606 607 try { 608 c = (Resolver) catalogs.elementAt(catPos); 609 } catch (ClassCastException e) { 610 String catfile = (String ) catalogs.elementAt(catPos); 611 c = (Resolver) newCatalog(); 612 613 try { 614 c.parseCatalog(catfile); 615 } catch (MalformedURLException mue) { 616 catalogManager.debug.message(1, "Malformed Catalog URL", catfile); 617 } catch (FileNotFoundException fnfe) { 618 catalogManager.debug.message(1, "Failed to load catalog, file not found", 619 catfile); 620 } catch (IOException ioe) { 621 catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile); 622 } 623 624 catalogs.setElementAt(c, catPos); 625 } 626 627 String resolved = null; 628 629 if (entityType == DOCTYPE) { 631 resolved = c.resolveDoctype(entityName, 632 publicId, 633 systemId); 634 if (resolved != null) { 635 resolutions.addElement(resolved); 637 return resolutions; 638 } 639 } else if (entityType == DOCUMENT) { 640 resolved = c.resolveDocument(); 641 if (resolved != null) { 642 resolutions.addElement(resolved); 644 return resolutions; 645 } 646 } else if (entityType == ENTITY) { 647 resolved = c.resolveEntity(entityName, 648 publicId, 649 systemId); 650 if (resolved != null) { 651 resolutions.addElement(resolved); 653 return resolutions; 654 } 655 } else if (entityType == NOTATION) { 656 resolved = c.resolveNotation(entityName, 657 publicId, 658 systemId); 659 if (resolved != null) { 660 resolutions.addElement(resolved); 662 return resolutions; 663 } 664 } else if (entityType == PUBLIC) { 665 resolved = c.resolvePublic(publicId, systemId); 666 if (resolved != null) { 667 resolutions.addElement(resolved); 669 return resolutions; 670 } 671 } else if (entityType == SYSTEM) { 672 Vector localResolutions = c.resolveAllSystem(systemId); 673 resolutions = appendVector(resolutions, localResolutions); 674 break; 675 } else if (entityType == SYSTEMREVERSE) { 676 Vector localResolutions = c.resolveAllSystemReverse(systemId); 677 resolutions = appendVector(resolutions, localResolutions); 678 } 679 } 680 681 if (resolutions != null) { 682 return resolutions; 683 } else { 684 return null; 685 } 686 } 687 } 688 689 690 691 692 | Popular Tags |