Author: veithen
Date: Fri Aug  7 18:32:57 2009
New Revision: 802137

URL: http://svn.apache.org/viewvc?rev=802137&view=rev
Log:
Normalized the behavior of the getEncoding, getVersion, isStandalone, 
standaloneSet and getCharacterEncodingScheme methods in XMLStreamReader. The 
dialect implementations now make sure that they all consistently throw an 
exception if the state of the parser is different than START_DOCUMENT (as 
suggested by the StAX specifications). Tested for non regression with Axis2 
trunk.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXDialectTest.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAStreamReaderWrapper.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPStreamReaderWrapper.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxStreamReaderWrapper.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAStreamReaderWrapper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAStreamReaderWrapper.java?rev=802137&r1=802136&r2=802137&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAStreamReaderWrapper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAStreamReaderWrapper.java
 Fri Aug  7 18:32:57 2009
@@ -37,6 +37,38 @@
         this.encodingFromStartBytes = encodingFromStartBytes;
     }
 
+    public String getCharacterEncodingScheme() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getCharacterEncodingScheme();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public String getVersion() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getVersion();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public boolean isStandalone() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.isStandalone();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public boolean standaloneSet() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.standaloneSet();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
     public int next() throws XMLStreamException {
         if (!hasNext()) {
             // The reference implementation throws an XMLStreamException in 
this case.
@@ -53,22 +85,26 @@
     }
 
     public String getEncoding() {
-        // TODO: this needs some more unit testing!
-        String encoding = super.getEncoding();
-        if (encoding != null) {
-            return encoding;
-        } else {
-            if (encodingFromStartBytes == null) {
-                // This means that the reader was created from a character 
stream
-                // ==> always return null
-                return null;
+        if (getEventType() == START_DOCUMENT) {
+            // TODO: this needs some more unit testing!
+            String encoding = super.getEncoding();
+            if (encoding != null) {
+                return encoding;
             } else {
-                // If an XML encoding declaration was present, return the 
specified
-                // encoding, otherwise fall back to the encoding we detected in
-                // the factory wrapper
-                encoding = getCharacterEncodingScheme();
-                return encoding == null ? encodingFromStartBytes : encoding;
+                if (encodingFromStartBytes == null) {
+                    // This means that the reader was created from a character 
stream
+                    // ==> always return null
+                    return null;
+                } else {
+                    // If an XML encoding declaration was present, return the 
specified
+                    // encoding, otherwise fall back to the encoding we 
detected in
+                    // the factory wrapper
+                    encoding = getCharacterEncodingScheme();
+                    return encoding == null ? encodingFromStartBytes : 
encoding;
+                }
             }
+        } else {
+            throw new IllegalStateException();
         }
     }
 

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPStreamReaderWrapper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPStreamReaderWrapper.java?rev=802137&r1=802136&r2=802137&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPStreamReaderWrapper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/SJSXPStreamReaderWrapper.java
 Fri Aug  7 18:32:57 2009
@@ -31,6 +31,46 @@
         super(parent);
     }
 
+    public String getCharacterEncodingScheme() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getCharacterEncodingScheme();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public String getEncoding() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getEncoding();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public String getVersion() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getVersion();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public boolean isStandalone() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.isStandalone();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public boolean standaloneSet() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.standaloneSet();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
     public String getLocalName() {
         // Fix for https://sjsxp.dev.java.net/issues/show_bug.cgi?id=21
         int event = super.getEventType();

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java?rev=802137&r1=802136&r2=802137&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialect.java
 Fri Aug  7 18:32:57 2009
@@ -63,6 +63,19 @@
  *             in appendix of the XML specifications.</li>
  *       </ul>
  *       </li>
+ *   <li>According to the table shown in the documentation of the
+ *       {...@link javax.xml.stream.XMLStreamReader} class, calls to
+ *       {...@link javax.xml.stream.XMLStreamReader#getEncoding()},
+ *       {...@link javax.xml.stream.XMLStreamReader#getVersion()},
+ *       {...@link javax.xml.stream.XMLStreamReader#isStandalone()},
+ *       {...@link javax.xml.stream.XMLStreamReader#standaloneSet()} and
+ *       {...@link 
javax.xml.stream.XMLStreamReader#getCharacterEncodingScheme()} are only allowed
+ *       in the {...@link javax.xml.stream.XMLStreamConstants#START_DOCUMENT} 
state. On the other
+ *       hand, this requirement is not mentioned in the documentation of the 
individual methods
+ *       and the majority of StAX implementations support calls to these 
methods in any state.
+ *       However, to improve portability, the dialect implementations 
normalize these methods to
+ *       throw an {...@link IllegalStateException} if they are called in a 
state other than
+ *       {...@link javax.xml.stream.XMLStreamConstants#START_DOCUMENT}.</li>
  * </ul>
  * <p>
  * Note that there are several ambiguities in the StAX specification which are 
not addressed by

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxStreamReaderWrapper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxStreamReaderWrapper.java?rev=802137&r1=802136&r2=802137&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxStreamReaderWrapper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/WoodstoxStreamReaderWrapper.java
 Fri Aug  7 18:32:57 2009
@@ -27,6 +27,46 @@
         super(reader);
     }
 
+    public String getCharacterEncodingScheme() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getCharacterEncodingScheme();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public String getEncoding() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getEncoding();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public String getVersion() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.getVersion();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public boolean isStandalone() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.isStandalone();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
+    public boolean standaloneSet() {
+        if (getEventType() == START_DOCUMENT) {
+            return super.standaloneSet();
+        } else {
+            throw new IllegalStateException();
+        }
+    }
+
     public boolean isCharacters() {
         // TODO: in the dialect detection we should take into account the 
Woodstox version,
         //       so that we can avoid creating the wrapper for Woodstox 
versions where this

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXDialectTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXDialectTest.java?rev=802137&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXDialectTest.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXDialectTest.java
 Fri Aug  7 18:32:57 2009
@@ -0,0 +1,96 @@
+/*
+ * 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.
+ */
+
+package org.apache.axiom.util.stax.dialect;
+
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.axiom.om.util.StAXUtils;
+
+public class StAXDialectTest extends TestCase {
+    public void testGetEncoding() throws Exception {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(new 
ByteArrayInputStream(
+                "<?xml version='1.0' 
encoding='iso-8859-15'?><root/>".getBytes("iso-8859-15")));
+        assertEquals("iso-8859-15", reader.getEncoding());
+        reader.next();
+        try {
+            reader.getEncoding();
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
+
+    public void testGetVersion() throws Exception {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(
+                new StringReader("<?xml version='1.0'?><root/>"));
+        assertEquals("1.0", reader.getVersion());
+        reader.next();
+        try {
+            reader.getVersion();
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
+
+    public void testIsStandalone() throws Exception {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(
+                new StringReader("<?xml version='1.0' 
standalone='no'?><root/>"));
+        assertEquals(false, reader.isStandalone());
+        reader.next();
+        try {
+            reader.isStandalone();
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
+
+    public void testStandaloneSet() throws Exception {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(
+                new StringReader("<?xml version='1.0'?><root/>"));
+        assertEquals(false, reader.standaloneSet());
+        reader.next();
+        try {
+            reader.standaloneSet();
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
+
+    public void testGetCharacterEncodingScheme() throws Exception {
+        XMLStreamReader reader = StAXUtils.createXMLStreamReader(new 
ByteArrayInputStream(
+                "<?xml version='1.0' 
encoding='iso-8859-15'?><root/>".getBytes("iso-8859-15")));
+        assertEquals("iso-8859-15", reader.getCharacterEncodingScheme());
+        reader.next();
+        try {
+            reader.getCharacterEncodingScheme();
+            fail("Expected IllegalStateException");
+        } catch (IllegalStateException ex) {
+            // Expected
+        }
+    }
+}

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXDialectTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to