We use the Filter task to do a lot of simple templating. In some places
we use blank values, to "turn stuff off". But in many places blank
values can cause problems.
Currently we setup a fail task to check required values are set. This
patch adds an optional required attribute to the Filter task. If the
required attribute is true, then a BUILD FAILED will result if the
value of the token is blank (value.equals("")) at the time of token
replacement.
Erik Meade
Index: docs/manual/CoreTasks/filter.html
===================================================================
RCS file: /home/cvspublic/ant/docs/manual/CoreTasks/filter.html,v
retrieving revision 1.10
diff -u -r1.10 filter.html
--- docs/manual/CoreTasks/filter.html 9 Feb 2004 21:50:05 -0000 1.10
+++ docs/manual/CoreTasks/filter.html 1 May 2004 04:59:22 -0000
@@ -36,6 +36,11 @@
<td align="center" valign="top">Yes*</td>
</tr>
<tr>
+ <td valign=top>required</td>
+ <td valign=top>Should the build fail if the value is equal to "" when replacing.</td>
+ <td valign=top align="center">No</td>
+ </tr>
+ <tr>
<td valign="top">filtersfile</td>
<td valign="top">The file from which the filters must be read. This file must be a formatted as a property file. </td>
<td align="center" valign="top">Yes*</td>
Index: docs/manual/CoreTypes/filterset.html
===================================================================
RCS file: /home/cvspublic/ant/docs/manual/CoreTypes/filterset.html,v
retrieving revision 1.14
diff -u -r1.14 filterset.html
--- docs/manual/CoreTypes/filterset.html 9 Feb 2004 21:50:07 -0000 1.14
+++ docs/manual/CoreTypes/filterset.html 1 May 2004 04:59:22 -0000
@@ -78,6 +78,11 @@
(eg., <code>Thursday, April 26, 2001</code>).</TD>
<TD vAlign=top align="center">Yes</TD>
</TR>
+ <TR>
+ <TD vAlign=top>required</TD>
+ <TD vAlign=top>Should the build fail if the value is equal to "" when replacing.</TD>
+ <TD vAlign=top align="center">No</TD>
+ </TR>
</TABLE>
<H2>Filtersfile</H2>
Index: src/etc/testcases/taskdefs/filter.xml
===================================================================
RCS file: /home/cvspublic/ant/src/etc/testcases/taskdefs/filter.xml,v
retrieving revision 1.6
diff -u -r1.6 filter.xml
--- src/etc/testcases/taskdefs/filter.xml 1 Jun 2002 12:26:36 -0000 1.6
+++ src/etc/testcases/taskdefs/filter.xml 1 May 2004 04:59:26 -0000
@@ -49,6 +49,11 @@
</copy>
</target>
+ <target name="test10">
+ <filter token="year" value="" required="yes"/>
+ <copy file="filter1.txt" tofile="filtered.tmp" filtering="yes" overwrite="yes"/>
+ </target>
+
<target name="cleanup">
<delete dir="taskdefs.tmp" />
</target>
Index: src/main/org/apache/tools/ant/taskdefs/Filter.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/taskdefs/Filter.java,v
retrieving revision 1.27
diff -u -r1.27 Filter.java
--- src/main/org/apache/tools/ant/taskdefs/Filter.java 9 Mar 2004 16:48:04 -0000 1.27
+++ src/main/org/apache/tools/ant/taskdefs/Filter.java 1 May 2004 04:59:26 -0000
@@ -36,6 +36,7 @@
private String token;
private String value;
private File filtersFile;
+ private boolean required;
/**
* The token string without @ delimiters.
@@ -51,6 +52,24 @@
*/
public void setValue(String value) {
this.value = value;
+ }
+
+ /**
+ * Will a BUILD FAILED be triggered if while replacing a token the value
+ * of that token has not been defined (i.e. value.equals("") == true).
+ *
+ * @return Returns true if an undefined value should cause a BUILD FAILED
+ */
+ public boolean isRequired() {
+ return required;
+ }
+
+ /**
+ * Is the value required to be defined.
+ * @param required The required to set.
+ */
+ public void setRequired(boolean required) {
+ this.required = required;
}
/**
Index: src/main/org/apache/tools/ant/types/FilterSet.java
===================================================================
RCS file: /home/cvspublic/ant/src/main/org/apache/tools/ant/types/FilterSet.java,v
retrieving revision 1.29
diff -u -r1.29 FilterSet.java
--- src/main/org/apache/tools/ant/types/FilterSet.java 9 Mar 2004 16:48:41 -0000 1.29
+++ src/main/org/apache/tools/ant/types/FilterSet.java 1 May 2004 04:59:27 -0000
@@ -47,6 +47,22 @@
/** The value which will replace the token in the filtering operation */
String value;
+ /** if the value is undefined and replaced, throw a BUILD FAILED */
+ boolean required;
+
+ /**
+ * Constructor for the Filter object
+ *
+ * @param token The token which will be replaced when filtering
+ * @param value The value which will replace the token when filtering
+ * @param required Should the build fail if the value is not defined
+ */
+ public Filter(String token, String value, boolean required) {
+ this.token = token;
+ this.value = value;
+ this.required = required;
+ }
+
/**
* Constructor for the Filter object
*
@@ -54,8 +70,7 @@
* @param value The value which will replace the token when filtering
*/
public Filter(String token, String value) {
- this.token = token;
- this.value = value;
+ this(token, value, false);
}
/**
@@ -355,6 +370,11 @@
b.append(line.substring(i, index));
if (tokens.containsKey(token)) {
value = (String) tokens.get(token);
+ if (value.equals("")) {
+ throw new BuildException(beginToken + token +
+ endToken + " is marked as required -> " +
+ value);
+ }
if (!value.equals(token)) {
// we have another token, let's parse it.
value = replaceTokens(value, token);
Index: src/testcases/org/apache/tools/ant/taskdefs/FilterTest.java
===================================================================
RCS file: /home/cvspublic/ant/src/testcases/org/apache/tools/ant/taskdefs/FilterTest.java,v
retrieving revision 1.13
diff -u -r1.13 FilterTest.java
--- src/testcases/org/apache/tools/ant/taskdefs/FilterTest.java 9 Mar 2004 16:48:57 -0000 1.13
+++ src/testcases/org/apache/tools/ant/taskdefs/FilterTest.java 1 May 2004 04:59:27 -0000
@@ -87,6 +87,10 @@
getFilteredFile("9", "taskdefs.tmp/filter3.txt"));
}
+ public void test10() {
+ expectBuildException("test10", "value is not defined, but required");
+ }
+
private String getFilteredFile(String testNumber, String filteredFile) {
String line = null;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]