Author: bodewig
Date: Fri Dec 5 08:08:46 2008
New Revision: 723779
URL: http://svn.apache.org/viewvc?rev=723779&view=rev
Log:
allow token and/or value of replace's replacefilter children to be specified as
nested elements. PR 39568.
Modified:
ant/core/trunk/WHATSNEW
ant/core/trunk/docs/manual/CoreTasks/replace.html
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml
Modified: ant/core/trunk/WHATSNEW
URL:
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=723779&r1=723778&r2=723779&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Fri Dec 5 08:08:46 2008
@@ -602,6 +602,11 @@
collections.
Bugzilla Report 24062.
+ * token and value of <replace>'s nested <replacefilter> can now also
+ be specified as nested elements to allow multiline content more
+ easily.
+ Bugzilla Report 39568.
+
Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================
Modified: ant/core/trunk/docs/manual/CoreTasks/replace.html
URL:
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/replace.html?rev=723779&r1=723778&r2=723779&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/replace.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/replace.html Fri Dec 5 08:08:46 2008
@@ -168,7 +168,8 @@
<tr>
<td valign="top">token</td>
<td valign="top">The string to search for.</td>
- <td align="center" valign="top">Yes</td>
+ <td align="center" valign="top">Yes unless a nested replacetoken
+ is specified</td>
</tr>
<tr>
<td valign="top">value</td>
@@ -180,6 +181,8 @@
<td valign="top">Name of the property whose value is to serve as the
replacement value.</td>
</tr>
</table>
+<p>Since Ant 1.8.0 token and value can be specified as nested elements
+ just like in the task itself.</p>
<p>If neither <i>value</i> nor <i>property</i> is used, the value provided
using the <code><replace></code> attribute <i>value</i> and/or the
<code><replacevalue></code> element is used. If no value was specified
using either of these options, the token is replaced with an empty string.
</p>
<h3>Examples</h3>
@@ -196,6 +199,10 @@
<replacefilter
token="@token3@"
property="property.key"/>
+ <replacefilter>
+ <replacetoken>@token4@</replacetoken>
+ <replacevalue>value4</replacevalue>
+ </replacefilter>
</replace>
</pre></blockquote>
<p>In file <code>configure.sh</code>, replace all instances of
"@token1@" with "defaultvalue", all instances of
"@token2@" with "value2", and all instances of
"@token3@" with the value of the property "property.key",
as it appears in property file <code>src/name.properties</code>.</p>
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java?rev=723779&r1=723778&r2=723779&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java Fri Dec
5 08:08:46 2008
@@ -107,8 +107,8 @@
* A filter to apply.
*/
public class Replacefilter {
- private String token;
- private String value;
+ private NestedString token;
+ private NestedString value;
private String replaceValue;
private String property;
@@ -122,13 +122,12 @@
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 for replacefilter.";
throw new BuildException(message);
}
- if ("".equals(token)) {
- String message = "The token attribute must not be an empty "
+ if ("".equals(token.getText())) {
+ String message = "The token must not be an empty "
+ "string.";
throw new BuildException(message);
}
@@ -170,7 +169,7 @@
if (property != null) {
return properties.getProperty(property);
} else if (value != null) {
- return value;
+ return value.getText();
} else if (Replace.this.value != null) {
return Replace.this.value.getText();
} else {
@@ -183,8 +182,8 @@
* Set the token to replace.
* @param token <code>String</code> token.
*/
- public void setToken(String token) {
- this.token = token;
+ public void setToken(String t) {
+ createReplaceToken().addText(t);
}
/**
@@ -192,7 +191,7 @@
* @return current <code>String</code> token.
*/
public String getToken() {
- return token;
+ return token.getText();
}
/**
@@ -201,7 +200,7 @@
* @param value <code>String</code> value to replace.
*/
public void setValue(String value) {
- this.value = value;
+ createReplaceValue().addText(value);
}
/**
@@ -209,7 +208,7 @@
* @return replacement or null.
*/
public String getValue() {
- return value;
+ return value.getText();
}
/**
@@ -231,6 +230,30 @@
}
/**
+ * Create a token to filter as the text of a nested element.
+ * @return nested token <code>NestedString</code> to configure.
+ * @since Ant 1.8.0
+ */
+ public NestedString createReplaceToken() {
+ if (token == null) {
+ token = new NestedString();
+ }
+ return token;
+ }
+
+ /**
+ * Create a string to replace the token as the text of a nested
element.
+ * @return replacement value <code>NestedString</code> to configure.
+ * @since Ant 1.8.0
+ */
+ public NestedString createReplaceValue() {
+ if (value == null) {
+ value = new NestedString();
+ }
+ return value;
+ }
+
+ /**
* Retrieves the output buffer of this filter. The filter guarantees
* that data is only appended to the end of this StringBuffer.
* @return The StringBuffer containing the output of this filter.
@@ -260,9 +283,10 @@
* output buffer.
*/
boolean process() {
- if (inputBuffer.length() > token.length()) {
+ String t = getToken();
+ if (inputBuffer.length() > t.length()) {
int pos = replace();
- pos = Math.max((inputBuffer.length() - token.length()), pos);
+ pos = Math.max((inputBuffer.length() - t.length()), pos);
outputBuffer.append(inputBuffer.substring(0, pos));
inputBuffer.delete(0, pos);
return true;
@@ -287,14 +311,15 @@
* replacement.
*/
private int replace() {
- int found = inputBuffer.indexOf(token);
+ String t = getToken();
+ int found = inputBuffer.indexOf(t);
int pos = -1;
- final int tokenLength = token.length();
+ final int tokenLength = t.length();
final int replaceValueLength = replaceValue.length();
while (found >= 0) {
inputBuffer.replace(found, found + tokenLength, replaceValue);
pos = found + replaceValueLength;
- found = inputBuffer.indexOf(token, pos);
+ found = inputBuffer.indexOf(t, pos);
++replaceCount;
}
return pos;
Modified: ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml?rev=723779&r1=723778&r2=723779&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml Fri Dec 5
08:08:46 2008
@@ -18,15 +18,30 @@
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
<import file="../antunit-base.xml" />
- <target name="testRCSupport">
+ <target name="setUp">
<mkdir dir="${output}"/>
<echo file="${output}/text.txt"><![CDATA[
Hello, world!
]]></echo>
+ </target>
+
+ <target name="testRCSupport" depends="setUp">
<replace token="world" value="Ant">
<file file="${output}/text.txt"/>
</replace>
<au:assertResourceContains
resource="${output}/text.txt" value="Hello, Ant!"/>
</target>
+
+ <target name="testNestedElementsOfFilter" depends="setUp">
+ <replace>
+ <file file="${output}/text.txt"/>
+ <replacefilter>
+ <replacetoken>world</replacetoken>
+ <replacevalue>Ant</replacevalue>
+ </replacefilter>
+ </replace>
+ <au:assertResourceContains
+ resource="${output}/text.txt" value="Hello, Ant!"/>
+ </target>
</project>