You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Phil Public <pu...@me.com> on 2011/12/06 23:14:30 UTC

UTF-8 and Files Created by Camel

Hello,

  I'm having difficulty creating files using Camel 2.8.0 that use the Euro
character '€' and the registered trademark character '®'.  I've tried to set
the encoding on the file URI to UTF-8, as well as on the Exchange via the
property Exchange.CHARSET_NAME and each time the output data file is
incorrect.  

  I've included a Camel test case below that demonstrates the issue.  When
printing the string via Java, everything works properly.  When writing to
the file, the content does not match the input string.

  Could someone advise me as the best way to properly output the UTF-8
character set via Camel?  Thanks!

Phil

====

/**
 * 
 */
package com.bglobal.etf.dixie.camel;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Test;

/**
 * Apache Camel test case for UTF-8 characters
 * A test file will be written to file:://c/tmp after this runs
 * I'm expecting the text in the file to match that of the testString
 * @author Philip Glebow
 * 
 */
public class CamelEncodingTest extends CamelTestSupport {

	private Log log = LogFactory.getLog(CamelEncodingTest.class);

	private String testString = "€ euro symbol, ® registered trade mark
symbol";

	private String fromURI = "direct:in";

	private String toURI = "file://c:/tmp?charset=utf-8";

	@Test
	public void test() {
		try {			
			log.info(testString);
			template.sendBodyAndProperty(fromURI, testString, Exchange.CHARSET_NAME,
"utf-8");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			Assert.fail(e.getMessage());
		}
	}

	@Override
	protected RouteBuilder createRouteBuilder() throws Exception {
		return new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from(fromURI).to(toURI);
			}
		};
	}

}
=====

--
View this message in context: http://camel.465427.n5.nabble.com/UTF-8-and-Files-Created-by-Camel-tp5053652p5053652.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: UTF-8 and Files Created by Camel

Posted by luc <lu...@42.nl>.
Hi,

I am also having some problems with unicode characters, more specifically
extended unicode characters, not being processed as I expected it to by the
file component. The only way I can get it working is by explicitly
converting to string before routing to the file component. Tests with a
simple Euro sign work either way. To me it seems that this should not be
necessary and should normally make no difference.

I will try to add (upload) a testcase that demonstrates the issue:

ExtendedUnicodeTestJiraDmBack414.java
<http://camel.465427.n5.nabble.com/file/n5730396/ExtendedUnicodeTestJiraDmBack414.java>  

Note that when using just Camel + Jdk, both tests with extended unicode
characters will fail, because of a Xalan bug (see
https://issues.apache.org/jira/browse/XALANJ-2560). By including saxon on
the classpath, the test "testSendExtendedUnicodeMessageExplicitToString"
will succeed. But the test "testSendExtendedUnicodeMessage" will still fail.

I hope everything is clear, if not let me know.

Luc.



--
View this message in context: http://camel.465427.n5.nabble.com/UTF-8-and-Files-Created-by-Camel-tp5053652p5730396.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: UTF-8 and Files Created by Camel

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

In Java code you must use the \u1234 notation to enter uni code
values, to enter the special symbols.

private String testString = "€ euro symbol, ® registered trade mark symbol";

Should be something alike

private String testString = "\u1234 euro symbol, \u1234 registered
trade mark symbol";
Where 1234 is the uni code number for the symbol.


On Tue, Dec 6, 2011 at 11:14 PM, Phil Public <pu...@me.com> wrote:
> Hello,
>
>  I'm having difficulty creating files using Camel 2.8.0 that use the Euro
> character '€' and the registered trademark character '®'.  I've tried to set
> the encoding on the file URI to UTF-8, as well as on the Exchange via the
> property Exchange.CHARSET_NAME and each time the output data file is
> incorrect.
>
>  I've included a Camel test case below that demonstrates the issue.  When
> printing the string via Java, everything works properly.  When writing to
> the file, the content does not match the input string.
>
>  Could someone advise me as the best way to properly output the UTF-8
> character set via Camel?  Thanks!
>
> Phil
>
> ====
>
> /**
>  *
>  */
> package com.bglobal.etf.dixie.camel;
>
> import org.apache.camel.Exchange;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.junit.Assert;
> import org.junit.Test;
>
> /**
>  * Apache Camel test case for UTF-8 characters
>  * A test file will be written to file:://c/tmp after this runs
>  * I'm expecting the text in the file to match that of the testString
>  * @author Philip Glebow
>  *
>  */
> public class CamelEncodingTest extends CamelTestSupport {
>
>        private Log log = LogFactory.getLog(CamelEncodingTest.class);
>
>        private String testString = "€ euro symbol, ® registered trade mark
> symbol";
>
>        private String fromURI = "direct:in";
>
>        private String toURI = "file://c:/tmp?charset=utf-8";
>
>        @Test
>        public void test() {
>                try {
>                        log.info(testString);
>                        template.sendBodyAndProperty(fromURI, testString, Exchange.CHARSET_NAME,
> "utf-8");
>                } catch (Exception e) {
>                        log.error(e.getMessage(), e);
>                        Assert.fail(e.getMessage());
>                }
>        }
>
>        @Override
>        protected RouteBuilder createRouteBuilder() throws Exception {
>                return new RouteBuilder() {
>
>                        @Override
>                        public void configure() throws Exception {
>                                from(fromURI).to(toURI);
>                        }
>                };
>        }
>
> }
> =====
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/UTF-8-and-Files-Created-by-Camel-tp5053652p5053652.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/