FileDocCategorySizeDatePackage
ContainsRegexpSelector.javaAPI DocApache Ant 1.705566Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types.selectors

ContainsRegexpSelector

public class ContainsRegexpSelector extends BaseExtendSelector implements org.apache.tools.ant.types.resources.selectors.ResourceSelector
Selector that filters files based on a regular expression.
since
Ant 1.6

Fields Summary
private String
userProvidedExpression
private org.apache.tools.ant.types.RegularExpression
myRegExp
private org.apache.tools.ant.util.regexp.Regexp
myExpression
public static final String
EXPRESSION_KEY
Key to used for parameterized custom selector
Constructors Summary
public ContainsRegexpSelector()
Creates a new ContainsRegexpSelector instance.


              
      
    
Methods Summary
public booleanisSelected(java.io.File basedir, java.lang.String filename, java.io.File file)
Tests a regular expression against each line of text in the file.

param
basedir the base directory the scan is being done from
param
filename is the name of the file to check
param
file is a java.io.File object the selector can use
return
whether the file should be selected or not

        return isSelected(new FileResource(file));
    
public booleanisSelected(org.apache.tools.ant.types.Resource r)
Tests a regular expression against each line of text in a Resource.

param
r the Resource to check.
return
whether the Resource is selected or not

        String teststr = null;
        BufferedReader in = null;

        // throw BuildException on error

        validate();

        if (r.isDirectory()) {
            return true;
        }

        if (myRegExp == null) {
            myRegExp = new RegularExpression();
            myRegExp.setPattern(userProvidedExpression);
            myExpression = myRegExp.getRegexp(getProject());
        }

        try {
            in = new BufferedReader(new InputStreamReader(r.getInputStream()));
        } catch (Exception e) {
            throw new BuildException("Could not get InputStream from "
                    + r.toLongString(), e);
        }
        try {
            teststr = in.readLine();

            while (teststr != null) {

                if (myExpression.matches(teststr)) {
                    return true;
                }
                teststr = in.readLine();
            }

            return false;
        } catch (IOException ioe) {
            throw new BuildException("Could not read " + r.toLongString());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    throw new BuildException("Could not close "
                                             + r.toLongString());
                }
            }
        }
    
public voidsetExpression(java.lang.String theexpression)
The regular expression used to search the file.

param
theexpression this must match a line in the file to be selected.

        this.userProvidedExpression = theexpression;
    
public voidsetParameters(org.apache.tools.ant.types.Parameter[] parameters)
When using this as a custom selector, this method will be called. It translates each parameter into the appropriate setXXX() call.

param
parameters the complete set of parameters for this selector

        super.setParameters(parameters);
        if (parameters != null) {
            for (int i = 0; i < parameters.length; i++) {
                String paramname = parameters[i].getName();
                if (EXPRESSION_KEY.equalsIgnoreCase(paramname)) {
                    setExpression(parameters[i].getValue());
                } else {
                    setError("Invalid parameter " + paramname);
                }
            }
        }
    
public java.lang.StringtoString()

return
a string describing this object

        StringBuffer buf = new StringBuffer(
                "{containsregexpselector expression: ");
        buf.append(userProvidedExpression);
        buf.append("}");
        return buf.toString();
    
public voidverifySettings()
Checks that an expression was specified.

        if (userProvidedExpression == null) {
            setError("The expression attribute is required");
        }