bodewig 02/04/12 03:17:53
Modified: . WHATSNEW
docs/manual/CoreTasks replace.html
src/main/org/apache/tools/ant/taskdefs Rename.java
Replace.java
Log:
Document new replacefilterfile attribute of <replace>
PR: 7320
Make sure <replace> resets its state.
Cosmetics.
Revision Changes Path
1.249 +4 -0 jakarta-ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
retrieving revision 1.248
retrieving revision 1.249
diff -u -r1.248 -r1.249
--- WHATSNEW 8 Apr 2002 17:43:39 -0000 1.248
+++ WHATSNEW 12 Apr 2002 10:17:52 -0000 1.249
@@ -293,6 +293,10 @@
* <javadoc> now supports a source attribute to enable javadoc to
handle assertions present in JDK 1.4 source code.
+* <replace> supports a new replacefilterfile attribute that
+ automatically turns all properties of a given file into
+ replacefilters.
+
Changes from Ant 1.4 to Ant 1.4.1
===========================================
1.8 +11 -3 jakarta-ant/docs/manual/CoreTasks/replace.html
Index: replace.html
===================================================================
RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/replace.html,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- replace.html 3 Feb 2002 22:00:42 -0000 1.7
+++ replace.html 12 Apr 2002 10:17:52 -0000 1.8
@@ -38,8 +38,9 @@
<tr>
<td valign="top">token</td>
<td valign="top">the token which must be replaced.</td>
- <td valign="top" align="center">Yes, unless a nested
<code>replacetoken</code>
- element is used.</td>
+ <td valign="top" align="center">Yes, unless a nested
+ <code>replacetoken</code> element or the replacefilterfile
+ attribute is used.</td>
</tr>
<tr>
<td valign="top">value</td>
@@ -61,6 +62,13 @@
<td valign="top" align="center">Yes only if <i>property</i> attribute of
<code><replacefilter></code> is used.</td>
</tr>
<tr>
+ <td valign="top">replacefilterfile</td>
+ <td valign="top">valid property file. Each property will be
+ treated as a replacefilter where <code>token</code> is the name of
+ the property and <code>value</code> is the properties value.
+ <td valign="top" align="center">No.</td>
+ </tr>
+ <tr>
<td valign="top">includes</td>
<td valign="top">comma separated list of patterns of files that must be
included. All files are included when omitted.</td>
@@ -169,7 +177,7 @@
<p><b>Note:</b> It is possible to use either the
<i>token</i>/<code><replacetoken></code> and
<i>value</i>/<code><replacevalue></code> attributes/elements, the nested
replacefilter elements, or both in the same operation.
</p>
<hr>
-<p align="center">Copyright © 2001 Apache Software Foundation. All
rights
+<p align="center">Copyright © 2001-2002 Apache Software Foundation. All
rights
Reserved.</p>
</body>
1.10 +4 -3
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Rename.java
Index: Rename.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Rename.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Rename.java 26 Jan 2002 19:27:13 -0000 1.9
+++ Rename.java 12 Apr 2002 10:17:53 -0000 1.10
@@ -65,7 +65,8 @@
*
* @author [EMAIL PROTECTED]
*
- * @deprecated The rename task is deprecated. Use move instead.
+ * @deprecated The rename task is deprecated since Ant 1.2. Use move
instead.
+ * @since Ant 1.1
*/
public class Rename extends Task {
@@ -91,7 +92,7 @@
}
/**
- * Sets wheter an existing file should be replaced.
+ * Sets whether an existing file should be replaced.
* @param replace <code>on</code>, if an existing file should be
replaced.
*/
public void setReplace(String replace) {
@@ -120,7 +121,7 @@
throw new BuildException("Unable to remove existing file " +
dest);
}
- }
+ }
if (!src.renameTo(dest)) {
throw new BuildException("Unable to rename " + src + " to " +
dest);
1.26 +89 -56
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Replace.java
Index: Replace.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Replace.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- Replace.java 4 Apr 2002 10:44:18 -0000 1.25
+++ Replace.java 12 Apr 2002 10:17:53 -0000 1.26
@@ -58,6 +58,7 @@
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
@@ -84,6 +85,8 @@
* @author Stefano Mazzocchi <a href="mailto:[EMAIL PROTECTED]">[EMAIL
PROTECTED]</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Erik Langenbach</a>
*
+ * @since Ant 1.1
+ *
* @ant.task category="filesystem"
*/
public class Replace extends MatchingTask {
@@ -132,32 +135,39 @@
public void validate() throws BuildException {
//Validate mandatory attributes
if (token == null) {
- String message = "token is a mandatory attribute " + "of
replacefilter.";
+ String message = "token is a mandatory attribute "
+ + "of replacefilter.";
throw new BuildException(message);
}
if ("".equals(token)) {
- String message ="The token attribute must not be an empty
string.";
+ String message = "The token attribute must not be an empty "
+ + "string.";
throw new BuildException(message);
}
//value and property are mutually exclusive attributes
if ((value != null) && (property != null)) {
- String message = "Either value or property " + "can be
specified, but a replacefilter " + "element cannot have both.";
+ String message = "Either value or property "
+ + "can be specified, but a replacefilter "
+ + "element cannot have both.";
throw new BuildException(message);
}
if ((property != null)) {
//the property attribute must have access to a property file
if (propertyFile == null) {
- String message = "The replacefilter's property attribute
" + "can only be used with the replacetask's " + "propertyFile attribute.";
+ String message = "The replacefilter's property attribute
"
+ + "can only be used with the replacetask's "
+ + "propertyFile attribute.";
throw new BuildException(message);
}
//Make sure property exists in property file
if (properties == null ||
- properties.getProperty(property) == null) {
- String message = "property \"" + property + "\" was not
found in " + propertyFile.getPath();
+ properties.getProperty(property) == null) {
+ String message = "property \"" + property
+ + "\" was not found in " + propertyFile.getPath();
throw new BuildException(message);
}
}
@@ -210,44 +220,55 @@
*/
public void execute() throws BuildException {
- if (replaceFilterFile != null) {
- Properties properties = getProperties(replaceFilterFile);
- Enumeration enum = properties.keys();
- while(enum.hasMoreElements()){
- String token = enum.nextElement().toString();
- Replacefilter replaceFilter = createReplacefilter();
- replaceFilter.setToken(token);
- replaceFilter.setValue(properties.getProperty(token));
- }
- }
-
- validateAttributes();
-
- if (propertyFile != null) {
- properties = getProperties(propertyFile);
- }
-
- validateReplacefilters();
- fileCount = 0;
- replaceCount = 0;
+ Vector savedFilters = (Vector) replacefilters.clone();
+ Properties savedProperties =
+ properties == null ? null : (Properties) properties.clone();
- if (src != null) {
- processFile(src);
- }
-
- if (dir != null) {
- DirectoryScanner ds = super.getDirectoryScanner(dir);
- String[] srcs = ds.getIncludedFiles();
-
- for(int i=0; i<srcs.length; i++) {
- File file = new File(dir,srcs[i]);
- processFile(file);
+ try {
+ if (replaceFilterFile != null) {
+ Properties props = getProperties(replaceFilterFile);
+ Enumeration enum = props.keys();
+ while(enum.hasMoreElements()){
+ String token = enum.nextElement().toString();
+ Replacefilter replaceFilter = createReplacefilter();
+ replaceFilter.setToken(token);
+ replaceFilter.setValue(props.getProperty(token));
+ }
}
- }
+
+ validateAttributes();
+
+ if (propertyFile != null) {
+ properties = getProperties(propertyFile);
+ }
+
+ validateReplacefilters();
+ fileCount = 0;
+ replaceCount = 0;
+
+ if (src != null) {
+ processFile(src);
+ }
+
+ if (dir != null) {
+ DirectoryScanner ds = super.getDirectoryScanner(dir);
+ String[] srcs = ds.getIncludedFiles();
+
+ for(int i=0; i<srcs.length; i++) {
+ File file = new File(dir,srcs[i]);
+ processFile(file);
+ }
+ }
+
+ if (summary) {
+ log("Replaced " + replaceCount + " occurrences in "
+ + fileCount + " files.", Project.MSG_INFO);
+ }
+ } finally {
+ replacefilters = savedFilters;
+ properties = savedProperties;
+ } // end of finally
- if (summary) {
- log("Replaced " + replaceCount + " occurrences in " + fileCount
+ " files.", Project.MSG_INFO);
- }
}
/**
@@ -258,11 +279,13 @@
*/
public void validateAttributes() throws BuildException {
if (src == null && dir == null) {
- String message = "Either the file or the dir attribute " + "must
be specified";
+ String message = "Either the file or the dir attribute "
+ + "must be specified";
throw new BuildException(message, location);
}
if (propertyFile != null && !propertyFile.exists()) {
- String message = "Property file " + propertyFile.getPath() + "
does not exist.";
+ String message = "Property file " + propertyFile.getPath()
+ + " does not exist.";
throw new BuildException(message, location);
}
if (token == null && replacefilters.size() == 0) {
@@ -285,7 +308,8 @@
public void validateReplacefilters()
throws BuildException {
for (int i = 0; i < replacefilters.size(); i++) {
- Replacefilter element = (Replacefilter)
replacefilters.elementAt(i);
+ Replacefilter element =
+ (Replacefilter) replacefilters.elementAt(i);
element.validate();
}
}
@@ -297,11 +321,13 @@
properties.load(new FileInputStream(propertyFile));
}
catch (FileNotFoundException e) {
- String message = "Property file (" + propertyFile.getPath() + ")
not found.";
+ String message = "Property file (" + propertyFile.getPath()
+ + ") not found.";
throw new BuildException(message);
}
catch (IOException e) {
- String message = "Property file (" + propertyFile.getPath() + ")
cannot be loaded.";
+ String message = "Property file (" + propertyFile.getPath()
+ + ") cannot be loaded.";
throw new BuildException(message);
}
@@ -318,7 +344,8 @@
*/
private void processFile(File src) throws BuildException {
if (!src.exists()) {
- throw new BuildException("Replace: source file " + src.getPath()
+ " doesn't exist", location);
+ throw new BuildException("Replace: source file " + src.getPath()
+ + " doesn't exist", location);
}
File temp = fileUtils.createTempFile("rep", ".tmp",
@@ -362,12 +389,14 @@
// line separators in values and tokens are "\n"
// in order to compare with the file contents, replace them
// as needed
- String linesep = System.getProperty("line.separator");
- String val = stringReplace(value.getText(), "\n", linesep);
- String tok = stringReplace(token.getText(), "\n", linesep);
-
+ String val = stringReplace(value.getText(), "\n",
+ StringUtils.LINE_SEP);
+ String tok = stringReplace(token.getText(), "\n",
+ StringUtils.LINE_SEP);
+
// for each found token, replace with value
- log("Replacing in " + src.getPath() + ": " + token.getText()
+ " --> " + value.getText(), Project.MSG_VERBOSE);
+ log("Replacing in " + src.getPath() + ": " + token.getText()
+ + " --> " + value.getText(), Project.MSG_VERBOSE);
newString = stringReplace(newString, tok, val);
}
@@ -397,7 +426,8 @@
}
} catch (IOException ioe) {
throw new BuildException("IOException in " + src + " - " +
- ioe.getClass().getName() + ":" +
ioe.getMessage(), ioe, location);
+ ioe.getClass().getName() + ":"
+ + ioe.getMessage(), ioe, location);
} finally {
if (reader != null) {
try {
@@ -423,8 +453,10 @@
Replacefilter filter = (Replacefilter)
replacefilters.elementAt(i);
//for each found token, replace with value
- log("Replacing in " + filename + ": " + filter.getToken() + "
--> " + filter.getReplaceValue(), Project.MSG_VERBOSE);
- newString = stringReplace(newString, filter.getToken(),
filter.getReplaceValue());
+ log("Replacing in " + filename + ": " + filter.getToken()
+ + " --> " + filter.getReplaceValue(), Project.MSG_VERBOSE);
+ newString = stringReplace(newString, filter.getToken(),
+ filter.getReplaceValue());
}
return newString;
@@ -441,7 +473,8 @@
/**
* Request a summary
*
- * @param summary true if you would like a summary logged of the replace
operation
+ * @param summary true if you would like a summary logged of the
+ * replace operation
*/
public void setSummary(boolean summary) {
this.summary = summary;
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>