diff --git a/WHATSNEW b/WHATSNEW index 4ba97742d..be0e03091 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -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 =================================== diff --git a/manual/Types/selectors.html b/manual/Types/selectors.html index 1ce803282..0c75d4ac9 100644 --- a/manual/Types/selectors.html +++ b/manual/Types/selectors.html @@ -133,6 +133,16 @@
Here is an example of how to use the Contains Selector:
diff --git a/src/main/org/apache/tools/ant/types/resources/StringResource.java b/src/main/org/apache/tools/ant/types/resources/StringResource.java index 78b4f53fe..a550b2b0e 100644 --- a/src/main/org/apache/tools/ant/types/resources/StringResource.java +++ b/src/main/org/apache/tools/ant/types/resources/StringResource.java @@ -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); diff --git a/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java b/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java index 66080b7c9..6dabaf4cb 100644 --- a/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java @@ -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); diff --git a/src/main/org/apache/tools/ant/util/ResourceUtils.java b/src/main/org/apache/tools/ant/util/ResourceUtils.java index e572a553c..7b6bc2557 100644 --- a/src/main/org/apache/tools/ant/util/ResourceUtils.java +++ b/src/main/org/apache/tools/ant/util/ResourceUtils.java @@ -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); diff --git a/src/tests/antunit/core/nested-text-test.xml b/src/tests/antunit/core/nested-text-test.xml index c7f9dfd3d..05fb4c853 100644 --- a/src/tests/antunit/core/nested-text-test.xml +++ b/src/tests/antunit/core/nested-text-test.xml @@ -32,12 +32,11 @@