FileDocCategorySizeDatePackage
LineContainsRegExp.javaAPI DocApache Ant 1.707282Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.filters

LineContainsRegExp

public final class LineContainsRegExp extends BaseParamFilterReader implements ChainableReader
Filter which includes only those lines that contain the user-specified regular expression matching strings. Example:
<linecontainsregexp>
<regexp pattern="foo*">
</linecontainsregexp>
Or:
<filterreader classname="org.apache.tools.ant.filters.LineContainsRegExp">
<param type="regexp" value="foo*"/>
</filterreader>
This will fetch all those lines that contain the pattern foo

Fields Summary
private static final String
REGEXP_KEY
Parameter name for the regular expression to filter on.
private static final String
NEGATE_KEY
Parameter name for the words to filter on.
private Vector
regexps
Vector that holds the expressions that input lines must contain.
private String
line
Remaining line to be read from this filter, or null if the next call to read() should read the original stream to find the next matching line.
private boolean
negate
Constructors Summary
public LineContainsRegExp()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public LineContainsRegExp(Reader in)
Creates a new filtered reader.

param
in A Reader object providing the underlying stream. Must not be null.

        super(in);
    
Methods Summary
public voidaddConfiguredRegexp(org.apache.tools.ant.types.RegularExpression regExp)
Adds a regexp element.

param
regExp The regexp element to add. Must not be null.

        this.regexps.addElement(regExp);
    
public java.io.Readerchain(java.io.Reader rdr)
Creates a new LineContainsRegExp using the passed in Reader for instantiation.

param
rdr A Reader object providing the underlying stream. Must not be null.
return
a new filter based on this configuration, but filtering the specified reader

        LineContainsRegExp newFilter = new LineContainsRegExp(rdr);
        newFilter.setRegexps(getRegexps());
        newFilter.setNegate(isNegated());
        return newFilter;
    
private java.util.VectorgetRegexps()
Returns the vector of regular expressions which must be contained within a line read from the original stream in order for it to match this filter.

return
the vector of regular expressions which must be contained within a line read from the original stream in order for it to match this filter. The returned object is "live" - in other words, changes made to the returned object are mirrored in the filter.

        return regexps;
    
private voidinitialize()
Parses parameters to add user defined regular expressions.

        Parameter[] params = getParameters();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if (REGEXP_KEY.equals(params[i].getType())) {
                    String pattern = params[i].getValue();
                    RegularExpression regexp = new RegularExpression();
                    regexp.setPattern(pattern);
                    regexps.addElement(regexp);
                } else if (NEGATE_KEY.equals(params[i].getType())) {
                    setNegate(Project.toBoolean(params[i].getValue()));
                }
            }
        }
    
public booleanisNegated()
Find out whether we have been negated.

return
boolean negation flag.

        return negate;
    
public intread()
Returns the next character in the filtered stream, only including lines from the original stream which match all of the specified regular expressions.

return
the next character in the resulting stream, or -1 if the end of the resulting stream has been reached
exception
IOException if the underlying stream throws an IOException during reading

        if (!getInitialized()) {
            initialize();
            setInitialized(true);
        }

        int ch = -1;

        if (line != null) {
            ch = line.charAt(0);
            if (line.length() == 1) {
                line = null;
            } else {
                line = line.substring(1);
            }
        } else {
            final int regexpsSize = regexps.size();

            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());
                    matches = re.matches(line);
                }
                if (matches ^ isNegated()) {
                    break;
                }
            }
            if (line != null) {
                return read();
            }
        }
        return ch;
    
public voidsetNegate(boolean b)
Set the negation mode. Default false (no negation).

param
b the boolean negation mode to set.

        negate = b;
    
private voidsetRegexps(java.util.Vector regexps)
Sets the vector of regular expressions which must be contained within a line read from the original stream in order for it to match this filter.

param
regexps A vector of regular expressions which must be contained within a line in order for it to match in this filter. Must not be null.

        this.regexps = regexps;