Re: Empty File
Rolf Lear <[email protected]> Sat, 07 Apr 2012 20:33:45 -0400
| Newsgroups | gmane.comp.java.jdom.general |
|---|---|
| Message-ID | <[email protected]> |
Hi Bashiro Interesting question, and I can understand why you would ask, but, technically, no. JDOM itself does not read the file. It passes it directly to the configured XML Parser (Typically the SAX Parser). In many instances, JDOM does not even know it is reading a file... it is often converted to a URL before JDOM is aware of its source. It would not be possible to have a consistent way to check the 'input'. Further, there is no consistent way to produce errors. In your case you would possibly want an empty Document returned from the parse, but most people would want an Exception. So, the logical answer is no. If you know you are parsing a file, it would be easy for you to check the File.length() before parsing. If it is not a file source, there is no way for JDOM to intercept the stream before the SAX parser, so no, it cannot be done. Out of interest, I tried it to see what JDOM does, and, for any file with any amount of plain white-space (newlines, spaces, etc.) it all produces the same Exception: Exception in thread "main" org.jdom2.input.JDOMParseException: Error on line -1: Premature end of file. at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:232) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:277) at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:264) at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116) at net.tuis.debug.EMptyFile.main(EMptyFile.java:15) Caused by: org.xml.sax.SAXParseException: Premature end of file. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source) If you consistently use Xerces, you may be able to trap the JDOMException, and check to see whether the SAXException error is on line -1... which appears, at least in my experimenting, to indicate whether the file has white-space-only content. Thus, you may be able to trap the empty-source problem after the fact. Rolf On 07/04/2012 8:07 PM, Bashiro wrote: > Hello Folks, > > I wonder if Jdom can check a file is empty before attempting to read ? > > Bashiro > Drammen-Norway > > _______________________________________________ > To control your jdom-interest membership: > http://www.jdom.org/mailman/options/jdom-interest/[email protected] > _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/[email protected]