Author: veithen
Date: Mon Aug  3 19:29:41 2009
New Revision: 800507

URL: http://svn.apache.org/viewvc?rev=800507&view=rev
Log:
Completed the dialect implementation for BEA's reference implementation.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAInputFactoryWrapper.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEADialect.java
    
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/StAXDialect.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
    webservices/commons/trunk/modules/axiom/modules/axiom-parser-tests/pom.xml

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEADialect.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEADialect.java?rev=800507&r1=800506&r2=800507&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEADialect.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEADialect.java
 Mon Aug  3 19:29:41 2009
@@ -48,7 +48,7 @@
     }
 
     public XMLStreamReader normalize(XMLStreamReader reader) {
-        return new BEAStreamReaderWrapper(reader);
+        return new BEAStreamReaderWrapper(reader, null);
     }
 
     public XMLStreamWriter normalize(XMLStreamWriter writer) {
@@ -56,7 +56,7 @@
     }
 
     public XMLInputFactory normalize(XMLInputFactory factory) {
-        return new NormalizingXMLInputFactoryWrapper(factory, this);
+        return new BEAInputFactoryWrapper(factory);
     }
 
     public XMLOutputFactory normalize(XMLOutputFactory factory) {

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAInputFactoryWrapper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAInputFactoryWrapper.java?rev=800507&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAInputFactoryWrapper.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/BEAInputFactoryWrapper.java
 Mon Aug  3 19:29:41 2009
@@ -0,0 +1,135 @@
+/*
+ * 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.IOException;
+import java.io.InputStream;
+import java.io.PushbackInputStream;
+import java.io.Reader;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.Source;
+
+import org.apache.axiom.util.stax.wrapper.XMLInputFactoryWrapper;
+
+public class BEAInputFactoryWrapper extends XMLInputFactoryWrapper {
+    public BEAInputFactoryWrapper(XMLInputFactory parent) {
+        super(parent);
+    }
+
+    public XMLStreamReader createXMLStreamReader(InputStream stream) throws 
XMLStreamException {
+        return createXMLStreamReader(null, stream);
+    }
+
+    public XMLStreamReader createXMLStreamReader(String systemId, InputStream 
stream)
+            throws XMLStreamException {
+        // The getEncoding() method of the stream reader produced by the 
reference implementation
+        // doesn't return complete information about the effective encoding. 
To work around this,
+        // we need to implement the detection algorithm described in Appendix 
F.1 of the
+        // XML 1.0 specifications (Fifth Edition). Note that the encoding 
determined here may be
+        // overridden by the XML encoding declaration, if present in the XML 
document. This
+        // information is already available from the stream reader, so that we 
don't need to
+        // reimplement this part.
+        // TODO: this needs some more unit testing!
+        byte[] startBytes = new byte[4];
+        try {
+            boolean useMark = stream.markSupported();
+            if (useMark) {
+                stream.mark(4);
+            } else {
+                stream = new PushbackInputStream(stream, 4);
+            }
+            int read = 0;
+            do {
+                int c = stream.read(startBytes, read, 4-read);
+                if (c == -1) {
+                    throw new XMLStreamException("Unexpected end of stream");
+                }
+                read += c;
+            } while (read < 4);
+            if (useMark) {
+                stream.reset();
+            } else {
+                ((PushbackInputStream)stream).unread(startBytes);
+            }
+        } catch (IOException ex) {
+            throw new XMLStreamException("Unable to read start bytes", ex);
+        }
+        int marker = (startBytes[0] & 0xFF) << 24 + (startBytes[1] & 0xFF) << 
16
+                + (startBytes[2] & 0xFF) << 8 + (startBytes[3] & 0xFF);
+        String encoding;
+        switch (marker) {
+            case 0x0000FEFF:
+            case 0xFFFE0000:
+            case 0x0000FFFE:
+            case 0xFEFF0000:
+            case 0x0000003C:
+            case 0x3C000000:
+            case 0x00003C00:
+            case 0x003C0000:
+                encoding = "UCS-4";
+                break;
+            case 0x003C003F:
+                encoding = "UTF-16BE";
+                break;
+            case 0x3C003F00:
+                encoding = "UTF-16LE";
+                break;
+            case 0x3C3F786D:
+                encoding = "UTF-8";
+                break;
+            default:
+                if ((marker & 0xFFFF0000) == 0xFEFF0000) {
+                    encoding = "UTF-16BE";
+                } else if ((marker & 0xFFFF0000) == 0xFFFE0000) {
+                    encoding = "UTF-16LE";
+                } else {
+                    encoding = "UTF-8";
+                }
+        }
+        XMLStreamReader reader;
+        if (systemId == null) {
+            reader = super.createXMLStreamReader(stream);
+        } else {
+            reader = super.createXMLStreamReader(systemId, stream);
+        }
+        return new BEAStreamReaderWrapper(reader, encoding);
+    }
+
+    public XMLStreamReader createXMLStreamReader(InputStream stream, String 
encoding)
+            throws XMLStreamException {
+        return new BEAStreamReaderWrapper(super.createXMLStreamReader(stream, 
encoding), null);
+    }
+
+    public XMLStreamReader createXMLStreamReader(Reader reader) throws 
XMLStreamException {
+        return new BEAStreamReaderWrapper(super.createXMLStreamReader(reader), 
null);
+    }
+
+    public XMLStreamReader createXMLStreamReader(Source source) throws 
XMLStreamException {
+        return new BEAStreamReaderWrapper(super.createXMLStreamReader(source), 
null);
+    }
+
+    public XMLStreamReader createXMLStreamReader(String systemId, Reader 
reader)
+            throws XMLStreamException {
+        return new 
BEAStreamReaderWrapper(super.createXMLStreamReader(systemId, reader), null);
+    }
+}

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

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=800507&r1=800506&r2=800507&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
 Mon Aug  3 19:29:41 2009
@@ -25,8 +25,14 @@
 import org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper;
 
 class BEAStreamReaderWrapper extends XMLStreamReaderWrapper {
-    public BEAStreamReaderWrapper(XMLStreamReader parent) {
+    /**
+     * The character set encoding as inferred from the start bytes of the 
stream.
+     */
+    private final String encodingFromStartBytes;
+    
+    public BEAStreamReaderWrapper(XMLStreamReader parent, String 
encodingFromStartBytes) {
         super(parent);
+        this.encodingFromStartBytes = encodingFromStartBytes;
     }
 
     public int next() throws XMLStreamException {
@@ -38,4 +44,24 @@
             return super.next();
         }
     }
+
+    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;
+            } 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;
+            }
+        }
+    }
 }

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=800507&r1=800506&r2=800507&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
 Mon Aug  3 19:29:41 2009
@@ -43,6 +43,26 @@
  *       normalizes the behavior of these methods so that they accept 
<code>null</code> values
  *       (in which case the methods will delegate to the corresponding 
variants without
  *       charset encoding parameter).</li>
+ *   <li>The StAX specifications require that {...@link 
javax.xml.stream.XMLStreamReader#getEncoding()}
+ *       returns the "input encoding if known or <code>null</code> if 
unknown". This requirement
+ *       is not precise enough to guarantee consistent behavior across 
different implementations.
+ *       In order to provide the consumer of the stream reader with complete 
and unambiguous information about
+ *       the encoding of the underlying stream, the dialect implementations 
normalize the
+ *       behavior of the {...@link 
javax.xml.stream.XMLStreamReader#getEncoding()} method such that
+ *       it returns a non null value if and only if the reader was created 
from a byte stream, in
+ *       which case the return value is the effective charset encoding used by 
the parser to
+ *       decode the byte stream. According to the XML specifications, this 
value is determined
+ *       by one of the following means:
+ *       <ul>
+ *         <li>The encoding was provided when the stream reader was created, 
i.e. as a parameter
+ *             to the {...@link 
javax.xml.stream.XMLInputFactory#createXMLStreamReader(java.io.InputStream, 
String)}
+ *             method. This is referred to as "external encoding information" 
by the XML
+ *             specifications.</li>
+ *         <li>The encoding was specified by the XML encoding declaration.</li>
+ *         <li>The encoding was detected using the first four bytes of the 
stream, as described
+ *             in appendix of the XML specifications.</li>
+ *       </ul>
+ *       </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/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java?rev=800507&r1=800506&r2=800507&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java
 Mon Aug  3 19:29:41 2009
@@ -105,4 +105,11 @@
             }
         });
     }
+    
+    public void testCreateXMLStreamWriterWithNullEncoding() throws Exception {
+        // This should not cause a NullPointerException
+        XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(System.out, 
null);
+        writer.writeEmptyElement("root");
+        writer.close();
+    }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-parser-tests/pom.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-parser-tests/pom.xml?rev=800507&r1=800506&r2=800507&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-parser-tests/pom.xml 
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-parser-tests/pom.xml 
Mon Aug  3 19:29:41 2009
@@ -150,11 +150,11 @@
                                     <artifactId>sjsxp</artifactId>
                                     <version>1.0.1</version>
                                 </artifactItem>
-                                <!-- artifactItem>
+                                <artifactItem>
                                     <groupId>stax</groupId>
                                     <artifactId>stax</artifactId>
                                     <version>1.2.0</version>
-                                </artifactItem -->
+                                </artifactItem>
                             </artifactItems>
                             
<outputDirectory>${project.build.directory}/parsers</outputDirectory>
                         </configuration>


Reply via email to