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 9.4 kB

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