Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 257   Methods: 10
NCLOC: 123   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
UpToDate.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2003 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.Enumeration;
 59   
 import java.util.Vector;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.DirectoryScanner;
 62   
 import org.apache.tools.ant.Project;
 63   
 import org.apache.tools.ant.Task;
 64   
 import org.apache.tools.ant.taskdefs.condition.Condition;
 65   
 import org.apache.tools.ant.types.FileSet;
 66   
 import org.apache.tools.ant.types.Mapper;
 67   
 import org.apache.tools.ant.util.FileNameMapper;
 68   
 import org.apache.tools.ant.util.MergingMapper;
 69   
 import org.apache.tools.ant.util.SourceFileScanner;
 70   
 
 71   
 /**
 72   
  * Sets the given property if the specified target has a timestamp
 73   
  * greater than all of the source files.
 74   
  *
 75   
  * @author William Ferguson 
 76   
  *         <a href="mailto:williamf@mincom.com">williamf@mincom.com</a> 
 77   
  * @author Hiroaki Nakamura 
 78   
  *         <a href="mailto:hnakamur@mc.neweb.ne.jp">hnakamur@mc.neweb.ne.jp</a>
 79   
  * @author Stefan Bodewig
 80   
  *
 81   
  * @since Ant 1.2
 82   
  *
 83   
  * @ant.task category="control"
 84   
  */
 85   
 
 86   
 public class UpToDate extends Task implements Condition {
 87   
 
 88   
     private String _property;
 89   
     private String _value;
 90   
     private File _sourceFile;
 91   
     private File _targetFile;
 92   
     private Vector sourceFileSets = new Vector();
 93   
 
 94   
     protected Mapper mapperElement = null;
 95   
 
 96   
     /**
 97   
      * The property to set if the target file is more up-to-date than
 98   
      * (each of) the source file(s).
 99   
      *
 100   
      * @param property the name of the property to set if Target is up-to-date.
 101   
      */
 102  0
     public void setProperty(String property) {
 103  0
         _property = property;
 104   
     }
 105   
 
 106   
     /**
 107   
      * The value to set the named property to if the target file is more
 108   
      * up-to-date than (each of) the source file(s). Defaults to 'true'.
 109   
      *
 110   
      * @param value the value to set the property to if Target is up-to-date
 111   
      */
 112  0
     public void setValue(String value) {
 113  0
         _value = value;
 114   
     }
 115   
 
 116   
     /**
 117   
      * Returns the value, or "true" if a specific value wasn't provided.
 118   
      */
 119  0
     private String getValue() {
 120  0
         return (_value != null) ? _value : "true";
 121   
     } 
 122   
 
 123   
     /**
 124   
      * The file which must be more up-to-date than (each of) the source file(s)
 125   
      * if the property is to be set.
 126   
      *
 127   
      * @param file the file we are checking against.
 128   
      */
 129  0
     public void setTargetFile(File file) {
 130  0
         _targetFile = file;
 131   
     }
 132   
 
 133   
     /**
 134   
      * The file that must be older than the target file
 135   
      * if the property is to be set.
 136   
      *
 137   
      * @param file the file we are checking against the target file.
 138   
      */
 139  0
     public void setSrcfile(File file) {
 140  0
         _sourceFile = file;
 141   
     }
 142   
 
 143   
     /**
 144   
      * Nested &lt;srcfiles&gt; element.
 145   
      */
 146  0
     public void addSrcfiles(FileSet fs) {
 147  0
         sourceFileSets.addElement(fs);
 148   
     }
 149   
 
 150   
     /**
 151   
      * Defines the FileNameMapper to use (nested mapper element).
 152   
      */
 153  0
     public Mapper createMapper() throws BuildException {
 154  0
         if (mapperElement != null) {
 155  0
             throw new BuildException("Cannot define more than one mapper",
 156   
                                      getLocation());
 157   
         }
 158  0
         mapperElement = new Mapper(getProject());
 159  0
         return mapperElement;
 160   
     }
 161   
 
 162   
     /**
 163   
      * Evaluate (all) target and source file(s) to
 164   
      * see if the target(s) is/are up-to-date.
 165   
      */
 166  0
     public boolean eval() {
 167  0
         if (sourceFileSets.size() == 0 && _sourceFile == null) {
 168  0
             throw new BuildException("At least one srcfile or a nested "
 169   
                                      + "<srcfiles> element must be set.");
 170   
         }
 171   
 
 172  0
         if (sourceFileSets.size() > 0 && _sourceFile != null) {
 173  0
             throw new BuildException("Cannot specify both the srcfile "
 174   
                                      + "attribute and a nested <srcfiles> "
 175   
                                      + "element.");
 176   
         }
 177   
 
 178  0
         if (_targetFile == null && mapperElement == null) {
 179  0
             throw new BuildException("The targetfile attribute or a nested "
 180   
                                      + "mapper element must be set.");
 181   
         }
 182   
 
 183   
         // if the target file is not there, then it can't be up-to-date
 184  0
         if (_targetFile != null && !_targetFile.exists()) {
 185  0
             return false;
 186   
         } 
 187   
 
 188   
         // if the source file isn't there, throw an exception
 189  0
         if (_sourceFile != null && !_sourceFile.exists()) {
 190  0
             throw new BuildException(_sourceFile.getAbsolutePath() 
 191   
                                      + " not found.");
 192   
         }
 193   
 
 194  0
         Enumeration enum = sourceFileSets.elements();
 195  0
         boolean upToDate = true;
 196  0
         while (upToDate && enum.hasMoreElements()) {
 197  0
             FileSet fs = (FileSet) enum.nextElement();
 198  0
             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 199  0
             upToDate = upToDate && scanDir(fs.getDir(getProject()),
 200   
                                            ds.getIncludedFiles());
 201   
         }
 202   
 
 203  0
         if (_sourceFile != null) {
 204  0
             if (mapperElement == null) {
 205  0
                 upToDate = upToDate &&
 206   
                     (_targetFile.lastModified() >= _sourceFile.lastModified());
 207   
             } else {
 208  0
                 SourceFileScanner sfs = new SourceFileScanner(this);
 209  0
                 upToDate = upToDate &&
 210   
                     (sfs.restrict(new String[] {_sourceFile.getAbsolutePath()},
 211   
                                   null, null,
 212   
                                   mapperElement.getImplementation())
 213   
                      .length == 0);
 214   
             }
 215   
         }
 216  0
         return upToDate;
 217   
     }
 218   
 
 219   
 
 220   
     /**
 221   
      * Sets property to true if target file(s) have a more recent timestamp
 222   
      * than (each of) the corresponding source file(s).
 223   
      */
 224  0
     public void execute() throws BuildException {
 225  0
         if (_property == null) {
 226  0
             throw new BuildException("property attribute is required.", 
 227   
                                      getLocation());
 228   
         }
 229  0
         boolean upToDate = eval();
 230  0
         if (upToDate) {
 231  0
             this.getProject().setNewProperty(_property, getValue());
 232  0
             if (mapperElement == null) {
 233  0
                 log("File \"" + _targetFile.getAbsolutePath() 
 234   
                     + "\" is up-to-date.", Project.MSG_VERBOSE);
 235   
             } else {
 236  0
                 log("All target files are up-to-date.",
 237   
                     Project.MSG_VERBOSE);
 238   
             }
 239   
         }
 240   
     }
 241   
 
 242  0
     protected boolean scanDir(File srcDir, String[] files) {
 243  0
         SourceFileScanner sfs = new SourceFileScanner(this);
 244  0
         FileNameMapper mapper = null;
 245  0
         File dir = srcDir;
 246  0
         if (mapperElement == null) {
 247  0
             MergingMapper mm = new MergingMapper();
 248  0
             mm.setTo(_targetFile.getAbsolutePath());
 249  0
             mapper = mm;
 250  0
             dir = null;
 251   
         } else {
 252  0
             mapper = mapperElement.getImplementation();
 253   
         }
 254  0
         return sfs.restrict(files, srcDir, dir, mapper).length == 0;
 255   
     }
 256   
 }
 257