diff --git a/WHATSNEW b/WHATSNEW index 40e609ed4..ea01769f6 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -598,6 +598,10 @@ Other changes: collections. Bugzilla Report 46341. + * now supports arbitrary filesystem based resource + collections. + Bugzilla Report 24062. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/replace.html b/docs/manual/CoreTasks/replace.html index bd0823e82..defff002a 100644 --- a/docs/manual/CoreTasks/replace.html +++ b/docs/manual/CoreTasks/replace.html @@ -130,6 +130,9 @@ have been regenerated by this task.

supports all attributes of <fileset> as well as the nested <include>, <exclude> and <patternset> elements.

+

Since Ant 1.8.0 this task supports any filesystem + based resource + collections as nested elements.

If either the text you want to replace or the replacement text cross line boundaries, you can use nested elements to specify them.

diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java index f304b569e..bee7640d2 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Replace.java +++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java @@ -32,11 +32,15 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.util.ArrayList; -import java.util.Enumeration; +import java.util.Iterator; import java.util.Properties; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.FileProvider; +import org.apache.tools.ant.types.resources.Union; import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.StringUtils; @@ -55,7 +59,7 @@ public class Replace extends MatchingTask { private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); - private File src = null; + private File sourceFile = null; private NestedString token = null; private NestedString value = new NestedString(); @@ -73,6 +77,8 @@ public class Replace extends MatchingTask { /** The encoding used to read and write files - if null, uses default */ private String encoding = null; + private Union resources; + /** * An inline string to use as the replacement text. */ @@ -464,9 +470,9 @@ public class Replace extends MatchingTask { try { if (replaceFilterFile != null) { Properties props = getProperties(replaceFilterFile); - Enumeration e = props.keys(); - while (e.hasMoreElements()) { - String tok = e.nextElement().toString(); + Iterator e = props.keySet().iterator(); + while (e.hasNext()) { + String tok = e.next().toString(); Replacefilter replaceFilter = createReplacefilter(); replaceFilter.setToken(tok); replaceFilter.setValue(props.getProperty(tok)); @@ -483,8 +489,8 @@ public class Replace extends MatchingTask { fileCount = 0; replaceCount = 0; - if (src != null) { - processFile(src); + if (sourceFile != null) { + processFile(sourceFile); } if (dir != null) { @@ -497,6 +503,15 @@ public class Replace extends MatchingTask { } } + if (resources != null) { + for (Iterator i = resources.iterator(); i.hasNext(); ) { + FileProvider fp = + (FileProvider) ((Resource) i.next()) + .as(FileProvider.class); + processFile(fp.getFile()); + } + } + if (summary) { log("Replaced " + replaceCount + " occurrences in " + fileCount + " files.", Project.MSG_INFO); @@ -515,9 +530,9 @@ public class Replace extends MatchingTask { * mandatory attribute is missing. */ public void validateAttributes() throws BuildException { - if (src == null && dir == null) { + if (sourceFile == null && dir == null && resources == null) { String message = "Either the file or the dir attribute " - + "must be specified"; + + "or nested resources must be specified"; throw new BuildException(message, getLocation()); } if (propertyFile != null && !propertyFile.exists()) { @@ -704,7 +719,7 @@ public class Replace extends MatchingTask { * @param file source File. */ public void setFile(File file) { - this.src = file; + this.sourceFile = file; } /** @@ -807,6 +822,21 @@ public class Replace extends MatchingTask { return filter; } + /** + * Support arbitrary file system based resource collections. + * + * @since Ant 1.8.0 + */ + public void addConfigured(ResourceCollection rc) { + if (!rc.isFilesystemOnly()) { + throw new BuildException("only filesystem resources are supported"); + } + if (resources == null) { + resources = new Union(); + } + resources.add(rc); + } + /** * Adds the token and value as first <replacefilter> element. * The token and value are always processed first. diff --git a/src/tests/antunit/taskdefs/replace-test.xml b/src/tests/antunit/taskdefs/replace-test.xml new file mode 100644 index 000000000..003637978 --- /dev/null +++ b/src/tests/antunit/taskdefs/replace-test.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + +