Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 334   Methods: 6
NCLOC: 154   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DependSet.java 55.6% 63.1% 83.3% 61.1%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-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.util.Date;
 59   
 import java.util.Enumeration;
 60   
 import java.util.Vector;
 61   
 import org.apache.tools.ant.BuildException;
 62   
 import org.apache.tools.ant.DirectoryScanner;
 63   
 import org.apache.tools.ant.Project;
 64   
 import org.apache.tools.ant.taskdefs.condition.Os;
 65   
 import org.apache.tools.ant.types.FileList;
 66   
 import org.apache.tools.ant.types.FileSet;
 67   
 
 68   
 /**
 69   
  * Examines and removes out of date target files.  If any of the target files
 70   
  * are out of date with respect to any of the source files, all target
 71   
  * files are removed.  This is useful where dependencies cannot be
 72   
  * computed (for example, dynamically interpreted parameters or files
 73   
  * that need to stay in synch but are not directly linked) or where
 74   
  * the ant task in question could compute them but does not (for
 75   
  * example, the linked DTD for an XML file using the style task).
 76   
  *
 77   
  * nested arguments:
 78   
  * <ul>
 79   
  * <li>srcfileset     (fileset describing the source files to examine)
 80   
  * <li>srcfilelist    (filelist describing the source files to examine)
 81   
  * <li>targetfileset  (fileset describing the target files to examine)
 82   
  * <li>targetfilelist (filelist describing the target files to examine)
 83   
  * </ul>
 84   
  * At least one instance of either a fileset or filelist for both source and
 85   
  * target are required.
 86   
  * <p>
 87   
  * This task will examine each of the source files against each of the target
 88   
  * files. If any target files are out of date with respect to any of the source
 89   
  * files, all targets are removed. If any files named in a (src or target)
 90   
  * filelist do not exist, all targets are removed.
 91   
  * Hint: If missing files should be ignored, specify them as include patterns
 92   
  * in filesets, rather than using filelists.
 93   
  * </p><p>
 94   
  * This task attempts to optimize speed of dependency checking.  It will stop
 95   
  * after the first out of date file is found and remove all targets, rather
 96   
  * than exhaustively checking every source vs target combination unnecessarily.
 97   
  * </p><p>
 98   
  * Example uses:
 99   
  * <ul><li>
 100   
  * Record the fact that an XML file must be up to date
 101   
  * with respect to its XSD (Schema file), even though the XML file
 102   
  * itself includes no reference to its XSD.
 103   
  * </li><li>
 104   
  * Record the fact that an XSL stylesheet includes other
 105   
  * sub-stylesheets
 106   
  * </li><li>
 107   
  * Record the fact that java files must be recompiled if the ant build
 108   
  * file changes
 109   
  * </li></ul>
 110   
  *
 111   
  * @author <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
 112   
  * @ant.task category="filesystem"
 113   
  * @version $Revision: 1.21 $ $Date: 2003/02/10 14:13:34 $
 114   
  * @since Ant 1.4
 115   
  */
 116   
 public class DependSet extends MatchingTask {
 117   
 
 118   
     private Vector sourceFileSets  = new Vector();
 119   
     private Vector sourceFileLists = new Vector();
 120   
     private Vector targetFileSets  = new Vector();
 121   
     private Vector targetFileLists = new Vector();
 122   
 
 123   
     /**
 124   
      * Creates a new DependSet Task.
 125   
      **/
 126  5
     public DependSet() {
 127   
     } //-- DependSet
 128   
 
 129   
     /**
 130   
      * Add a set of source files.
 131   
      */
 132  0
     public void addSrcfileset(FileSet fs) {
 133  0
         sourceFileSets.addElement(fs);
 134   
     }
 135   
 
 136   
     /**
 137   
      * Add a list of source files.
 138   
      */
 139  3
     public void addSrcfilelist(FileList fl) {
 140  3
         sourceFileLists.addElement(fl);
 141   
     }
 142   
 
 143   
     /**
 144   
      * Add a set of target files.
 145   
      */
 146  1
     public void addTargetfileset(FileSet fs) {
 147  1
         targetFileSets.addElement(fs);
 148   
     }
 149   
 
 150   
     /**
 151   
      * Add a list of target files.
 152   
      */
 153  1
     public void addTargetfilelist(FileList fl) {
 154  1
         targetFileLists.addElement(fl);
 155   
     }
 156   
 
 157   
     /**
 158   
      * Executes the task.
 159   
      */
 160   
 
 161  4
     public void execute() throws BuildException {
 162   
 
 163  4
         if ((sourceFileSets.size() == 0) && (sourceFileLists.size() == 0)) {
 164  2
           throw new BuildException("At least one <srcfileset> or <srcfilelist>"
 165   
                                    + " element must be set");
 166   
         }
 167   
 
 168  2
         if ((targetFileSets.size() == 0) && (targetFileLists.size() == 0)) {
 169  0
           throw new BuildException("At least one <targetfileset> or"
 170   
                                    + " <targetfilelist> element must be set");
 171   
         }
 172   
 
 173  2
         long now = (new Date()).getTime();
 174   
         /*
 175   
           If we're on Windows, we have to munge the time up to 2 secs to
 176   
           be able to check file modification times.
 177   
           (Windows has a max resolution of two secs for modification times)
 178   
         */
 179  2
         if (Os.isFamily("windows")) {
 180  0
             now += 2000;
 181   
         }
 182   
 
 183   
         //
 184   
         // Grab all the target files specified via filesets
 185   
         //
 186  2
         Vector  allTargets         = new Vector();
 187  2
         long oldestTargetTime = 0;
 188  2
         File oldestTarget = null;
 189  2
         Enumeration enumTargetSets = targetFileSets.elements();
 190  2
         while (enumTargetSets.hasMoreElements()) {
 191   
 
 192  1
            FileSet targetFS          = (FileSet) enumTargetSets.nextElement();
 193  1
            if (!targetFS.getDir(getProject()).exists()) {
 194   
                // this is the same as if it was empty, no target files found
 195  0
                continue;
 196   
            }
 197   
 
 198  1
            DirectoryScanner targetDS = targetFS.getDirectoryScanner(getProject());
 199  1
            String[] targetFiles      = targetDS.getIncludedFiles();
 200   
 
 201  1
            for (int i = 0; i < targetFiles.length; i++) {
 202   
 
 203  0
               File dest = new File(targetFS.getDir(getProject()), targetFiles[i]);
 204  0
               allTargets.addElement(dest);
 205   
 
 206  0
               if (dest.lastModified() > now) {
 207  0
                  log("Warning: " + targetFiles[i] + " modified in the future.",
 208   
                      Project.MSG_WARN);
 209   
               }
 210   
 
 211  0
               if (oldestTarget == null ||
 212   
                   dest.lastModified() < oldestTargetTime) {
 213  0
                   oldestTargetTime = dest.lastModified();
 214  0
                   oldestTarget = dest;
 215   
               }
 216   
            }
 217   
         }
 218   
 
 219   
         //
 220   
         // Grab all the target files specified via filelists
 221   
         //
 222  2
         boolean upToDate            = true;
 223  2
         Enumeration enumTargetLists = targetFileLists.elements();
 224  2
         while (enumTargetLists.hasMoreElements()) {
 225   
 
 226  1
            FileList targetFL    = (FileList) enumTargetLists.nextElement();
 227  1
            String[] targetFiles = targetFL.getFiles(getProject());
 228   
 
 229  1
            for (int i = 0; i < targetFiles.length; i++) {
 230   
 
 231  1
               File dest = new File(targetFL.getDir(getProject()), targetFiles[i]);
 232  1
               if (!dest.exists()) {
 233  0
                  log(targetFiles[i] + " does not exist.", Project.MSG_VERBOSE);
 234  0
                  upToDate = false;
 235  0
                  continue;
 236   
               } else {
 237  1
                  allTargets.addElement(dest);
 238   
               }
 239  1
               if (dest.lastModified() > now) {
 240  0
                  log("Warning: " + targetFiles[i] + " modified in the future.",
 241   
                      Project.MSG_WARN);
 242   
               }
 243   
 
 244  1
               if (oldestTarget == null ||
 245   
                   dest.lastModified() < oldestTargetTime) {
 246  1
                   oldestTargetTime = dest.lastModified();
 247  1
                   oldestTarget = dest;
 248   
               }
 249   
            }
 250   
         }
 251  2
         if (oldestTarget != null) {
 252  1
             log(oldestTarget + " is oldest target file", Project.MSG_VERBOSE);
 253   
         } else {
 254   
             // no target files, then we cannot remove any target files and
 255   
             // skip the following tests right away
 256  1
             upToDate = false;
 257   
         }
 258   
 
 259   
         //
 260   
         // Check targets vs source files specified via filelists
 261   
         //
 262  2
         if (upToDate) {
 263  1
            Enumeration enumSourceLists = sourceFileLists.elements();
 264  1
            while (upToDate && enumSourceLists.hasMoreElements()) {
 265   
 
 266  1
               FileList sourceFL    = (FileList) enumSourceLists.nextElement();
 267  1
               String[] sourceFiles = sourceFL.getFiles(getProject());
 268   
 
 269  1
               for (int i = 0; upToDate && i < sourceFiles.length; i++) {
 270  1
                  File src = new File(sourceFL.getDir(getProject()), sourceFiles[i]);
 271   
 
 272  1
                  if (src.lastModified() > now) {
 273  0
                     log("Warning: " + sourceFiles[i]
 274   
                         + " modified in the future.", Project.MSG_WARN);
 275   
                  }
 276   
 
 277  1
                  if (!src.exists()) {
 278  0
                     log(sourceFiles[i] + " does not exist.",
 279   
                         Project.MSG_VERBOSE);
 280  0
                     upToDate = false;
 281  0
                     break;
 282   
                  }
 283   
 
 284  1
                  if (src.lastModified() > oldestTargetTime) {
 285  1
                     upToDate = false;
 286  1
                     log(oldestTarget + " is out of date with respect to " +
 287   
                         sourceFiles[i], Project.MSG_VERBOSE);
 288   
                  }
 289   
               }
 290   
            }
 291   
         }
 292   
 
 293   
         //
 294   
         // Check targets vs source files specified via filesets
 295   
         //
 296  2
         if (upToDate) {
 297  0
            Enumeration enumSourceSets = sourceFileSets.elements();
 298  0
            while (upToDate && enumSourceSets.hasMoreElements()) {
 299   
 
 300  0
               FileSet sourceFS         = (FileSet) enumSourceSets.nextElement();
 301  0
               DirectoryScanner sourceDS = sourceFS.getDirectoryScanner(getProject());
 302  0
               String[] sourceFiles      = sourceDS.getIncludedFiles();
 303   
 
 304  0
               for (int i = 0; upToDate && i < sourceFiles.length; i++) {
 305  0
                  File src = new File(sourceFS.getDir(getProject()), sourceFiles[i]);
 306   
 
 307  0
                  if (src.lastModified() > now) {
 308  0
                     log("Warning: " + sourceFiles[i]
 309   
                         + " modified in the future.", Project.MSG_WARN);
 310   
                  }
 311   
 
 312  0
                  if (src.lastModified() > oldestTargetTime) {
 313  0
                     upToDate = false;
 314  0
                     log(oldestTarget + " is out of date with respect to " +
 315   
                         sourceFiles[i], Project.MSG_VERBOSE);
 316   
                  }
 317   
               }
 318   
            }
 319   
         }
 320   
 
 321  2
         if (!upToDate) {
 322  2
            log("Deleting all target files. ", Project.MSG_VERBOSE);
 323  2
            for (Enumeration e = allTargets.elements(); e.hasMoreElements();) {
 324  1
               File fileToRemove = (File) e.nextElement();
 325  1
               log("Deleting file " + fileToRemove.getAbsolutePath(),
 326   
                   Project.MSG_VERBOSE);
 327  1
               fileToRemove.delete();
 328   
            }
 329   
         }
 330   
 
 331   
     } //-- execute
 332   
 
 333   
 } //-- DependSet.java
 334