Return-Path: Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 50300 invoked from network); 23 May 2003 13:28:44 -0000 Received: from niobium.golden.net (199.166.210.90) by daedalus.apache.org with SMTP; 23 May 2003 13:28:44 -0000 Received: from dowman.net (186-111.SPEEDe.golden.net [216.75.186.111]) by niobium.golden.net (8.11.6/8.10.1) with ESMTP id h4NDShW16620 for ; Fri, 23 May 2003 09:28:44 -0400 (EDT) Date: Fri, 23 May 2003 09:28:42 -0400 Subject: Re: [digester] problem loading the dtd when using getResourceAsStream() Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v552) From: p To: "Jakarta Commons Users List" Content-Transfer-Encoding: 7bit In-Reply-To: <20030520143053.D30445@icarus.apache.org> Message-Id: <78CED659-8D22-11D7-A80E-000393BCFC62@dowman.net> X-Mailer: Apple Mail (2.552) X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N On Tuesday, May 20, 2003, at 05:37 PM, Craig R. McClanahan wrote: > > On Tue, 20 May 2003, p wrote: > >> Date: Tue, 20 May 2003 16:39:16 -0400 >> From: p >> Reply-To: Jakarta Commons Users List >> 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: >> >> >> >> 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 for the help, I changed it to use getResource() instead of getResourceAsStream(), which returns a file:/ url, and from there it's like your example. Theoretically, the file that I'm finding with getResource() could actually be inside a jar file, instead of a file on the filesystem, so I don't know what would happen in that case... I'm not sure what type of URL would be returned and whether the digester would be able to resolve something relative to it, but I'm not worrying about it :-) thanks.