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 2010/01/29 11:03:21 UTC

svn commit: r904442 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/model/dataformat/ components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/ components/camel-xstream/s...

Author: ningjiang
Date: Fri Jan 29 10:03:19 2010
New Revision: 904442

URL: http://svn.apache.org/viewvc?rev=904442&view=rev
Log:
CAMEL-2407 support the encoding property in xstream dataformat

Added:
    camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java   (with props)
    camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java
    camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/XStreamDataFormat.java
    camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectTest.java
    camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalListTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java?rev=904442&r1=904441&r2=904442&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java Fri Jan 29 10:03:19 2010
@@ -260,6 +260,13 @@
     }
     
     /**
+     * Uses the xstream by setting the encoding
+     */
+    public T xstream(String encoding) {
+        return dataFormat(new XStreamDataFormat(encoding));
+    }
+    
+    /**
      * Uses the XML Security data format
      */
     public T secureXML() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java?rev=904442&r1=904441&r2=904442&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/XStreamDataFormat.java Fri Jan 29 10:03:19 2010
@@ -20,8 +20,11 @@
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.namespace.QName;
 
 import org.apache.camel.model.DataFormatDefinition;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * Represents the XStream XML {@link org.apache.camel.spi.DataFormat}
@@ -31,18 +34,31 @@
 @XmlRootElement(name = "xstream")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class XStreamDataFormat extends DataFormatDefinition {
-    @XmlAttribute(required = false)
-    private Boolean prettyPrint;
+    @XmlAttribute
+    private String encoding;
 
     public XStreamDataFormat() {
         super("xstream");
     }
-
-    public Boolean getPrettyPrint() {
-        return prettyPrint;
+    
+    public XStreamDataFormat(String encoding) {
+        this();
+        setEncoding(encoding);
+    }
+    
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
     }
 
-    public void setPrettyPrint(Boolean prettyPrint) {
-        this.prettyPrint = prettyPrint;
+    public String getEncoding() {
+        return encoding;
+    }
+    
+    @Override
+    protected void configureDataFormat(DataFormat dataFormat) {
+        if (encoding != null) {
+            setProperty(dataFormat, "encoding", encoding);
+        }
     }
+        
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/XStreamDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/XStreamDataFormat.java?rev=904442&r1=904441&r2=904442&view=diff
==============================================================================
--- camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/XStreamDataFormat.java (original)
+++ camel/trunk/components/camel-xstream/src/main/java/org/apache/camel/dataformat/xstream/XStreamDataFormat.java Fri Jan 29 10:03:19 2010
@@ -40,6 +40,7 @@
  * @version $Revision$
  */
 public class XStreamDataFormat extends AbstractXStreamWrapper  {
+    String encoding;
     
     public XStreamDataFormat() {
     }
@@ -47,6 +48,14 @@
     public XStreamDataFormat(XStream xstream) {
         super(xstream);
     }
+    
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+    
+    public String getEncoding() {
+        return encoding;
+    }
 
     /**
      * A factory method which takes a collection of types to be annotated
@@ -70,14 +79,23 @@
             xstream.processAnnotations(type);
         }
         return answer;
-    }    
+    }
+    
+    // just make sure the exchange property can override the xmlstream encoding setting
+    protected void updateCharactorEncodingInfo(Exchange exchange) {
+        if (exchange.getProperty(Exchange.CHARSET_NAME) == null && encoding != null) {
+            exchange.setProperty(Exchange.CHARSET_NAME, encoding);
+        }
+    }
 
     protected HierarchicalStreamWriter createHierarchicalStreamWriter(Exchange exchange, Object body, OutputStream stream) throws XMLStreamException {
+        updateCharactorEncodingInfo(exchange);
         XMLStreamWriter xmlWriter = getStaxConverter().createXMLStreamWriter(stream, exchange);
         return new StaxWriter(new QNameMap(), xmlWriter);
     }
 
     protected HierarchicalStreamReader createHierarchicalStreamReader(Exchange exchange, InputStream stream) throws XMLStreamException {
+        updateCharactorEncodingInfo(exchange);
         XMLStreamReader xmlReader = getStaxConverter().createXMLStreamReader(stream, exchange);
         return new StaxReader(new QNameMap(), xmlReader);
     }

Modified: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectTest.java?rev=904442&r1=904441&r2=904442&view=diff
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectTest.java (original)
+++ camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalDomainObjectTest.java Fri Jan 29 10:03:19 2010
@@ -22,7 +22,7 @@
 import org.junit.Test;
 
 /**
- * Marhsal tests with domain objects.
+ * Marshal tests with domain objects.
  */
 public class MarshalDomainObjectTest extends CamelTestSupport {
 
@@ -87,9 +87,9 @@
                 from("direct:in").marshal().xstream().to("mock:result");
 
                 // just used for helping to marshal
-                from("direct:marshal").marshal().xstream();
+                from("direct:marshal").marshal().xstream("UTF-8");
 
-                from("direct:reverse").unmarshal().xstream().to("mock:reverse");
+                from("direct:reverse").unmarshal().xstream("UTF-8").to("mock:reverse");
             }
         };
     }

Modified: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalListTest.java?rev=904442&r1=904441&r2=904442&view=diff
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalListTest.java (original)
+++ camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/MarshalListTest.java Fri Jan 29 10:03:19 2010
@@ -64,11 +64,30 @@
 
         mock.assertIsSatisfied();
     }
+    
+    @Test
+    public void testSetEncodingOnXstream() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived(
+            "<?xml version='1.0' encoding='UTF-8'?><list><map><entry><string>city</string>"
+                + "<string>London\u0E08</string></entry></map></list>");
+
+        List<Map<Object, String>> body = new ArrayList<Map<Object, String>>();
+        Map<Object, String> row = new HashMap<Object, String>();
+        row.put("city", "London\u0E08");
+        body.add(row);
+
+        template.sendBody("direct:in-UTF-8", body);
+
+        mock.assertIsSatisfied();
+    }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
                 from("direct:in").marshal().xstream().to("mock:result");
+                from("direct:in-UTF-8").marshal().xstream("UTF-8").to("mock:result");
             }
         };
     }

Added: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java?rev=904442&view=auto
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java (added)
+++ camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java Fri Jan 29 10:03:19 2010
@@ -0,0 +1,45 @@
+/**
+ * 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.dataformat.xstream;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Service;
+import org.apache.camel.spring.SpringCamelContext;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class SpringMarshalListTest extends MarshalListTest {
+    
+    protected CamelContext createCamelContext() throws Exception {
+        setUseRouteBuilder(false);
+
+        final AbstractXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml");
+        setCamelContextService(new Service() {
+            public void start() throws Exception {
+                applicationContext.start();
+
+            }
+
+            public void stop() throws Exception {
+                applicationContext.stop();
+            }
+        });
+
+        return SpringCamelContext.springCamelContext(applicationContext);        
+    }
+
+}

Propchange: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-xstream/src/test/java/org/apache/camel/dataformat/xstream/SpringMarshalListTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml?rev=904442&view=auto
==============================================================================
--- camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml (added)
+++ camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml Fri Jan 29 10:03:19 2010
@@ -0,0 +1,49 @@
+<?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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <!-- START SNIPPET: e1 -->
+    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+        
+        <!-- we define the json xstream data formats to be used (xstream is default) -->
+        <dataFormats>
+            <xstream id="xstream-utf8" encoding="UTF-8"/>
+            <xstream id="xstream-default"/>
+        </dataFormats>
+
+        <route>
+            <from uri="direct:in"/>
+            <marshal ref="xstream-default"/>
+            <to uri="mock:result"/>
+        </route>
+
+        <route>
+            <from uri="direct:in-UTF-8"/>
+            <marshal ref="xstream-utf8"/>
+            <to uri="mock:result"/>
+        </route>
+
+    </camelContext>
+    <!-- END SNIPPET: e1 -->
+
+</beans>

Propchange: camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-xstream/src/test/resources/org/apache/camel/dataformat/xstream/SpringMarshalListTest.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml