From 75a677f70106e16a72273368148b5cb68711252d Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 5 Dec 2008 16:08:46 +0000 Subject: [PATCH] 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 --- WHATSNEW | 5 ++ docs/manual/CoreTasks/replace.html | 9 ++- .../apache/tools/ant/taskdefs/Replace.java | 59 +++++++++++++------ src/tests/antunit/taskdefs/replace-test.xml | 17 +++++- 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index ea01769f6..335787d63 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -602,6 +602,11 @@ Other changes: collections. Bugzilla Report 24062. + * token and value of 's nested 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 ============================================= diff --git a/docs/manual/CoreTasks/replace.html b/docs/manual/CoreTasks/replace.html index defff002a..0b0eba3ba 100644 --- a/docs/manual/CoreTasks/replace.html +++ b/docs/manual/CoreTasks/replace.html @@ -168,7 +168,8 @@ token]]></replacevalue> token The string to search for. - Yes + Yes unless a nested replacetoken + is specified value @@ -180,6 +181,8 @@ token]]></replacevalue> Name of the property whose value is to serve as the replacement value. +

Since Ant 1.8.0 token and value can be specified as nested elements + just like in the task itself.

If neither value nor property is used, the value provided using the <replace> attribute value and/or the <replacevalue> element is used. If no value was specified using either of these options, the token is replaced with an empty string.

Examples

@@ -196,6 +199,10 @@ token]]></replacevalue> <replacefilter token="@token3@" property="property.key"/> + <replacefilter> + <replacetoken>@token4@</replacetoken> + <replacevalue>value4</replacevalue> + </replacefilter> </replace>

In file configure.sh, 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 src/name.properties.

diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java index bee7640d2..978541f7b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Replace.java +++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java @@ -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 String 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 String token. */ public String getToken() { - return token; + return token.getText(); } /** @@ -201,7 +200,7 @@ public class Replace extends MatchingTask { * @param value String 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 NestedString 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 NestedString 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; diff --git a/src/tests/antunit/taskdefs/replace-test.xml b/src/tests/antunit/taskdefs/replace-test.xml index 003637978..52cf03c60 100644 --- a/src/tests/antunit/taskdefs/replace-test.xml +++ b/src/tests/antunit/taskdefs/replace-test.xml @@ -18,15 +18,30 @@ - + + + + + + + + + + world + Ant + + + +