Browse Source

Add negate attribute to linecontains and linecontainsregexp filterreaders.

PR: 34374


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278124 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 20 years ago
parent
commit
86cd020ce9
6 changed files with 144 additions and 33 deletions
  1. +55
    -0
      docs/manual/CoreTypes/filterchain.html
  2. +20
    -0
      src/etc/testcases/filters/build.xml
  3. +3
    -0
      src/etc/testcases/filters/expected/negatelinecontains.test
  4. +30
    -15
      src/main/org/apache/tools/ant/filters/LineContains.java
  5. +32
    -18
      src/main/org/apache/tools/ant/filters/LineContainsRegExp.java
  6. +4
    -0
      src/testcases/org/apache/tools/ant/filters/LineContainsTest.java

+ 55
- 0
docs/manual/CoreTypes/filterchain.html View File

@@ -308,6 +308,12 @@ strings.
<td vAlign=top align="center">Substring to be searched for.</td>
<td vAlign=top align="center">Yes</td>
</tr>
<tr>
<td vAlign=top>negate</td>
<td vAlign=top align="center">Whether to select
<i>non-</i>matching lines only. <b>Since Ant 1.7</b></td>
<td vAlign=top align="center">No</td>
</tr>
</table>
<p>
<h4>Example:</h4>
@@ -329,11 +335,46 @@ Convenience method:
&lt;/linecontains&gt;
</pre></blockquote>

Negation:
<blockquote><pre>
&lt;filterreader classname=&quot;org.apache.tools.ant.filters.LineContains&quot;&gt;
&lt;param type=&quot;negate&quot; value=&quot;true&quot;/&gt;
&lt;param type=&quot;contains&quot; value=&quot;foo&quot;/&gt;
&lt;param type=&quot;contains&quot; value=&quot;bar&quot;/&gt;
&lt;/filterreader&gt;
</pre></blockquote>
<i>or</i>
<blockquote><pre>
&lt;linecontains negate=&quot;true&quot;&gt;
&lt;contains value=&quot;foo&quot;/&gt;
&lt;contains value=&quot;bar&quot;/&gt;
&lt;/linecontains&gt;
</pre></blockquote>

<h3><a name="linecontainsregexp">LineContainsRegExp</a></h3>

Filter which includes only those lines that contain the user-specified
regular expression matching strings.

<table cellSpacing=0 cellPadding=2 border=1>
<tr>
<td vAlign=top><b>Parameter Type</b></td>
<td vAlign=top><b>Parameter Value</b></td>
<td vAlign=top align="center"><b>Required</b></td>
</tr>
<tr>
<td vAlign=top>regexp</td>
<td vAlign=top align="center">Regular expression to be searched for.</td>
<td vAlign=top align="center">Yes</td>
</tr>
<tr>
<td vAlign=top>negate</td>
<td vAlign=top align="center">Whether to select
<i>non-</i>matching lines only. <b>Since Ant 1.7</b></td>
<td vAlign=top align="center">No</td>
</tr>
</table>

See <a href="../CoreTypes/regexp.html">Regexp Type</a> for the description of the nested element regexp and of
the choice of regular expression implementation.
<h4>Example:</h4>
@@ -352,6 +393,20 @@ Convenience method:
&lt;/linecontainsregexp&gt;
</pre></blockquote>

Negation:
<blockquote><pre>
&lt;filterreader classname=&quot;org.apache.tools.ant.filters.LineContainsRegExp&quot;&gt;
&lt;param type=&quot;negate&quot; value=&quot;true&quot;/&gt;
&lt;param type=&quot;regexp&quot; value=&quot;foo*&quot;/&gt;
&lt;/filterreader&gt;
</pre></blockquote>
<i>or</i>
<blockquote><pre>
&lt;linecontainsregexp negate=&quot;true&quot;&gt;
&lt;regexp pattern=&quot;foo*&quot;/&gt;
&lt;/linecontainsregexp&gt;
</pre></blockquote>

<h3><a name="prefixlines">PrefixLines</a></h3>

Attaches a prefix to every line.


+ 20
- 0
src/etc/testcases/filters/build.xml View File

@@ -25,6 +25,26 @@
</fixcrlf>-->
</target>
<target name="testNegateLineContains" depends="init">
<copy file="input/linecontains.test"
tofile="result/negatelinecontains.test">
<filterchain>
<filterreader classname="org.apache.tools.ant.filters.LineContains">
<param type="negate" value="true"/>
<param type="contains" value="beta"/>
</filterreader>
</filterchain>
</copy>
<fail>
<condition>
<not>
<filesmatch file1="result/negatelinecontains.test"
file2="expected/negatelinecontains.test" />
</not>
</condition>
</fail>
</target>
<target name="testEscapeUnicode" depends="init">
<copy todir="result" encoding="UTF-8">
<fileset dir="input">


+ 3
- 0
src/etc/testcases/filters/expected/negatelinecontains.test View File

@@ -0,0 +1,3 @@
This is line 1 with alpha.
This is line 4 with gamma.
This is line 6 with delta.

+ 30
- 15
src/main/org/apache/tools/ant/filters/LineContains.java View File

@@ -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()));
}
}
}


+ 32
- 18
src/main/org/apache/tools/ant/filters/LineContainsRegExp.java View File

@@ -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()));
}
}
}


+ 4
- 0
src/testcases/org/apache/tools/ant/filters/LineContainsTest.java View File

@@ -48,4 +48,8 @@ public class LineContainsTest extends BuildFileTest {
assertTrue(FILE_UTILS.contentEquals(expected, result));
}

public void testNegateLineContains() throws IOException {
executeTarget("testNegateLineContains");
}

}

Loading…
Cancel
Save