1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.DirectoryScanner; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.Task; 28 import org.apache.tools.ant.taskdefs.condition.Condition; 29 import org.apache.tools.ant.types.Resource; 30 import org.apache.tools.ant.types.FileSet; 31 import org.apache.tools.ant.types.resources.Union; 32 import org.apache.tools.ant.types.Mapper; 33 import org.apache.tools.ant.util.FileNameMapper; 34 import org.apache.tools.ant.util.MergingMapper; 35 import org.apache.tools.ant.util.ResourceUtils; 36 import org.apache.tools.ant.util.SourceFileScanner; 37 38 46 47 public class UpToDate extends Task implements Condition { 48 49 private String property; 50 private String value; 51 private File sourceFile; 52 private File targetFile; 53 private Vector sourceFileSets = new Vector (); 54 private Union sourceResources = new Union(); 55 56 protected Mapper mapperElement = null; 58 60 66 public void setProperty(final String property) { 67 this.property = property; 68 } 69 70 76 public void setValue(final String value) { 77 this.value = value; 78 } 79 80 83 private String getValue() { 84 return (value != null) ? value : "true"; 85 } 86 87 93 public void setTargetFile(final File file) { 94 this.targetFile = file; 95 } 96 97 103 public void setSrcfile(final File file) { 104 this.sourceFile = file; 105 } 106 107 111 public void addSrcfiles(final FileSet fs) { 112 sourceFileSets.addElement(fs); 113 } 114 115 120 public Union createSrcResources() { 121 return sourceResources; 122 } 123 124 129 public Mapper createMapper() throws BuildException { 130 if (mapperElement != null) { 131 throw new BuildException("Cannot define more than one mapper", 132 getLocation()); 133 } 134 mapperElement = new Mapper(getProject()); 135 return mapperElement; 136 } 137 138 143 public void add(FileNameMapper fileNameMapper) { 144 createMapper().add(fileNameMapper); 145 } 146 147 152 public boolean eval() { 153 if (sourceFileSets.size() == 0 && sourceResources.size() == 0 154 && sourceFile == null) { 155 throw new BuildException("At least one srcfile or a nested " 156 + "<srcfiles> or <srcresources> element " 157 + "must be set."); 158 } 159 160 if ((sourceFileSets.size() > 0 || sourceResources.size() > 0) 161 && sourceFile != null) { 162 throw new BuildException("Cannot specify both the srcfile " 163 + "attribute and a nested <srcfiles> " 164 + "or <srcresources> element."); 165 } 166 167 if (targetFile == null && mapperElement == null) { 168 throw new BuildException("The targetfile attribute or a nested " 169 + "mapper element must be set."); 170 } 171 172 if (targetFile != null && !targetFile.exists()) { 174 log("The targetfile \"" + targetFile.getAbsolutePath() 175 + "\" does not exist.", Project.MSG_VERBOSE); 176 return false; 177 } 178 179 if (sourceFile != null && !sourceFile.exists()) { 181 throw new BuildException(sourceFile.getAbsolutePath() 182 + " not found."); 183 } 184 185 boolean upToDate = true; 186 if (sourceFile != null) { 187 if (mapperElement == null) { 188 upToDate = upToDate 189 && (targetFile.lastModified() >= sourceFile.lastModified()); 190 } else { 191 SourceFileScanner sfs = new SourceFileScanner(this); 192 upToDate = upToDate 193 && (sfs.restrict(new String [] {sourceFile.getAbsolutePath()}, 194 null, null, 195 mapperElement.getImplementation()).length == 0); 196 } 197 } 198 199 Enumeration e = sourceFileSets.elements(); 204 while (upToDate && e.hasMoreElements()) { 205 FileSet fs = (FileSet) e.nextElement(); 206 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 207 upToDate = upToDate && scanDir(fs.getDir(getProject()), 208 ds.getIncludedFiles()); 209 } 210 211 if (upToDate) { 212 Resource[] r = sourceResources.listResources(); 213 upToDate = upToDate 214 && (ResourceUtils.selectOutOfDateSources( 215 this, r, getMapper(), getProject()).length == 0); 216 } 217 218 return upToDate; 219 } 220 221 222 227 public void execute() throws BuildException { 228 if (property == null) { 229 throw new BuildException("property attribute is required.", 230 getLocation()); 231 } 232 boolean upToDate = eval(); 233 if (upToDate) { 234 getProject().setNewProperty(property, getValue()); 235 if (mapperElement == null) { 236 log("File \"" + targetFile.getAbsolutePath() 237 + "\" is up-to-date.", Project.MSG_VERBOSE); 238 } else { 239 log("All target files are up-to-date.", 240 Project.MSG_VERBOSE); 241 } 242 } 243 } 244 245 251 protected boolean scanDir(File srcDir, String [] files) { 252 SourceFileScanner sfs = new SourceFileScanner(this); 253 FileNameMapper mapper = getMapper(); 254 File dir = srcDir; 255 if (mapperElement == null) { 256 dir = null; 257 } 258 return sfs.restrict(files, srcDir, dir, mapper).length == 0; 259 } 260 261 private FileNameMapper getMapper() { 262 FileNameMapper mapper = null; 263 if (mapperElement == null) { 264 MergingMapper mm = new MergingMapper(); 265 mm.setTo(targetFile.getAbsolutePath()); 266 mapper = mm; 267 } else { 268 mapper = mapperElement.getImplementation(); 269 } 270 return mapper; 271 } 272 } 273 | Popular Tags |