[14627] | 1 | /*
|
---|
| 2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | *
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | package org.apache.tools.ant.taskdefs;
|
---|
| 19 |
|
---|
| 20 | import java.io.File;
|
---|
| 21 | import java.util.Enumeration;
|
---|
| 22 | import java.util.Vector;
|
---|
| 23 | import org.apache.tools.ant.BuildException;
|
---|
| 24 | import org.apache.tools.ant.DirectoryScanner;
|
---|
| 25 | import org.apache.tools.ant.Project;
|
---|
| 26 | import org.apache.tools.ant.Task;
|
---|
| 27 | import org.apache.tools.ant.taskdefs.condition.Condition;
|
---|
| 28 | import org.apache.tools.ant.types.FileSet;
|
---|
| 29 | import org.apache.tools.ant.types.Mapper;
|
---|
| 30 | import org.apache.tools.ant.util.FileNameMapper;
|
---|
| 31 | import org.apache.tools.ant.util.MergingMapper;
|
---|
| 32 | import org.apache.tools.ant.util.SourceFileScanner;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Sets the given property if the specified target has a timestamp
|
---|
| 36 | * greater than all of the source files.
|
---|
| 37 | *
|
---|
| 38 | * @since Ant 1.2
|
---|
| 39 | *
|
---|
| 40 | * @ant.task category="control"
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | public class UpToDate extends Task implements Condition {
|
---|
| 44 |
|
---|
| 45 | private String property;
|
---|
| 46 | private String value;
|
---|
| 47 | private File sourceFile;
|
---|
| 48 | private File targetFile;
|
---|
| 49 | private Vector sourceFileSets = new Vector();
|
---|
| 50 |
|
---|
| 51 | protected Mapper mapperElement = null;
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * The property to set if the target file is more up-to-date than
|
---|
| 55 | * (each of) the source file(s).
|
---|
| 56 | *
|
---|
| 57 | * @param property the name of the property to set if Target is up-to-date.
|
---|
| 58 | */
|
---|
| 59 | public void setProperty(final String property) {
|
---|
| 60 | this.property = property;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * The value to set the named property to if the target file is more
|
---|
| 65 | * up-to-date than (each of) the source file(s). Defaults to 'true'.
|
---|
| 66 | *
|
---|
| 67 | * @param value the value to set the property to if Target is up-to-date
|
---|
| 68 | */
|
---|
| 69 | public void setValue(final String value) {
|
---|
| 70 | this.value = value;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * Returns the value, or "true" if a specific value wasn't provided.
|
---|
| 75 | */
|
---|
| 76 | private String getValue() {
|
---|
| 77 | return (value != null) ? value : "true";
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * The file which must be more up-to-date than (each of) the source file(s)
|
---|
| 82 | * if the property is to be set.
|
---|
| 83 | *
|
---|
| 84 | * @param file the file we are checking against.
|
---|
| 85 | */
|
---|
| 86 | public void setTargetFile(final File file) {
|
---|
| 87 | this.targetFile = file;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * The file that must be older than the target file
|
---|
| 92 | * if the property is to be set.
|
---|
| 93 | *
|
---|
| 94 | * @param file the file we are checking against the target file.
|
---|
| 95 | */
|
---|
| 96 | public void setSrcfile(final File file) {
|
---|
| 97 | this.sourceFile = file;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Nested <srcfiles> element.
|
---|
| 102 | * @param fs the source files
|
---|
| 103 | */
|
---|
| 104 | public void addSrcfiles(final FileSet fs) {
|
---|
| 105 | sourceFileSets.addElement(fs);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Defines the FileNameMapper to use (nested mapper element).
|
---|
| 110 | * @return a mapper to be configured
|
---|
| 111 | * @throws BuildException if more than one mapper is defined
|
---|
| 112 | */
|
---|
| 113 | public Mapper createMapper() throws BuildException {
|
---|
| 114 | if (mapperElement != null) {
|
---|
| 115 | throw new BuildException("Cannot define more than one mapper",
|
---|
| 116 | getLocation());
|
---|
| 117 | }
|
---|
| 118 | mapperElement = new Mapper(getProject());
|
---|
| 119 | return mapperElement;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
| 123 | * A nested filenamemapper
|
---|
| 124 | * @param fileNameMapper the mapper to add
|
---|
| 125 | * @since Ant 1.6.3
|
---|
| 126 | */
|
---|
| 127 | public void add(FileNameMapper fileNameMapper) {
|
---|
| 128 | createMapper().add(fileNameMapper);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * Evaluate (all) target and source file(s) to
|
---|
| 133 | * see if the target(s) is/are up-to-date.
|
---|
| 134 | * @return true if the target(s) is/are up-to-date
|
---|
| 135 | */
|
---|
| 136 | public boolean eval() {
|
---|
| 137 | if (sourceFileSets.size() == 0 && sourceFile == null) {
|
---|
| 138 | throw new BuildException("At least one srcfile or a nested "
|
---|
| 139 | + "<srcfiles> element must be set.");
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | if (sourceFileSets.size() > 0 && sourceFile != null) {
|
---|
| 143 | throw new BuildException("Cannot specify both the srcfile "
|
---|
| 144 | + "attribute and a nested <srcfiles> "
|
---|
| 145 | + "element.");
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | if (targetFile == null && mapperElement == null) {
|
---|
| 149 | throw new BuildException("The targetfile attribute or a nested "
|
---|
| 150 | + "mapper element must be set.");
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // if the target file is not there, then it can't be up-to-date
|
---|
| 154 | if (targetFile != null && !targetFile.exists()) {
|
---|
| 155 | log("The targetfile \"" + targetFile.getAbsolutePath()
|
---|
| 156 | + "\" does not exist.", Project.MSG_VERBOSE);
|
---|
| 157 | return false;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | // if the source file isn't there, throw an exception
|
---|
| 161 | if (sourceFile != null && !sourceFile.exists()) {
|
---|
| 162 | throw new BuildException(sourceFile.getAbsolutePath()
|
---|
| 163 | + " not found.");
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | Enumeration e = sourceFileSets.elements();
|
---|
| 167 | boolean upToDate = true;
|
---|
| 168 | while (upToDate && e.hasMoreElements()) {
|
---|
| 169 | FileSet fs = (FileSet) e.nextElement();
|
---|
| 170 | DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
---|
| 171 | upToDate = upToDate && scanDir(fs.getDir(getProject()),
|
---|
| 172 | ds.getIncludedFiles());
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | if (sourceFile != null) {
|
---|
| 176 | if (mapperElement == null) {
|
---|
| 177 | upToDate = upToDate
|
---|
| 178 | && (targetFile.lastModified() >= sourceFile.lastModified());
|
---|
| 179 | } else {
|
---|
| 180 | SourceFileScanner sfs = new SourceFileScanner(this);
|
---|
| 181 | upToDate = upToDate
|
---|
| 182 | && (sfs.restrict(new String[] {sourceFile.getAbsolutePath()},
|
---|
| 183 | null, null,
|
---|
| 184 | mapperElement.getImplementation()).length == 0);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | return upToDate;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | /**
|
---|
| 192 | * Sets property to true if target file(s) have a more recent timestamp
|
---|
| 193 | * than (each of) the corresponding source file(s).
|
---|
| 194 | * @throws BuildException on error
|
---|
| 195 | */
|
---|
| 196 | public void execute() throws BuildException {
|
---|
| 197 | if (property == null) {
|
---|
| 198 | throw new BuildException("property attribute is required.",
|
---|
| 199 | getLocation());
|
---|
| 200 | }
|
---|
| 201 | boolean upToDate = eval();
|
---|
| 202 | if (upToDate) {
|
---|
| 203 | getProject().setNewProperty(property, getValue());
|
---|
| 204 | if (mapperElement == null) {
|
---|
| 205 | log("File \"" + targetFile.getAbsolutePath()
|
---|
| 206 | + "\" is up-to-date.", Project.MSG_VERBOSE);
|
---|
| 207 | } else {
|
---|
| 208 | log("All target files are up-to-date.",
|
---|
| 209 | Project.MSG_VERBOSE);
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /**
|
---|
| 215 | * Scan a directory for files to check for "up to date"ness
|
---|
| 216 | * @param srcDir the directory
|
---|
| 217 | * @param files the files to scan for
|
---|
| 218 | * @return true if the files are up to date
|
---|
| 219 | */
|
---|
| 220 | protected boolean scanDir(File srcDir, String[] files) {
|
---|
| 221 | SourceFileScanner sfs = new SourceFileScanner(this);
|
---|
| 222 | FileNameMapper mapper = null;
|
---|
| 223 | File dir = srcDir;
|
---|
| 224 | if (mapperElement == null) {
|
---|
| 225 | MergingMapper mm = new MergingMapper();
|
---|
| 226 | mm.setTo(targetFile.getAbsolutePath());
|
---|
| 227 | mapper = mm;
|
---|
| 228 | dir = null;
|
---|
| 229 | } else {
|
---|
| 230 | mapper = mapperElement.getImplementation();
|
---|
| 231 | }
|
---|
| 232 | return sfs.restrict(files, srcDir, dir, mapper).length == 0;
|
---|
| 233 | }
|
---|
| 234 | }
|
---|