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

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