git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1054711 13f79535-47bb-0310-9956-ffa450edef68master
@@ -20,6 +20,8 @@ package org.apache.tools.ant.filters; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.Reader; | import java.io.Reader; | ||||
import org.apache.tools.ant.util.UnicodeUtil; | |||||
/** | /** | ||||
* This method converts non-latin characters to unicode escapes. | * This method converts non-latin characters to unicode escapes. | ||||
* Useful to load properties containing non latin | * Useful to load properties containing non latin | ||||
@@ -85,14 +87,7 @@ public class EscapeUnicode | |||||
if (ch != -1) { | if (ch != -1) { | ||||
char achar = (char) ch; | char achar = (char) ch; | ||||
if (achar >= '\u0080') { | 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 = '\\'; | ch = '\\'; | ||||
} | } | ||||
} | } | ||||
@@ -567,12 +567,8 @@ public class LayoutPreservingProperties extends Properties { | |||||
* @return the unicode escape sequence | * @return the unicode escape sequence | ||||
*/ | */ | ||||
private String escapeUnicode(char ch) { | 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} | * Remove the comments in the leading up the {@link logicalLines} | ||||
@@ -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; | |||||
} | |||||
} |
@@ -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()); | |||||
} | |||||
} |