Author: cmueller Date: Tue Jan 4 21:22:05 2011 New Revision: 1055182 URL: http://svn.apache.org/viewvc?rev=1055182&view=rev Log: CAMEL-3483: csv unmarshal and maybe other components uses default encoding
Added: camel/trunk/components/camel-flatpack/src/test/data/charset/ camel/trunk/components/camel-flatpack/src/test/data/charset/INVENTORY-Charset.txt (with props) camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/CharsetTest.java (with props) camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/CharsetTest-context.xml (with props) Modified: camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java Modified: camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java?rev=1055182&r1=1055181&r2=1055182&view=diff ============================================================================== --- camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java (original) +++ camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java Tue Jan 4 21:22:05 2011 @@ -33,6 +33,7 @@ import net.sf.flatpack.writer.DelimiterW import net.sf.flatpack.writer.FixedWriterFactory; import net.sf.flatpack.writer.Writer; import org.apache.camel.Exchange; +import org.apache.camel.converter.IOConverter; import org.apache.camel.spi.DataFormat; import org.apache.camel.util.ObjectHelper; import org.apache.commons.logging.Log; @@ -95,7 +96,7 @@ public class FlatpackDataFormat implemen } public Object unmarshal(Exchange exchange, InputStream stream) throws Exception { - InputStreamReader reader = new InputStreamReader(stream); + InputStreamReader reader = new InputStreamReader(stream, IOConverter.getCharsetName(exchange)); try { Parser parser = createParser(exchange, reader); DataSet dataSet = parser.parse(); @@ -163,13 +164,13 @@ public class FlatpackDataFormat implemen if (isFixed()) { Resource resource = getDefinition(); ObjectHelper.notNull(resource, "resource"); - return getParserFactory().newFixedLengthParser(new InputStreamReader(resource.getInputStream()), bodyReader); + return getParserFactory().newFixedLengthParser(new InputStreamReader(resource.getInputStream(), IOConverter.getCharsetName(exchange)), bodyReader); } else { Resource resource = getDefinition(); if (resource == null) { return getParserFactory().newDelimitedParser(bodyReader, delimiter, textQualifier); } else { - return getParserFactory().newDelimitedParser(new InputStreamReader(resource.getInputStream()), bodyReader, delimiter, textQualifier, ignoreFirstRecord); + return getParserFactory().newDelimitedParser(new InputStreamReader(resource.getInputStream(), IOConverter.getCharsetName(exchange)), bodyReader, delimiter, textQualifier, ignoreFirstRecord); } } } @@ -178,8 +179,8 @@ public class FlatpackDataFormat implemen if (isFixed()) { Resource resource = getDefinition(); ObjectHelper.notNull(resource, "resource"); - FixedWriterFactory factory = new FixedWriterFactory(new InputStreamReader(resource.getInputStream())); - return factory.createWriter(new OutputStreamWriter(stream)); + FixedWriterFactory factory = new FixedWriterFactory(new InputStreamReader(resource.getInputStream(), IOConverter.getCharsetName(exchange))); + return factory.createWriter(new OutputStreamWriter(stream, IOConverter.getCharsetName(exchange))); } else { Resource resource = getDefinition(); if (resource == null) { @@ -188,13 +189,11 @@ public class FlatpackDataFormat implemen for (String key : firstRow.keySet()) { factory.addColumnTitle(key); } - return factory.createWriter(new OutputStreamWriter(stream)); + return factory.createWriter(new OutputStreamWriter(stream, IOConverter.getCharsetName(exchange))); } else { - DelimiterWriterFactory factory = new DelimiterWriterFactory(new InputStreamReader(resource.getInputStream()), delimiter, textQualifier); - return factory.createWriter(new OutputStreamWriter(stream)); + DelimiterWriterFactory factory = new DelimiterWriterFactory(new InputStreamReader(resource.getInputStream(), IOConverter.getCharsetName(exchange)), delimiter, textQualifier); + return factory.createWriter(new OutputStreamWriter(stream, IOConverter.getCharsetName(exchange))); } } } - - -} +} \ No newline at end of file Added: camel/trunk/components/camel-flatpack/src/test/data/charset/INVENTORY-Charset.txt URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/test/data/charset/INVENTORY-Charset.txt?rev=1055182&view=auto ============================================================================== --- camel/trunk/components/camel-flatpack/src/test/data/charset/INVENTORY-Charset.txt (added) +++ camel/trunk/components/camel-flatpack/src/test/data/charset/INVENTORY-Charset.txt Tue Jan 4 21:22:05 2011 @@ -0,0 +1,5 @@ +"ITEM_DESC","IN_STOCK","PRICE","LAST_RECV_DT" +"SOME VALVE","2","5.00","20050101" +"AN ENGINE","100","1000.00","20040601" +"A BELT","45",".50","20030101" +"A ÃöÃ","1000","2.75","20050101" Propchange: camel/trunk/components/camel-flatpack/src/test/data/charset/INVENTORY-Charset.txt ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/CharsetTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/CharsetTest.java?rev=1055182&view=auto ============================================================================== --- camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/CharsetTest.java (added) +++ camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/CharsetTest.java Tue Jan 4 21:22:05 2011 @@ -0,0 +1,65 @@ +/** + * 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.component.flatpack; + +import java.util.List; +import java.util.Map; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.util.CastUtils; +import org.apache.camel.util.ObjectHelper; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Test; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @version $Revision: 834787 $ + */ +...@contextconfiguration +public class CharsetTest extends AbstractJUnit4SpringContextTests { + private static final transient Log LOG = LogFactory.getLog(FixedLengthTest.class); + + @EndpointInject(uri = "mock:results") + protected MockEndpoint results; + + protected String[] expectedItemDesc = {"SOME VALVE", "AN ENGINE", "A BELT", "A ÃöÃ"}; + + @Test + public void testCamel() throws Exception { + results.expectedMessageCount(4); + results.assertIsSatisfied(); + + int counter = 0; + List<Exchange> list = results.getReceivedExchanges(); + for (Exchange exchange : list) { + Message in = exchange.getIn(); + Map<String, String> body = CastUtils.cast(in.getBody(Map.class)); + assertNotNull("Should have found body as a Map but was: " + ObjectHelper.className(in.getBody()), body); + assertEquals("ITEM_DESC", expectedItemDesc[counter], body.get("ITEM_DESC")); + LOG.info("Result: " + counter + " = " + body); + counter++; + } + } +} \ No newline at end of file Propchange: camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/CharsetTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/CharsetTest-context.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/CharsetTest-context.xml?rev=1055182&view=auto ============================================================================== --- camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/CharsetTest-context.xml (added) +++ camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/CharsetTest-context.xml Tue Jan 4 21:22:05 2011 @@ -0,0 +1,40 @@ +<?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.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <!-- START SNIPPET: example --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="file://src/test/data/charset?noop=true"/> + <to uri="flatpack:delim:INVENTORY-Delimited.pzmap.xml"/> + </route> + + <route> + <from uri="flatpack:delim:INVENTORY-Delimited.pzmap.xml"/> + <convertBodyTo type="java.util.Map"/> + <to uri="mock:results"/> + </route> + </camelContext> + <!-- END SNIPPET: example --> + +</beans> \ No newline at end of file Propchange: camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/CharsetTest-context.xml ------------------------------------------------------------------------------ svn:mime-type = text/plain