Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 282   Methods: 12
NCLOC: 150   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
DateSelector.java 73.5% 75.4% 91.7% 76.6%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.types.selectors;
 56   
 
 57   
 import java.io.File;
 58   
 import java.text.DateFormat;
 59   
 import java.text.ParseException;
 60   
 import java.util.Locale;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.taskdefs.condition.Os;
 63   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 64   
 import org.apache.tools.ant.types.Parameter;
 65   
 
 66   
 /**
 67   
  * Selector that chooses files based on their last modified date.
 68   
  *
 69   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 70   
  * @since 1.5
 71   
  */
 72   
 public class DateSelector extends BaseExtendSelector {
 73   
 
 74   
     private long millis = -1;
 75   
     private String dateTime = null;
 76   
     private boolean includeDirs = false;
 77   
     private int granularity = 0;
 78   
     private int cmp = 2;
 79   
     public final static String MILLIS_KEY = "millis";
 80   
     public final static String DATETIME_KEY = "datetime";
 81   
     public final static String CHECKDIRS_KEY = "checkdirs";
 82   
     public final static String GRANULARITY_KEY = "granularity";
 83   
     public final static String WHEN_KEY = "when";
 84   
 
 85  17
     public DateSelector() {
 86  17
         if (Os.isFamily("dos")) {
 87  0
             granularity = 2000;
 88   
         }
 89   
     }
 90   
 
 91  0
     public String toString() {
 92  0
         StringBuffer buf = new StringBuffer("{dateselector date: ");
 93  0
         buf.append(dateTime);
 94  0
         buf.append(" compare: ");
 95  0
         if (cmp == 0) {
 96  0
             buf.append("before");
 97   
         }
 98  0
         else if (cmp == 1) {
 99  0
             buf.append("after");
 100   
         } else {
 101  0
             buf.append("equal");
 102   
         }
 103  0
         buf.append(" granularity: ");
 104  0
         buf.append(granularity);
 105  0
         buf.append("}");
 106  0
         return buf.toString();
 107   
     }
 108   
 
 109   
     /**
 110   
      * For users that prefer to express time in milliseconds since 1970
 111   
      *
 112   
      * @param millis the time to compare file's last modified date to,
 113   
      *        expressed in milliseconds
 114   
      */
 115  11
     public void setMillis(long millis) {
 116  11
         this.millis = millis;
 117   
     }
 118   
 
 119   
     /**
 120   
      * Returns the millisecond value the selector is set for.
 121   
      */
 122  1
     public long getMillis() {
 123  1
         return millis;
 124   
     }
 125   
 
 126   
     /**
 127   
      * Sets the date. The user must supply it in MM/DD/YYYY HH:MM AM_PM
 128   
      * format
 129   
      *
 130   
      * @param dateTime a string in MM/DD/YYYY HH:MM AM_PM format
 131   
      */
 132  9
     public void setDatetime(String dateTime) {
 133  9
         this.dateTime = dateTime;
 134  9
         if (dateTime != null) {
 135  9
             DateFormat df = DateFormat.getDateTimeInstance(
 136   
                                                     DateFormat.SHORT,
 137   
                                                     DateFormat.SHORT,
 138   
                                                     Locale.US);
 139  9
             try {
 140  9
                 setMillis(df.parse(dateTime).getTime());
 141  8
                 if (millis < 0) {
 142  1
                     setError("Date of " + dateTime
 143   
                         + " results in negative milliseconds value relative"
 144   
                         + " to epoch (January 1, 1970, 00:00:00 GMT).");
 145   
                 }
 146   
             } catch (ParseException pe) {
 147  1
                     setError("Date of " + dateTime
 148   
                         + " Cannot be parsed correctly. It should be in"
 149   
                         + " MM/DD/YYYY HH:MM AM_PM format.");
 150   
             }
 151   
         }
 152   
     }
 153   
 
 154   
     /**
 155   
      * Should we be checking dates on directories?
 156   
      *
 157   
      * @param includeDirs whether to check the timestamp on directories
 158   
      */
 159  1
     public void setCheckdirs(boolean includeDirs) {
 160  1
         this.includeDirs = includeDirs;
 161   
     }
 162   
 
 163   
     /**
 164   
      * Sets the number of milliseconds leeway we will give before we consider
 165   
      * a file not to have matched a date.
 166   
      */
 167  2
     public void setGranularity(int granularity) {
 168  2
         this.granularity = granularity;
 169   
     }
 170   
 
 171   
     /**
 172   
      * Sets the type of comparison to be done on the file's last modified
 173   
      * date.
 174   
      *
 175   
      * @param cmp The comparison to perform, an EnumeratedAttribute
 176   
      */
 177  10
     public void setWhen(TimeComparisons cmp) {
 178  10
         this.cmp = cmp.getIndex();
 179   
     }
 180   
 
 181   
     /**
 182   
      * When using this as a custom selector, this method will be called.
 183   
      * It translates each parameter into the appropriate setXXX() call.
 184   
      *
 185   
      * @param parameters the complete set of parameters for this selector
 186   
      */
 187  4
     public void setParameters(Parameter[] parameters) {
 188  4
         super.setParameters(parameters);
 189  4
         if (parameters != null) {
 190  4
             for (int i = 0; i < parameters.length; i++) {
 191  5
                 String paramname = parameters[i].getName();
 192  5
                 if (MILLIS_KEY.equalsIgnoreCase(paramname)) {
 193  1
                     try {
 194  1
                         setMillis(new Long(parameters[i].getValue()
 195   
                                 ).longValue());
 196   
                     } catch (NumberFormatException nfe) {
 197  1
                         setError("Invalid millisecond setting " +
 198   
                             parameters[i].getValue());
 199   
                     }
 200   
                 }
 201  4
                 else if (DATETIME_KEY.equalsIgnoreCase(paramname)) {
 202  1
                     setDatetime(parameters[i].getValue());
 203   
                 }
 204  3
                 else if (CHECKDIRS_KEY.equalsIgnoreCase(paramname)) {
 205  0
                     setCheckdirs(Project.toBoolean(parameters[i].getValue()));
 206   
                 }
 207  3
                 else if (GRANULARITY_KEY.equalsIgnoreCase(paramname)) {
 208  1
                     try {
 209  1
                         setGranularity(new Integer(parameters[i].getValue()
 210   
                                 ).intValue());
 211   
                     } catch (NumberFormatException nfe) {
 212  1
                         setError("Invalid granularity setting " +
 213   
                             parameters[i].getValue());
 214   
                     }
 215   
                 }
 216  2
                 else if (WHEN_KEY.equalsIgnoreCase(paramname)) {
 217  1
                     TimeComparisons cmp = new TimeComparisons();
 218  1
                     cmp.setValue(parameters[i].getValue());
 219  1
                     setWhen(cmp);
 220   
                 }
 221   
                 else {
 222  1
                     setError("Invalid parameter " + paramname);
 223   
                 }
 224   
             }
 225   
         }
 226   
     }
 227   
 
 228   
     /**
 229   
      * This is a consistency check to ensure the selector's required
 230   
      * values have been set.
 231   
      */
 232  121
     public void verifySettings() {
 233  121
         if (dateTime == null && millis < 0) {
 234  1
             setError("You must provide a datetime or the number of "
 235   
                 + "milliseconds.");
 236   
         }
 237  120
         else if (millis < 0) {
 238  0
             setError("Date of " + dateTime
 239   
                 + " results in negative milliseconds"
 240   
                 + " value relative to epoch (January 1, 1970, 00:00:00 GMT).");
 241   
         }
 242   
     }
 243   
 
 244   
     /**
 245   
      * The heart of the matter. This is where the selector gets to decide
 246   
      * on the inclusion of a file in a particular fileset.
 247   
      *
 248   
      * @param basedir the base directory the scan is being done from
 249   
      * @param filename is the name of the file to check
 250   
      * @param file is a java.io.File object the selector can use
 251   
      * @return whether the file should be selected or not
 252   
      */
 253  127
     public boolean isSelected(File basedir, String filename, File file) {
 254  127
         validate();
 255  120
         if (file.isDirectory() && (includeDirs == false)) {
 256  18
             return true;
 257   
         }
 258  102
         if (cmp == 0) {
 259  42
             return ((file.lastModified() - granularity) < millis);
 260   
         }
 261  60
         else if (cmp == 1) {
 262  40
             return ((file.lastModified() + granularity) > millis);
 263   
         }
 264   
         else {
 265  20
             return (Math.abs(file.lastModified() -  millis) <= granularity);
 266   
         }
 267   
     }
 268   
 
 269   
     /**
 270   
      * Enumerated attribute with the values for time comparison.
 271   
      * <p>
 272   
      */
 273   
     public static class TimeComparisons extends EnumeratedAttribute {
 274  4
         public String[] getValues() {
 275  4
             return new String[] {"before", "after", "equal"};
 276   
         }
 277   
     }
 278   
 
 279   
 }
 280   
 
 281   
 
 282