You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/06/23 09:32:09 UTC

svn commit: r670459 - in /servicemix/smx3/trunk/core/servicemix-core/src: main/java/org/apache/servicemix/components/util/ main/java/org/apache/servicemix/jbi/jaxp/ test/java/org/apache/servicemix/components/util/

Author: gertv
Date: Mon Jun 23 00:32:08 2008
New Revision: 670459

URL: http://svn.apache.org/viewvc?rev=670459&view=rev
Log:
SM-1415: Allow to specify encoding on default marshaler

Added:
    servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java
Modified:
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/DefaultFileMarshaler.java
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java

Modified: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/DefaultFileMarshaler.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/DefaultFileMarshaler.java?rev=670459&r1=670458&r2=670459&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/DefaultFileMarshaler.java (original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/components/util/DefaultFileMarshaler.java Mon Jun 23 00:32:08 2008
@@ -19,9 +19,11 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
 
 import javax.jbi.JBIException;
 import javax.jbi.messaging.MessageExchange;
@@ -54,10 +56,15 @@
 
     private Expression fileName = FILE_NAME_EXPRESSION;
     private Expression content = FILE_CONTENT_EXPRESSION;
+    private String encoding;
 
     public void readMessage(MessageExchange exchange, NormalizedMessage message, 
                             InputStream in, String path) throws IOException, JBIException {
-        message.setContent(new StreamSource(in, path));
+        if (encoding == null) {
+            message.setContent(new StreamSource(in, path));
+        } else {
+            message.setContent(new StreamSource(new InputStreamReader(in, Charset.forName(encoding)), path));
+        }
         message.setProperty(FILE_NAME_PROPERTY, new File(path).getName());
         message.setProperty(FILE_PATH_PROPERTY, path);
     }
@@ -97,6 +104,14 @@
     public void setFileName(Expression fileName) {
         this.fileName = fileName;
     }
+    
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+    
+    public String getEncoding() {
+        return encoding;
+    }
 
     // Implementation methods
     //-------------------------------------------------------------------------
@@ -134,9 +149,10 @@
             throw new NoMessageContentAvailableException(exchange);
         }
         try {
-            getTransformer().toResult(src, new StreamResult(out));
+            getTransformer().toResult(src, new StreamResult(out), encoding);
         } catch (TransformerException e) {
             throw new MessagingException(e);
         }
     }
+
 }

Modified: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java?rev=670459&r1=670458&r2=670459&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java (original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java Mon Jun 23 00:32:08 2008
@@ -97,17 +97,31 @@
     }
 
     /**
-     * Converts the given input Source into the required result
+     * Converts the given input Source into the required result, using the default charset
      */
     public void toResult(Source source, Result result) throws TransformerException {
+        toResult(source, result, defaultCharset);
+    }
+
+    /**
+     * Converts the given input Source into the required result, using the specified encoding
+     * @param source the input Source
+     * @param result the output Result
+     * @param charset the required charset, if you specify <code>null</code> the default charset will be used
+     */
+    public void toResult(Source source, Result result, String charset)
+        throws TransformerConfigurationException, TransformerException {
         if (source == null) {
             return;
         }
+        if (charset == null) {
+            charset = defaultCharset;
+        }
         Transformer transformer = createTransfomer();
         if (transformer == null) {
             throw new TransformerException("Could not create a transformer - JAXP is misconfigured!");
         }
-        transformer.setOutputProperty(OutputKeys.ENCODING, defaultCharset);
+        transformer.setOutputProperty(OutputKeys.ENCODING, charset);
         transformer.transform(source, result);
     }
 

Added: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java?rev=670459&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/components/util/DefaultFileMarshalerTest.java Mon Jun 23 00:32:08 2008
@@ -0,0 +1,75 @@
+/*
+ * 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.servicemix.components.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.Charset;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.tck.mock.MockMessageExchange;
+import org.apache.servicemix.tck.mock.MockNormalizedMessage;
+
+/**
+ * Test cases for {@link DefaultFileMarshaler}
+ */
+public class DefaultFileMarshalerTest extends TestCase {
+    
+    private static final String MESSAGE = "<test>l'élève est à l'école</test>";
+    private static final SourceTransformer TRANSFORMER = new SourceTransformer();
+    private DefaultFileMarshaler marshaler = new DefaultFileMarshaler();
+    
+    public void testReadExplicitEncoding() throws Exception {
+        //create a mock exchange
+        MessageExchange exchange = createMockExchange();
+        
+        //have the marshaler read the message as ISO-8859-1, encoded as ISO-8859-1
+        ByteArrayInputStream stream = new ByteArrayInputStream(Charset.forName("ISO-8859-1").encode(MESSAGE).array());
+        marshaler.setEncoding("ISO-8859-1");
+        marshaler.readMessage(exchange, exchange.getMessage("in"), stream, "/any/thing/will/do");
+        
+        //make sure that the end result is the same and no exception is thrown
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + MESSAGE, TRANSFORMER.contentToString(exchange.getMessage("in")));
+    }
+    
+    public void testWriteExplicitEncoding() throws Exception {
+        //create a mock exchange
+        MessageExchange exchange = createMockExchange();
+        exchange.getMessage("in").setContent(new StringSource(MESSAGE));
+        
+        //have the marshaler read the message as ISO-8859-1, encoded as ISO-8859-1
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        marshaler.setEncoding("ISO-8859-1");
+        marshaler.writeMessage(exchange, exchange.getMessage("in"), stream, "/any/thing/will/do");
+        
+        //make sure that the end result is the same and no exception is thrown
+        assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + MESSAGE, stream.toString("ISO-8859-1"));
+    }    
+
+    private MessageExchange createMockExchange() throws MessagingException {
+        MessageExchange exchange = new MockMessageExchange();
+        exchange.setMessage(new MockNormalizedMessage(), "in");
+        return exchange;
+    }
+
+}