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