mercredi 17 janvier 2018

How to use DOMCatalogParser?

I am trying to parse an XML catalog file using the following code:

  ByteArrayInputStream bais = ... // loading xml from a file        
  DOMCatalogReader r = new DOMCatalogReader();
  String targetClass = "MyCatalogParser";

  r.setCatalogParser(null, "catalog", targetClass);
  Catalog catalog = new Catalog();
  InputStream is = bais;
  r.readCatalog(catalog, is); // line 73

MyCatalogParser is my custom DOMCatalogParser class:

import com.sun.org.apache.xml.internal.resolver.Catalog;
import com.sun.org.apache.xml.internal.resolver.readers.DOMCatalogParser;

public class MyCatalogParser implements DOMCatalogParser {

    @Override
    public void parseCatalogEntry(Catalog catalog, Node node) {
        System.out.println("test");
    }

}

The XML I am trying to parse is the following:

<?xml version="1.0"?>
<catalog >
  <group  prefer="public"  xml:base="a" >
    <public 
       publicId="b"
       uri="docbook45/docbookx.dtd"/>

    <system
       systemId="c"
       uri="docbook45/docbookx.dtd"/>

    <system
       systemId="d"
       uri="docbook45/docbookx.dtd"/>
  </group>
</catalog>

I use the following Java version:

$ java -version
openjdk version "1.8.0_151"

Unfortunately I get the following exception:

Exception in thread "main" com.sun.org.apache.xml.internal.resolver.CatalogException: Catalog Exception 6
    at com.sun.org.apache.xml.internal.resolver.readers.DOMCatalogReader.readCatalog(DOMCatalogReader.java:202)
    at Main.main(Main.java:73)

Digging a bit further, I found out that for some reason MyCatalogParser cannot be loaded:

try {
  domParser = (DOMCatalogParser) ReflectUtil.forName(domParserClass).newInstance();
} catch (ClassNotFoundException cnfe) {
  catalog.getCatalogManager().debug.message(1, "Cannot load XML Catalog Parser class", domParserClass);
  throw new CatalogException(CatalogException.UNPARSEABLE);

It seems like the classloader does not find the application class MyCatalogParser.

I tried to look for other implementations of DOMCatalogParser but did not find any in the JDK nor on the Internet.

Is there a way to make this work knowing that the classloader cannot load any application class extending DOMCatalogParser and that there are no implementation of DOMCatalogParser in the JDK?





Aucun commentaire:

Enregistrer un commentaire