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

DateSelector

public class DateSelector extends BaseExtendSelector
Selector that chooses files based on their last modified date.
since
1.5

Fields Summary
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
Utilities used for file operations
private long
millis
private String
dateTime
private boolean
includeDirs
private long
granularity
private String
pattern
private org.apache.tools.ant.types.TimeComparison
when
public static final String
MILLIS_KEY
Key to used for parameterized custom selector
public static final String
DATETIME_KEY
Key to used for parameterized custom selector
public static final String
CHECKDIRS_KEY
Key to used for parameterized custom selector
public static final String
GRANULARITY_KEY
Key to used for parameterized custom selector
public static final String
WHEN_KEY
Key to used for parameterized custom selector
public static final String
PATTERN_KEY
Key to used for parameterized custom selector
Constructors Summary
public DateSelector()
Creates a new DateSelector instance.


              
      
        granularity = FILE_UTILS.getFileTimestampGranularity();
    
Methods Summary
public longgetMillis()
Returns the millisecond value the selector is set for.

return
the millisecond value.

        if (dateTime != null) {
            validate();
        }
        return millis;
    
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 from which the scan is being performed.
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 is selected.


        validate();

        return (file.isDirectory() && !includeDirs)
            || when.evaluate(file.lastModified(), millis, granularity);
    
public voidsetCheckdirs(boolean includeDirs)
Set whether to check dates on directories.

param
includeDirs whether to check the timestamp on directories.

        this.includeDirs = includeDirs;
    
public voidsetDatetime(java.lang.String dateTime)
Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM format, unless an alternate pattern is specified via the pattern attribute.

param
dateTime a formatted date String.

        this.dateTime = dateTime;
        millis = -1;
    
public voidsetGranularity(int granularity)
Sets the number of milliseconds leeway we will give before we consider a file not to have matched a date.

param
granularity the number of milliseconds leeway.

        this.granularity = granularity;
    
public voidsetMillis(long millis)
Set the time; for users who prefer to express time in ms since 1970.

param
millis the time to compare file's last modified date to, expressed in milliseconds.

        this.millis = millis;
    
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 (MILLIS_KEY.equalsIgnoreCase(paramname)) {
                    try {
                        setMillis(new Long(parameters[i].getValue()
                        ).longValue());
                    } catch (NumberFormatException nfe) {
                        setError("Invalid millisecond setting "
                                + parameters[i].getValue());
                    }
                } else if (DATETIME_KEY.equalsIgnoreCase(paramname)) {
                    setDatetime(parameters[i].getValue());
                } else if (CHECKDIRS_KEY.equalsIgnoreCase(paramname)) {
                    setCheckdirs(Project.toBoolean(parameters[i].getValue()));
                } else if (GRANULARITY_KEY.equalsIgnoreCase(paramname)) {
                    try {
                        setGranularity(new Integer(parameters[i].getValue()
                        ).intValue());
                    } catch (NumberFormatException nfe) {
                        setError("Invalid granularity setting "
                            + parameters[i].getValue());
                    }
                } else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
                    setWhen(new TimeComparison(parameters[i].getValue()));
                } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) {
                    setPattern(parameters[i].getValue());
                } else {
                    setError("Invalid parameter " + paramname);
                }
            }
        }
    
public voidsetPattern(java.lang.String pattern)
Sets the pattern to be used for the SimpleDateFormat.

param
pattern the pattern that defines the date format.

        this.pattern = pattern;
    
public voidsetWhen(org.apache.tools.ant.types.selectors.DateSelector$TimeComparisons tcmp)
Sets the type of comparison to be done on the file's last modified date.

param
tcmp The comparison to perform, an EnumeratedAttribute.

        setWhen((TimeComparison) tcmp);
    
public voidsetWhen(org.apache.tools.ant.types.TimeComparison t)
Set the comparison type.

param
t TimeComparison object.

        when = t;
    
public java.lang.StringtoString()

return
a string describing this object

        StringBuffer buf = new StringBuffer("{dateselector date: ");
        buf.append(dateTime);
        buf.append(" compare: ").append(when.getValue());
        buf.append(" granularity: ");
        buf.append(granularity);
        if (pattern != null) {
            buf.append(" pattern: ").append(pattern);
        }
        buf.append("}");
        return buf.toString();
    
public voidverifySettings()
This is a consistency check to ensure the selector's required values have been set.

        if (dateTime == null && millis < 0) {
            setError("You must provide a datetime or the number of "
                    + "milliseconds.");
        } else if (millis < 0 && dateTime != null) {
            // check millis and only set it once.
            DateFormat df = ((pattern == null)
                ? DateFormat.getDateTimeInstance(
                    DateFormat.SHORT, DateFormat.SHORT, Locale.US)
                : new SimpleDateFormat(pattern));

            try {
                setMillis(df.parse(dateTime).getTime());
                if (millis < 0) {
                    setError("Date of " + dateTime
                        + " results in negative milliseconds value"
                        + " relative to epoch (January 1, 1970, 00:00:00 GMT).");
                }
            } catch (ParseException pe) {
                setError("Date of " + dateTime
                        + " Cannot be parsed correctly. It should be in"
                        + ((pattern == null)
                        ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format.");
            }
        }