ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anto...@apache.org
Subject svn commit: r1054711 - in /ant/core/trunk/src: main/org/apache/tools/ant/filters/ main/org/apache/tools/ant/util/ tests/junit/org/apache/tools/ant/util/
Date Mon, 03 Jan 2011 18:56:53 GMT
Author: antoine
Date: Mon Jan  3 18:56:52 2011
New Revision: 1054711

URL: http://svn.apache.org/viewvc?rev=1054711&view=rev
Log:
fix for bug 50515, incorrect unicode escapes in propertyfile task

Added:
    ant/core/trunk/src/main/org/apache/tools/ant/util/UnicodeUtil.java
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java
Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/filters/EscapeUnicode.java
    ant/core/trunk/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/filters/EscapeUnicode.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/filters/EscapeUnicode.java?rev=1054711&r1=1054710&r2=1054711&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/filters/EscapeUnicode.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/filters/EscapeUnicode.java Mon Jan  3 18:56:52
2011
@@ -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 = '\\';
                 }
             }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java?rev=1054711&r1=1054710&r2=1054711&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/LayoutPreservingProperties.java Mon
Jan  3 18:56:52 2011
@@ -567,12 +567,8 @@ public class LayoutPreservingProperties 
      * @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}

Added: ant/core/trunk/src/main/org/apache/tools/ant/util/UnicodeUtil.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/UnicodeUtil.java?rev=1054711&view=auto
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/UnicodeUtil.java (added)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/UnicodeUtil.java Mon Jan  3 18:56:52
2011
@@ -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;	   
+   }
+}

Added: ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java?rev=1054711&view=auto
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java (added)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/UnicodeUtilTest.java Mon Jan
 3 18:56:52 2011
@@ -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());
+    }
+
+}



Mime
View raw message