FileDocCategorySizeDatePackage
DepthSelector.javaAPI DocApache Ant 1.706325Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.types.selectors

DepthSelector

public class DepthSelector extends BaseExtendSelector
Selector that filters files based on the how deep in the directory tree they are.
since
1.5

Fields Summary
public int
min
min attribute
public int
max
max attribute
public static final String
MIN_KEY
Used for parameterized custom selector
public static final String
MAX_KEY
Used for parameterized custom selector
Constructors Summary
public DepthSelector()
Creates a new DepthSelector 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. Most of the work for this selector is offloaded into SelectorUtils, a static class that provides the same services for both FilenameSelector and DirectoryScanner.

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


        // throw BuildException on error
        validate();

        int depth = -1;
        // If you felt daring, you could cache the basedir absolute path
        String absBase = basedir.getAbsolutePath();
        String absFile = file.getAbsolutePath();
        StringTokenizer tokBase = new StringTokenizer(absBase,
                File.separator);
        StringTokenizer tokFile = new StringTokenizer(absFile,
                File.separator);
        while (tokFile.hasMoreTokens()) {
            String filetoken = tokFile.nextToken();
            if (tokBase.hasMoreTokens()) {
                String basetoken = tokBase.nextToken();
                // Sanity check. Ditch it if you want faster performance
                if (!basetoken.equals(filetoken)) {
                    throw new BuildException("File " + filename
                            + " does not appear within " + absBase
                            + "directory");
                }
            } else {
                depth += 1;
                if (max > -1 && depth > max) {
                    return false;
                }
            }
        }
        if (tokBase.hasMoreTokens()) {
            throw new BuildException("File " + filename
                + " is outside of " + absBase + "directory tree");
        }
        if (min > -1 && depth < min) {
            return false;
        }
        return true;
    
public voidsetMax(int max)
The minimum depth below the basedir before a file is selected.

param
max maximum directory levels below basedir to go

        this.max = max;
    
public voidsetMin(int min)
The minimum depth below the basedir before a file is selected.

param
min minimum directory levels below basedir to go

        this.min = min;
    
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 (MIN_KEY.equalsIgnoreCase(paramname)) {
                    try {
                        setMin(Integer.parseInt(parameters[i].getValue()));
                    } catch (NumberFormatException nfe1) {
                        setError("Invalid minimum value "
                                + parameters[i].getValue());
                    }
                } else if (MAX_KEY.equalsIgnoreCase(paramname)) {
                    try {
                        setMax(Integer.parseInt(parameters[i].getValue()));
                    } catch (NumberFormatException nfe1) {
                        setError("Invalid maximum value "
                                + parameters[i].getValue());
                    }
                } else {
                    setError("Invalid parameter " + paramname);
                }
            }
        }
    
public java.lang.StringtoString()

return
a string describing this object

        StringBuffer buf = new StringBuffer("{depthselector min: ");
        buf.append(min);
        buf.append(" max: ");
        buf.append(max);
        buf.append("}");
        return buf.toString();
    
public voidverifySettings()
Checks to make sure all settings are kosher. In this case, it means that the max depth is not lower than the min depth.

        if (min < 0 && max < 0) {
            setError("You must set at least one of the min or the "
                    + "max levels.");
        }
        if (max < min && max > -1) {
            setError("The maximum depth is lower than the minimum.");
        }