Updated Branches:
  refs/heads/master 65616ee43 -> 688ec2df4

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/f4f599d0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f4f599d0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f4f599d0

Branch: refs/heads/master
Commit: f4f599d0ac2f8dcde06a6f390b9842eb39af8e20
Parents: 65616ee
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Tue Dec 10 15:01:23 2013 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Tue Dec 10 15:01:23 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/f4f599d0/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 39171e9..d841a15 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;
@@ -243,11 +244,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();
         }
     }
     
@@ -840,8 +854,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));
     }
 
     /**
@@ -854,8 +867,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/f4f599d0/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();
 

Reply via email to