commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1410929 - in /commons/proper/digester/trunk: core/src/main/java/org/apache/commons/digester3/ core/src/test/java/org/apache/commons/digester3/ core/src/test/resources/org/apache/commons/digester3/ src/changes/
Date Sun, 18 Nov 2012 17:11:59 GMT
Author: simonetripodi
Date: Sun Nov 18 17:11:58 2012
New Revision: 1410929

URL: http://svn.apache.org/viewvc?rev=1410929&view=rev
Log:
[DIGESTER-172] Even with custom ErrorHandler, SAX errors are still written to stderr - patch
provided by Ivan Diana

Added:
    commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
  (with props)
    commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
  (with props)
Modified:
    commons/proper/digester/trunk/core/src/main/java/org/apache/commons/digester3/Digester.java
    commons/proper/digester/trunk/src/changes/changes.xml

Modified: commons/proper/digester/trunk/core/src/main/java/org/apache/commons/digester3/Digester.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/core/src/main/java/org/apache/commons/digester3/Digester.java?rev=1410929&r1=1410928&r2=1410929&view=diff
==============================================================================
--- commons/proper/digester/trunk/core/src/main/java/org/apache/commons/digester3/Digester.java
(original)
+++ commons/proper/digester/trunk/core/src/main/java/org/apache/commons/digester3/Digester.java
Sun Nov 18 17:11:58 2012
@@ -811,7 +811,15 @@ public class Digester
             reader.setEntityResolver( entityResolver );
         }
 
-        reader.setErrorHandler( this );
+        if ( this.errorHandler != null )
+        {
+            reader.setErrorHandler( this.errorHandler );
+        }
+        else
+        {
+            reader.setErrorHandler( this );
+        }
+
         return reader;
     }
 
@@ -1523,12 +1531,8 @@ public class Digester
     public void error( SAXParseException exception )
         throws SAXException
     {
-        log.error( "Parse Error at line " + exception.getLineNumber() + " column " + exception.getColumnNumber()
+ ": "
-            + exception.getMessage(), exception );
-        if ( errorHandler != null )
-        {
-            errorHandler.error( exception );
-        }
+    	log.error( "Parse Error at line " + exception.getLineNumber() + " column " + exception.getColumnNumber()
+ ": "
+                + exception.getMessage(), exception );
     }
 
     /**
@@ -1539,11 +1543,7 @@ public class Digester
         throws SAXException
     {
         log.error( "Parse Fatal Error at line " + exception.getLineNumber() + " column "
+ exception.getColumnNumber()
-            + ": " + exception.getMessage(), exception );
-        if ( errorHandler != null )
-        {
-            errorHandler.fatalError( exception );
-        }
+                + ": " + exception.getMessage(), exception );
     }
 
     /**
@@ -1553,13 +1553,8 @@ public class Digester
     public void warning( SAXParseException exception )
         throws SAXException
     {
-        if ( errorHandler != null )
-        {
-            log.warn( "Parse Warning Error at line " + exception.getLineNumber() + " column
"
-                          + exception.getColumnNumber() + ": " + exception.getMessage(),
exception );
-
-            errorHandler.warning( exception );
-        }
+        log.warn( "Parse Warning Error at line " + exception.getLineNumber() + " column "
+                + exception.getColumnNumber() + ": " + exception.getMessage(), exception
);
     }
 
     // ------------------------------------------------------- Public Methods
@@ -1647,12 +1642,6 @@ public class Digester
                        e );
             throw e;
         }
-        catch ( SAXException e )
-        {
-            log.error( format( "An error occurred while parsing XML from '%s', see nested
exceptions", systemId ),
-                       e );
-            throw e;
-        }
         cleanup();
         return this.<T> getRoot();
     }

Added: commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java?rev=1410929&view=auto
==============================================================================
--- commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
(added)
+++ commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
Sun Nov 18 17:11:58 2012
@@ -0,0 +1,170 @@
+package org.apache.commons.digester3;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.apache.commons.digester3.binder.DigesterLoader.newLoader;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+
+import org.apache.commons.digester3.binder.AbstractRulesModule;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Test;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public class ErrorHandlerTest
+{
+
+    @Test
+    public void noCustomErrorHandler()
+    {
+
+        try
+        {
+            newLoader( new AbstractRulesModule()
+            {
+                @Override
+                protected void configure()
+                {
+                    forPattern( "employee" ).createObject().ofType( Employee.class );
+                    forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute(
"name" );
+                }
+            } ).newDigester().parse( getClass().getResource( "Test-digester-172-wrong.xml"
) );
+            fail( "Expected SAXException" );
+        }
+        catch ( IOException e )
+        {
+            fail( "Expected SAXException" );
+        }
+        catch ( SAXException e )
+        {
+            // expected
+        }
+
+    }
+
+    @Test
+    public void customErrorHandlerWithStack()
+    {
+
+        ErrorHandler customErrorHandler = new ErrorHandler()
+        {
+            Log log = LogFactory.getLog( this.getClass() );
+
+            public void warning( SAXParseException arg0 )
+                throws SAXException
+            {
+                log.warn( "Custom Warn Handler" );
+            }
+
+            public void fatalError( SAXParseException e )
+                throws SAXException
+            {
+                log.fatal( "Custom Fatal Error Handler", e );
+            }
+
+            public void error( SAXParseException e )
+                throws SAXException
+            {
+                log.error( "Custom Error Handler", e );
+            }
+        };
+
+        try
+        {
+            Digester digester = newLoader( new AbstractRulesModule()
+            {
+                @Override
+                protected void configure()
+                {
+                    forPattern( "employee" ).createObject().ofType( Employee.class );
+                    forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute(
"name" );
+                }
+            } ).newDigester();
+            digester.setErrorHandler( customErrorHandler );
+            digester.parse( getClass().getResource( "Test-digester-172-wrong.xml" ) );
+            fail( "Expected SAXException" );
+        }
+        catch ( IOException e )
+        {
+            fail( "Expected SAXException" );
+        }
+        catch ( SAXException e )
+        {
+            // expected
+        }
+    }
+
+    @Test
+    public void customErrorHandler()
+    {
+
+        ErrorHandler customErrorHandler = new ErrorHandler()
+        {
+            Log log = LogFactory.getLog( this.getClass() );
+
+            public void warning( SAXParseException arg0 )
+                throws SAXException
+            {
+                log.warn( "Custom Warn Handler" );
+            }
+
+            public void fatalError( SAXParseException e )
+                throws SAXException
+            {
+                log.fatal( "Custom Fatal Error Handler" );
+            }
+
+            public void error( SAXParseException e )
+                throws SAXException
+            {
+                log.error( "Custom Error Handler" );
+            }
+        };
+
+        try
+        {
+            Digester digester = newLoader( new AbstractRulesModule()
+            {
+                @Override
+                protected void configure()
+                {
+                    forPattern( "employee" ).createObject().ofType( Employee.class );
+                    forPattern( "employee/firstName" ).setBeanProperty().extractPropertyNameFromAttribute(
"name" );
+                }
+            } ).newDigester();
+            digester.setErrorHandler( customErrorHandler );
+            digester.parse( getClass().getResource( "Test-digester-172-wrong.xml" ) );
+            fail( "No Expected SAXException" );
+
+        }
+        catch ( IOException e )
+        {
+            fail( "Expected SAXException" );
+        }
+        catch ( SAXException e )
+        {
+            // expected
+        }
+    }
+}

Propchange: commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/digester/trunk/core/src/test/java/org/apache/commons/digester3/ErrorHandlerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml?rev=1410929&view=auto
==============================================================================
--- commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
(added)
+++ commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
Sun Nov 18 17:11:58 2012
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+  
+      http://www.apache.org/licenses/LICENSE-2.0
+  
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<employee>
+  <firstName>First Name</firstName>
+</wrongCloserTagName>

Propchange: commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: commons/proper/digester/trunk/core/src/test/resources/org/apache/commons/digester3/Test-digester-172-wrong.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: commons/proper/digester/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/changes/changes.xml?rev=1410929&r1=1410928&r2=1410929&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/changes/changes.xml (original)
+++ commons/proper/digester/trunk/src/changes/changes.xml Sun Nov 18 17:11:58 2012
@@ -26,6 +26,9 @@
     <action dev="simonetripodi" type="fix" issue="DIGESTER-174" due-to="Andreas Sahlbach">
       Inner List Annotation has wrong @Target for most of the predefined annotation rules
     </action>
+    <action dev="simonetripodi" type="fix" issue="DIGESTER-172" due-to="Ivan Diana">
+      Even with custom ErrorHandler, SAX errors are still written to stderr
+    </action>
     <action dev="simonetripodi" type="fix" issue="DIGESTER-170" due-to="Dale Wijnand">
       Digester.pop(String) throws EmptyStackException where API doc says it returns null
     </action>



Mime
View raw message