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. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.util.Enumeration;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.DirectoryScanner;
  23. import org.apache.tools.ant.Project;
  24. import org.apache.tools.ant.Task;
  25. import org.apache.tools.ant.taskdefs.condition.Condition;
  26. import org.apache.tools.ant.types.FileSet;
  27. import org.apache.tools.ant.types.Mapper;
  28. import org.apache.tools.ant.util.FileNameMapper;
  29. import org.apache.tools.ant.util.MergingMapper;
  30. import org.apache.tools.ant.util.SourceFileScanner;
  31. /**
  32. * Sets the given property if the specified target has a timestamp
  33. * greater than all of the source files.
  34. *
  35. * @since Ant 1.2
  36. *
  37. * @ant.task category="control"
  38. */
  39. public class UpToDate extends Task implements Condition {
  40. private String _property;
  41. private String _value;
  42. private File _sourceFile;
  43. private File _targetFile;
  44. private Vector sourceFileSets = new Vector();
  45. protected Mapper mapperElement = null;
  46. /**
  47. * The property to set if the target file is more up-to-date than
  48. * (each of) the source file(s).
  49. *
  50. * @param property the name of the property to set if Target is up-to-date.
  51. */
  52. public void setProperty(String property) {
  53. _property = property;
  54. }
  55. /**
  56. * The value to set the named property to if the target file is more
  57. * up-to-date than (each of) the source file(s). Defaults to 'true'.
  58. *
  59. * @param value the value to set the property to if Target is up-to-date
  60. */
  61. public void setValue(String value) {
  62. _value = value;
  63. }
  64. /**
  65. * Returns the value, or "true" if a specific value wasn't provided.
  66. */
  67. private String getValue() {
  68. return (_value != null) ? _value : "true";
  69. }
  70. /**
  71. * The file which must be more up-to-date than (each of) the source file(s)
  72. * if the property is to be set.
  73. *
  74. * @param file the file we are checking against.
  75. */
  76. public void setTargetFile(File file) {
  77. _targetFile = file;
  78. }
  79. /**
  80. * The file that must be older than the target file
  81. * if the property is to be set.
  82. *
  83. * @param file the file we are checking against the target file.
  84. */
  85. public void setSrcfile(File file) {
  86. _sourceFile = file;
  87. }
  88. /**
  89. * Nested <srcfiles> element.
  90. */
  91. public void addSrcfiles(FileSet fs) {
  92. sourceFileSets.addElement(fs);
  93. }
  94. /**
  95. * Defines the FileNameMapper to use (nested mapper element).
  96. */
  97. public Mapper createMapper() throws BuildException {
  98. if (mapperElement != null) {
  99. throw new BuildException("Cannot define more than one mapper",
  100. getLocation());
  101. }
  102. mapperElement = new Mapper(getProject());
  103. return mapperElement;
  104. }
  105. /**
  106. * Evaluate (all) target and source file(s) to
  107. * see if the target(s) is/are up-to-date.
  108. */
  109. public boolean eval() {
  110. if (sourceFileSets.size() == 0 && _sourceFile == null) {
  111. throw new BuildException("At least one srcfile or a nested "
  112. + "<srcfiles> element must be set.");
  113. }
  114. if (sourceFileSets.size() > 0 && _sourceFile != null) {
  115. throw new BuildException("Cannot specify both the srcfile "
  116. + "attribute and a nested <srcfiles> "
  117. + "element.");
  118. }
  119. if (_targetFile == null && mapperElement == null) {
  120. throw new BuildException("The targetfile attribute or a nested "
  121. + "mapper element must be set.");
  122. }
  123. // if the target file is not there, then it can't be up-to-date
  124. if (_targetFile != null && !_targetFile.exists()) {
  125. log("The targetfile \"" + _targetFile.getAbsolutePath()
  126. + "\" does not exist.", Project.MSG_VERBOSE);
  127. return false;
  128. }
  129. // if the source file isn't there, throw an exception
  130. if (_sourceFile != null && !_sourceFile.exists()) {
  131. throw new BuildException(_sourceFile.getAbsolutePath()
  132. + " not found.");
  133. }
  134. Enumeration e = sourceFileSets.elements();
  135. boolean upToDate = true;
  136. while (upToDate && e.hasMoreElements()) {
  137. FileSet fs = (FileSet) e.nextElement();
  138. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  139. upToDate = upToDate && scanDir(fs.getDir(getProject()),
  140. ds.getIncludedFiles());
  141. }
  142. if (_sourceFile != null) {
  143. if (mapperElement == null) {
  144. upToDate = upToDate
  145. && (_targetFile.lastModified() >= _sourceFile.lastModified());
  146. } else {
  147. SourceFileScanner sfs = new SourceFileScanner(this);
  148. upToDate = upToDate
  149. && (sfs.restrict(new String[] {_sourceFile.getAbsolutePath()},
  150. null, null,
  151. mapperElement.getImplementation()).length == 0);
  152. }
  153. }
  154. return upToDate;
  155. }
  156. /**
  157. * Sets property to true if target file(s) have a more recent timestamp
  158. * than (each of) the corresponding source file(s).
  159. */
  160. public void execute() throws BuildException {
  161. if (_property == null) {
  162. throw new BuildException("property attribute is required.",
  163. getLocation());
  164. }
  165. boolean upToDate = eval();
  166. if (upToDate) {
  167. this.getProject().setNewProperty(_property, getValue());
  168. if (mapperElement == null) {
  169. log("File \"" + _targetFile.getAbsolutePath()
  170. + "\" is up-to-date.", Project.MSG_VERBOSE);
  171. } else {
  172. log("All target files are up-to-date.",
  173. Project.MSG_VERBOSE);
  174. }
  175. }
  176. }
  177. protected boolean scanDir(File srcDir, String[] files) {
  178. SourceFileScanner sfs = new SourceFileScanner(this);
  179. FileNameMapper mapper = null;
  180. File dir = srcDir;
  181. if (mapperElement == null) {
  182. MergingMapper mm = new MergingMapper();
  183. mm.setTo(_targetFile.getAbsolutePath());
  184. mapper = mm;
  185. dir = null;
  186. } else {
  187. mapper = mapperElement.getImplementation();
  188. }
  189. return sfs.restrict(files, srcDir, dir, mapper).length == 0;
  190. }
  191. }