From bb2695af1464b52cacdd3d3672e428358c3142dd Mon Sep 17 00:00:00 2001 From: Antoine Levy-Lambert Date: Mon, 3 Jan 2011 18:56:52 +0000 Subject: [PATCH] fix for bug 50515, incorrect unicode escapes in propertyfile task git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1054711 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/filters/EscapeUnicode.java | 11 ++--- .../ant/util/LayoutPreservingProperties.java | 8 +--- .../apache/tools/ant/util/UnicodeUtil.java | 42 +++++++++++++++++++ .../tools/ant/util/UnicodeUtilTest.java | 30 +++++++++++++ 4 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/main/org/apache/tools/ant/util/UnicodeUtil.java create mode 100644 src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java diff --git a/src/main/org/apache/tools/ant/filters/EscapeUnicode.java b/src/main/org/apache/tools/ant/filters/EscapeUnicode.java index 558ad34c3..dd4543959 100644 --- a/src/main/org/apache/tools/ant/filters/EscapeUnicode.java +++ b/src/main/org/apache/tools/ant/filters/EscapeUnicode.java @@ -20,6 +20,8 @@ package org.apache.tools.ant.filters; import java.io.IOException; import java.io.Reader; +import org.apache.tools.ant.util.UnicodeUtil; + /** * This method converts non-latin characters to unicode escapes. * Useful to load properties containing non latin @@ -85,14 +87,7 @@ public class EscapeUnicode if (ch != -1) { char achar = (char) ch; if (achar >= '\u0080') { - unicodeBuf = new StringBuffer("u0000"); - String s = Integer.toHexString(ch); - //replace the last 0s by the chars contained in s - for (int i = 0; i < s.length(); i++) { - unicodeBuf.setCharAt(unicodeBuf.length() - - s.length() + i, - s.charAt(i)); - } + unicodeBuf = UnicodeUtil.EscapeUnicode(achar); ch = '\\'; } } diff --git a/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java b/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java index 26dd1631e..ecab3407a 100644 --- a/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java +++ b/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java @@ -567,12 +567,8 @@ public class LayoutPreservingProperties extends Properties { * @return the unicode escape sequence */ private String escapeUnicode(char ch) { - StringBuffer buffy = new StringBuffer("\\u"); - String hex = Integer.toHexString((int)ch); - buffy.append("0000".substring(4-hex.length())); - buffy.append(hex); - return buffy.toString(); - } + return "\\" + UnicodeUtil.EscapeUnicode(ch); + } /** * Remove the comments in the leading up the {@link logicalLines} diff --git a/src/main/org/apache/tools/ant/util/UnicodeUtil.java b/src/main/org/apache/tools/ant/util/UnicodeUtil.java new file mode 100644 index 000000000..39ea7b4f1 --- /dev/null +++ b/src/main/org/apache/tools/ant/util/UnicodeUtil.java @@ -0,0 +1,42 @@ +/* + * 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.tools.ant.util; + +/** + * Contains one helper method to create a backslash u escape + * + * @since Ant 1.8.3 + */ +public class UnicodeUtil { + /** + * returns the unicode representation of a char without the leading backslash + * @param ch + * @return unicode representation of a char for property files + */ + public static StringBuffer EscapeUnicode(char ch) { + StringBuffer unicodeBuf = new StringBuffer("u0000"); + String s = Integer.toHexString(ch); + //replace the last 0s by the chars contained in s + for (int i = 0; i < s.length(); i++) { + unicodeBuf.setCharAt(unicodeBuf.length() + - s.length() + i, + s.charAt(i)); + } + return unicodeBuf; + } +} diff --git a/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java b/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java new file mode 100644 index 000000000..261b28d1e --- /dev/null +++ b/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java @@ -0,0 +1,30 @@ +/* + * 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.tools.ant.util; + +import junit.framework.TestCase; + +public class UnicodeUtilTest extends TestCase { + + public void testChineseWord() { + String word = "\u81ea\u7531"; + assertEquals("u81ea", UnicodeUtil.EscapeUnicode(word.charAt(0)).toString()); + assertEquals("u7531", UnicodeUtil.EscapeUnicode(word.charAt(1)).toString()); + } + +}