From 3094f588408bd4c0805d305db7b68aecb4a60e1c Mon Sep 17 00:00:00 2001
From: Jaikiran Pai
foo
and
* bar
.
*
+ * Starting Ant 1.10.4, the {@code matchAny} attribute can be used to control whether any one
+ * of the user-specified strings is expected to be contained in the line or all
+ * of them are expected to be contained.
+ *
+ * For example:
+ *
+ * <linecontains matchAny="true"> + * * <contains value="foo"> + * * <contains value="bar"> + * * </linecontains>+ * + * This will include only those lines that contain either
foo
or bar
.
+ *
*/
public final class LineContains
extends BaseParamFilterReader
@@ -67,6 +80,8 @@ public final class LineContains
private boolean negate = false;
+ private boolean matchAny = false;
+
/**
* Constructor for "dummy" instances.
*
@@ -116,9 +131,24 @@ public final class LineContains
for (line = readLine(); line != null; line = readLine()) {
boolean matches = true;
- for (int i = 0; matches && i < containsSize; i++) {
- String containsStr = contains.elementAt(i);
+ for (int i = 0; i < containsSize; i++) {
+ final String containsStr = contains.elementAt(i);
matches = line.contains(containsStr);
+ if (!matches) {
+ if (this.matchAny) {
+ // this one didn't match, but we are expected to have
+ // any one of them match. so try next
+ continue;
+ } else {
+ // all were expected to match, but this one didn't.
+ // so no point checking the rest
+ break;
+ }
+ } else if (this.matchAny) {
+ // we were expected to match any of the contains
+ // and this one did. so no more checks needed
+ break;
+ }
}
if (matches ^ isNegated()) {
break;
@@ -157,6 +187,31 @@ public final class LineContains
return negate;
}
+ /**
+ *
+ * @param matchAny True if this {@link LineContains} is considered a match,
+ * if {@code any} of the {@code contains} value match. False
+ * if {@code all} of the {@code contains} value are expected
+ * to match
+ * @since Ant 1.10.4
+ *
+ */
+ public void setMatchAny(final boolean matchAny) {
+ this.matchAny = matchAny;
+ }
+
+ /**
+ * @return Returns true if this {@link LineContains} is considered a match,
+ * if {@code any} of the {@code contains} value match. False
+ * if {@code all} of the {@code contains} value are expected
+ * to match
+ *
+ * @since Ant 1.10.4
+ */
+ public boolean isMatchAny() {
+ return this.matchAny;
+ }
+
/**
* 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.
@@ -195,6 +250,7 @@ public final class LineContains
LineContains newFilter = new LineContains(rdr);
newFilter.setContains(getContains());
newFilter.setNegate(isNegated());
+ newFilter.setMatchAny(isMatchAny());
return newFilter;
}
diff --git a/src/tests/junit/org/apache/tools/ant/filters/LineContainsTest.java b/src/tests/junit/org/apache/tools/ant/filters/LineContainsTest.java
index 5127c8639..e28c316ea 100644
--- a/src/tests/junit/org/apache/tools/ant/filters/LineContainsTest.java
+++ b/src/tests/junit/org/apache/tools/ant/filters/LineContainsTest.java
@@ -53,4 +53,30 @@ public class LineContainsTest {
buildRule.executeTarget("testNegateLineContains");
}
+ /**
+ * Tests that the {@code matchAny} attribute of {@link LineContains} works as expected
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testLineContainsMatchAny() throws IOException {
+ buildRule.executeTarget("testMatchAny");
+ File expected = buildRule.getProject().resolveFile("expected/linecontains-matchany.test");
+ File result = new File(buildRule.getProject().getProperty("output"), "linecontains.test");
+ assertEquals(FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result));
+ }
+
+ /**
+ * Tests that the {@code matchAny} attribute when used with the {@code negate} attribute
+ * of {@link LineContains} works as expected
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testLineContainsMatchAnyNegate() throws IOException {
+ buildRule.executeTarget("testMatchAnyNegate");
+ File expected = buildRule.getProject().resolveFile("expected/linecontains-matchany-negate.test");
+ File result = new File(buildRule.getProject().getProperty("output"), "linecontains.test");
+ assertEquals(FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result));
+ }
}