Author: scheu
Date: Thu Mar 11 00:59:57 2010
New Revision: 921652

URL: http://svn.apache.org/viewvc?rev=921652&view=rev
Log:
WSCOMMONS-527
Contributor:Rich Scheuerle
Changed SafeXMLStreamReader to rethrow the prior exception if the parser is in 
an invalid state.
Added a validation test.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/resources/invalid_xml.xml
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SafeXMLStreamReader.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/builder/StAXOMBuilderTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SafeXMLStreamReader.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SafeXMLStreamReader.java?rev=921652&r1=921651&r2=921652&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SafeXMLStreamReader.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/SafeXMLStreamReader.java
 Thu Mar 11 00:59:57 2010
@@ -24,6 +24,8 @@ import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.util.StreamReaderDelegate;
 
 import org.apache.axiom.util.stax.wrapper.XMLStreamReaderContainer;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * XMLStreamReader wrapper that prevents access to the underlying parser
@@ -55,10 +57,15 @@ import org.apache.axiom.util.stax.wrappe
  * This class provides a simple way to prevent this type of issue by wrapping 
the underlying
  * parser implementation. After the first parsing error occurs, the wrapper 
prevents any call
  * to {...@link XMLStreamReader#next()} and similar methods on the underlying 
parser.
- * Any attempt to do so will immediately result in an error.
+ * Any attempt to do so will immediately result in the original error being 
thrown.
  */
 public class SafeXMLStreamReader extends StreamReaderDelegate implements 
XMLStreamReaderContainer {
+    
+    private static Log log = LogFactory.getLog(SafeXMLStreamReader.class);
     private boolean parserError;
+    private XMLStreamException xmlStreamException = null;
+    private RuntimeException runtimeException = null;
+    private Error error = null;
 
     public SafeXMLStreamReader(XMLStreamReader reader) {
         super(reader);
@@ -66,8 +73,18 @@ public class SafeXMLStreamReader extends
 
     private void checkError() throws XMLStreamException {
         if (parserError) {
-            throw new XMLStreamException(
-                    "Trying to read events from a parser that already reported 
an error before");
+            if (log.isDebugEnabled()) {
+                log.debug("Trying to read events from a parser that already 
reported an error before");
+            }
+            if (xmlStreamException != null) {
+                throw xmlStreamException;
+            } else if (runtimeException != null) {
+                throw runtimeException;
+            } else if (error != null) {
+                throw error;
+            } else {
+                throw new XMLStreamException("Trying to read events from a 
parser that already reported an error before");
+            }
         }
     }
 
@@ -76,12 +93,15 @@ public class SafeXMLStreamReader extends
             return super.getElementText();
         } catch (XMLStreamException ex) {
             parserError = true;
+            xmlStreamException = ex;
             throw ex;
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -91,9 +111,11 @@ public class SafeXMLStreamReader extends
             return super.getPIData();
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -103,9 +125,11 @@ public class SafeXMLStreamReader extends
             return super.getText();
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -115,9 +139,11 @@ public class SafeXMLStreamReader extends
             return super.getTextCharacters();
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -129,12 +155,15 @@ public class SafeXMLStreamReader extends
             return super.getTextCharacters(sourceStart, target, targetStart, 
length);
         } catch (XMLStreamException ex) {
             parserError = true;
+            xmlStreamException = ex;
             throw ex;
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -144,9 +173,11 @@ public class SafeXMLStreamReader extends
             return super.getTextLength();
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -156,9 +187,11 @@ public class SafeXMLStreamReader extends
             return super.getTextStart();
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -169,12 +202,15 @@ public class SafeXMLStreamReader extends
             return super.hasNext();
         } catch (XMLStreamException ex) {
             parserError = true;
+            xmlStreamException = ex;
             throw ex;
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -185,12 +221,15 @@ public class SafeXMLStreamReader extends
             return super.next();
         } catch (XMLStreamException ex) {
             parserError = true;
+            xmlStreamException = ex;
             throw ex;
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }
@@ -201,12 +240,15 @@ public class SafeXMLStreamReader extends
             return super.nextTag();
         } catch (XMLStreamException ex) {
             parserError = true;
+            xmlStreamException = ex;
             throw ex;
         } catch (RuntimeException ex) {
             parserError = true;
+            runtimeException = ex;
             throw ex;
         } catch (Error ex) {
             parserError = true;
+            error = ex;
             throw ex;
         }
     }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/builder/StAXOMBuilderTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/builder/StAXOMBuilderTest.java?rev=921652&r1=921651&r2=921652&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/builder/StAXOMBuilderTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/builder/StAXOMBuilderTest.java
 Thu Mar 11 00:59:57 2010
@@ -27,8 +27,11 @@ import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
 import org.apache.axiom.om.util.StAXUtils;
 
+import java.io.FileReader;
 import java.util.Iterator;
 
+import javax.xml.stream.XMLInputFactory;
+
 public class StAXOMBuilderTest extends AbstractTestCase {
     StAXOMBuilder stAXOMBuilder;
     private OMElement rootElement;
@@ -119,4 +122,37 @@ public class StAXOMBuilderTest extends A
         
         assertTrue(childrenCount == 5);
     }
+    
+ public void testInvalidXML() throws Exception {
+        
+        StAXOMBuilder stAXOMBuilder =
+                
OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getSOAP11Factory(),
+                                                        
XMLInputFactory.newInstance()
+                                                                       
.createXMLStreamReader(
+                                                                         
getTestResource("invalid_xml.xml")));
+        
+        Exception exception = null;
+        while (exception == null || stAXOMBuilder.isCompleted()) {
+            try {
+                stAXOMBuilder.next();
+            } catch (Exception e) {
+                exception =e;
+            }
+        }
+        
+        assertTrue("Expected an exception because invalid_xml.xml is wrong", 
exception != null);
+        
+        // Intentionally call builder again to make sure the same error is 
returned.
+        Exception exception2 = null;
+        try {
+            stAXOMBuilder.next();
+        } catch (Exception e) {
+            exception2 = e;
+        }
+        
+        assertTrue("Expected a second exception because invalid_xml.xml is 
wrong", exception2 != null);
+        assertTrue("Expected the same exception. first=" + exception + " 
second=" + exception2, 
+                    exception.getMessage().equals(exception2.getMessage()));
+        
+    }
 }
\ No newline at end of file

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/resources/invalid_xml.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/resources/invalid_xml.xml?rev=921652&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/resources/invalid_xml.xml
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/resources/invalid_xml.xml
 Thu Mar 11 00:59:57 2010
@@ -0,0 +1,54 @@
+<!--
+  ~ 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.
+  -->
+
+<ns1:Root xmlns:ns1="http://www.apache.org";
+    xmlns:ns2="http://www.opensource.lk";>
+    <ns2:developers > <!-- note ns3 prefix declaration is missing-->
+        <ns3:developer>
+            <name>Davanum Srinivas</name>
+            <id>dims</id>
+            <email>[email protected]</email>
+            <organization>Computer Associates</organization>
+        </ns3:developer>
+        <ns3:developer>
+            <name>Glen Daniels</name>
+            <id>glen</id>
+            <email>[email protected]</email>
+            <organization>Sonic Software</organization>
+        </ns3:developer>
+        <ns3:developer>
+            <name>Sanjiva Weerawarana</name>
+            <id>sanjiva</id>
+            <email>[email protected]</email>
+            <organization>IBM/LSF</organization>
+        </ns3:developer>
+        <ns3:developer>
+            <name>Eran Chinthaka</name>
+            <id>chinthaka</id>
+            <email>[email protected]</email>
+            <organization>LSF/Eurocenter DDC</organization>
+        </ns3:developer>
+        <ns3:developer>
+            <name>Jaliya Ekanayake</name>
+            <id>jaliya</id>
+            <email>[email protected]</email>
+            <organization>Virtusa/LSF</organization>
+        </ns3:developer>
+    </ns2:developers>
+</ns1:Root>
\ No newline at end of file


Reply via email to