cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject [2/4] git commit: [CXF-5733] Use standard way of generating SAX events with StaxSource
Date Wed, 07 May 2014 17:57:54 GMT
[CXF-5733] Use standard way of generating SAX events with StaxSource


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/109e40d4
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/109e40d4
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/109e40d4

Branch: refs/heads/master
Commit: 109e40d4511f7f5eaafd73e0a6e43877d7f6f6bc
Parents: 167de59
Author: Grzegorz Grzybek <gr.grzybek@gmail.com>
Authored: Wed May 7 12:47:04 2014 +0200
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Wed May 7 13:19:35 2014 -0400

----------------------------------------------------------------------
 core/pom.xml                                    |  5 ++
 .../org/apache/cxf/staxutils/StaxSource.java    | 43 ++++++------
 .../org/apache/cxf/staxutils/StaxUtilsTest.java | 71 +++++++++++++++++++-
 .../org/apache/cxf/staxutils/resources/copy.xsl | 30 +++++++++
 4 files changed, 124 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/109e40d4/core/pom.xml
----------------------------------------------------------------------
diff --git a/core/pom.xml b/core/pom.xml
index 7ec7609..006826b 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -138,6 +138,11 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>xalan</groupId>
+            <artifactId>xalan</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-beans</artifactId>
             <optional>true</optional>

http://git-wip-us.apache.org/repos/asf/cxf/blob/109e40d4/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java b/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java
index 06f28e5..ca024fe 100644
--- a/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java
+++ b/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java
@@ -117,6 +117,15 @@ public class StaxSource extends SAXSource implements XMLReader {
                     String qname = prefix != null && prefix.length() > 0 
                         ? prefix + ":" + localName : localName;
                     contentHandler.endElement(uri, localName, qname);
+                    // namespaces
+                    for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
+                        String nsPrefix = streamReader.getNamespacePrefix(i);
+                        String nsUri = streamReader.getNamespaceURI(i);
+                        if (nsUri == null) {
+                            nsUri = "";
+                        }
+                        contentHandler.endPrefixMapping(nsPrefix);
+                    }
                     break;
                 }
                 case XMLStreamConstants.ENTITY_DECLARATION:
@@ -135,6 +144,15 @@ public class StaxSource extends SAXSource implements XMLReader {
                     String prefix = streamReader.getPrefix();
                     String qname = prefix != null && prefix.length() > 0 
                         ? prefix + ":" + localName : localName;
+                    // namespaces
+                    for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
+                        String nsPrefix = streamReader.getNamespacePrefix(i);
+                        String nsUri = streamReader.getNamespaceURI(i);
+                        if (nsUri == null) {
+                            nsUri = "";
+                        }
+                        contentHandler.startPrefixMapping(nsPrefix, nsUri);
+                    }
                     contentHandler.startElement(uri == null ? "" : uri, localName, qname,
getAttributes());
                     break;
                 }
@@ -171,30 +189,7 @@ public class StaxSource extends SAXSource implements XMLReader {
 
     protected Attributes getAttributes() {
         AttributesImpl attrs = new AttributesImpl();
-        // Adding namespace declaration as attributes is necessary because
-        // the xalan implementation that ships with SUN JDK 1.4 is bugged
-        // and does not handle the startPrefixMapping method
-        for (int i = 0; i < streamReader.getNamespaceCount(); i++) {
-            String prefix = streamReader.getNamespacePrefix(i);
-            String uri = streamReader.getNamespaceURI(i);
-            if (uri == null) {
-                uri = "";
-            }
-            // Default namespace
-            if (prefix == null || prefix.length() == 0) {
-                attrs.addAttribute("", 
-                                   "", 
-                                   XMLConstants.XMLNS_ATTRIBUTE, 
-                                   "CDATA", 
-                                   uri);
-            } else {
-                attrs.addAttribute(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, 
-                                   prefix, 
-                                   XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix, 
-                                   "CDATA", 
-                                   uri);
-            }
-        }
+
         for (int i = 0; i < streamReader.getAttributeCount(); i++) {
             String uri = streamReader.getAttributeNamespace(i);
             String localName = streamReader.getAttributeLocalName(i);

http://git-wip-us.apache.org/repos/asf/cxf/blob/109e40d4/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java b/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
index 89b2ce9..d862e60 100644
--- a/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java
@@ -32,8 +32,13 @@ import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.stream.events.XMLEvent;
+import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
@@ -46,6 +51,8 @@ import org.apache.cxf.helpers.DOMUtils;
 import org.junit.Assert;
 import org.junit.Test;
 
+import static org.hamcrest.core.IsEqual.equalTo;
+
 public class StaxUtilsTest extends Assert {
 
     @Test
@@ -297,7 +304,69 @@ public class StaxUtilsTest extends Assert {
         writer.flush();
         baos.flush();
     }
-       
+
+    @Test
+    public void testDefaultPrefixInRootElementWithIdentityTransformer() throws Exception
{
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
+        StringReader stringReader = new StringReader(xml);
+        StreamSource source = new StreamSource(stringReader);
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
+        StaxSource staxSource = new StaxSource(reader);
+        TransformerFactory trf = TransformerFactory.newInstance();
+        Transformer transformer = trf.newTransformer();
+        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+        transformer.transform(staxSource, new StreamResult(baos));
+        writer.flush();
+        baos.flush();
+        assertThat(new String(baos.toByteArray()), equalTo(xml));
+    }
+
+    @Test
+    public void testDefaultPrefixInRootElementWithXalanCopyTransformer() throws Exception
{
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
+        StringReader stringReader = new StringReader(xml);
+        StreamSource source = new StreamSource(stringReader);
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
+        StaxSource staxSource = new StaxSource(reader);
+        TransformerFactory trf = TransformerFactory.newInstance();
+        Transformer transformer = trf.newTransformer(new StreamSource(getTestStream("./resources/copy.xsl")));
+        System.out.println("Used transformer: " + transformer.getClass().getName());
+        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+        transformer.transform(staxSource, new StreamResult(baos));
+        writer.flush();
+        baos.flush();
+        assertThat(new String(baos.toByteArray()), equalTo(xml));
+    }
+
+    @Test
+    public void testDefaultPrefixInRootElementWithJDKInternalCopyTransformer() throws Exception
{
+        TransformerFactory trf = null;
+        try {
+            trf = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
null);
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            String xml = "<root xmlns=\"urn:org.apache.cxf:test\">Text</root>";
+            StringReader stringReader = new StringReader(xml);
+            StreamSource source = new StreamSource(stringReader);
+            XMLStreamReader reader = StaxUtils.createXMLStreamReader(source);
+            XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(baos);
+            StaxSource staxSource = new StaxSource(reader);
+            Transformer transformer = trf.newTransformer(new StreamSource(getTestStream("./resources/copy.xsl")));
+            System.out.println("Used transformer: " + transformer.getClass().getName());
+            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+            transformer.transform(staxSource, new StreamResult(baos));
+            writer.flush();
+            baos.flush();
+            assertThat(new String(baos.toByteArray()), equalTo(xml));
+        } catch (Throwable throwable) {
+            // ignore on non Sun/Oracle JDK
+            return;
+        }
+    }
+
     @Test
     public void testCXF3193() throws Exception {
         String testString = "<a:elem1 xmlns:a=\"test\" xmlns:b=\"test\" a:attr1=\"value\"/>";

http://git-wip-us.apache.org/repos/asf/cxf/blob/109e40d4/core/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl b/core/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl
new file mode 100644
index 0000000..a9223e8
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl
@@ -0,0 +1,30 @@
+<?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.
+-->
+<xsl:stylesheet version="1.0"
+                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+    <!-- Idiomatic Copy Transformation -->
+    <xsl:template match="@*|node()">
+        <xsl:copy>
+            <xsl:apply-templates select="@*|node()" />
+        </xsl:copy>
+    </xsl:template>
+
+</xsl:stylesheet>


Mime
View raw message