StringResource default encoding becomes UTF-8 changing ResourceUtils to use the String's encoding when copying from a String Bug Report 54606 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1452118 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -18,6 +18,8 @@ Changes that could break older environments: | |||
| * 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 | |||
| =================================== | |||
| @@ -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> | |||
| @@ -39,7 +39,8 @@ public class StringResource extends Resource { | |||
| 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 Resource { | |||
| * @param r the Reference to set. | |||
| */ | |||
| public void setRefid(Reference r) { | |||
| if (encoding != null) { | |||
| if (encoding != DEFAULT_ENCODING) { | |||
| throw tooManyAttributes(); | |||
| } | |||
| super.setRefid(r); | |||
| @@ -42,6 +42,7 @@ public class ContainsSelector extends BaseExtendSelector implements ResourceSele | |||
| 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 */ | |||
| @@ -82,6 +83,15 @@ public class ContainsSelector extends BaseExtendSelector implements ResourceSele | |||
| this.contains = contains; | |||
| } | |||
| /** | |||
| * 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. | |||
| * | |||
| @@ -176,7 +186,11 @@ public class ContainsSelector extends BaseExtendSelector implements ResourceSele | |||
| } | |||
| 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); | |||
| @@ -48,6 +48,7 @@ import org.apache.tools.ant.types.resources.FileResource; | |||
| 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); | |||
| @@ -32,12 +32,11 @@ | |||
| <!-- https://issues.apache.org/bugzilla/show_bug.cgi?id=46285 --> | |||
| <target name="testNumericEntities"> | |||
| <echo>ä©</echo> | |||
| <loadresource property="foo"> | |||
| <au:logcontent/> | |||
| <echo encoding="UTF-8" file="${output}/abc.txt">ä©</echo> | |||
| <loadresource property="foo" encoding="UTF-8"> | |||
| <file file="${output}/abc.txt"/> | |||
| </loadresource> | |||
| <au:assertPropertyEquals name="foo" value="ä©"/> | |||
| <au:assertLogContains text="ä©"/> | |||
| </target> | |||
| </project> | |||
| @@ -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> | |||
| @@ -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; | |||