diff options
author | Sven Gothel <[email protected]> | 2011-02-25 01:36:13 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-02-25 01:36:13 +0100 |
commit | d8cede517dee35baafa0ca02016f4c6445fb4fa9 (patch) | |
tree | bc83871249ff82e0870aa9317e490e1acac4980b | |
parent | 52c005fb1ce1339cf6dca30f4a30f6edc430d597 (diff) |
-rw-r--r-- | nbproject/private/private.properties | 2 | ||||
-rw-r--r-- | nbproject/private/private.xml | 8 | ||||
-rw-r--r-- | src/Main.java | 7 | ||||
-rw-r--r-- | src/TolerantXMLErrorHandler.java | 103 | ||||
-rw-r--r-- | src/XMLTolerantErrorHandler.java | 128 |
5 files changed, 142 insertions, 106 deletions
diff --git a/nbproject/private/private.properties b/nbproject/private/private.properties index a81cb4f..ad38b64 100644 --- a/nbproject/private/private.properties +++ b/nbproject/private/private.properties @@ -3,4 +3,4 @@ do.depend=false do.jar=true javac.debug=true javadoc.preview=true -user.properties.file=/home/sven/.netbeans/6.9/build.properties +user.properties.file=/home/sven/.netbeans/7.0beta2/build.properties diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 8ca126a..9cac8db 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -1,2 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> -<project-private xmlns="http://www.netbeans.org/ns/project-private/1"/> +<project-private xmlns="http://www.netbeans.org/ns/project-private/1"> + <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/> + <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/1"> + <file>file:/usr/local/projects/JOGL/jenkins/test-XmlTolerantInput01/src/Main.java</file> + <file>file:/usr/local/projects/JOGL/jenkins/test-XmlTolerantInput01/src/XMLTolerantErrorHandler.java</file> + </open-files> +</project-private> diff --git a/src/Main.java b/src/Main.java index 99d98c5..e1eb1aa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,12 +4,17 @@ import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; +import org.xml.sax.SAXException; 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)); + try { + XMLTolerantErrorHandler.attach(saxReader, true); + } catch (SAXException ex) { + ex.printStackTrace(); + } Document result; try { result = saxReader.read(xmlReport); diff --git a/src/TolerantXMLErrorHandler.java b/src/TolerantXMLErrorHandler.java deleted file mode 100644 index 8e46a33..0000000 --- a/src/TolerantXMLErrorHandler.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * 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("************************************************"); - } -} diff --git a/src/XMLTolerantErrorHandler.java b/src/XMLTolerantErrorHandler.java new file mode 100644 index 0000000..b7629b0 --- /dev/null +++ b/src/XMLTolerantErrorHandler.java @@ -0,0 +1,128 @@ +/* + * The MIT License + * + * Copyright 2011 JogAmp Community, Sven Gothel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +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; + +/** + * Tolerant XML ErrorHandler. + * <p> + * The following XML error cases are currently tolerated (ignored): + * <ul> + * <li> XERCES - FatalError - Content is not allowed in trailing section.</li> + * </ul> + * </p> + * + */ +public class XMLTolerantErrorHandler 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; + + /** + * Attach an instance of this class as this reader's error handler. + * This will also set the feature {@link #CONTINUE_AFTER_FATAL_ERROR_FEATURE CONTINUE_AFTER_FATAL_ERROR_FEATURE}, + * assuming the SAX parser is XERCES. + * @param reader + */ + public static void attach(SAXReader reader, boolean ignoreSetFeatureException) + throws SAXException + { + try { + reader.setFeature(XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE, true); + } catch (SAXException ex) { + if(!ignoreSetFeatureException) { + throw ex; + } + ex.printStackTrace(); + } + reader.setErrorHandler(new XMLTolerantErrorHandler()); + } + + /** + * Attach an instance of this class as this reader's error handler. + * This will also set the feature {@link #CONTINUE_AFTER_FATAL_ERROR_FEATURE CONTINUE_AFTER_FATAL_ERROR_FEATURE}, + * assuming the SAX parser is XERCES. + * @param reader + * @return true if successful, otherwise false + */ + public static void attach(XMLReader reader, boolean ignoreSetFeatureException) + throws SAXException + { + try { + reader.setFeature(XERCES_CONTINUE_AFTER_FATAL_ERROR_FEATURE, true); + } catch (SAXException ex) { + if(!ignoreSetFeatureException) { + throw ex; + } + ex.printStackTrace(); + } + reader.setErrorHandler(new XMLTolerantErrorHandler()); + } + + public static boolean exceptionMessageEquals(Throwable cause, final Class exceptionType, final String exceptionMessage) { + while ( null != cause ) { + if( exceptionType.isInstance(cause) ) { + if( exceptionMessage.equals(cause.getMessage()) ) { + return true; + } + } + cause = cause.getCause(); + } + return false; + } + + public void warning(SAXParseException exception) throws SAXException { + System.err.println("Warning: " + exception.getMessage()); + exception.printStackTrace(); + } + + public void error(SAXParseException exception) throws SAXException { + System.err.println("Error: " + exception.getMessage()); + exception.printStackTrace(); + } + + public void fatalError(SAXParseException exception) throws SAXException { + if(exceptionMessageEquals(exception, SAXException.class, msgContentIllegalInTrailingMisc)) { + printCatched("fatalError(SAXException "+msgContentIllegalInTrailingMisc+")", exception); + return; // keep going .. + } + System.err.println("FatalError: " + exception.getMessage()); + 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("************************************************"); + } +} |