commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: [digester] problem loading the dtd when using getResourceAsStream()
Date Tue, 20 May 2003 21:37:51 GMT


On Tue, 20 May 2003, p wrote:

> Date: Tue, 20 May 2003 16:39:16 -0400
> From: p <ll@dowman.net>
> Reply-To: Jakarta Commons Users List <commons-user@jakarta.apache.org>
> To: commons-user@jakarta.apache.org
> Subject: [digester] problem loading the dtd when using
>     getResourceAsStream()
>
> Hi,
>
> My XML file has a DTD declaration like this:
>
> <!DOCTYPE help SYSTEM "help.dtd">
>
> The DTD file is in the same directory as the XML file, which works when
> opening the file in an XML editor. But I am loading the file in my
> application (a web application) as a resource, like this:
>
> InputStream in = getClass().getResourceAsStream(helpFileName);
>
> Digester d = new Digester();
> d.setValidating(true);
> // ...
> d.parse(in);
>
> The error I get at runtime is:
> org.xml.sax.SAXParseException: Relative URI "help.dtd"; can not be
> resolved without a base URI.
>

That's because the InputStream you ultimately hand in to the XML parser
does not have any URI associated with it, so the parser has no mechanism
to resolve relative URIs.

> I still get this error if I do setValidating(false), which I find odd.
>

Even with validating turned off, the parser is still going to process the
doctype reference.

> So my question is, what should I use for the DTD URI? Or how should I
> be doing this differently? I can't specify an absolute URI starting
> with a '/' because I don't know where the app will be deployed on the
> sytem (which is why I'm using getResourceAsStream()). The DTD file is
> available as a resource, but how can I tell the digester where to find
> it?
>
> I think I could put the DTD on a web server somewhere and use a URI
> starting with "http://", but there are many reasons why I don't want to
> do that, so hopefully somebody will enlighten me with a better answer!
>

Another alternative is to use the org.xml.sax.InputSource mechanism to
provide a URI for the base document.  That way, the XML parser will
understand how to deal with relative references.  One approach (if you're
using files):

  URL url = (new File("... path to file ...")).toURL();
  InputSource is = new InputSource(url.toExternalForm());
  is.setByteStream(url.openStream());
  digester.parse(is);

In principle, this approach works for any sort of URL that you have a
resolver for (such as http and ftp).  The relative path "help.dtd" will be
resolved against the absolute URL being utilized.

> Thanks.

Craig McClanahan

Mime
View raw message