You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Werner Guttmann <We...@msdw.com> on 2000/08/18 18:40:35 UTC

Using XMLCatalogHandler as EntityResolver()

Hi,

just looked (for the first time) into using XMLCatalogHandler as base
class for mapping "http://..." style SYSTEM ids in the DOCTYPE
definition to local files.

Here's the class I use to register with a SAXParser as EntityResolver.
It basically just extends XMLCatalogFile and adds some debug statements.

class MyXMLCatalogHandler extends XMLCatalogHandler {

    public void loadCatalog (InputSource source) throws Exception {}

    public InputSource resolveEntity (String publicId, String systemId)
      throws SAXException, IOException {
      InputSource source = super.resolveEntity(publicId, systemId);
      System.out.println("source=" +
source.getPublicId()+"/"+source.getSystemId());
      return source;
    }
}

and that's how I register the class with an XMLReader.

 XMLCatalogHandler catalog = new MyXMLCatalogHandler();
 catalog.addSystemMapping("http://www.msdw.com/DTD/2000/marginparams.dtd",

     "file:///c|/download/marginparams.dtd");
 reader.setEntityResolver(catalog);

When I run the sample program, I get the following exception ...

[Fatal Error] File"http://www.msdw.com/DTD/2000/marginparams.dtd" not
found.
XMl exception reading document.
org.xml.sax.SAXParseException:
File"http://www.msdw.com/DTD/2000/marginparams.dtd" not found
at org.apache.xerces.framework.XMLParser.reportError(XMLParser:975)
ar
org.apache.xerces.readers.DefaultEntityHandler.startReadingFromExternalEntity(DefaultEntityHandler:768)

at ...

What I do not understand is why I am still getting this exception,
especially since MyXMLCatalogHandler does return the correct
InputSource.

I subsequently has a look at the source code of Xerces 1.1.3, and at
DefaultReaderFactory in particular. At line 143 the following statement

        // create new input stream
        InputStream is = source.getByteStream();
        if (is == null) {

            // create url and open the stream
            URL url = new URL(systemId);
            is = url.openStream();
        }

uses the systemId for the creation of the URL rather than
source.getSystemId().

Since I am fairly new to Xerces (well, at least its source code ... ;-))
I would be very careful to say that I have found a bug. Can somebody
please tell me whether I am wrong, and if so, how I am supposed to use
the XMLCatalogHandler to resolve SYSTEM ids.

Regards
Werner Guttmann