ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anto...@apache.org
Subject svn commit: r1452118 - in /ant/core/trunk: ./ manual/Types/ src/main/org/apache/tools/ant/types/resources/ src/main/org/apache/tools/ant/types/selectors/ src/main/org/apache/tools/ant/util/ src/tests/antunit/core/ src/tests/antunit/taskdefs/ src/tests/...
Date Sun, 03 Mar 2013 20:23:52 GMT
Author: antoine
Date: Sun Mar  3 20:23:51 2013
New Revision: 1452118

URL: http://svn.apache.org/r1452118
Log:
adding an encoding attribute to the contains selector
StringResource default encoding becomes UTF-8
changing ResourceUtils to use the String's encoding when copying from a String
Bug Report 54606

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/manual/Types/selectors.html
    ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java
    ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java
    ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java
    ant/core/trunk/src/tests/antunit/core/nested-text-test.xml
    ant/core/trunk/src/tests/antunit/taskdefs/echo-test.xml
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Sun Mar  3 20:23:51 2013
@@ -18,6 +18,8 @@ Changes that could break older environme
 
  * Removing the Perforce Ant tasks replaced by tasks supplied by Perforce Inc.
 
+ * Setting the default encoding of StringResource to UTF-8 instead of null
+
 Fixed bugs:
 -----------
 
@@ -81,6 +83,10 @@ Fixed bugs:
    sizes differed by more than 2 GB.
    Bugzilla Report 54623
 
+ * Unable to encode properly into UTF-8 when the system property file.encoding is
+   set to ANSI_X3.4-1968.
+   Bugzilla Report 54606
+
 Other changes:
 --------------
 
@@ -124,6 +130,10 @@ Other changes:
  * add the possibility to suppress stdout in the sshexec task.
    Bugzilla Report 50270.
 
+ * add an encoding attribute to the contains selector.
+   This will be useful to use the contains selector if the encoding of the VM is different
from the encoding
+   of the files being selected.
+
 Changes from Ant 1.8.3 TO Ant 1.8.4
 ===================================
 

Modified: ant/core/trunk/manual/Types/selectors.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/manual/Types/selectors.html?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/manual/Types/selectors.html (original)
+++ ant/core/trunk/manual/Types/selectors.html Sun Mar  3 20:23:51 2013
@@ -133,6 +133,16 @@
         </td>
         <td valign="top" align="center">No</td>
       </tr>
+        <tr>
+            <td valign="top">encoding</td>
+            <td valign="top">Encoding of the resources being selected.
+                Required in practice if the encoding of the files being
+                selected is different from the default encoding of the JVM
+                where Ant is running.
+                Since Ant 1.9.0
+            </td>
+            <td valign="top" align="center">No</td>
+        </tr>
     </table>
 
     <p>Here is an example of how to use the Contains Selector:</p>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/resources/StringResource.java Sun Mar
 3 20:23:51 2013
@@ -39,7 +39,8 @@ public class StringResource extends Reso
     private static final int STRING_MAGIC
         = Resource.getMagicNumber("StringResource".getBytes());
 
-    private String encoding = null;
+    private static final String DEFAULT_ENCODING = "UTF-8";
+    private String encoding = DEFAULT_ENCODING;
 
     /**
      * Default constructor.
@@ -212,7 +213,7 @@ public class StringResource extends Reso
      * @param r the Reference to set.
      */
     public void setRefid(Reference r) {
-        if (encoding != null) {
+        if (encoding != DEFAULT_ENCODING) {
             throw tooManyAttributes();
         }
         super.setRefid(r);

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java Sun
Mar  3 20:23:51 2013
@@ -42,6 +42,7 @@ public class ContainsSelector extends Ba
     private String contains = null;
     private boolean casesensitive = true;
     private boolean ignorewhitespace = false;
+    private String encoding = null;
     /** Key to used for parameterized custom selector */
     public static final String EXPRESSION_KEY = "expression";
     /** Used for parameterized custom selector */
@@ -83,6 +84,15 @@ public class ContainsSelector extends Ba
     }
 
     /**
+     * The encoding of the resources processed
+     * @since Ant 1.9.0
+     * @param encoding encoding of the resources processed
+     */
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
+    /**
      * Whether to ignore case in the string being searched.
      *
      * @param casesensitive whether to pay attention to case sensitivity
@@ -176,7 +186,11 @@ public class ContainsSelector extends Ba
         }
         BufferedReader in = null;
         try {
-            in = new BufferedReader(new InputStreamReader(r.getInputStream()));
+            if (encoding != null) {
+                in = new BufferedReader(new InputStreamReader(r.getInputStream(), encoding));
+            }   else {
+                in = new BufferedReader(new InputStreamReader(r.getInputStream()));
+            }
         } catch (Exception e) {
             throw new BuildException("Could not get InputStream from "
                     + r.toLongString(), e);

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/ResourceUtils.java Sun Mar  3 20:23:51
2013
@@ -48,6 +48,7 @@ import org.apache.tools.ant.types.resour
 import org.apache.tools.ant.types.resources.Union;
 import org.apache.tools.ant.types.resources.Restrict;
 import org.apache.tools.ant.types.resources.Resources;
+import org.apache.tools.ant.types.resources.StringResource;
 import org.apache.tools.ant.types.resources.Touchable;
 import org.apache.tools.ant.types.resources.selectors.Date;
 import org.apache.tools.ant.types.resources.selectors.ResourceSelector;
@@ -393,7 +394,12 @@ public class ResourceUtils {
                                              && filters.hasFilters());
         final boolean filterChainsAvailable = (filterChains != null
                                                && filterChains.size() > 0);
-
+        String effectiveInputEncoding = null;
+        if (source instanceof StringResource) {
+             effectiveInputEncoding = ((StringResource) source).getEncoding();
+        } else {
+            effectiveInputEncoding = inputEncoding;
+        }
         File destFile = null;
         if (dest.as(FileProvider.class) != null) {
             destFile = dest.as(FileProvider.class).getFile();
@@ -413,11 +419,11 @@ public class ResourceUtils {
             BufferedWriter out = null;
             try {
                 InputStreamReader isr = null;
-                if (inputEncoding == null) {
+                if (effectiveInputEncoding == null) {
                     isr = new InputStreamReader(source.getInputStream());
                 } else {
                     isr = new InputStreamReader(source.getInputStream(),
-                                                inputEncoding);
+                                                effectiveInputEncoding);
                 }
                 in = new BufferedReader(isr);
                 OutputStream os = getOutputStream(dest, append, project);
@@ -457,18 +463,18 @@ public class ResourceUtils {
                 FileUtils.close(in);
             }
         } else if (filterChainsAvailable
-                   || (inputEncoding != null
-                       && !inputEncoding.equals(outputEncoding))
-                   || (inputEncoding == null && outputEncoding != null)) {
+                   || (effectiveInputEncoding != null
+                       && !effectiveInputEncoding.equals(outputEncoding))
+                   || (effectiveInputEncoding == null && outputEncoding != null))
{
             BufferedReader in = null;
             BufferedWriter out = null;
             try {
                 InputStreamReader isr = null;
-                if (inputEncoding == null) {
+                if (effectiveInputEncoding == null) {
                     isr = new InputStreamReader(source.getInputStream());
                 } else {
                     isr = new InputStreamReader(source.getInputStream(),
-                                                inputEncoding);
+                                                effectiveInputEncoding);
                 }
                 in = new BufferedReader(isr);
                 OutputStream os = getOutputStream(dest, append, project);

Modified: ant/core/trunk/src/tests/antunit/core/nested-text-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/core/nested-text-test.xml?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/core/nested-text-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/core/nested-text-test.xml Sun Mar  3 20:23:51 2013
@@ -32,12 +32,11 @@
 
   <!-- https://issues.apache.org/bugzilla/show_bug.cgi?id=46285 -->
   <target name="testNumericEntities">
-    <echo>&#xe4;&#169;</echo>
-    <loadresource property="foo">
-      <au:logcontent/>
+    <echo encoding="UTF-8" file="${output}/abc.txt">&#xe4;&#169;</echo>
+    <loadresource property="foo" encoding="UTF-8">
+      <file file="${output}/abc.txt"/>
     </loadresource>
     <au:assertPropertyEquals name="foo" value="ä©"/>
-    <au:assertLogContains text="ä©"/>
   </target>
 
 </project>

Modified: ant/core/trunk/src/tests/antunit/taskdefs/echo-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/echo-test.xml?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/echo-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/echo-test.xml Sun Mar  3 20:23:51 2013
@@ -127,9 +127,9 @@
       <resourcecount count="1">
         <restrict>
           <concat encoding="UTF-8">
-            <file file="${output}/echo8.txt" />
+            <file file="${output}/echo8.txt"/>
           </concat>
-          <contains text="${char}" />
+          <contains text="${char}" encoding="UTF-8"/>
         </restrict>
       </resourcecount>
     </au:assertTrue>

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java?rev=1452118&r1=1452117&r2=1452118&view=diff
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java (original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/EchoTest.java Sun Mar  3
20:23:51 2013
@@ -18,19 +18,20 @@
 
 package org.apache.tools.ant.taskdefs;
 
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
+import java.io.*;
 
 import junit.framework.TestCase;
 
 import org.apache.tools.ant.DefaultLogger;
 import org.apache.tools.ant.Project;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * Test Java-dependent parts of the Echo task.
  */
 public class EchoTest extends TestCase {
 
+    private File removeThis;
     /**
      * Create a new EchoTest.
      * @param name
@@ -50,7 +51,38 @@ public class EchoTest extends TestCase {
         echo.execute();
         assertEquals("[testLogBlankEcho] ", logger.lastLoggedMessage );
     }
- 
+
+    public void testLogUTF8Echo() {
+        Project p = new Project();
+        p.init();
+        EchoTestLogger logger = new EchoTestLogger();
+        p.addBuildListener(logger);
+        Echo echo = new Echo();
+        echo.setProject(p);
+        echo.setTaskName("testLogUTF8Echo");
+        echo.setMessage("\u00e4\u00a9");
+        removeThis = new File("abc.txt");
+        echo.setFile(removeThis);
+        echo.setEncoding("UTF-8");
+        echo.execute();
+        FileUtils fu = FileUtils.getFileUtils();
+        try {
+        String x = FileUtils.readFully(new InputStreamReader(new FileInputStream(removeThis),
"UTF-8" ));
+            assertEquals(x,"\u00e4\u00a9");
+        } catch (Exception exc) {
+
+        }
+    }
+
+    public void tearDown() {
+        if (removeThis != null && removeThis.exists()) {
+            if (!removeThis.delete())
+            {
+                removeThis.deleteOnExit();
+            }
+        }
+    }
+
     private class EchoTestLogger extends DefaultLogger {
         String lastLoggedMessage;
         



Mime
View raw message