Skip to content

Instantly share code, notes, and snippets.

@greut
Created September 27, 2016 22:58
Show Gist options
  • Save greut/5f59d0f6e4215f81b870728575debeda to your computer and use it in GitHub Desktop.
Save greut/5f59d0f6e4215f81b870728575debeda to your computer and use it in GitHub Desktop.
public DocbookXMLCatalogResolver implements XMLEntityResolver {
// ...
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
throws IOException, XNIException {
if (resourceIdentifier.getNamespace().equals("http://docbook.org/ns/docbook")) {
resourceIdentifier.setNamespace("http://docbook.org/xml/5.0/xsd/docbook.xsd");
}
XMLInputSource source = resolver.resolveEntity(resourceIdentifier);
if (resourceIdentifier.getNamespace().equals("http://docbook.org/xml/5.0/xsd/docbook.xsd")) {
resourceIdentifier.setNamespace("http://docbook.org/ns/docbook");
}
return source;
}
}
@greut
Copy link
Author

greut commented Sep 27, 2016

Some context...

dependencies {
    compile 'net.sf.docbook:docbook-xsl:1.79.1:resources@zip'
    compile 'net.sf.docbook:docbook-xml:5.0:xsd-resources@zip'
    compile group: 'net.sf.docbook', name: 'docbook-xsl-saxon', version: '1.0.1-pre'
    compile group: 'org.apache.xmlgraphics', name: 'fop', version: '2.1'
    compile group: 'net.sf.xslthl', name: 'xslthl', version: '2.1.0'
    compile group: 'xml-resolver', name: 'xml-resolver', version: '1.2'
    compile group: 'xerces', name: 'xercesImpl', version: '2.11.0'
    compile 'saxon:saxon:6.5.3'
}
            ClassLoader classLoader = Convert.class.getClassLoader();
            List<String> catalogFiles = new ArrayList<String>();
            String docbookCatalogName = "docbook/catalog.xml";
            URL docbookCatalog = classLoader.getResource(docbookCatalogName);
            if (docbookCatalog == null) {
                throw new IllegalStateException(
                        "Docbook catalog " + docbookCatalogName + " could not be found in " + classLoader);
            }

            catalogFiles.add(docbookCatalog.toExternalForm());

            Enumeration<URL> enumeration = classLoader.getResources("catalog.xml");
            while (enumeration.hasMoreElements()) {
                URL resource = (URL) enumeration.nextElement();
                catalogFiles.add(resource.toExternalForm());
            }

            DocbookXMLCatalogResolver xmlResolver = new DocbookXMLCatalogResolver();
            xmlResolver.setCatalogList(catalogFiles.toArray(new String[] {}));
            xmlResolver.setPreferPublic(true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment