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.util;
  18. import java.io.File;
  19. import java.util.Vector;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.types.ResourceFactory;
  22. import org.apache.tools.ant.types.Resource;
  23. /**
  24. * Utility class that collects the functionality of the various
  25. * scanDir methods that have been scattered in several tasks before.
  26. *
  27. * <p>The only method returns an array of source files. The array is a
  28. * subset of the files given as a parameter and holds only those that
  29. * are newer than their corresponding target files.</p>
  30. *
  31. */
  32. public class SourceFileScanner implements ResourceFactory {
  33. protected Task task;
  34. private FileUtils fileUtils;
  35. private File destDir; // base directory of the fileset
  36. /**
  37. * @param task The task we should log messages through
  38. */
  39. public SourceFileScanner(Task task) {
  40. this.task = task;
  41. fileUtils = FileUtils.newFileUtils();
  42. }
  43. /**
  44. * Restrict the given set of files to those that are newer than
  45. * their corresponding target files.
  46. *
  47. * @param files the original set of files
  48. * @param srcDir all files are relative to this directory
  49. * @param destDir target files live here. if null file names
  50. * returned by the mapper are assumed to be absolute.
  51. * @param mapper knows how to construct a target file names from
  52. * source file names.
  53. */
  54. public String[] restrict(String[] files, File srcDir, File destDir,
  55. FileNameMapper mapper) {
  56. return restrict(files, srcDir, destDir, mapper,
  57. fileUtils.getFileTimestampGranularity());
  58. }
  59. /**
  60. * Restrict the given set of files to those that are newer than
  61. * their corresponding target files.
  62. *
  63. * @param files the original set of files
  64. * @param srcDir all files are relative to this directory
  65. * @param destDir target files live here. if null file names
  66. * returned by the mapper are assumed to be absolute.
  67. * @param mapper knows how to construct a target file names from
  68. * source file names.
  69. * @param granularity The number of milliseconds leeway to give
  70. * before deciding a target is out of date.
  71. *
  72. * @since Ant 1.6.2
  73. */
  74. public String[] restrict(String[] files, File srcDir, File destDir,
  75. FileNameMapper mapper, long granularity) {
  76. // record destdir for later use in getResource
  77. this.destDir = destDir;
  78. Vector v = new Vector();
  79. for (int i = 0; i < files.length; i++) {
  80. File src = fileUtils.resolveFile(srcDir, files[i]);
  81. v.addElement(new Resource(files[i], src.exists(),
  82. src.lastModified(), src.isDirectory()));
  83. }
  84. Resource[] sourceresources = new Resource[v.size()];
  85. v.copyInto(sourceresources);
  86. // build the list of sources which are out of date with
  87. // respect to the target
  88. Resource[] outofdate =
  89. ResourceUtils.selectOutOfDateSources(task, sourceresources,
  90. mapper, this, granularity);
  91. String[] result = new String[outofdate.length];
  92. for (int counter = 0; counter < outofdate.length; counter++) {
  93. result[counter] = outofdate[counter].getName();
  94. }
  95. return result;
  96. }
  97. /**
  98. * Convinience layer on top of restrict that returns the source
  99. * files as File objects (containing absolute paths if srcDir is
  100. * absolute).
  101. */
  102. public File[] restrictAsFiles(String[] files, File srcDir, File destDir,
  103. FileNameMapper mapper) {
  104. return restrictAsFiles(files, srcDir, destDir, mapper,
  105. fileUtils.getFileTimestampGranularity());
  106. }
  107. /**
  108. * Convinience layer on top of restrict that returns the source
  109. * files as File objects (containing absolute paths if srcDir is
  110. * absolute).
  111. *
  112. * @since Ant 1.6.2
  113. */
  114. public File[] restrictAsFiles(String[] files, File srcDir, File destDir,
  115. FileNameMapper mapper, long granularity) {
  116. String[] res = restrict(files, srcDir, destDir, mapper, granularity);
  117. File[] result = new File[res.length];
  118. for (int i = 0; i < res.length; i++) {
  119. result[i] = new File(srcDir, res[i]);
  120. }
  121. return result;
  122. }
  123. /**
  124. * returns resource information for a file at destination
  125. * @param name relative path of file at destination
  126. * @return data concerning a file whose relative path to destDir is name
  127. *
  128. * @since Ant 1.5.2
  129. */
  130. public Resource getResource(String name) {
  131. File src = fileUtils.resolveFile(destDir, name);
  132. return new Resource(name, src.exists(), src.lastModified(),
  133. src.isDirectory());
  134. }
  135. }