diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.java | 23 | ||||
-rw-r--r-- | src/TolerantXMLErrorHandler.java | 103 |
2 files changed, 126 insertions, 0 deletions
diff --git a/src/Main.java b/src/Main.java index e69de29..99d98c5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -0,0 +1,23 @@ +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; + +public class Main { + public static void main(String[] argv) { + File xmlReport = new File(argv[0]); + SAXReader saxReader = new SAXReader(); + System.err.println("Attached TolerantXMLErrorHandler: " + TolerantXMLErrorHandler.attach(saxReader)); + Document result; + try { + result = saxReader.read(xmlReport); + } catch (DocumentException ex) { + ex.printStackTrace(); + return; + } + Element root = result.getRootElement(); + System.out.println("ROOT: "+root); + } +} diff --git a/src/TolerantXMLErrorHandler.java b/src/TolerantXMLErrorHandler.java new file mode 100644 index 0000000..8e46a33 --- /dev/null +++ b/src/TolerantXMLErrorHandler.java @@ -0,0 +1,103 @@ +/** + * Copyright 2011 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +import org.dom4j.io.SAXReader; +import org.xml.sax.ErrorHandler; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; +import org.xml.sax.XMLReader; + +public class TolerantXMLErrorHandler implements ErrorHandler { + public static final String XERCES_FEATURE_PREFIX = "http://apache.org/xml/features/"; + public static final String CONTINUE_AFTER_FATAL_ERROR_FEATURE = "continue-after-fatal-error"; + public static final String msgContentIllegalInTrailingMisc = "Content is not allowed in trailing section."; + + public static final String XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE = + XERCES_FEATURE_PREFIX + CONTINUE_AFTER_FATAL_ERROR_FEATURE; + + public static boolean attach(SAXReader reader) { + boolean ok = true; + try { + reader.setFeature(XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE, true); + } catch (SAXException ex) { + ex.printStackTrace(); + return false; + } + reader.setErrorHandler(new TolerantXMLErrorHandler()); + return true; + } + + public static boolean attach(XMLReader reader) { + boolean ok = true; + try { + reader.setFeature(XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE, true); + } catch (SAXException ex) { + ex.printStackTrace(); + return false; + } + reader.setErrorHandler(new TolerantXMLErrorHandler()); + return true; + } + + public static boolean exceptionMessageEquals(Exception ex, final Class type, final String message) { + Throwable cause = ex; + while ( null != cause ) { + if( type.isInstance(cause) ) { + if( message.equals(cause.getMessage()) ) { + return true; + } + } + cause = cause.getCause(); + } + return false; + } + + public void warning(SAXParseException exception) throws SAXException { + exception.printStackTrace(); + } + + public void error(SAXParseException exception) throws SAXException { + exception.printStackTrace(); + } + + public void fatalError(SAXParseException exception) throws SAXException { + if(exceptionMessageEquals(exception, SAXException.class, msgContentIllegalInTrailingMisc)) { + printCatched("fatalError(SAXException "+msgContentIllegalInTrailingMisc+")", exception); + return; // keep going .. + } + throw exception; + } + + static void printCatched(String header, Exception ex) { + System.err.println("************************************************"); + System.err.println("Catched: " + header); + System.err.println("------------------------------------------------"); + ex.printStackTrace(); + System.err.println("************************************************"); + } +} |