1 16 17 package org.apache.xerces.util; 18 19 import java.io.IOException ; 20 21 import org.xml.sax.InputSource ; 22 import org.xml.sax.SAXException ; 23 import org.xml.sax.ext.EntityResolver2 ; 24 25 import org.w3c.dom.ls.LSInput ; 26 import org.w3c.dom.ls.LSResourceResolver ; 27 28 import javax.xml.parsers.SAXParserFactory ; 29 30 import org.apache.xerces.dom.DOMInputImpl; 31 import org.apache.xerces.jaxp.SAXParserFactoryImpl; 32 33 import org.apache.xerces.xni.XNIException; 34 import org.apache.xerces.xni.XMLResourceIdentifier; 35 36 import org.apache.xerces.xni.parser.XMLEntityResolver; 37 import org.apache.xerces.xni.parser.XMLInputSource; 38 39 import org.apache.xml.resolver.Catalog; 40 import org.apache.xml.resolver.CatalogManager; 41 import org.apache.xml.resolver.readers.OASISXMLCatalogReader; 42 import org.apache.xml.resolver.readers.SAXCatalogReader; 43 44 64 public class XMLCatalogResolver 65 implements XMLEntityResolver, EntityResolver2 , LSResourceResolver { 66 67 68 private CatalogManager fResolverCatalogManager = null; 69 70 71 private Catalog fCatalog = null; 72 73 74 private String [] fCatalogsList = null; 75 76 80 private boolean fCatalogsChanged = true; 81 82 83 private boolean fPreferPublic = true; 84 85 91 private boolean fUseLiteralSystemId = true; 92 93 96 public XMLCatalogResolver () { 97 this(null, true); 98 } 99 100 106 public XMLCatalogResolver (String [] catalogs) { 107 this(catalogs, true); 108 } 109 110 118 public XMLCatalogResolver (String [] catalogs, boolean preferPublic) { 119 init(catalogs, preferPublic); 120 } 121 122 127 public final synchronized String [] getCatalogList () { 128 return (fCatalogsList != null) 129 ? (String []) fCatalogsList.clone() : null; 130 } 131 132 141 public final synchronized void setCatalogList (String [] catalogs) { 142 fCatalogsChanged = true; 143 fCatalogsList = (catalogs != null) 144 ? (String []) catalogs.clone() : null; 145 } 146 147 150 public final synchronized void clear () { 151 fCatalog = null; 152 } 153 154 164 public final boolean getPreferPublic () { 165 return fPreferPublic; 166 } 167 168 176 public final void setPreferPublic (boolean preferPublic) { 177 fPreferPublic = preferPublic; 178 fResolverCatalogManager.setPreferPublic(preferPublic); 179 } 180 181 193 public final boolean getUseLiteralSystemId () { 194 return fUseLiteralSystemId; 195 } 196 197 217 public final void setUseLiteralSystemId (boolean useLiteralSystemId) { 218 fUseLiteralSystemId = useLiteralSystemId; 219 } 220 221 234 public InputSource resolveEntity(String publicId, String systemId) 235 throws SAXException , IOException { 236 237 String resolvedId = null; 238 if (publicId != null && systemId != null) { 239 resolvedId = resolvePublic(publicId, systemId); 240 } 241 else if (systemId != null) { 242 resolvedId = resolveSystem(systemId); 243 } 244 245 if (resolvedId != null) { 246 InputSource source = new InputSource (resolvedId); 247 source.setPublicId(publicId); 248 return source; 249 } 250 return null; 251 } 252 253 268 public InputSource resolveEntity(String name, String publicId, 269 String baseURI, String systemId) throws SAXException , IOException { 270 271 String resolvedId = null; 272 273 if (!getUseLiteralSystemId() && baseURI != null) { 274 try { 276 URI uri = new URI(new URI(baseURI), systemId); 277 systemId = uri.toString(); 278 } 279 catch (URI.MalformedURIException ex) {} 281 } 282 283 if (publicId != null && systemId != null) { 284 resolvedId = resolvePublic(publicId, systemId); 285 } 286 else if (systemId != null) { 287 resolvedId = resolveSystem(systemId); 288 } 289 290 if (resolvedId != null) { 291 InputSource source = new InputSource (resolvedId); 292 source.setPublicId(publicId); 293 return source; 294 } 295 return null; 296 } 297 298 309 public InputSource getExternalSubset(String name, String baseURI) 310 throws SAXException , IOException { 311 return null; 312 } 313 314 330 public LSInput resolveResource(String type, String namespaceURI, 331 String publicId, String systemId, String baseURI) { 332 333 String resolvedId = null; 334 335 try { 336 if (namespaceURI != null) { 340 resolvedId = resolveURI(namespaceURI); 341 } 342 343 if (!getUseLiteralSystemId() && baseURI != null) { 344 try { 346 URI uri = new URI(new URI(baseURI), systemId); 347 systemId = uri.toString(); 348 } 349 catch (URI.MalformedURIException ex) {} 351 } 352 353 if (resolvedId == null) { 359 if (publicId != null && systemId != null) { 360 resolvedId = resolvePublic(publicId, systemId); 361 } 362 else if (systemId != null) { 363 resolvedId = resolveSystem(systemId); 364 } 365 } 366 } 367 catch (IOException ex) {} 369 370 if (resolvedId != null) { 371 return new DOMInputImpl(publicId, resolvedId, baseURI); 372 } 373 return null; 374 } 375 376 377 389 public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) 390 throws XNIException, IOException { 391 392 String resolvedId = resolveIdentifier(resourceIdentifier); 393 if (resolvedId != null) { 394 return new XMLInputSource(resourceIdentifier.getPublicId(), 395 resolvedId, 396 resourceIdentifier.getBaseSystemId()); 397 } 398 return null; 399 } 400 401 412 public String resolveIdentifier(XMLResourceIdentifier resourceIdentifier) 413 throws IOException , XNIException { 414 415 String resolvedId = null; 416 417 String namespace = resourceIdentifier.getNamespace(); 421 if (namespace != null) { 422 resolvedId = resolveURI(namespace); 423 } 424 425 if (resolvedId == null) { 431 String publicId = resourceIdentifier.getPublicId(); 432 String systemId = getUseLiteralSystemId() 433 ? resourceIdentifier.getLiteralSystemId() 434 : resourceIdentifier.getExpandedSystemId(); 435 if (publicId != null && systemId != null) { 436 resolvedId = resolvePublic(publicId, systemId); 437 } 438 else if (systemId != null) { 439 resolvedId = resolveSystem(systemId); 440 } 441 } 442 return resolvedId; 443 } 444 445 461 public final synchronized String resolveSystem (String systemId) 462 throws IOException { 463 464 if (fCatalogsChanged) { 465 parseCatalogs(); 466 fCatalogsChanged = false; 467 } 468 return (fCatalog != null) 469 ? fCatalog.resolveSystem(systemId) : null; 470 } 471 472 487 public final synchronized String resolvePublic (String publicId, String systemId) 488 throws IOException { 489 490 if (fCatalogsChanged) { 491 parseCatalogs(); 492 fCatalogsChanged = false; 493 } 494 return (fCatalog != null) 495 ? fCatalog.resolvePublic(publicId, systemId) : null; 496 } 497 498 516 public final synchronized String resolveURI (String uri) 517 throws IOException { 518 519 if (fCatalogsChanged) { 520 parseCatalogs(); 521 fCatalogsChanged = false; 522 } 523 return (fCatalog != null) 524 ? fCatalog.resolveURI(uri) : null; 525 } 526 527 533 private void init (String [] catalogs, boolean preferPublic) { 534 fCatalogsList = (catalogs != null) ? (String []) catalogs.clone() : null; 535 fPreferPublic = preferPublic; 536 fResolverCatalogManager = new CatalogManager(); 537 fResolverCatalogManager.setAllowOasisXMLCatalogPI(false); 538 fResolverCatalogManager.setCatalogClassName("org.apache.xml.resolver.Catalog"); 539 fResolverCatalogManager.setCatalogFiles(""); 540 fResolverCatalogManager.setIgnoreMissingProperties(true); 541 fResolverCatalogManager.setPreferPublic(fPreferPublic); 542 fResolverCatalogManager.setRelativeCatalogs(false); 543 fResolverCatalogManager.setUseStaticCatalog(false); 544 fResolverCatalogManager.setVerbosity(0); 545 } 546 547 553 private void parseCatalogs () throws IOException { 554 if (fCatalogsList != null) { 555 fCatalog = new Catalog(fResolverCatalogManager); 556 attachReaderToCatalog(fCatalog); 557 for (int i = 0; i < fCatalogsList.length; ++i) { 558 String catalog = fCatalogsList[i]; 559 if (catalog != null && catalog.length() > 0) { 560 fCatalog.parseCatalog(catalog); 561 } 562 } 563 } 564 else { 565 fCatalog = null; 566 } 567 } 568 569 572 private void attachReaderToCatalog (Catalog catalog) { 573 574 SAXParserFactory spf = new SAXParserFactoryImpl(); 575 spf.setNamespaceAware(true); 576 spf.setValidating(false); 577 578 SAXCatalogReader saxReader = new SAXCatalogReader(spf); 579 saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog", 580 "org.apache.xml.resolver.readers.OASISXMLCatalogReader"); 581 catalog.addReader("application/xml", saxReader); 582 } 583 } 584 | Popular Tags |