You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/05/08 11:31:20 UTC
[3/3] git commit: [CAMEL-7424] copy new version of StaxSource from
CXF (with tests)
[CAMEL-7424] copy new version of StaxSource from CXF (with tests)
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/70826d8b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/70826d8b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/70826d8b
Branch: refs/heads/camel-2.13.x
Commit: 70826d8b46902a5081f9aa9c37bbcee462e0f3aa
Parents: 6a75fdc
Author: Grzegorz Grzybek <gr...@gmail.com>
Authored: Thu May 8 10:34:17 2014 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu May 8 11:31:03 2014 +0200
----------------------------------------------------------------------
.../apache/camel/converter/jaxp/StaxSource.java | 56 ++++++++---------
.../camel/converter/jaxp/StaxSourceTest.java | 63 ++++++++++++++++++++
.../src/test/resources/xslt/common/copy.xsl | 28 +++++++++
3 files changed, 116 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/70826d8b/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxSource.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxSource.java b/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxSource.java
index 0fb6a6d..27bc0f6 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxSource.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/jaxp/StaxSource.java
@@ -17,7 +17,6 @@
package org.apache.camel.converter.jaxp;
-import javax.xml.XMLConstants;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -42,7 +41,7 @@ public class StaxSource extends SAXSource implements XMLReader {
private XMLStreamReader streamReader;
private ContentHandler contentHandler;
-
+
private LexicalHandler lexicalHandler;
public StaxSource(XMLStreamReader streamReader) {
@@ -101,7 +100,7 @@ public class StaxSource extends SAXSource implements XMLReader {
int start = streamReader.getTextStart();
char[] chars = streamReader.getTextCharacters();
lexicalHandler.comment(chars, start, length);
- }
+ }
break;
case XMLStreamConstants.DTD:
break;
@@ -112,9 +111,18 @@ public class StaxSource extends SAXSource implements XMLReader {
String uri = streamReader.getNamespaceURI();
String localName = streamReader.getLocalName();
String prefix = streamReader.getPrefix();
- String qname = prefix != null && prefix.length() > 0
+ 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:
@@ -131,8 +139,17 @@ public class StaxSource extends SAXSource implements XMLReader {
String uri = streamReader.getNamespaceURI();
String localName = streamReader.getLocalName();
String prefix = streamReader.getPrefix();
- String qname = prefix != null && prefix.length() > 0
+ 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;
}
@@ -169,30 +186,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);
@@ -218,7 +212,7 @@ public class StaxSource extends SAXSource implements XMLReader {
return false;
}
- public void setFeature(String name, boolean value)
+ public void setFeature(String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException {
}
@@ -226,7 +220,7 @@ public class StaxSource extends SAXSource implements XMLReader {
return null;
}
- public void setProperty(String name, Object value)
+ public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException {
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
lexicalHandler = (LexicalHandler) value;
http://git-wip-us.apache.org/repos/asf/camel/blob/70826d8b/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxSourceTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxSourceTest.java b/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxSourceTest.java
new file mode 100644
index 0000000..3d83d77
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/converter/jaxp/StaxSourceTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.camel.converter.jaxp;
+
+import java.io.ByteArrayOutputStream;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class StaxSourceTest extends ContextTestSupport {
+
+ private static final String TEST_XML = "<root xmlns=\"urn:org.apache.camel:test\">Text</root>";
+
+ private static final Charset UTF_8 = Charset.forName("UTF-8");
+
+ public void testDefaultPrefixInRootElementWithCopyTransformer() throws Exception {
+ TransformerFactory trf = TransformerFactory.newInstance();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ XMLStreamReader reader = context.getTypeConverter().mandatoryConvertTo(XMLStreamReader.class,
+ new StringReader(TEST_XML));
+ // ensure UTF-8 encoding
+ Exchange exchange = new DefaultExchange(context);
+ exchange.setProperty(Exchange.CHARSET_NAME, UTF_8.toString());
+ XMLStreamWriter writer = context.getTypeConverter().mandatoryConvertTo(XMLStreamWriter.class, exchange, baos);
+ StaxSource staxSource = new StaxSource(reader);
+ StreamSource templateSource = new StreamSource(getClass().getResourceAsStream("/xslt/common/copy.xsl"));
+ Transformer transformer = trf.newTransformer(templateSource);
+ //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(TEST_XML));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/70826d8b/camel-core/src/test/resources/xslt/common/copy.xsl
----------------------------------------------------------------------
diff --git a/camel-core/src/test/resources/xslt/common/copy.xsl b/camel-core/src/test/resources/xslt/common/copy.xsl
new file mode 100644
index 0000000..0284ba2
--- /dev/null
+++ b/camel-core/src/test/resources/xslt/common/copy.xsl
@@ -0,0 +1,28 @@
+<?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>