[jdom-interest] Re: SAXBuilder.build( File )

Jason Hunter jhunter at xquery.com
Fri Nov 19 14:29:31 PST 2004


Elias, did you catch the part in my email where I said, "But then I 
believe the SAXParser will lose the ability to naturally resolve 
relative paths."  This simple fix is not so simple.

If you change from passing a string to passing a stream, you'll also 
have to pass a smart HandlerBase or DefaultHandler instance to handle 
external entity resolution.  Before going to all that trouble, I'd like 
to know why parsers aren't closing the streams they open and which 
parsers we're talking about here.

-jh-

Elias Ross wrote:

> A fix sounds pretty simple to me.
> 
> Have SAXBuilder.build(File) do this:
> 
> build(File f) {
>   InputStream is = FileInputStream(f);
>   try {
>     return build(is);
>   } finally {
>     is.close();
>   }
> }
> 
> build(URL url) {
>   InputStream = ...;
>   try {
>     return build(is);
>   } finally {
>     is.close();
>   }
> }
> 
> On Fri, 2004-11-19 at 12:14, Jason Hunter wrote:
> 
>>Dave Jarvis wrote:
>>
>>
>>>Hi, guys.
>>>
>>>Sorry for not using the servlet archive and mailing list; it's big, 
>>>unwieldy, tough to search, and severely limited in many respects. (I did 
>>>search for about 15 minutes for this bug.)
>>>
>>>There is a bug in JDOM 1.0 with SAXBuilder.build( File ) (and likely 
>>>build( URL )): the input stream is never closed. Since the file 
>>>descriptor is not closed, this can eventually lead into a "too many open 
>>>files" error (I have not tested this scenario; just a guess).
>>>
>>>On Windows platforms, however, if a file is parsed using build( File ), 
>>>then that File object cannot subsequently be renamed because Windows 
>>>locks open files.
>>>
>>>Windows test code:
>>>
>>>    File file = new File( "file.xml" );
>>>    SAXBuilder builder = getSAXBuilder();
>>>    Document document = builder.build( file );
>>>    boolean result = file.renameTo( new File( "renamed.xml" ) );
>>>
>>>    if( !result ) System.out.println( "Rename Failed" );
>>>
>>>Under Unix, the file is renamed, as expected.
>>>
>>>The fix is trivial:
>>>
>>>    File file = new File( "file.xml" );
>>>    FileInputStream in = new FileInputStream( file );
>>>    SAXBuilder builder = getSAXBuilder();
>>>    Document document = builder.build( in );
>>>    in.close();
>>>    boolean result = file.renameTo( new File( "renamed.xml" ) );
>>>
>>>    if( !result ) System.out.println( "Rename Failed" );
>>>
>>>I caught a suggestion by Brett that the build( File ) be removed, back 
>>>in 2000. Because both File and URL have no way of getting any 
>>>java.io.InputStream(Reader) that may be related to the object in 
>>>question, I would recommend removing them.
>>>
>>>Sincerely,
>>>Dave Jarvis
>>
>>_______________________________________________
>>To control your jdom-interest membership:
>>http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>>
> 
> 


More information about the jdom-interest mailing list