Repository: cxf Updated Branches: refs/heads/2.7.x-fixes 5acddd2f2 -> 78bb7e5f4
[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/7f46e0e7 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/7f46e0e7 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/7f46e0e7 Branch: refs/heads/2.7.x-fixes Commit: 7f46e0e763a063876b56bfb940906965f996fc54 Parents: 5acddd2 Author: Grzegorz Grzybek <[email protected]> Authored: Wed May 7 12:47:04 2014 +0200 Committer: Daniel Kulp <[email protected]> Committed: Tue May 13 15:00:32 2014 -0400 ---------------------------------------------------------------------- api/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/7f46e0e7/api/pom.xml ---------------------------------------------------------------------- diff --git a/api/pom.xml b/api/pom.xml index 3e3ccd4..600c93f 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -124,6 +124,11 @@ <optional>true</optional> </dependency> <dependency> + <groupId>xalan</groupId> + <artifactId>xalan</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <scope>provided</scope> http://git-wip-us.apache.org/repos/asf/cxf/blob/7f46e0e7/api/src/main/java/org/apache/cxf/staxutils/StaxSource.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/cxf/staxutils/StaxSource.java b/api/src/main/java/org/apache/cxf/staxutils/StaxSource.java index 06f28e5..ca024fe 100644 --- a/api/src/main/java/org/apache/cxf/staxutils/StaxSource.java +++ b/api/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/7f46e0e7/api/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java b/api/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java index 17eb0f6..53ad86f 100644 --- a/api/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java +++ b/api/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.XMLUtils; 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/7f46e0e7/api/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl ---------------------------------------------------------------------- diff --git a/api/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl b/api/src/test/java/org/apache/cxf/staxutils/resources/copy.xsl new file mode 100644 index 0000000..a9223e8 --- /dev/null +++ b/api/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>
