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

LineContains

public final class LineContains extends BaseParamFilterReader implements ChainableReader
Filter which includes only those lines that contain all the user-specified strings. Example:
<linecontains>
<contains value="foo">
<contains value="bar">
</linecontains>
Or:
<filterreader classname="org.apache.tools.ant.filters.LineContains">
<param type="contains" value="foo"/>
<param type="contains" value="bar"/>
</filterreader>
This will include only those lines that contain foo and bar.

Fields Summary
private static final String
CONTAINS_KEY
Parameter name for the words to filter on.
private static final String
NEGATE_KEY
Parameter name for the words to filter on.
private Vector
contains
Vector that holds the strings 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 LineContains()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public LineContains(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 voidaddConfiguredContains(org.apache.tools.ant.filters.LineContains$Contains contains)
Adds a contains element.

param
contains The contains element to add. Must not be null.

        this.contains.addElement(contains.getValue());
    
public java.io.Readerchain(java.io.Reader rdr)
Creates a new LineContains 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

        LineContains newFilter = new LineContains(rdr);
        newFilter.setContains(getContains());
        newFilter.setNegate(isNegated());
        return newFilter;
    
private java.util.VectorgetContains()
Returns the vector of words which must be contained within a line read from the original stream in order for it to match this filter.

return
the vector of words 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 contains;
    
private voidinitialize()
Parses the parameters to add user-defined contains strings.

        Parameter[] params = getParameters();
        if (params != null) {
            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()));
                }
            }
        }
    
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 contain all of the specified words.

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 containsSize = contains.size();

            for (line = readLine(); line != null; line = readLine()) {
                boolean matches = true;
                for (int i = 0; matches && i < containsSize; i++) {
                    String containsStr = (String) contains.elementAt(i);
                    matches = line.indexOf(containsStr) >= 0;
                }
                if (matches ^ isNegated()) {
                    break;
                }
            }
            if (line != null) {
                return read();
            }
        }
        return ch;
    
private voidsetContains(java.util.Vector contains)
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.

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

        this.contains = contains;
    
public voidsetNegate(boolean b)
Set the negation mode. Default false (no negation).

param
b the boolean negation mode to set.

        negate = b;