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. import com.ibm.ivj.util.base.IvjException;
  19. import com.ibm.ivj.util.base.Package;
  20. import com.ibm.ivj.util.base.Project;
  21. import java.io.File;
  22. import java.util.Enumeration;
  23. import java.util.StringTokenizer;
  24. import java.util.Vector;
  25. import org.apache.tools.ant.DirectoryScanner;
  26. /**
  27. * Class for scanning a Visual Age for Java workspace for packages matching
  28. * a certain criteria.
  29. * <p>
  30. * These criteria consist of a set of include and exclude patterns. With these
  31. * patterns, you can select which packages you want to have included, and which
  32. * packages you want to have excluded. You can add patterns to be excluded by
  33. * default with the addDefaultExcludes method. The patters that are excluded
  34. * by default include
  35. * <ul>
  36. * <li>IBM*\**</li>
  37. * <li>Java class libraries\**</li>
  38. * <li>Sun class libraries*\**</li>
  39. * <li>JSP Page Compile Generated Code\**</li>
  40. * <li>VisualAge*\**</li>
  41. * </ul>
  42. * <p>
  43. * This class works like DirectoryScanner.
  44. *
  45. * @see org.apache.tools.ant.DirectoryScanner
  46. *
  47. */
  48. class VAJWorkspaceScanner extends DirectoryScanner {
  49. // Patterns that should be excluded by default.
  50. private static final String[] DEFAULTEXCLUDES = {
  51. "IBM*/**",
  52. "Java class libraries/**",
  53. "Sun class libraries*/**",
  54. "JSP Page Compile Generated Code/**",
  55. "VisualAge*/**",
  56. };
  57. // The packages that where found and matched at least
  58. // one includes, and matched no excludes.
  59. private Vector packagesIncluded = new Vector();
  60. /**
  61. * Adds the array with default exclusions to the current exclusions set.
  62. */
  63. public void addDefaultExcludes() {
  64. int excludesLength = excludes == null ? 0 : excludes.length;
  65. String[] newExcludes;
  66. newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length];
  67. if (excludesLength > 0) {
  68. System.arraycopy(excludes, 0, newExcludes, 0, excludesLength);
  69. }
  70. for (int i = 0; i < DEFAULTEXCLUDES.length; i++) {
  71. newExcludes[i + excludesLength] = DEFAULTEXCLUDES[i].
  72. replace('/', File.separatorChar).
  73. replace('\\', File.separatorChar);
  74. }
  75. excludes = newExcludes;
  76. }
  77. /**
  78. * Finds all Projects specified in include patterns.
  79. *
  80. * @return the projects
  81. */
  82. public Vector findMatchingProjects() {
  83. Project[] projects = VAJLocalUtil.getWorkspace().getProjects();
  84. Vector matchingProjects = new Vector();
  85. boolean allProjectsMatch = false;
  86. for (int i = 0; i < projects.length; i++) {
  87. Project project = projects[i];
  88. for (int j = 0; j < includes.length && !allProjectsMatch; j++) {
  89. StringTokenizer tok =
  90. new StringTokenizer(includes[j], File.separator);
  91. String projectNamePattern = tok.nextToken();
  92. if (projectNamePattern.equals("**")) {
  93. // if an include pattern starts with '**',
  94. // all projects match
  95. allProjectsMatch = true;
  96. } else
  97. if (match(projectNamePattern, project.getName())) {
  98. matchingProjects.addElement(project);
  99. break;
  100. }
  101. }
  102. }
  103. if (allProjectsMatch) {
  104. matchingProjects = new Vector();
  105. for (int i = 0; i < projects.length; i++) {
  106. matchingProjects.addElement(projects[i]);
  107. }
  108. }
  109. return matchingProjects;
  110. }
  111. /**
  112. * Get the names of the packages that matched at least one of the include
  113. * patterns, and didn't match one of the exclude patterns.
  114. *
  115. * @return the matching packages
  116. */
  117. public Package[] getIncludedPackages() {
  118. int count = packagesIncluded.size();
  119. Package[] packages = new Package[count];
  120. for (int i = 0; i < count; i++) {
  121. packages[i] = (Package) packagesIncluded.elementAt(i);
  122. }
  123. return packages;
  124. }
  125. /**
  126. * Scans the workspace for packages that match at least one include
  127. * pattern, and don't match any exclude patterns.
  128. *
  129. */
  130. public void scan() {
  131. if (includes == null) {
  132. // No includes supplied, so set it to 'matches all'
  133. includes = new String[1];
  134. includes[0] = "**";
  135. }
  136. if (excludes == null) {
  137. excludes = new String[0];
  138. }
  139. // only scan projects which are included in at least one include pattern
  140. Vector matchingProjects = findMatchingProjects();
  141. for (Enumeration e = matchingProjects.elements(); e.hasMoreElements();) {
  142. Project project = (Project) e.nextElement();
  143. scanProject(project);
  144. }
  145. }
  146. /**
  147. * Scans a project for packages that match at least one include
  148. * pattern, and don't match any exclude patterns.
  149. *
  150. */
  151. public void scanProject(Project project) {
  152. try {
  153. Package[] packages = project.getPackages();
  154. if (packages != null) {
  155. for (int i = 0; i < packages.length; i++) {
  156. Package item = packages[i];
  157. // replace '.' by file seperator because the patterns are
  158. // using file seperator syntax (and we can use the match
  159. // methods this way).
  160. String name =
  161. project.getName()
  162. + File.separator
  163. + item.getName().replace('.', File.separatorChar);
  164. if (isIncluded(name) && !isExcluded(name)) {
  165. packagesIncluded.addElement(item);
  166. }
  167. }
  168. }
  169. } catch (IvjException e) {
  170. throw VAJLocalUtil.createBuildException("VA Exception occurred: ", e);
  171. }
  172. }
  173. }