[jdom-interest] Empty File
jdom at tuis.net
Sat Apr 7 17:33:45 PDT 2012
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.
Caused by: org.xml.sax.SAXParseException: Premature end of file.
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.
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 ?
> To control your jdom-interest membership:
More information about the jdom-interest