Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

Jason Hunter

2004-11-19

Replies:

Hi Dave,

I'm not so sure it's a JDOM issue. build(File) converts the File to a
URL string, then wraps the URL string as an InputSource and passes the
InputSource to the SAXParser.parse() method. In other words, your File
goes in to the underlying SAX parser as a string rather than as a file
handle. It's therefore up to the SAX parser to close the file after it
opens it. InputSource doesn't even have a close() method since it has
no open() functionality.

Which parsers are you having trouble with?

Now if this is a common bug in SAX parsers we could add a workaround by
doing what you do below, but then I believe the SAXParser will lose the
ability to naturally resolve relative paths.

CCing the list in case people know more about this issue.

-jh-

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@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.