1 3 56 57 package org.jboss.util.xml.catalog; 58 59 import java.io.IOException ; 60 import java.io.InputStream ; 61 import java.io.FileNotFoundException ; 62 import java.util.Enumeration ; 63 import java.util.Vector ; 64 import java.net.URL ; 65 import java.net.URLConnection ; 66 import java.net.MalformedURLException ; 67 68 import org.jboss.util.xml.catalog.readers.ExtendedXMLCatalogReader; 69 import org.jboss.util.xml.catalog.readers.OASISXMLCatalogReader; 70 import org.jboss.util.xml.catalog.readers.SAXCatalogReader; 71 import org.jboss.util.xml.catalog.readers.TR9401CatalogReader; 72 import org.jboss.util.xml.catalog.readers.XCatalogReader; 73 74 import javax.xml.parsers.SAXParserFactory ; 75 76 87 public class Resolver extends Catalog { 88 93 public static final int URISUFFIX = CatalogEntry.addEntryType("URISUFFIX", 2); 94 95 101 public static final int SYSTEMSUFFIX = CatalogEntry.addEntryType("SYSTEMSUFFIX", 2); 102 103 108 public static final int RESOLVER = CatalogEntry.addEntryType("RESOLVER", 1); 109 110 118 public static final int SYSTEMREVERSE 119 = CatalogEntry.addEntryType("SYSTEMREVERSE", 1); 120 121 124 public void setupReaders() { 125 SAXParserFactory spf = SAXParserFactory.newInstance(); 126 spf.setNamespaceAware(true); 127 spf.setValidating(false); 128 129 SAXCatalogReader saxReader = new SAXCatalogReader(spf); 130 131 saxReader.setCatalogParser(null, "XMLCatalog", 132 XCatalogReader.class.getName()); 133 134 saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, 135 "catalog", 136 ExtendedXMLCatalogReader.class.getName()); 137 138 addReader("application/xml", saxReader); 139 140 TR9401CatalogReader textReader = new TR9401CatalogReader(); 141 addReader("text/plain", textReader); 142 } 143 144 154 public void addEntry(CatalogEntry entry) { 155 int type = entry.getEntryType(); 156 157 if (type == URISUFFIX) { 158 String suffix = normalizeURI(entry.getEntryArg(0)); 159 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 160 161 entry.setEntryArg(1, fsi); 162 163 catalogManager.debug.message(4, "URISUFFIX", suffix, fsi); 164 } else if (type == SYSTEMSUFFIX) { 165 String suffix = normalizeURI(entry.getEntryArg(0)); 166 String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1))); 167 168 entry.setEntryArg(1, fsi); 169 170 catalogManager.debug.message(4, "SYSTEMSUFFIX", suffix, fsi); 171 } 172 173 super.addEntry(entry); 174 } 175 176 196 public String resolveURI(String uri) 197 throws MalformedURLException , IOException { 198 199 String resolved = super.resolveURI(uri); 200 if (resolved != null) { 201 return resolved; 202 } 203 204 Enumeration enumt = catalogEntries.elements(); 205 while (enumt.hasMoreElements()) { 206 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 207 if (e.getEntryType() == RESOLVER) { 208 resolved = resolveExternalSystem(uri, e.getEntryArg(0)); 209 if (resolved != null) { 210 return resolved; 211 } 212 } else if (e.getEntryType() == URISUFFIX) { 213 String suffix = e.getEntryArg(0); 214 String result = e.getEntryArg(1); 215 216 if (suffix.length() <= uri.length() 217 && uri.substring(uri.length()-suffix.length()).equals(suffix)) { 218 return result; 219 } 220 } 221 } 222 223 return resolveSubordinateCatalogs(Catalog.URI, 225 null, 226 null, 227 uri); 228 } 229 230 253 public String resolveSystem(String systemId) 254 throws MalformedURLException , IOException { 255 256 String resolved = super.resolveSystem(systemId); 257 if (resolved != null) { 258 return resolved; 259 } 260 261 Enumeration enumt = catalogEntries.elements(); 262 while (enumt.hasMoreElements()) { 263 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 264 if (e.getEntryType() == RESOLVER) { 265 resolved = resolveExternalSystem(systemId, e.getEntryArg(0)); 266 if (resolved != null) { 267 return resolved; 268 } 269 } else if (e.getEntryType() == SYSTEMSUFFIX) { 270 String suffix = e.getEntryArg(0); 271 String result = e.getEntryArg(1); 272 273 if (suffix.length() <= systemId.length() 274 && systemId.substring(systemId.length()-suffix.length()).equals(suffix)) { 275 return result; 276 } 277 } 278 } 279 280 return resolveSubordinateCatalogs(Catalog.SYSTEM, 281 null, 282 null, 283 systemId); 284 } 285 286 315 public String resolvePublic(String publicId, String systemId) 316 throws MalformedURLException , IOException { 317 318 String resolved = super.resolvePublic(publicId, systemId); 319 if (resolved != null) { 320 return resolved; 321 } 322 323 Enumeration enumt = catalogEntries.elements(); 324 while (enumt.hasMoreElements()) { 325 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 326 if (e.getEntryType() == RESOLVER) { 327 if (systemId != null) { 328 resolved = resolveExternalSystem(systemId, 329 e.getEntryArg(0)); 330 if (resolved != null) { 331 return resolved; 332 } 333 } 334 resolved = resolveExternalPublic(publicId, e.getEntryArg(0)); 335 if (resolved != null) { 336 return resolved; 337 } 338 } 339 } 340 341 return resolveSubordinateCatalogs(Catalog.PUBLIC, 342 null, 343 publicId, 344 systemId); 345 } 346 347 355 protected String resolveExternalSystem(String systemId, String resolver) 356 throws MalformedURLException , IOException { 357 Resolver r = queryResolver(resolver, "i2l", systemId, null); 358 if (r != null) { 359 return r.resolveSystem(systemId); 360 } else { 361 return null; 362 } 363 } 364 365 373 protected String resolveExternalPublic(String publicId, String resolver) 374 throws MalformedURLException , IOException { 375 Resolver r = queryResolver(resolver, "fpi2l", publicId, null); 376 if (r != null) { 377 return r.resolvePublic(publicId, null); 378 } else { 379 return null; 380 } 381 } 382 383 393 protected Resolver queryResolver(String resolver, 394 String command, 395 String arg1, 396 String arg2) { 397 InputStream iStream = null; 398 String RFC2483 = resolver + "?command=" + command 399 + "&format=tr9401&uri=" + arg1 400 + "&uri2=" + arg2; 401 String line = null; 402 403 try { 404 URL url = new URL (RFC2483); 405 406 URLConnection urlCon = url.openConnection(); 407 408 urlCon.setUseCaches(false); 409 410 Resolver r = (Resolver) newCatalog(); 411 412 String cType = urlCon.getContentType(); 413 414 if (cType.indexOf(";") > 0) { 416 cType = cType.substring(0, cType.indexOf(";")); 417 } 418 419 r.parseCatalog(cType, urlCon.getInputStream()); 420 421 return r; 422 } catch (CatalogException cex) { 423 if (cex.getExceptionType() == CatalogException.UNPARSEABLE) { 424 catalogManager.debug.message(1, "Unparseable catalog: " + RFC2483); 425 } else if (cex.getExceptionType() 426 == CatalogException.UNKNOWN_FORMAT) { 427 catalogManager.debug.message(1, "Unknown catalog format: " + RFC2483); 428 } 429 return null; 430 } catch (MalformedURLException mue) { 431 catalogManager.debug.message(1, "Malformed resolver URL: " + RFC2483); 432 return null; 433 } catch (IOException ie) { 434 catalogManager.debug.message(1, "I/O Exception opening resolver: " + RFC2483); 435 return null; 436 } 437 } 438 439 446 private Vector appendVector(Vector vec, Vector appvec) { 447 if (appvec != null) { 448 for (int count = 0; count < appvec.size(); count++) { 449 vec.addElement(appvec.elementAt(count)); 450 } 451 } 452 return vec; 453 } 454 455 462 public Vector resolveAllSystemReverse(String systemId) 463 throws MalformedURLException , IOException { 464 Vector resolved = new Vector (); 465 466 if (systemId != null) { 468 Vector localResolved = resolveLocalSystemReverse(systemId); 469 resolved = appendVector(resolved, localResolved); 470 } 471 472 Vector subResolved = resolveAllSubordinateCatalogs(SYSTEMREVERSE, 474 null, 475 null, 476 systemId); 477 478 return appendVector(resolved, subResolved); 479 } 480 481 488 public String resolveSystemReverse(String systemId) 489 throws MalformedURLException , IOException { 490 Vector resolved = resolveAllSystemReverse(systemId); 491 if (resolved != null && resolved.size() > 0) { 492 return (String ) resolved.elementAt(0); 493 } else { 494 return null; 495 } 496 } 497 498 525 public Vector resolveAllSystem(String systemId) 526 throws MalformedURLException , IOException { 527 Vector resolutions = new Vector (); 528 529 if (systemId != null) { 531 Vector localResolutions = resolveAllLocalSystem(systemId); 532 resolutions = appendVector(resolutions, localResolutions); 533 } 534 535 Vector subResolutions = resolveAllSubordinateCatalogs(SYSTEM, 537 null, 538 null, 539 systemId); 540 resolutions = appendVector(resolutions, subResolutions); 541 542 if (resolutions.size() > 0) { 543 return resolutions; 544 } else { 545 return null; 546 } 547 } 548 549 560 private Vector resolveAllLocalSystem(String systemId) { 561 Vector map = new Vector (); 562 String osname = System.getProperty("os.name"); 563 boolean windows = (osname.indexOf("Windows") >= 0); 564 Enumeration enumt = catalogEntries.elements(); 565 while (enumt.hasMoreElements()) { 566 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 567 if (e.getEntryType() == SYSTEM 568 && (e.getEntryArg(0).equals(systemId) 569 || (windows 570 && e.getEntryArg(0).equalsIgnoreCase(systemId)))) { 571 map.addElement(e.getEntryArg(1)); 572 } 573 } 574 if (map.size() == 0) { 575 return null; 576 } else { 577 return map; 578 } 579 } 580 581 588 private Vector resolveLocalSystemReverse(String systemId) { 589 Vector map = new Vector (); 590 String osname = System.getProperty("os.name"); 591 boolean windows = (osname.indexOf("Windows") >= 0); 592 Enumeration enumt = catalogEntries.elements(); 593 while (enumt.hasMoreElements()) { 594 CatalogEntry e = (CatalogEntry) enumt.nextElement(); 595 if (e.getEntryType() == SYSTEM 596 && (e.getEntryArg(1).equals(systemId) 597 || (windows 598 && e.getEntryArg(1).equalsIgnoreCase(systemId)))) { 599 map.addElement(e.getEntryArg(0)); 600 } 601 } 602 if (map.size() == 0) { 603 return null; 604 } else { 605 return map; 606 } 607 } 608 609 637 private synchronized Vector resolveAllSubordinateCatalogs(int entityType, 638 String entityName, 639 String publicId, 640 String systemId) 641 throws MalformedURLException , IOException { 642 643 Vector resolutions = new Vector (); 644 645 for (int catPos = 0; catPos < catalogs.size(); catPos++) { 646 Resolver c = null; 647 648 try { 649 c = (Resolver) catalogs.elementAt(catPos); 650 } catch (ClassCastException e) { 651 String catfile = (String ) catalogs.elementAt(catPos); 652 c = (Resolver) newCatalog(); 653 654 try { 655 c.parseCatalog(catfile); 656 } catch (MalformedURLException mue) { 657 catalogManager.debug.message(1, "Malformed Catalog URL", catfile); 658 } catch (FileNotFoundException fnfe) { 659 catalogManager.debug.message(1, "Failed to load catalog, file not found", 660 catfile); 661 } catch (IOException ioe) { 662 catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile); 663 } 664 665 catalogs.setElementAt(c, catPos); 666 } 667 668 String resolved = null; 669 670 if (entityType == DOCTYPE) { 672 resolved = c.resolveDoctype(entityName, 673 publicId, 674 systemId); 675 if (resolved != null) { 676 resolutions.addElement(resolved); 678 return resolutions; 679 } 680 } else if (entityType == DOCUMENT) { 681 resolved = c.resolveDocument(); 682 if (resolved != null) { 683 resolutions.addElement(resolved); 685 return resolutions; 686 } 687 } else if (entityType == ENTITY) { 688 resolved = c.resolveEntity(entityName, 689 publicId, 690 systemId); 691 if (resolved != null) { 692 resolutions.addElement(resolved); 694 return resolutions; 695 } 696 } else if (entityType == NOTATION) { 697 resolved = c.resolveNotation(entityName, 698 publicId, 699 systemId); 700 if (resolved != null) { 701 resolutions.addElement(resolved); 703 return resolutions; 704 } 705 } else if (entityType == PUBLIC) { 706 resolved = c.resolvePublic(publicId, systemId); 707 if (resolved != null) { 708 resolutions.addElement(resolved); 710 return resolutions; 711 } 712 } else if (entityType == SYSTEM) { 713 Vector localResolutions = c.resolveAllSystem(systemId); 714 resolutions = appendVector(resolutions, localResolutions); 715 break; 716 } else if (entityType == SYSTEMREVERSE) { 717 Vector localResolutions = c.resolveAllSystemReverse(systemId); 718 resolutions = appendVector(resolutions, localResolutions); 719 } 720 } 721 722 if (resolutions != null) { 723 return resolutions; 724 } else { 725 return null; 726 } 727 } 728 } 729 730 731 732 733 | Popular Tags |