FileDocCategorySizeDatePackage
ContainsSelector.javaAPI DocApache Ant 1.707083Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.types.selectors

ContainsSelector

public class ContainsSelector extends BaseExtendSelector implements org.apache.tools.ant.types.resources.selectors.ResourceSelector
Selector that filters files/resources based on whether they contain a particular string.
since
1.5

Fields Summary
private String
contains
private boolean
casesensitive
private boolean
ignorewhitespace
public static final String
EXPRESSION_KEY
Key to used for parameterized custom selector
public static final String
CONTAINS_KEY
Used for parameterized custom selector
public static final String
CASE_KEY
Used for parameterized custom selector
public static final String
WHITESPACE_KEY
Used for parameterized custom selector
Constructors Summary
public ContainsSelector()
Creates a new ContainsSelector instance.



              
      
    
Methods Summary
public booleanisSelected(java.io.File basedir, java.lang.String filename, java.io.File file)
The heart of the matter. This is where the selector gets to decide on the inclusion of a file in a particular fileset.

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)
The heart of the matter. This is where the selector gets to decide on the inclusion of a Resource.

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


        // throw BuildException on error
        validate();

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

        String userstr = contains;
        if (!casesensitive) {
            userstr = contains.toLowerCase();
        }
        if (ignorewhitespace) {
            userstr = SelectorUtils.removeWhitespace(userstr);
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(r.getInputStream()));
        } catch (Exception e) {
            throw new BuildException("Could not get InputStream from "
                    + r.toLongString(), e);
        }
        try {
            String teststr = in.readLine();
            while (teststr != null) {
                if (!casesensitive) {
                    teststr = teststr.toLowerCase();
                }
                if (ignorewhitespace) {
                    teststr = SelectorUtils.removeWhitespace(teststr);
                }
                if (teststr.indexOf(userstr) > -1) {
                    return true;
                }
                teststr = in.readLine();
            }
            return false;
        } catch (IOException ioe) {
            throw new BuildException("Could not read " + r.toLongString());
        } finally {
            FileUtils.close(in);
        }
    
public voidsetCasesensitive(boolean casesensitive)
Whether to ignore case in the string being searched.

param
casesensitive whether to pay attention to case sensitivity

        this.casesensitive = casesensitive;
    
public voidsetIgnorewhitespace(boolean ignorewhitespace)
Whether to ignore whitespace in the string being searched.

param
ignorewhitespace whether to ignore any whitespace (spaces, tabs, etc.) in the searchstring

        this.ignorewhitespace = ignorewhitespace;
    
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 (CONTAINS_KEY.equalsIgnoreCase(paramname)) {
                    setText(parameters[i].getValue());
                } else if (CASE_KEY.equalsIgnoreCase(paramname)) {
                    setCasesensitive(Project.toBoolean(
                            parameters[i].getValue()));
                } else if (WHITESPACE_KEY.equalsIgnoreCase(paramname)) {
                    setIgnorewhitespace(Project.toBoolean(
                            parameters[i].getValue()));
                } else {
                    setError("Invalid parameter " + paramname);
                }
            }
        }
    
public voidsetText(java.lang.String contains)
The string to search for within a file.

param
contains the string that a file must contain to be selected.

        this.contains = contains;
    
public java.lang.StringtoString()

return
a string describing this object

        StringBuffer buf = new StringBuffer("{containsselector text: ");
        buf.append('"").append(contains).append('"");
        buf.append(" casesensitive: ");
        buf.append(casesensitive ? "true" : "false");
        buf.append(" ignorewhitespace: ");
        buf.append(ignorewhitespace ? "true" : "false");
        buf.append("}");
        return buf.toString();
    
public voidverifySettings()
Checks to make sure all settings are kosher. In this case, it means that the pattern attribute has been set.

        if (contains == null) {
            setError("The text attribute is required");
        }