ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [5/8] ant git commit: builtin implementation of ascii2native
Date Sun, 24 Jul 2016 11:11:11 GMT
builtin implementation of ascii2native


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/4ef2554a
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/4ef2554a
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/4ef2554a

Branch: refs/heads/master
Commit: 4ef2554a53506a186bc11b5b505ec64cf902595f
Parents: 51bd1cb
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Sun Jul 24 12:51:53 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sun Jul 24 12:51:53 2016 +0200

----------------------------------------------------------------------
 manual/Tasks/native2ascii.html                  |  2 +-
 .../native2ascii/BuiltinNative2Ascii.java       | 44 +++++++++++++++-----
 .../tools/ant/util/Native2AsciiUtils.java       | 38 +++++++++++++++++
 .../taskdefs/optional/native2ascii-test.xml     | 13 +++---
 .../tools/ant/util/Native2AsciiUtilsTest.java   | 30 +++++++++++++
 5 files changed, 109 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/4ef2554a/manual/Tasks/native2ascii.html
----------------------------------------------------------------------
diff --git a/manual/Tasks/native2ascii.html b/manual/Tasks/native2ascii.html
index acbd460..ecc065a 100644
--- a/manual/Tasks/native2ascii.html
+++ b/manual/Tasks/native2ascii.html
@@ -81,7 +81,7 @@
         <td>reverse</td>
         <td>Reverse the sense of the conversion,
           i.e. convert from ASCII to native <b>only supported by the
-            sun converter</b></td>
+            sun and builtin converters</b></td>
         <td align="center">No</td>
       </tr>
       <tr>

http://git-wip-us.apache.org/repos/asf/ant/blob/4ef2554a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java
b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java
index b9e600c..db91a26 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/native2ascii/BuiltinNative2Ascii.java
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
@@ -45,20 +46,15 @@ public class BuiltinNative2Ascii implements Native2AsciiAdapter {
 
     public final boolean convert(Native2Ascii args, File srcFile,
                                  File destFile) throws BuildException {
+        boolean reverse = args.getReverse();
+        String encoding = args.getEncoding();
         BufferedReader input = null;
         try {
-            if (args.getEncoding() != null) {
-                input = new BufferedReader(new InputStreamReader(
-                    new FileInputStream(srcFile), args.getEncoding()));
-            } else {
-                input = new BufferedReader(new FileReader(srcFile));
-            }
+            input = getReader(srcFile, encoding, reverse);
             try {
-                BufferedWriter output = new BufferedWriter(
-                    new OutputStreamWriter(new FileOutputStream(destFile),
-                                           "ASCII"));
+                Writer output = getWriter(destFile, encoding, reverse);
                 try {
-                    translate(input, output, args.getReverse());
+                    translate(input, output, reverse);
                 } finally {
                     FileUtils.close(output);
                 }
@@ -71,11 +67,37 @@ public class BuiltinNative2Ascii implements Native2AsciiAdapter {
         }
     }
 
+    private BufferedReader getReader(File srcFile, String encoding,
+                                     boolean reverse) throws IOException {
+        if (!reverse && encoding != null) {
+            return new BufferedReader(new InputStreamReader(
+                new FileInputStream(srcFile), encoding));
+        }
+        return new BufferedReader(new FileReader(srcFile));
+    }
+
+    private Writer getWriter(File destFile, String encoding,
+                             boolean reverse) throws IOException {
+        if (!reverse) {
+            encoding = "ASCII";
+        }
+        if (encoding != null) {
+            return new BufferedWriter(
+                new OutputStreamWriter(new FileOutputStream(destFile),
+                                       encoding));
+        }
+        return new BufferedWriter(new FileWriter(destFile));
+    }
+
     private void translate(BufferedReader input, Writer output,
                            boolean reverse) throws IOException {
         String line = null;
         while ((line = input.readLine()) != null) {
-            output.write(Native2AsciiUtils.native2ascii(line));
+            if (!reverse) {
+                output.write(Native2AsciiUtils.native2ascii(line));
+            } else {
+                output.write(Native2AsciiUtils.ascii2native(line));
+            }
             output.write(StringUtils.LINE_SEP);
         }
     }

http://git-wip-us.apache.org/repos/asf/ant/blob/4ef2554a/src/main/org/apache/tools/ant/util/Native2AsciiUtils.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/util/Native2AsciiUtils.java b/src/main/org/apache/tools/ant/util/Native2AsciiUtils.java
index acdfaaa..a69d031 100644
--- a/src/main/org/apache/tools/ant/util/Native2AsciiUtils.java
+++ b/src/main/org/apache/tools/ant/util/Native2AsciiUtils.java
@@ -48,4 +48,42 @@ public class Native2AsciiUtils {
         }
         return sb.toString();
     }
+
+    /**
+     * Replaces Unicode-Escapes.
+     * <p>Expects to be called once per line if applied to a file.</p>
+     * @param line the input line
+     * @return the translated line
+     */
+    public static String ascii2native(String line) {
+        StringBuilder sb = new StringBuilder();
+        int inputLen = line.length();
+        for (int i = 0; i < inputLen; i++) {
+            char c = line.charAt(i);
+            if (c != '\\' || i >= inputLen - 5) {
+                sb.append(c);
+            } else { // backslash with enough remaining characters
+                char u = line.charAt(++i);
+                if (u == 'u') {
+                    int unescaped = tryParse(line, i + 1);
+                    if (unescaped >= 0) {
+                        sb.append((char) unescaped);
+                        i += 4;
+                        continue;
+                    }
+                }
+                // not a unicode escape
+                sb.append(c).append(u);
+            }
+        }
+        return sb.toString();
+    }
+
+    private static int tryParse(String line, int startIdx) {
+        try {
+            return Integer.parseInt(line.substring(startIdx, startIdx + 4), 16);
+        } catch (NumberFormatException ex) {
+            return -1;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/ant/blob/4ef2554a/src/tests/antunit/taskdefs/optional/native2ascii-test.xml
----------------------------------------------------------------------
diff --git a/src/tests/antunit/taskdefs/optional/native2ascii-test.xml b/src/tests/antunit/taskdefs/optional/native2ascii-test.xml
index 59030e9..be521de 100644
--- a/src/tests/antunit/taskdefs/optional/native2ascii-test.xml
+++ b/src/tests/antunit/taskdefs/optional/native2ascii-test.xml
@@ -95,12 +95,6 @@ public class Adapter implements Native2AsciiAdapter {
               value="\u00e4\u00f6\u00fc=\u00c4\u00d6\u00dc"/>
   </target>
 
-  <target name="testUTF8-To-ASCII" depends="-setup-UTF8-To-ASCII">
-    <native2ascii-def/>
-    <assertTranslatedOutput file="umlauts.properties" encoding="ASCII"
-                            expected="${umlauts.expected}"/>
-  </target>
-
   <target name="testUTF8-To-ASCII-sun" depends="-setup-UTF8-To-ASCII"
           description="https://bz.apache.org/bugzilla/show_bug.cgi?id=59855"
           unless="jdk1.9+">
@@ -131,4 +125,11 @@ public class Adapter implements Native2AsciiAdapter {
                             expected="${umlauts.expected}"/>
   </target>
 
+  <target name="testASCII-To-UTF8-builtin" depends="-setup-ASCII-To-UTF8"
+          description="https://bz.apache.org/bugzilla/show_bug.cgi?id=59855">
+    <native2ascii-def implementation="builtin" reverse="true"/>
+    <assertTranslatedOutput file="umlauts.properties" encoding="UTF-8"
+                            expected="${umlauts.expected}"/>
+  </target>
+
 </project>

http://git-wip-us.apache.org/repos/asf/ant/blob/4ef2554a/src/tests/junit/org/apache/tools/ant/util/Native2AsciiUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/util/Native2AsciiUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/Native2AsciiUtilsTest.java
index 75719f2..f3b54e6 100644
--- a/src/tests/junit/org/apache/tools/ant/util/Native2AsciiUtilsTest.java
+++ b/src/tests/junit/org/apache/tools/ant/util/Native2AsciiUtilsTest.java
@@ -43,4 +43,34 @@ public class Native2AsciiUtilsTest {
         assertEquals("\\u00e4\\u01f6\\u12fc",
                      Native2AsciiUtils.native2ascii("\u00e4\u01f6\u12fc"));
     }
+
+    @Test
+    public void doesntTouchNonEscapes() {
+        StringBuilder sb = new StringBuilder();
+        for (char i = 0; i < 128; i++) {
+            sb.append(i);
+        }
+        assertEquals(sb.toString(), Native2AsciiUtils.ascii2native(sb.toString()));
+    }
+
+    @Test
+    public void unescapes() {
+        assertEquals("\u00e4\u00f6\u00fc",
+                     Native2AsciiUtils.ascii2native("\\u00e4\\u00f6\\u00fc"));
+    }
+
+    @Test
+    public void leavesNonUnicodeBackslashesAlone() {
+        assertEquals("\\abcdef", Native2AsciiUtils.ascii2native("\\abcdef"));
+        assertEquals("\\u012j", Native2AsciiUtils.ascii2native("\\u012j"));
+    }
+
+    @Test
+    public void dealsWithUnfinishedEscapes() {
+        assertEquals("\u00e4", Native2AsciiUtils.ascii2native("\\u00e4"));
+        assertEquals("\\u00e", Native2AsciiUtils.ascii2native("\\u00e"));
+        assertEquals("\\u00", Native2AsciiUtils.ascii2native("\\u00"));
+        assertEquals("\\u0", Native2AsciiUtils.ascii2native("\\u0"));
+    }
+
 }


Mime
View raw message