You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2013/12/10 08:43:33 UTC

[3/4] git commit: CAMEL-6854 Fixed the Type conversion issue between DOMSource and InputStream breaks on Windows with thanks to Stephan and Aki

CAMEL-6854 Fixed the Type conversion issue between DOMSource and InputStream breaks on Windows with thanks to Stephan and Aki


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/68b33618
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/68b33618
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/68b33618

Branch: refs/heads/camel-2.11.x
Commit: 68b3361819ac8e59a64ecd997a4788f6302c8b86
Parents: f4c113f
Author: Willem Jiang <wi...@gmail.com>
Authored: Tue Dec 10 15:01:23 2013 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Tue Dec 10 15:42:46 2013 +0800

----------------------------------------------------------------------
 .../camel/converter/jaxp/XmlConverter.java      | 28 ++++++++++++++------
 .../camel/converter/jaxp/XmlConverterTest.java  | 20 ++++++++++++++
 2 files changed, 40 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/68b33618/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
index 487314a..bf00fcc 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
@@ -17,6 +17,7 @@
 package org.apache.camel.converter.jaxp;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -263,11 +264,24 @@ public class XmlConverter {
      */
     @Converter
     public byte[] toByteArray(Source source, Exchange exchange) throws TransformerException {
-        String answer = toString(source, exchange);
-        if (exchange != null) {
-            return exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, answer);
+        if (source == null) {
+            return null;
+        } else if (source instanceof BytesSource) {
+            return ((BytesSource)source).getData();
         } else {
-            return answer.getBytes();
+            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+            if (exchange != null) {
+                // check the camelContext properties first
+                Properties properties = ObjectHelper.getCamelPropertiesWithPrefix(OUTPUT_PROPERTIES_PREFIX,
+                                                                                  exchange.getContext());
+                if (properties.size() > 0) {
+                    toResult(source, new StreamResult(buffer), properties);
+                    return buffer.toByteArray();
+                }
+            }
+            // using the old way to deal with it
+            toResult(source, new StreamResult(buffer));
+            return buffer.toByteArray();
         }
     }
     
@@ -870,8 +884,7 @@ public class XmlConverter {
     
     @Converter
     public InputStream toInputStream(DOMSource source, Exchange exchange) throws TransformerException, IOException {
-        String s = toString(source, exchange);
-        return new ByteArrayInputStream(s.getBytes());
+        return new ByteArrayInputStream(toByteArray(source, exchange));
     }
 
     /**
@@ -884,8 +897,7 @@ public class XmlConverter {
     
     @Converter
     public InputStream toInputStream(Document dom, Exchange exchange) throws TransformerException, IOException {
-        String s = toString(dom, exchange);
-        return new ByteArrayInputStream(s.getBytes());
+        return toInputStream(new DOMSource(dom), exchange);
     }
 
     @Converter

http://git-wip-us.apache.org/repos/asf/camel/blob/68b33618/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
index 5955354..e35a3db 100644
--- a/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
+++ b/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
@@ -429,6 +429,15 @@ public class XmlConverterTest extends ContextTestSupport {
         assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, is));
     }
 
+    public void testToInputStreamNonAsciiFromDocument() throws Exception {
+        XmlConverter conv = new XmlConverter();
+        Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>\u99f1\u99ddb\u00e4r</foo>");
+
+        InputStream is = conv.toInputStream(doc, null);
+        assertNotNull(is);
+        assertEquals("<foo>\u99f1\u99ddb\u00e4r</foo>", context.getTypeConverter().convertTo(String.class, is));
+    }
+
     public void testToDocumentFromFile() throws Exception {
         XmlConverter conv = new XmlConverter();
         File file = new File("src/test/resources/org/apache/camel/converter/stream/test.xml");
@@ -450,6 +459,17 @@ public class XmlConverterTest extends ContextTestSupport {
         assertEquals("<foo>bar</foo>", s);
     }
 
+    public void testToInputStreamNonAsciiByDomSource() throws Exception {
+        XmlConverter conv = new XmlConverter();
+
+        DOMSource source = conv.toDOMSource("<foo>\u99f1\u99ddb\u00e4r</foo>");
+        InputStream out = conv.toInputStream(source, null);
+        assertNotSame(source, out);
+
+        String s = context.getTypeConverter().convertTo(String.class, out);
+        assertEquals("<foo>\u99f1\u99ddb\u00e4r</foo>", s);
+    }
+
     public void testToInputSource() throws Exception {
         XmlConverter conv = new XmlConverter();