package org.madore.damlengine; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.MalformedURLException; import javax.xml.parsers.SAXParserFactory; import org.w3c.dom.ls.LSResourceResolver; import org.w3c.dom.ls.LSInput; import org.xml.sax.SAXException; import org.xml.sax.EntityResolver; import org.xml.sax.ext.EntityResolver2; import org.xml.sax.InputSource; import org.apache.xml.resolver.CatalogManager; import org.apache.xml.resolver.Catalog; import org.apache.xml.resolver.readers.SAXCatalogReader; import org.apache.xml.resolver.readers.OASISXMLCatalogReader; import org.apache.xerces.dom.DOMInputImpl; import org.apache.xerces.jaxp.SAXParserFactoryImpl; public final class Resolver implements LSResourceResolver, EntityResolver, EntityResolver2 { private static final String catalogFiles[] = { "/etc/xml/catalog" }; private CatalogManager catalogManager = null; private Catalog catalog = null; public static final class BootstrapResolver extends org.apache.xml.resolver.helpers.BootstrapResolver { @Override public InputSource resolveEntity(String publicId, String systemId) { String resolved = null; if ( publicId.equals("-//GlobalTransCorp//DTD XML Catalogs " +"V1.0-Based Extension V1.0//EN") ) { resolved = "file:///usr/share/xml/schema/xml-core/tr9401.dtd"; } if ( resolved != null ) { InputSource source = new InputSource(resolved); source.setPublicId(publicId); return source; } return super.resolveEntity(publicId, systemId); } } public Resolver() { catalogManager = new CatalogManager(); // catalogManager.setVerbosity(42); catalogManager.setPreferPublic(true); catalogManager.setRelativeCatalogs(false); catalogManager.setBootstrapResolver(new BootstrapResolver()); catalog = new Catalog(catalogManager); SAXParserFactory spf = new SAXParserFactoryImpl(); spf.setNamespaceAware(true); spf.setValidating(false); SAXCatalogReader saxReader = new SAXCatalogReader(spf); saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName, "catalog", "org.apache.xml.resolver.readers." +"OASISXMLCatalogReader"); catalog.addReader("application/xml", saxReader); for ( String f : catalogFiles ) { try { catalog.parseCatalog(f); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } } public String resolvePublic(String publicId, String systemId) { try { return catalog.resolvePublic(publicId, systemId); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } public String resolveSystem(String systemId) { try { return catalog.resolveSystem(systemId); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } @SuppressWarnings("unused") // namespaceURI parameter is unused protected String resolve(String namespaceURI, String publicId, String systemId, String baseURI) { String resolved; if ( publicId != null ) resolved = resolvePublic(publicId, systemId); else resolved = resolveSystem(systemId); if ( resolved != null ) return resolved; if ( baseURI != null ) { try { URI u = new URI(baseURI); URI v = u.resolve(systemId); return v.toString(); } catch ( URISyntaxException e ) { // Fail and continue to next return. } } return systemId; } // Interface LSResourceResolver public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { String resolved = resolve(namespaceURI, publicId, systemId, baseURI); return new DOMInputImpl(publicId, resolved, baseURI); } // Interface EntityResolver public InputSource resolveEntity(String publicId, String systemId) throws SAXException { String resolved = resolve(null, publicId, systemId, null); InputSource source = new InputSource(resolved); source.setPublicId(publicId); return source; } // Interface EntityResolver2 public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException { String resolved = resolve(null, publicId, systemId, baseURI); InputSource source = new InputSource(resolved); source.setPublicId(publicId); return source; } public InputSource getExternalSubset(String name, String baseURI) { return null; } }