You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpToDate.java 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. * A nested filenamemapper
  107. * @param fileNameMapper the mapper to add
  108. * @since Ant 1.6.3
  109. */
  110. public void add(FileNameMapper fileNameMapper) {
  111. createMapper().add(fileNameMapper);
  112. }
  113. /**
  114. * Evaluate (all) target and source file(s) to
  115. * see if the target(s) is/are up-to-date.
  116. */
  117. public boolean eval() {
  118. if (sourceFileSets.size() == 0 && _sourceFile == null) {
  119. throw new BuildException("At least one srcfile or a nested "
  120. + "<srcfiles> element must be set.");
  121. }
  122. if (sourceFileSets.size() > 0 && _sourceFile != null) {
  123. throw new BuildException("Cannot specify both the srcfile "
  124. + "attribute and a nested <srcfiles> "
  125. + "element.");
  126. }
  127. if (_targetFile == null && mapperElement == null) {
  128. throw new BuildException("The targetfile attribute or a nested "
  129. + "mapper element must be set.");
  130. }
  131. // if the target file is not there, then it can't be up-to-date
  132. if (_targetFile != null && !_targetFile.exists()) {
  133. log("The targetfile \"" + _targetFile.getAbsolutePath()
  134. + "\" does not exist.", Project.MSG_VERBOSE);
  135. return false;
  136. }
  137. // if the source file isn't there, throw an exception
  138. if (_sourceFile != null && !_sourceFile.exists()) {
  139. throw new BuildException(_sourceFile.getAbsolutePath()
  140. + " not found.");
  141. }
  142. Enumeration e = sourceFileSets.elements();
  143. boolean upToDate = true;
  144. while (upToDate && e.hasMoreElements()) {
  145. FileSet fs = (FileSet) e.nextElement();
  146. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  147. upToDate = upToDate && scanDir(fs.getDir(getProject()),
  148. ds.getIncludedFiles());
  149. }
  150. if (_sourceFile != null) {
  151. if (mapperElement == null) {
  152. upToDate = upToDate
  153. && (_targetFile.lastModified() >= _sourceFile.lastModified());
  154. } else {
  155. SourceFileScanner sfs = new SourceFileScanner(this);
  156. upToDate = upToDate
  157. && (sfs.restrict(new String[] {_sourceFile.getAbsolutePath()},
  158. null, null,
  159. mapperElement.getImplementation()).length == 0);
  160. }
  161. }
  162. return upToDate;
  163. }
  164. /**
  165. * Sets property to true if target file(s) have a more recent timestamp
  166. * than (each of) the corresponding source file(s).
  167. */
  168. public void execute() throws BuildException {
  169. if (_property == null) {
  170. throw new BuildException("property attribute is required.",
  171. getLocation());
  172. }
  173. boolean upToDate = eval();
  174. if (upToDate) {
  175. this.getProject().setNewProperty(_property, getValue());
  176. if (mapperElement == null) {
  177. log("File \"" + _targetFile.getAbsolutePath()
  178. + "\" is up-to-date.", Project.MSG_VERBOSE);
  179. } else {
  180. log("All target files are up-to-date.",
  181. Project.MSG_VERBOSE);
  182. }
  183. }
  184. }
  185. protected boolean scanDir(File srcDir, String[] files) {
  186. SourceFileScanner sfs = new SourceFileScanner(this);
  187. FileNameMapper mapper = null;
  188. File dir = srcDir;
  189. if (mapperElement == null) {
  190. MergingMapper mm = new MergingMapper();
  191. mm.setTo(_targetFile.getAbsolutePath());
  192. mapper = mm;
  193. dir = null;
  194. } else {
  195. mapper = mapperElement.getImplementation();
  196. }
  197. return sfs.restrict(files, srcDir, dir, mapper).length == 0;
  198. }
  199. }