Totally new to Java and OOP so there might be something very obvious to solve the problem. I have the following code as part of an XML viewer; the second code block below shows the code that cause the problem and the first code block is added for completeness. In the source they are consecutive.
DocumentBuilderFactory factory;
DocumentBuilder parser;
Document document;
String Error;
// setup the XML parser
try {
factory = DocumentBuilderFactory.newInstance();
}
catch (FactoryConfigurationError eFactoryConfigurationError) {
Error = "XML\n" + eFactoryConfigurationError;
JOptionPane.showMessageDialog(null, Error, "Software Error", JOptionPane.ERROR_MESSAGE);
return;
}
factory.setIgnoringComments(false); /* get comments */
factory.setCoalescing(false); /* get separate entries for cdata, text etc */
factory.setNamespaceAware(false);
factory.setValidating(false);
try {
// prevent the use of a possible DTD
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
parser = factory.newDocumentBuilder();
}
catch (ParserConfigurationException eParserConfigurationException) {
Error = "XML\n" + eParserConfigurationException;
JOptionPane.showMessageDialog(null, Error, "Software Error", JOptionPane.ERROR_MESSAGE);
return;
}
// parse the file
try {
document = parser.parse(chooser.getSelectedFile());
}
catch (SAXException eSAXException) {
Error = eSAXException.getMessage();
JOptionPane.showMessageDialog(null, Error, "XML error", JOptionPane.ERROR_MESSAGE);
return;
}
catch (IOException eIOException) {
Error = "" + eIOException.getMessage();
JOptionPane.showMessageDialog(null, Error, "File error", JOptionPane.ERROR_MESSAGE);
return;
}
If an xml file (indicated by chooser.getSelectedFile()) contains an error, a SAXException is thrown.
When I run the program in WindowsXP, I've found that the xml file is locked for writes by the parser and the lock is not released when the error occurs. At this stage I must close the java application to clear the lock. The problem does not occur if no SAXException is thrown.
So I'm looking for a way to remove the lock. Any advice?
PS 1)
I use an editor (PFE, very old) to edit the xml file and next reload it in the java application that contains the above code. The editor tells me that the file is in use when it tries to rename the file (as part of a save operation) after the exception is thrown (and even crashes after that, but that is something else).
PS 2)
I actually consider this a bug as the lock is released if there are no errors; I would expect the lock behaviour to be identical in an error and a none error situation; but I might be wrong here.
Edited by: WimS on Nov 13, 2009 5:51 AM