1. /*
  2. * Copyright 2001-2002,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.types.optional.depend;
  18. import java.io.File;
  19. import java.util.Enumeration;
  20. import java.util.Hashtable;
  21. import java.util.Vector;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.DirectoryScanner;
  24. import org.apache.tools.ant.types.Path;
  25. import org.apache.tools.ant.util.depend.DependencyAnalyzer;
  26. /**
  27. * An interface used to describe the actions required by any type of
  28. * directory scanner.
  29. *
  30. */
  31. public class DependScanner extends DirectoryScanner {
  32. /**
  33. * The name of the analyzer to use by default.
  34. */
  35. public static final String DEFAULT_ANALYZER_CLASS
  36. = "org.apache.tools.ant.util.depend.bcel.FullAnalyzer";
  37. /**
  38. * The base directory for the scan
  39. */
  40. private File basedir;
  41. /**
  42. * The root classes to drive the search for dependent classes
  43. */
  44. private Vector rootClasses;
  45. /**
  46. * The names of the classes to include in the fileset
  47. */
  48. private Vector included;
  49. /**
  50. * The parent scanner which gives the basic set of files. Only files which
  51. * are in this set and which can be reached from a root class will end
  52. * up being included in the result set
  53. */
  54. private DirectoryScanner parentScanner;
  55. /**
  56. * Create a DependScanner, using the given scanner to provide the basic
  57. * set of files from which class files come.
  58. *
  59. * @param parentScanner the DirectoryScanner which returns the files from
  60. * which class files must come.
  61. */
  62. public DependScanner(DirectoryScanner parentScanner) {
  63. this.parentScanner = parentScanner;
  64. }
  65. /**
  66. * Sets the basedir for scanning. This is the directory that is scanned
  67. * recursively.
  68. *
  69. * @param basedir the basedir for scanning
  70. */
  71. public void setBasedir(File basedir) {
  72. this.basedir = basedir;
  73. }
  74. /**
  75. * Gets the basedir that is used for scanning.
  76. *
  77. * @return the basedir that is used for scanning
  78. */
  79. public File getBasedir() { return basedir; }
  80. /**
  81. * Sets the root classes to be used to drive the scan.
  82. *
  83. * @param rootClasses the rootClasses to be used for this scan
  84. */
  85. public void setRootClasses(Vector rootClasses) {
  86. this.rootClasses = rootClasses;
  87. }
  88. /**
  89. * Get the names of the class files, baseClass depends on
  90. *
  91. * @return the names of the files
  92. */
  93. public String[] getIncludedFiles() {
  94. int count = included.size();
  95. String[] files = new String[count];
  96. for (int i = 0; i < count; i++) {
  97. files[i] = (String) included.elementAt(i);
  98. }
  99. return files;
  100. }
  101. /**
  102. * Scans the base directory for files that baseClass depends on
  103. *
  104. * @exception IllegalStateException when basedir was set incorrecly
  105. */
  106. public void scan() throws IllegalStateException {
  107. included = new Vector();
  108. String analyzerClassName = DEFAULT_ANALYZER_CLASS;
  109. DependencyAnalyzer analyzer = null;
  110. try {
  111. Class analyzerClass = Class.forName(analyzerClassName);
  112. analyzer = (DependencyAnalyzer) analyzerClass.newInstance();
  113. } catch (Exception e) {
  114. throw new BuildException("Unable to load dependency analyzer: "
  115. + analyzerClassName, e);
  116. }
  117. analyzer.addClassPath(new Path(null, basedir.getPath()));
  118. for (Enumeration e = rootClasses.elements(); e.hasMoreElements();) {
  119. String rootClass = (String) e.nextElement();
  120. analyzer.addRootClass(rootClass);
  121. }
  122. Enumeration e = analyzer.getClassDependencies();
  123. String[] parentFiles = parentScanner.getIncludedFiles();
  124. Hashtable parentSet = new Hashtable();
  125. for (int i = 0; i < parentFiles.length; ++i) {
  126. parentSet.put(parentFiles[i], parentFiles[i]);
  127. }
  128. while (e.hasMoreElements()) {
  129. String classname = (String) e.nextElement();
  130. String filename = classname.replace('.', File.separatorChar);
  131. filename = filename + ".class";
  132. File depFile = new File(basedir, filename);
  133. if (depFile.exists() && parentSet.containsKey(filename)) {
  134. // This is included
  135. included.addElement(filename);
  136. }
  137. }
  138. }
  139. /**
  140. * @see DirectoryScanner#addDefaultExcludes
  141. */
  142. public void addDefaultExcludes() {
  143. }
  144. /**
  145. * @see DirectoryScanner#getExcludedDirectories
  146. */
  147. public String[] getExcludedDirectories() {
  148. return null;
  149. }
  150. /**
  151. * @see DirectoryScanner#getExcludedFiles
  152. */
  153. public String[] getExcludedFiles() {
  154. return null;
  155. }
  156. /**
  157. * @see DirectoryScanner#getIncludedDirectories
  158. */
  159. public String[] getIncludedDirectories() {
  160. return new String[0];
  161. }
  162. /**
  163. * @see DirectoryScanner#getNotIncludedDirectories
  164. */
  165. public String[] getNotIncludedDirectories() {
  166. return null;
  167. }
  168. /**
  169. * @see DirectoryScanner#getNotIncludedFiles
  170. */
  171. public String[] getNotIncludedFiles() {
  172. return null;
  173. }
  174. /**
  175. * @see DirectoryScanner#setExcludes
  176. */
  177. public void setExcludes(String[] excludes) {
  178. }
  179. /**
  180. * @see DirectoryScanner#setIncludes
  181. */
  182. public void setIncludes(String[] includes) {
  183. }
  184. /**
  185. * @see DirectoryScanner#setCaseSensitive
  186. */
  187. public void setCaseSensitive(boolean isCaseSensitive) {
  188. }
  189. }