Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 246   Methods: 8
NCLOC: 124   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Touch.java 80% 82.7% 100% 83.3%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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.taskdefs;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.IOException;
 59   
 import java.text.DateFormat;
 60   
 import java.text.ParseException;
 61   
 import java.util.Locale;
 62   
 import java.util.Vector;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.DirectoryScanner;
 65   
 import org.apache.tools.ant.Project;
 66   
 import org.apache.tools.ant.Task;
 67   
 import org.apache.tools.ant.types.FileSet;
 68   
 import org.apache.tools.ant.util.FileUtils;
 69   
 import org.apache.tools.ant.util.JavaEnvUtils;
 70   
 
 71   
 /**
 72   
  * Touch a file and/or fileset(s); corresponds to the Unix touch command.
 73   
  *
 74   
  * <p>If the file to touch doesn't exist, an empty one is
 75   
  * created. </p>
 76   
  *
 77   
  * <p>Note: Setting the modification time of files is not supported in
 78   
  * JDK 1.1.</p>
 79   
  *
 80   
  * @author Stefan Bodewig 
 81   
  * @author <a href="mailto:mj@servidium.com">Michael J. Sikorsky</a>
 82   
  * @author <a href="mailto:shaw@servidium.com">Robert Shaw</a>
 83   
  *
 84   
  * @since Ant 1.1
 85   
  *
 86   
  * @ant.task category="filesystem"
 87   
  */
 88   
 public class Touch extends Task {
 89   
 
 90   
     private File file;              
 91   
     private long millis = -1;
 92   
     private String dateTime;
 93   
     private Vector filesets = new Vector();
 94   
     private FileUtils fileUtils;
 95   
 
 96  106
     public Touch() {
 97  106
         fileUtils = FileUtils.newFileUtils();
 98   
     }
 99   
 
 100   
     /**
 101   
      * Sets a single source file to touch.  If the file does not exist
 102   
      * an empty file will be created.
 103   
      */
 104  99
     public void setFile(File file) {
 105  99
         this.file = file;
 106   
     }
 107   
 
 108   
     /**
 109   
      * the new modification time of the file
 110   
      * in milliseconds since midnight Jan 1 1970.
 111   
      * Optional, default=now
 112   
      */
 113  42
     public void setMillis(long millis) {
 114  42
         this.millis = millis;
 115   
     }
 116   
 
 117   
     /**
 118   
      * the new modification time of the file
 119   
      * in the format MM/DD/YYYY HH:MM AM <i>or</i> PM;
 120   
      * Optional, default=now
 121   
      */
 122  42
     public void setDatetime(String dateTime) {
 123  42
         this.dateTime = dateTime;
 124   
     }
 125   
 
 126   
     /**
 127   
      * Add a set of files to touch
 128   
      */
 129  7
     public void addFileset(FileSet set) {
 130  7
         filesets.addElement(set);
 131   
     }
 132   
 
 133   
     /**
 134   
      * Execute the touch operation.
 135   
      */
 136  106
     public void execute() throws BuildException {
 137  106
         long savedMillis = millis;
 138   
 
 139  106
         if (file == null && filesets.size() == 0) {
 140  0
             throw 
 141   
                 new BuildException("Specify at least one source - a file or "
 142   
                                    + "a fileset.");
 143   
         }
 144   
 
 145  106
         if (file != null && file.exists() && file.isDirectory()) {
 146  0
             throw new BuildException("Use a fileset to touch directories.");
 147   
         }
 148   
 
 149  106
         try {
 150  106
             if (dateTime != null) {
 151  42
                 DateFormat df = 
 152   
                     DateFormat.getDateTimeInstance(DateFormat.SHORT,
 153   
                                                    DateFormat.SHORT,
 154   
                                                    Locale.US);
 155  42
                 try {
 156  42
                     setMillis(df.parse(dateTime).getTime());
 157  42
                     if (millis < 0) {
 158  0
                         throw new BuildException("Date of " + dateTime
 159   
                                                  + " results in negative "
 160   
                                                  + "milliseconds value "
 161   
                                                  + "relative to epoch "
 162   
                                                  + "(January 1, 1970, "
 163   
                                                  + "00:00:00 GMT).");
 164   
                     }
 165   
                 } catch (ParseException pe) {
 166  0
                     throw new BuildException(pe.getMessage(), pe, getLocation());
 167   
                 }
 168   
             }
 169   
 
 170  106
             touch();
 171   
         } finally {
 172  106
             millis = savedMillis;
 173   
         }
 174   
     }
 175   
 
 176   
     /**
 177   
      * Does the actual work. Entry point for Untar and Expand as well.
 178   
      */
 179  106
     protected void touch() throws BuildException {
 180  106
         if (file != null) {
 181  99
             if (!file.exists()) {
 182  59
                 log("Creating " + file, Project.MSG_INFO);
 183  59
                 try {
 184  59
                     fileUtils.createNewFile(file);
 185   
                 } catch (IOException ioe) {
 186  0
                     throw new BuildException("Could not create " + file, ioe, 
 187   
                                              getLocation());
 188   
                 }
 189   
             }
 190   
         }
 191   
 
 192  106
         if (millis >= 0 && 
 193   
             JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
 194  0
             log("modification time of files cannot be set in JDK 1.1",
 195   
                 Project.MSG_WARN);
 196  0
             return;
 197   
         } 
 198   
 
 199  106
         boolean resetMillis = false;
 200  106
         if (millis < 0) {
 201  64
             resetMillis = true;
 202  64
             millis = System.currentTimeMillis();
 203   
         }
 204   
 
 205  106
         if (file != null) {
 206  99
             touch(file);
 207   
         }
 208   
 
 209   
         // deal with the filesets
 210  106
         for (int i = 0; i < filesets.size(); i++) {
 211  7
             FileSet fs = (FileSet) filesets.elementAt(i);
 212  7
             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 213  7
             File fromDir = fs.getDir(getProject());
 214   
 
 215  7
             String[] srcFiles = ds.getIncludedFiles();
 216  7
             String[] srcDirs = ds.getIncludedDirectories();
 217   
 
 218  7
             for (int j = 0; j < srcFiles.length ; j++) {
 219  70
                 touch(new File(fromDir, srcFiles[j]));
 220   
             }
 221   
          
 222  7
             for (int j = 0; j < srcDirs.length ; j++) {
 223  28
                 touch(new File(fromDir, srcDirs[j]));
 224   
             }
 225   
         }
 226   
 
 227  106
         if (resetMillis) {
 228  64
             millis = -1;
 229   
         }
 230   
     }
 231   
 
 232  197
     protected void touch(File file) throws BuildException {
 233  197
         if (!file.canWrite()) {
 234  0
             throw new BuildException("Can not change modification date of "
 235   
                                      + "read-only file " + file);
 236   
         }
 237   
 
 238  197
         if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
 239  0
             return;
 240   
         }
 241   
 
 242  197
         fileUtils.setFileLastModified(file, millis);
 243   
     }
 244   
 
 245   
 }
 246