You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Jeff Caddel <jc...@cox.net> on 2003/03/21 13:38:54 UTC

Digester Best Practice

Any reasons for one of these approaches being better/worse than the other?

 URL url = 
Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/test.xml"); 

 InputStream input = url.openStream();
 Digester digester = new Digester();
 digester.parse(input);


 URL url = 
Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/test.xml"); 

 InputSource is = new InputSource(url.toExternalForm());
 is.setByteStream(input);
 InputSream input = url.openStream();
 Digester digester = new Digester();
 digester.parse(is);


ActionServlet does it the second way (InputSource), Tiles code does it 
the first way (InputStream).  I ran into a problem getting Digester to 
parse xml documents that use "includes" with the InputStream method:

<!DOCTYPE xxx PUBLIC "-//xx//xx 1.0//EN" "http://localhost/my-dtd.dtd" [
<!ENTITY users SYSTEM "users.xml">
]>
<my-xml>
     &users;
</my-xml>

It throws "org.xml.sax.SAXParseException: Relative URI "users.xml"; can 
not be resolved without a base URI." 
Switching to the InputSource method let Digester do it's thing, but I'm 
unclear on why.


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Digester Best Practice

Posted by Jeff Caddel <jc...@cox.net>.
Ahhh.  Ok.  So something inside InputSource retains a reference to the 
base uri which keeps the parser happy.  Makes perfect sense.  

We use xml includes a lot for our internal config files so knowing about 
this is a big time saver.

>Using the second form, with an InputSource, lets Struts say "the absolute
>URL of the containing document is xxxxx", so that relative URLs can be
>resolved correctly.
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Digester Best Practice

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 21 Mar 2003, Jeff Caddel wrote:

> Date: Fri, 21 Mar 2003 05:38:54 -0700
> From: Jeff Caddel <jc...@cox.net>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Digester Best Practice
>
> Any reasons for one of these approaches being better/worse than the other?
>
>  URL url =
> Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/test.xml");
>
>  InputStream input = url.openStream();
>  Digester digester = new Digester();
>  digester.parse(input);
>
>
>  URL url =
> Thread.currentThread().getContextClassLoader().getResource("/WEB-INF/test.xml");
>
>  InputSource is = new InputSource(url.toExternalForm());
>  is.setByteStream(input);
>  InputSream input = url.openStream();
>  Digester digester = new Digester();
>  digester.parse(is);
>
>
> ActionServlet does it the second way (InputSource), Tiles code does it
> the first way (InputStream).  I ran into a problem getting Digester to
> parse xml documents that use "includes" with the InputStream method:
>
> <!DOCTYPE xxx PUBLIC "-//xx//xx 1.0//EN" "http://localhost/my-dtd.dtd" [
> <!ENTITY users SYSTEM "users.xml">
> ]>
> <my-xml>
>      &users;
> </my-xml>
>
> It throws "org.xml.sax.SAXParseException: Relative URI "users.xml"; can
> not be resolved without a base URI."
> Switching to the InputSource method let Digester do it's thing, but I'm
> unclear on why.
>

The reason Struts does it the second way is to make includes work :-).

Consider what the XML parser has to do when it sees the "&users;" entity,
and you're using the first form:

    "Aha, I need to go find the 'users.xml' document and insert
    it here.  OK, 'users.xml' is a relative path, so I need to
    resolve it relative to the absolute path of the containing
    document.  And the URL for that is .... ooops ..."

Using the second form, with an InputSource, lets Struts say "the absolute
URL of the containing document is xxxxx", so that relative URLs can be
resolved correctly.

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org