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

PresentSelector

public class PresentSelector extends BaseSelector
Selector that filters files based on whether they appear in another directory tree. It can contain a mapper element, so isn't available as an ExtendSelector (since those parameters can't hold other elements).
since
1.5

Fields Summary
private File
targetdir
private org.apache.tools.ant.types.Mapper
mapperElement
private org.apache.tools.ant.util.FileNameMapper
map
private boolean
destmustexist
Constructors Summary
public PresentSelector()
Creates a new PresentSelector instance.


              
      
    
Methods Summary
public org.apache.tools.ant.types.MappercreateMapper()
Defines the FileNameMapper to use (nested mapper element).

return
a mapper to be configured
throws
BuildException if more that one mapper defined

        if (mapperElement != null) {
            throw new BuildException("Cannot define more than one mapper");
        }
        mapperElement = new Mapper(getProject());
        return mapperElement;
    
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


        // throw BuildException on error
        validate();

        // Determine file whose existence is to be checked
        String[] destfiles = map.mapFileName(filename);
        // If filename does not match the To attribute of the mapper
        // then filter it out of the files we are considering
        if (destfiles == null) {
            return false;
        }
        // Sanity check
        if (destfiles.length != 1 || destfiles[0] == null) {
            throw new BuildException("Invalid destination file results for "
                    + targetdir + " with filename " + filename);
        }
        String destname = destfiles[0];
        File destfile = new File(targetdir, destname);
        return destfile.exists() == destmustexist;
    
public voidsetPresent(org.apache.tools.ant.types.selectors.PresentSelector$FilePresence fp)
This sets whether to select a file if its dest file is present. It could be a negate boolean, but by doing things this way, we get some documentation on how the system works. A user looking at the documentation should clearly understand that the ONLY files whose presence is being tested are those that already exist in the source directory, hence the lack of a destonly option.

param
fp An attribute set to either srconlyboth.

        if (fp.getIndex() == 0) {
            destmustexist = false;
        }
    
public voidsetTargetdir(java.io.File targetdir)
The name of the file or directory which is checked for matching files.

param
targetdir the directory to scan looking for matching files.

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

return
a string describing this object

        StringBuffer buf = new StringBuffer("{presentselector targetdir: ");
        if (targetdir == null) {
            buf.append("NOT YET SET");
        } else {
            buf.append(targetdir.getName());
        }
        buf.append(" present: ");
        if (destmustexist) {
            buf.append("both");
        } else {
            buf.append("srconly");
        }
        if (map != null) {
            buf.append(map.toString());
        } else if (mapperElement != null) {
            buf.append(mapperElement.toString());
        }
        buf.append("}");
        return buf.toString();
    
public voidverifySettings()
Checks to make sure all settings are kosher. In this case, it means that the targetdir attribute has been set and we have a mapper.

        if (targetdir == null) {
            setError("The targetdir attribute is required.");
        }
        if (mapperElement == null) {
            map = new IdentityMapper();
        } else {
            map = mapperElement.getImplementation();
        }
        if (map == null) {
            setError("Could not set <mapper> element.");
        }