Another day, another strange error with SAX, Java, and friends.
I need to iterate over a list of File
objects and pass them to a SAX parser. However, the parser fails because of an IOException
. However, the various File
object methods confirm that the file does indeed exist.
The output which I get:
11:53:57.838 [MainThread] DEBUG DefaultReactionFinder - C:\project\trunk\application\config\reactions\TestReactions.xml
11:53:57.841 [MainThread] ERROR DefaultReactionFinder - C:\project\trunk\application\config\reactions\null (The system cannot find the file specified)
So the problem is obviously that null
in the second line. I've tried nearly all variations of passing the file as a parameter to the parser, including as a String
(both from getAbsolutePath()
and entered by hand), as a URI
and, even more weirdly, as a FileInputStream
(for this I get the same error, except that the entire relative path gets reported as null, so C:\project\trunk\null
).
All that I can think of is that the SAXParserFactory
is incorrectly configured. I have no idea what is wrong, though.
Here is the code concerned:
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
try {
parser = factory.newSAXParser();
}
catch (ParserConfigurationException e) {
throw new InstantiationException("Error configuring an XML parser. Given error message: \"" + e.getMessage() + "\".");
}
catch (SAXException e) {
throw new InstantiationException("Error creating a SAX parser. Given error message: \"" + e.getMessage() + "\".");
}
...
for (File f : fileLister.getFileList()) {
logger.debug(f.getAbsolutePath());
try {
parser.parse(f, new ReactionHandler(input));
//FileInputStream fs = new FileInputStream(f);
//parser.parse(fs, new ReactionHandler(input));
//fs.close();
}
catch (IOException e) {
logger.error(e.getMessage());
throw new ReactionNotFoundException("An error occurred processing file \"" + f + "\".");
}
...
}
I have made no special provisions to provide a custom SAX parser implementation: I use the system default. Any help would be greatly appreciated!
Edit More information:
My code when I use streams:
FileInputStream fs = new FileInputStream(f);
InputSource is = new InputSource(fs);
//is.setSystemId(f.toURI().toString());
parser.parse(is, new ReactionHandler(input));
Which gives output
11:07:10.703 [MainThread] DEBUG DefaultReactionFinder - C:\project\trunk\application\config\reactions\TestReactions.xml
11:07:10.706 [MainThread] ERROR DefaultReactionFinder - C:\project\trunk\application\null (The system cannot find the file specified)
which shows that the relative directory of the XML file is not resolved correctly. If I include the line which is commented out, the relative directory is once again resolved correctly. This makes me think that somewhere some setting is incorrect...