Browse Source

allow token and/or value of replace's replacefilter children to be specified as nested elements. PR 39568.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@723779 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
75a677f701
4 changed files with 71 additions and 19 deletions
  1. +5
    -0
      WHATSNEW
  2. +8
    -1
      docs/manual/CoreTasks/replace.html
  3. +42
    -17
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  4. +16
    -1
      src/tests/antunit/taskdefs/replace-test.xml

+ 5
- 0
WHATSNEW View File

@@ -602,6 +602,11 @@ Other changes:
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
=============================================



+ 8
- 1
docs/manual/CoreTasks/replace.html View File

@@ -168,7 +168,8 @@ token]]&gt;&lt;/replacevalue&gt;
<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 @@ token]]&gt;&lt;/replacevalue&gt;
<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>&lt;replace&gt;</code> attribute <i>value</i> and/or the <code>&lt;replacevalue&gt;</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 @@ token]]&gt;&lt;/replacevalue&gt;
&lt;replacefilter
token=&quot;@token3@&quot;
property=&quot;property.key&quot;/&gt;
&lt;replacefilter&gt;
&lt;replacetoken&gt;@token4@&lt;/replacetoken&gt;
&lt;replacevalue&gt;value4&lt;/replacevalue&gt;
&lt;/replacefilter&gt;
&lt;/replace&gt;
</pre></blockquote>
<p>In file <code>configure.sh</code>, replace all instances of &quot;@token1@&quot; with &quot;defaultvalue&quot;, all instances of &quot;@token2@&quot; with &quot;value2&quot;, and all instances of &quot;@token3@&quot; with the value of the property &quot;property.key&quot;, as it appears in property file <code>src/name.properties</code>.</p>


+ 42
- 17
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -107,8 +107,8 @@ public class Replace extends MatchingTask {
* 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 class Replace extends MatchingTask {
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 @@ public class Replace extends MatchingTask {
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 @@ public class Replace extends MatchingTask {
* 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 @@ public class Replace extends MatchingTask {
* @return current <code>String</code> token.
*/
public String getToken() {
return token;
return token.getText();
}

/**
@@ -201,7 +200,7 @@ public class Replace extends MatchingTask {
* @param value <code>String</code> value to replace.
*/
public void setValue(String value) {
this.value = value;
createReplaceValue().addText(value);
}

/**
@@ -209,7 +208,7 @@ public class Replace extends MatchingTask {
* @return replacement or null.
*/
public String getValue() {
return value;
return value.getText();
}

/**
@@ -230,6 +229,30 @@ public class Replace extends MatchingTask {
return property;
}

/**
* 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.
@@ -260,9 +283,10 @@ public class Replace extends MatchingTask {
* 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 @@ public class Replace extends MatchingTask {
* 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;


+ 16
- 1
src/tests/antunit/taskdefs/replace-test.xml View File

@@ -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>

Loading…
Cancel
Save