KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > optional > depend > ClassfileSet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types.optional.depend;
19
20 import java.util.Vector JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.DirectoryScanner;
24 import org.apache.tools.ant.types.FileSet;
25
26 /**
27  * A ClassfileSet is a FileSet that enlists all classes that depend on a
28  * certain set of root classes.
29  *
30  * ClassfileSet extends FileSet, its inherited properties
31  * defining the domain searched for dependent classes.
32  *
33  */

34 public class ClassfileSet extends FileSet {
35     /**
36      * The list of root classes for this class file set. These are the
37      * classes which must be included in the fileset and which are the
38      * starting point for the dependency search.
39      */

40     private Vector JavaDoc rootClasses = new Vector JavaDoc();
41
42     /**
43      * The list of filesets which contain root classes.
44      */

45     private Vector JavaDoc rootFileSets = new Vector JavaDoc();
46
47     /**
48      * Inner class used to contain info about root classes.
49      */

50     public static class ClassRoot {
51         /** The name of the root class */
52         private String JavaDoc rootClass;
53
54         /**
55          * Set the root class name.
56          *
57          * @param name the name of the root class.
58          */

59         public void setClassname(String JavaDoc name) {
60             this.rootClass = name;
61         }
62
63         /**
64          * Get the name of the root class.
65          *
66          * @return the name of the root class.
67          */

68         public String JavaDoc getClassname() {
69             return rootClass;
70         }
71     }
72
73     /**
74      * Default constructor.
75      */

76     public ClassfileSet() {
77     }
78
79     /**
80      * Add a fileset to which contains a collection of root classes used to
81      * drive the search from classes.
82      *
83      * @param rootFileSet a root file set to be used to search for dependent
84      * classes.
85      */

86     public void addRootFileset(FileSet rootFileSet) {
87         rootFileSets.addElement(rootFileSet);
88     }
89
90     /**
91      * Create a ClassfileSet from another ClassfileSet.
92      *
93      * @param s the other classfileset.
94      */

95     protected ClassfileSet(ClassfileSet s) {
96         super(s);
97         rootClasses = (Vector JavaDoc) s.rootClasses.clone();
98     }
99
100     /**
101      * Set the root class attribute.
102      *
103      * @param rootClass the name of the root class.
104      */

105     public void setRootClass(String JavaDoc rootClass) {
106         rootClasses.addElement(rootClass);
107     }
108
109     /**
110      * Return the DirectoryScanner associated with this FileSet.
111      *
112      * @param p the project used to resolve dirs, etc.
113      *
114      * @return a dependency scanner.
115      */

116     public DirectoryScanner getDirectoryScanner(Project p) {
117         if (isReference()) {
118             return getRef(p).getDirectoryScanner(p);
119         }
120         Vector JavaDoc allRootClasses = (Vector JavaDoc) rootClasses.clone();
121         for (Enumeration JavaDoc e = rootFileSets.elements(); e.hasMoreElements();) {
122             FileSet additionalRootSet = (FileSet) e.nextElement();
123             DirectoryScanner additionalScanner
124                 = additionalRootSet.getDirectoryScanner(p);
125             String JavaDoc[] files = additionalScanner.getIncludedFiles();
126             for (int i = 0; i < files.length; ++i) {
127                 if (files[i].endsWith(".class")) {
128                     String JavaDoc classFilePath
129                         = files[i].substring(0, files[i].length() - 6);
130                     String JavaDoc className
131                         = classFilePath.replace('/', '.').replace('\\', '.');
132                     allRootClasses.addElement(className);
133                 }
134             }
135         }
136         DirectoryScanner parentScanner = super.getDirectoryScanner(p);
137         DependScanner scanner = new DependScanner(parentScanner);
138         scanner.setBasedir(getDir(p));
139         scanner.setRootClasses(allRootClasses);
140         scanner.scan();
141         return scanner;
142     }
143
144     /**
145      * Add a nested root class definition to this class file set.
146      *
147      * @param root the configured class root.
148      */

149     public void addConfiguredRoot(ClassRoot root) {
150         rootClasses.addElement(root.getClassname());
151     }
152
153     /**
154      * Clone this data type.
155      *
156      * @return a clone of the class file set.
157      */

158     public Object JavaDoc clone() {
159         return new ClassfileSet(isReference()
160             ? (ClassfileSet) (getRef(getProject())) : this);
161     }
162
163 }
164
Popular Tags