KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ide > VAJWorkspaceScanner


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.taskdefs.optional.ide;
18
19 import com.ibm.ivj.util.base.IvjException;
20 import com.ibm.ivj.util.base.Package;
21 import com.ibm.ivj.util.base.Project;
22 import java.io.File JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.tools.ant.DirectoryScanner;
27
28 /**
29  * Class for scanning a Visual Age for Java workspace for packages matching
30  * a certain criteria.
31  * <p>
32  * These criteria consist of a set of include and exclude patterns. With these
33  * patterns, you can select which packages you want to have included, and which
34  * packages you want to have excluded. You can add patterns to be excluded by
35  * default with the addDefaultExcludes method. The patters that are excluded
36  * by default include
37  * <ul>
38  * <li>IBM*\**</li>
39  * <li>Java class libraries\**</li>
40  * <li>Sun class libraries*\**</li>
41  * <li>JSP Page Compile Generated Code\**</li>
42  * <li>VisualAge*\**</li>
43  * </ul>
44  * <p>
45  * This class works like DirectoryScanner.
46  *
47  * @see org.apache.tools.ant.DirectoryScanner
48  *
49  */

50 class VAJWorkspaceScanner extends DirectoryScanner {
51
52     // Patterns that should be excluded by default.
53
private static final String JavaDoc[] DEFAULTEXCLUDES = {
54         "IBM*/**",
55         "Java class libraries/**",
56         "Sun class libraries*/**",
57         "JSP Page Compile Generated Code/**",
58         "VisualAge*/**",
59     };
60
61     // The packages that where found and matched at least
62
// one includes, and matched no excludes.
63
private Vector JavaDoc packagesIncluded = new Vector JavaDoc();
64
65     /**
66      * Adds the array with default exclusions to the current exclusions set.
67      */

68     public void addDefaultExcludes() {
69         int excludesLength = excludes == null ? 0 : excludes.length;
70         String JavaDoc[] newExcludes;
71         newExcludes = new String JavaDoc[excludesLength + DEFAULTEXCLUDES.length];
72         if (excludesLength > 0) {
73             System.arraycopy(excludes, 0, newExcludes, 0, excludesLength);
74         }
75         for (int i = 0; i < DEFAULTEXCLUDES.length; i++) {
76             newExcludes[i + excludesLength] = DEFAULTEXCLUDES[i].
77                 replace('/', File.separatorChar).
78                 replace('\\', File.separatorChar);
79         }
80         excludes = newExcludes;
81     }
82
83     /**
84      * Finds all Projects specified in include patterns.
85      *
86      * @return the projects
87      */

88     public Vector JavaDoc findMatchingProjects() {
89         Project[] projects = VAJLocalUtil.getWorkspace().getProjects();
90
91         Vector JavaDoc matchingProjects = new Vector JavaDoc();
92
93         boolean allProjectsMatch = false;
94         for (int i = 0; i < projects.length; i++) {
95             Project project = projects[i];
96             for (int j = 0; j < includes.length && !allProjectsMatch; j++) {
97                 StringTokenizer JavaDoc tok =
98                     new StringTokenizer JavaDoc(includes[j], File.separator);
99                 String JavaDoc projectNamePattern = tok.nextToken();
100                 if (projectNamePattern.equals("**")) {
101                     // if an include pattern starts with '**',
102
// all projects match
103
allProjectsMatch = true;
104                 } else
105                     if (match(projectNamePattern, project.getName())) {
106                         matchingProjects.addElement(project);
107                         break;
108                     }
109             }
110         }
111
112         if (allProjectsMatch) {
113             matchingProjects = new Vector JavaDoc();
114             for (int i = 0; i < projects.length; i++) {
115                 matchingProjects.addElement(projects[i]);
116             }
117         }
118
119         return matchingProjects;
120     }
121
122     /**
123      * Get the names of the packages that matched at least one of the include
124      * patterns, and didn't match one of the exclude patterns.
125      *
126      * @return the matching packages
127      */

128     public Package JavaDoc[] getIncludedPackages() {
129         int count = packagesIncluded.size();
130         Package JavaDoc[] packages = new Package JavaDoc[count];
131         for (int i = 0; i < count; i++) {
132             packages[i] = (Package JavaDoc) packagesIncluded.elementAt(i);
133         }
134         return packages;
135     }
136
137     /**
138      * Scans the workspace for packages that match at least one include
139      * pattern, and don't match any exclude patterns.
140      *
141      */

142     public void scan() {
143         if (includes == null) {
144             // No includes supplied, so set it to 'matches all'
145
includes = new String JavaDoc[1];
146             includes[0] = "**";
147         }
148         if (excludes == null) {
149             excludes = new String JavaDoc[0];
150         }
151
152         // only scan projects which are included in at least one include pattern
153
Vector JavaDoc matchingProjects = findMatchingProjects();
154         for (Enumeration JavaDoc e = matchingProjects.elements(); e.hasMoreElements();) {
155             Project project = (Project) e.nextElement();
156             scanProject(project);
157         }
158     }
159
160     /**
161      * Scans a project for packages that match at least one include
162      * pattern, and don't match any exclude patterns.
163      *
164      */

165     public void scanProject(Project project) {
166         try {
167             Package JavaDoc[] packages = project.getPackages();
168             if (packages != null) {
169                 for (int i = 0; i < packages.length; i++) {
170                     Package JavaDoc item = packages[i];
171                     // replace '.' by file seperator because the patterns are
172
// using file seperator syntax (and we can use the match
173
// methods this way).
174
String JavaDoc name =
175                         project.getName()
176                         + File.separator
177                         + item.getName().replace('.', File.separatorChar);
178                     if (isIncluded(name) && !isExcluded(name)) {
179                         packagesIncluded.addElement(item);
180                     }
181                 }
182             }
183         } catch (IvjException e) {
184             throw VAJLocalUtil.createBuildException("VA Exception occurred: ", e);
185         }
186     }
187 }
188
Popular Tags