Home » apache-ant-1.8.1 » org.apache.tools » ant » types » selectors » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    *
   17    */
   18   
   19   package org.apache.tools.ant.types.selectors;
   20   
   21   import java.io.File;
   22   import java.text.DateFormat;
   23   import java.text.SimpleDateFormat;
   24   import java.text.ParseException;
   25   import java.util.Locale;
   26   
   27   import org.apache.tools.ant.Project;
   28   import org.apache.tools.ant.types.Parameter;
   29   import org.apache.tools.ant.types.TimeComparison;
   30   import org.apache.tools.ant.util.FileUtils;
   31   
   32   /**
   33    * Selector that chooses files based on their last modified date.
   34    *
   35    * @since 1.5
   36    */
   37   public class DateSelector extends BaseExtendSelector {
   38   
   39       /** Utilities used for file operations */
   40       private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
   41   
   42       private long millis = -1;
   43       private String dateTime = null;
   44       private boolean includeDirs = false;
   45       private long granularity = 0;
   46       private String pattern;
   47       private TimeComparison when = TimeComparison.EQUAL;
   48   
   49       /** Key to used for parameterized custom selector */
   50       public static final String MILLIS_KEY = "millis";
   51       /** Key to used for parameterized custom selector */
   52       public static final String DATETIME_KEY = "datetime";
   53       /** Key to used for parameterized custom selector */
   54       public static final String CHECKDIRS_KEY = "checkdirs";
   55       /** Key to used for parameterized custom selector */
   56       public static final String GRANULARITY_KEY = "granularity";
   57       /** Key to used for parameterized custom selector */
   58       public static final String WHEN_KEY = "when";
   59       /** Key to used for parameterized custom selector */
   60       public static final String PATTERN_KEY = "pattern";
   61   
   62       /**
   63        * Creates a new <code>DateSelector</code> instance.
   64        *
   65        */
   66       public DateSelector() {
   67           granularity = FILE_UTILS.getFileTimestampGranularity();
   68       }
   69   
   70       /**
   71        * @return a string describing this object
   72        */
   73       public String toString() {
   74           StringBuffer buf = new StringBuffer("{dateselector date: ");
   75           buf.append(dateTime);
   76           buf.append(" compare: ").append(when.getValue());
   77           buf.append(" granularity: ");
   78           buf.append(granularity);
   79           if (pattern != null) {
   80               buf.append(" pattern: ").append(pattern);
   81           }
   82           buf.append("}");
   83           return buf.toString();
   84       }
   85   
   86       /**
   87        * Set the time; for users who prefer to express time in ms since 1970.
   88        *
   89        * @param millis the time to compare file's last modified date to,
   90        *        expressed in milliseconds.
   91        */
   92       public void setMillis(long millis) {
   93           this.millis = millis;
   94       }
   95   
   96       /**
   97        * Returns the millisecond value the selector is set for.
   98        * @return the millisecond value.
   99        */
  100       public long getMillis() {
  101           if (dateTime != null) {
  102               validate();
  103           }
  104           return millis;
  105       }
  106   
  107       /**
  108        * Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM format,
  109        * unless an alternate pattern is specified via the pattern attribute.
  110        *
  111        * @param dateTime a formatted date <code>String</code>.
  112        */
  113       public void setDatetime(String dateTime) {
  114           this.dateTime = dateTime;
  115           millis = -1;
  116       }
  117   
  118       /**
  119        * Set whether to check dates on directories.
  120        *
  121        * @param includeDirs whether to check the timestamp on directories.
  122        */
  123       public void setCheckdirs(boolean includeDirs) {
  124           this.includeDirs = includeDirs;
  125       }
  126   
  127       /**
  128        * Sets the number of milliseconds leeway we will give before we consider
  129        * a file not to have matched a date.
  130        * @param granularity the number of milliseconds leeway.
  131        */
  132       public void setGranularity(int granularity) {
  133           this.granularity = granularity;
  134       }
  135   
  136       /**
  137        * Sets the type of comparison to be done on the file's last modified
  138        * date.
  139        *
  140        * @param tcmp The comparison to perform, an EnumeratedAttribute.
  141        */
  142       public void setWhen(TimeComparisons tcmp) {
  143           setWhen((TimeComparison) tcmp);
  144       }
  145   
  146       /**
  147        * Set the comparison type.
  148        * @param t TimeComparison object.
  149        */
  150       public void setWhen(TimeComparison t) {
  151           when = t;
  152       }
  153   
  154       /**
  155        * Sets the pattern to be used for the SimpleDateFormat.
  156        *
  157        * @param pattern the pattern that defines the date format.
  158        */
  159       public void setPattern(String pattern) {
  160           this.pattern = pattern;
  161       }
  162   
  163       /**
  164        * When using this as a custom selector, this method will be called.
  165        * It translates each parameter into the appropriate setXXX() call.
  166        *
  167        * @param parameters the complete set of parameters for this selector.
  168        */
  169       public void setParameters(Parameter[] parameters) {
  170           super.setParameters(parameters);
  171           if (parameters != null) {
  172               for (int i = 0; i < parameters.length; i++) {
  173                   String paramname = parameters[i].getName();
  174                   if (MILLIS_KEY.equalsIgnoreCase(paramname)) {
  175                       try {
  176                           setMillis(Long.parseLong(parameters[i].getValue()));
  177                       } catch (NumberFormatException nfe) {
  178                           setError("Invalid millisecond setting "
  179                                   + parameters[i].getValue());
  180                       }
  181                   } else if (DATETIME_KEY.equalsIgnoreCase(paramname)) {
  182                       setDatetime(parameters[i].getValue());
  183                   } else if (CHECKDIRS_KEY.equalsIgnoreCase(paramname)) {
  184                       setCheckdirs(Project.toBoolean(parameters[i].getValue()));
  185                   } else if (GRANULARITY_KEY.equalsIgnoreCase(paramname)) {
  186                       try {
  187                           setGranularity(Integer.parseInt(parameters[i].getValue()));
  188                       } catch (NumberFormatException nfe) {
  189                           setError("Invalid granularity setting "
  190                               + parameters[i].getValue());
  191                       }
  192                   } else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
  193                       setWhen(new TimeComparison(parameters[i].getValue()));
  194                   } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) {
  195                       setPattern(parameters[i].getValue());
  196                   } else {
  197                       setError("Invalid parameter " + paramname);
  198                   }
  199               }
  200           }
  201       }
  202   
  203       /**
  204        * This is a consistency check to ensure the selector's required
  205        * values have been set.
  206        */
  207       public void verifySettings() {
  208           if (dateTime == null && millis < 0) {
  209               setError("You must provide a datetime or the number of "
  210                       + "milliseconds.");
  211           } else if (millis < 0 && dateTime != null) {
  212               // check millis and only set it once.
  213               DateFormat df = ((pattern == null)
  214                   ? DateFormat.getDateTimeInstance(
  215                       DateFormat.SHORT, DateFormat.SHORT, Locale.US)
  216                   : new SimpleDateFormat(pattern));
  217   
  218               try {
  219                   setMillis(df.parse(dateTime).getTime());
  220                   if (millis < 0) {
  221                       setError("Date of " + dateTime
  222                           + " results in negative milliseconds value"
  223                           + " relative to epoch (January 1, 1970, 00:00:00 GMT).");
  224                   }
  225               } catch (ParseException pe) {
  226                   setError("Date of " + dateTime
  227                           + " Cannot be parsed correctly. It should be in"
  228                           + ((pattern == null)
  229                           ? " MM/DD/YYYY HH:MM AM_PM" : pattern) + " format.");
  230               }
  231           }
  232       }
  233   
  234       /**
  235        * The heart of the matter. This is where the selector gets to decide
  236        * on the inclusion of a file in a particular fileset.
  237        *
  238        * @param basedir the base directory from which the scan is being performed.
  239        * @param filename is the name of the file to check.
  240        * @param file is a java.io.File object the selector can use.
  241        * @return whether the file is selected.
  242        */
  243       public boolean isSelected(File basedir, String filename, File file) {
  244   
  245           validate();
  246   
  247           return (file.isDirectory() && !includeDirs)
  248               || when.evaluate(file.lastModified(), millis, granularity);
  249       }
  250   
  251       /**
  252        * Enumerated attribute with the values for time comparison.
  253        * <p>
  254        */
  255       public static class TimeComparisons extends TimeComparison {
  256       }
  257   
  258   }
  259   
  260   

Home » apache-ant-1.8.1 » org.apache.tools » ant » types » selectors » [javadoc | source]