You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2016/02/24 00:45:54 UTC

svn commit: r1731990 - in /webservices/axiom/trunk: aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/ aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/ implementations/axiom-dom/src/test/java/org/apache/ax...

Author: veithen
Date: Tue Feb 23 23:45:53 2016
New Revision: 1731990

URL: http://svn.apache.org/viewvc?rev=1731990&view=rev
Log:
When serializing to an OutputStream, make sure that the encoding in the XML declaration matches the actual output encoding.

Added:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/XmlDeclarationRewriterHandler.java   (with props)
Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomDocumentSupport.aj
    webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMImplementationTest.java
    webservices/axiom/trunk/implementations/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/OMImplementationTest.java
    webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/dimension/serialization/SerializeToXMLStreamWriter.java

Added: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/XmlDeclarationRewriterHandler.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/XmlDeclarationRewriterHandler.java?rev=1731990&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/XmlDeclarationRewriterHandler.java (added)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/XmlDeclarationRewriterHandler.java Tue Feb 23 23:45:53 2016
@@ -0,0 +1,44 @@
+/*
+ * 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.om.impl.common.serializer.push;
+
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlHandler;
+import org.apache.axiom.core.stream.XmlHandlerWrapper;
+import org.apache.axiom.om.OMOutputFormat;
+
+public final class XmlDeclarationRewriterHandler extends XmlHandlerWrapper {
+    private final OMOutputFormat format;
+    
+    public XmlDeclarationRewriterHandler(XmlHandler parent, OMOutputFormat format) {
+        super(parent);
+        this.format = format;
+    }
+
+    @Override
+    public void startDocument(String inputEncoding, String xmlVersion, String xmlEncoding,
+            boolean standalone) throws StreamException {
+        if (!format.isIgnoreXMLDeclaration()) {
+            if (format.getCharSetEncoding() != null) {
+                xmlEncoding = format.getCharSetEncoding();
+            }
+            super.startDocument(inputEncoding, xmlVersion, xmlEncoding, standalone);
+        }
+    }
+}

Propchange: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/XmlDeclarationRewriterHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj?rev=1731990&r1=1731989&r2=1731990&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerSupport.aj Tue Feb 23 23:45:53 2016
@@ -61,6 +61,7 @@ import org.apache.axiom.om.impl.common.S
 import org.apache.axiom.om.impl.common.builder.StAXHelper;
 import org.apache.axiom.om.impl.common.serializer.pull.OMXMLStreamReaderExAdapter;
 import org.apache.axiom.om.impl.common.serializer.pull.PullSerializer;
+import org.apache.axiom.om.impl.common.serializer.push.XmlDeclarationRewriterHandler;
 import org.apache.axiom.om.impl.common.serializer.push.XsiTypeFilterHandler;
 import org.apache.axiom.om.impl.common.serializer.push.sax.XMLReaderImpl;
 import org.apache.axiom.om.impl.common.serializer.push.stax.StAXSerializer;
@@ -267,9 +268,9 @@ public aspect AxiomContainerSupport {
         return result;
     }
 
-    private XmlHandler AxiomContainer.createSerializer(XMLStreamWriter writer, boolean useExistingNamespaceContext) {
+    private XmlHandler AxiomContainer.createSerializer(MTOMXMLStreamWriter writer, boolean useExistingNamespaceContext) {
         StAXSerializer serializer = new StAXSerializer(writer);
-        XmlHandler handler = serializer;
+        XmlHandler handler = new XmlDeclarationRewriterHandler(serializer, writer.getOutputFormat());
         CoreElement contextElement = getContextElement();
         if (contextElement != null) {
             handler = new XsiTypeFilterHandler(handler, contextElement);

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomDocumentSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomDocumentSupport.aj?rev=1731990&r1=1731989&r2=1731990&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomDocumentSupport.aj (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomDocumentSupport.aj Tue Feb 23 23:45:53 2016
@@ -63,19 +63,7 @@ public aspect AxiomDocumentSupport {
     // Overridden in AxiomSOAPMessageSupport
     public void AxiomDocument.internalSerialize(XmlHandler handler, OMOutputFormat format,
             boolean cache, boolean includeXMLDeclaration) throws StreamException {
-        if (includeXMLDeclaration) {
-            //Check whether the OMOutput char encoding and OMDocument char
-            //encoding matches, if not use char encoding of OMOutput
-            String encoding = format.getCharSetEncoding();
-            if (encoding == null || "".equals(encoding)) {
-                encoding = getCharsetEncoding();
-            }
-            String version = getXMLVersion();
-            if (version == null) {
-                version = "1.0";
-            }
-            handler.startDocument(null, version, encoding, true);
-        }
+        handler.startDocument(coreGetInputEncoding(), coreGetXmlVersion(), coreGetXmlEncoding(), coreIsStandalone());
         serializeChildren(handler, format, cache);
         handler.endDocument();
     }

Modified: webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMImplementationTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMImplementationTest.java?rev=1731990&r1=1731989&r2=1731990&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMImplementationTest.java (original)
+++ webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/OMImplementationTest.java Tue Feb 23 23:45:53 2016
@@ -23,9 +23,6 @@ import junit.framework.TestSuite;
 
 import org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactory;
 import org.apache.axiom.ts.om.OMTestSuiteBuilder;
-import org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOM;
-import org.apache.axiom.ts.om.container.TestSerialize;
-import org.apache.axiom.ts.om.document.TestClone;
 import org.apache.axiom.ts.om.document.TestDigest;
 import org.apache.axiom.ts.om.element.TestGetChildrenWithName4;
 import org.apache.axiom.ts.om.element.sr.TestClose;
@@ -41,12 +38,6 @@ public class OMImplementationTest extend
         builder.exclude(TestInsertSiblingAfterOnChild.class);
         builder.exclude(TestInsertSiblingBeforeOnChild.class);
         
-        // TODO: this case is not working because Axiom generates an XML declaration
-        //       but uses another charset encoding to serialize the document
-        builder.exclude(TestSerialize.class, "(&(file=iso-8859-1.xml)(container=document))");
-        builder.exclude(TestCreateOMBuilderFromDOM.class, "(file=iso-8859-1.xml)");
-        builder.exclude(TestClone.class, "(file=iso-8859-1.xml)");
-        
         // TODO: investigate why this is not working with DOOM
         builder.exclude(TestGetChildrenWithName4.class);
 

Modified: webservices/axiom/trunk/implementations/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/OMImplementationTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/OMImplementationTest.java?rev=1731990&r1=1731989&r2=1731990&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/OMImplementationTest.java (original)
+++ webservices/axiom/trunk/implementations/axiom-impl/src/test/java/org/apache/axiom/om/impl/llom/OMImplementationTest.java Tue Feb 23 23:45:53 2016
@@ -23,9 +23,6 @@ import junit.framework.TestSuite;
 
 import org.apache.axiom.om.impl.llom.factory.OMLinkedListMetaFactory;
 import org.apache.axiom.ts.om.OMTestSuiteBuilder;
-import org.apache.axiom.ts.om.builder.TestCreateOMBuilderFromDOM;
-import org.apache.axiom.ts.om.container.TestSerialize;
-import org.apache.axiom.ts.om.document.TestClone;
 import org.apache.axiom.ts.om.document.TestDigest;
 import org.apache.axiom.ts.om.element.sr.TestClose;
 import org.apache.axiom.ts.om.node.TestInsertSiblingAfterOnChild;
@@ -39,12 +36,6 @@ public class OMImplementationTest extend
         builder.exclude(TestInsertSiblingAfterOnChild.class);
         builder.exclude(TestInsertSiblingBeforeOnChild.class);
         
-        // TODO: this case is not working because Axiom generates an XML declaration
-        //       but uses another charset encoding to serialize the document
-        builder.exclude(TestSerialize.class, "(&(file=iso-8859-1.xml)(container=document))");
-        builder.exclude(TestCreateOMBuilderFromDOM.class, "(file=iso-8859-1.xml)");
-        builder.exclude(TestClone.class, "(file=iso-8859-1.xml)");
-        
         // TODO: if there is a comment node surrounded by text, then these text nodes need to be merged
         builder.exclude(TestDigest.class, "(|(file=digest3.xml)(file=digest4.xml))");
         

Modified: webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/dimension/serialization/SerializeToXMLStreamWriter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/dimension/serialization/SerializeToXMLStreamWriter.java?rev=1731990&r1=1731989&r2=1731990&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/dimension/serialization/SerializeToXMLStreamWriter.java (original)
+++ webservices/axiom/trunk/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/dimension/serialization/SerializeToXMLStreamWriter.java Tue Feb 23 23:45:53 2016
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
 import javax.xml.stream.XMLStreamWriter;
 
 import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.util.StAXUtils;
 import org.apache.axiom.testutils.suite.MatrixTestCase;
 
@@ -44,7 +45,17 @@ public class SerializeToXMLStreamWriter
 
     public XML serialize(OMContainer container) throws Exception {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(baos);
+        String encoding = null;
+        // Since Axiom has no way of knowing the encoding used by the XMLStreamWriter,
+        // it will just keep the original one when writing the XML declaration. Adjust
+        // the output encoding so that it will match the encoding in the XML declaration.
+        if (container instanceof OMDocument) {
+            encoding = ((OMDocument)container).getXMLEncoding();
+        }
+        if (encoding == null) {
+            encoding = "UTF-8";
+        }
+        XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(baos, encoding);
         if (cache) {
             container.serialize(writer);
         } else {