diff --git a/docs/manual/CoreTypes/filterchain.html b/docs/manual/CoreTypes/filterchain.html
index 8a37d712b..23627a251 100644
--- a/docs/manual/CoreTypes/filterchain.html
+++ b/docs/manual/CoreTypes/filterchain.html
@@ -308,6 +308,12 @@ strings.
Substring to be searched for. |
Yes |
+
+ negate |
+ Whether to select
+ non-matching lines only. Since Ant 1.7 |
+ No |
+
Example:
@@ -329,11 +335,46 @@ Convenience method:
</linecontains>
+Negation:
+
+<filterreader classname="org.apache.tools.ant.filters.LineContains">
+ <param type="negate" value="true"/>
+ <param type="contains" value="foo"/>
+ <param type="contains" value="bar"/>
+</filterreader>
+
+or
+
+<linecontains negate="true">
+ <contains value="foo"/>
+ <contains value="bar"/>
+</linecontains>
+
+
Filter which includes only those lines that contain the user-specified
regular expression matching strings.
+
+
+ Parameter Type |
+ Parameter Value |
+ Required |
+
+
+ regexp |
+ Regular expression to be searched for. |
+ Yes |
+
+
+ negate |
+ Whether to select
+ non-matching lines only. Since Ant 1.7 |
+ No |
+
+
+
See Regexp Type for the description of the nested element regexp and of
the choice of regular expression implementation.
Example:
@@ -352,6 +393,20 @@ Convenience method:
</linecontainsregexp>
+Negation:
+
+<filterreader classname="org.apache.tools.ant.filters.LineContainsRegExp">
+ <param type="negate" value="true"/>
+ <param type="regexp" value="foo*"/>
+</filterreader>
+
+or
+
+<linecontainsregexp negate="true">
+ <regexp pattern="foo*"/>
+</linecontainsregexp>
+
+
Attaches a prefix to every line.
diff --git a/src/etc/testcases/filters/build.xml b/src/etc/testcases/filters/build.xml
index b0ec6c3d0..32e5a9786 100644
--- a/src/etc/testcases/filters/build.xml
+++ b/src/etc/testcases/filters/build.xml
@@ -25,6 +25,26 @@
-->
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/etc/testcases/filters/expected/negatelinecontains.test b/src/etc/testcases/filters/expected/negatelinecontains.test
new file mode 100644
index 000000000..a1437e90b
--- /dev/null
+++ b/src/etc/testcases/filters/expected/negatelinecontains.test
@@ -0,0 +1,3 @@
+This is line 1 with alpha.
+This is line 4 with gamma.
+This is line 6 with delta.
diff --git a/src/main/org/apache/tools/ant/filters/LineContains.java b/src/main/org/apache/tools/ant/filters/LineContains.java
index d3489aab7..6e0474f07 100644
--- a/src/main/org/apache/tools/ant/filters/LineContains.java
+++ b/src/main/org/apache/tools/ant/filters/LineContains.java
@@ -19,6 +19,7 @@ package org.apache.tools.ant.filters;
import java.io.IOException;
import java.io.Reader;
import java.util.Vector;
+import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Parameter;
/**
@@ -49,6 +50,9 @@ public final class LineContains
/** Parameter name for the words to filter on. */
private static final String CONTAINS_KEY = "contains";
+ /** Parameter name for the words to filter on. */
+ private static final String NEGATE_KEY = "negate";
+
/** Vector that holds the strings that input lines must contain. */
private Vector contains = new Vector();
@@ -59,6 +63,8 @@ public final class LineContains
*/
private String line = null;
+ private boolean negate = false;
+
/**
* Constructor for "dummy" instances.
*
@@ -104,31 +110,22 @@ public final class LineContains
line = line.substring(1);
}
} else {
- line = readLine();
final int containsSize = contains.size();
- while (line != null) {
- for (int i = 0; i < containsSize; i++) {
+ for (line = readLine(); line != null; line = readLine()) {
+ boolean matches = true;
+ for (int i = 0; matches && i < containsSize; i++) {
String containsStr = (String) contains.elementAt(i);
- if (line.indexOf(containsStr) == -1) {
- line = null;
- break;
- }
+ matches = line.indexOf(containsStr) >= 0;
}
-
- if (line == null) {
- // line didn't match
- line = readLine();
- } else {
+ if (matches ^ isNegated()) {
break;
}
}
-
if (line != null) {
return read();
}
}
-
return ch;
}
@@ -142,6 +139,22 @@ public final class LineContains
this.contains.addElement(contains.getValue());
}
+ /**
+ * Set the negation mode. Default false (no negation).
+ * @param b the boolean negation mode to set.
+ */
+ public void setNegate(boolean b) {
+ negate = b;
+ }
+
+ /**
+ * Find out whether we have been negated.
+ * @return boolean negation flag.
+ */
+ public boolean isNegated() {
+ return negate;
+ }
+
/**
* Sets the vector of words which must be contained within a line read
* from the original stream in order for it to match this filter.
@@ -179,7 +192,7 @@ public final class LineContains
public Reader chain(final Reader rdr) {
LineContains newFilter = new LineContains(rdr);
newFilter.setContains(getContains());
- newFilter.setInitialized(true);
+ newFilter.setNegate(isNegated());
return newFilter;
}
@@ -192,6 +205,8 @@ public final class LineContains
for (int i = 0; i < params.length; i++) {
if (CONTAINS_KEY.equals(params[i].getType())) {
contains.addElement(params[i].getValue());
+ } else if (NEGATE_KEY.equals(params[i].getType())) {
+ setNegate(Project.toBoolean(params[i].getValue()));
}
}
}
diff --git a/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java b/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java
index 0e6f55429..44b40b436 100644
--- a/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java
+++ b/src/main/org/apache/tools/ant/filters/LineContainsRegExp.java
@@ -19,6 +19,7 @@ package org.apache.tools.ant.filters;
import java.io.IOException;
import java.io.Reader;
import java.util.Vector;
+import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Parameter;
import org.apache.tools.ant.types.RegularExpression;
import org.apache.tools.ant.util.regexp.Regexp;
@@ -47,6 +48,9 @@ public final class LineContainsRegExp
/** Parameter name for the regular expression to filter on. */
private static final String REGEXP_KEY = "regexp";
+ /** Parameter name for the words to filter on. */
+ private static final String NEGATE_KEY = "negate";
+
/** Vector that holds the expressions that input lines must contain. */
private Vector regexps = new Vector();
@@ -57,6 +61,8 @@ public final class LineContainsRegExp
*/
private String line = null;
+ private boolean negate = false;
+
/**
* Constructor for "dummy" instances.
*
@@ -103,34 +109,24 @@ public final class LineContainsRegExp
line = line.substring(1);
}
} else {
- line = readLine();
final int regexpsSize = regexps.size();
- while (line != null) {
- for (int i = 0; i < regexpsSize; i++) {
- RegularExpression regexp = (RegularExpression)
- regexps.elementAt(i);
+ for (line = readLine(); line != null; line = readLine()) {
+ boolean matches = true;
+ for (int i = 0; matches && i < regexpsSize; i++) {
+ RegularExpression regexp
+ = (RegularExpression) regexps.elementAt(i);
Regexp re = regexp.getRegexp(getProject());
- boolean matches = re.matches(line);
- if (!matches) {
- line = null;
- break;
- }
+ matches = re.matches(line);
}
-
- if (line == null) {
- // line didn't match
- line = readLine();
- } else {
+ if (matches ^ isNegated()) {
break;
}
}
-
if (line != null) {
return read();
}
}
-
return ch;
}
@@ -184,10 +180,26 @@ public final class LineContainsRegExp
public Reader chain(final Reader rdr) {
LineContainsRegExp newFilter = new LineContainsRegExp(rdr);
newFilter.setRegexps(getRegexps());
- newFilter.setInitialized(true);
+ newFilter.setNegate(isNegated());
return newFilter;
}
+ /**
+ * Set the negation mode. Default false (no negation).
+ * @param b the boolean negation mode to set.
+ */
+ public void setNegate(boolean b) {
+ negate = b;
+ }
+
+ /**
+ * Find out whether we have been negated.
+ * @return boolean negation flag.
+ */
+ public boolean isNegated() {
+ return negate;
+ }
+
/**
* Parses parameters to add user defined regular expressions.
*/
@@ -200,6 +212,8 @@ public final class LineContainsRegExp
RegularExpression regexp = new RegularExpression();
regexp.setPattern(pattern);
regexps.addElement(regexp);
+ } else if (NEGATE_KEY.equals(params[i].getType())) {
+ setNegate(Project.toBoolean(params[i].getValue()));
}
}
}
diff --git a/src/testcases/org/apache/tools/ant/filters/LineContainsTest.java b/src/testcases/org/apache/tools/ant/filters/LineContainsTest.java
index e9e052f2d..49fe390c4 100644
--- a/src/testcases/org/apache/tools/ant/filters/LineContainsTest.java
+++ b/src/testcases/org/apache/tools/ant/filters/LineContainsTest.java
@@ -48,4 +48,8 @@ public class LineContainsTest extends BuildFileTest {
assertTrue(FILE_UTILS.contentEquals(expected, result));
}
+ public void testNegateLineContains() throws IOException {
+ executeTarget("testNegateLineContains");
+ }
+
}