LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java xml parser keeps file locked after SAXException (https://www.linuxquestions.org/questions/programming-9/java-xml-parser-keeps-file-locked-after-saxexception-768613/)

Wim Sturkenboom 11-12-2009 03:31 AM

java xml parser keeps file locked after SAXException
 
Part of my program contains the below code to parse an xml file using standard java classes. My interest is in the blue part at the end where the code is doing the actual parsing.

Please note that I'm quite new to java and OOP, so there might be something very obvious that I have overlooked.
Code:

        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.

So I'm looking for a way to remove the lock. Any advice?


PS The editor that I use for editing the file tells me that the file is in use when I try to save the file.

gzunk 11-13-2009 06:52 AM

Not sure if this is related to your problem, but it would explain it:

http://bugs.sun.com/bugdatabase/view...bug_id=6359560

Essentially, Windows keeps a lock on memory mapped files even after you have closed them. Which makes memory mapped files useless on Windows. It's feasible that the XML parser implementation that you're using uses memory mapped files to read the file in.

You could try using a different XML parser implementation and seeing if that works.

Wim Sturkenboom 11-13-2009 10:27 PM

Thanks for the reply. I'm not 100% sure if it's the same (kind of) problem. I think that in that case windows would also keep the lock when a file is succesfully parsed.

I have posted the question on the sun forums as well and will see what will happen.

inthemill 02-24-2011 09:32 AM

Just in case some one, like me, finds this thread searching for an answer to this question.
Here is my solution:

instead of
Code:

        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;
        }

write
Code:

        InputStream stream = null;
        try {
            stream = new FileInputStream(chooser.getSelectedFile());
            document = parser.parse(stream);
        }
        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;
        }finally{
            if(stream != null){
                stream.close();
            }
        }



All times are GMT -5. The time now is 04:34 PM.