3 * Copyright (C) 1999 Len Trigg (trigg@cs.waikato.ac.nz)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 import java.io.FilenameFilter;
24 import java.util.Enumeration;
25 import java.util.StringTokenizer;
26 import java.util.Vector;
27 import java.util.zip.ZipFile;
28 import java.util.zip.ZipEntry;
31 * Converts an unqualified class name to import statements by scanning
32 * through the classpath.
34 * @author Len Trigg (trigg@cs.waikato.ac.nz)
35 * @version 1.0 - 6 May 1999
37 public class ImportWizard {
39 /** Stores the list of all classes in the classpath */
40 public static Vector CLASS_LIST = new Vector(500);
42 /** Build the list of classes */
45 // System.err.println("Making class list");
48 // System.err.println("Done (" + CLASS_LIST.size() + " classes)");
52 public static void buildClassList() {
53 String classPath = System.getProperty("java.class.path");
54 // System.out.println("classpath = " + classPath);
56 String classPathSeparator = File.pathSeparator;
59 String classPath2 = System.getProperty("sun.boot.class.path");
60 if (classPath2 != null)
61 classPath += classPathSeparator + classPath2;
63 StringTokenizer st = new StringTokenizer(classPath, classPathSeparator);
64 while (st.hasMoreTokens()) {
65 String classPathEntry = st.nextToken();
66 File classPathFile = new File(classPathEntry);
67 if (classPathFile.exists()) {
68 if (classPathEntry.toLowerCase().endsWith(".jar")) {
69 addClassesFromZip(CLASS_LIST, classPathFile);
70 } else if (classPathEntry.toLowerCase().endsWith(".zip")) {
71 addClassesFromZip(CLASS_LIST, classPathFile);
72 } else if (classPathFile.isDirectory()) {
73 addClassesFromDir(CLASS_LIST, classPathFile, classPathFile);
84 * Adds the classes from the supplied Zip file to the class list.
86 * @param classList the Vector to add the classes to
87 * @param classPathFile the File to scan as a zip file
89 public static void addClassesFromZip(Vector classList,
91 // System.out.println("Processing jar/zip file: " + classPathFile);
94 ZipFile zipFile = new ZipFile(classPathFile);
95 Enumeration enum = zipFile.entries();
96 while (enum.hasMoreElements()) {
97 ZipEntry zipEntry = (ZipEntry)enum.nextElement();
98 String current = zipEntry.getName();
99 if (current.toLowerCase().endsWith(".class")) {
100 current = current.substring(0, current.length() - 6);
101 current = current.replace('/', '.');
102 current = current.replace('\\', '.');
103 classList.addElement(current);
106 } catch (Exception ex) {
107 System.err.println("Problem opening " + classPathFile + " with zip.");
113 * Adds the classes from the supplied directory to the class list.
115 * @param classList the Vector to add the classes to
116 * @param classPathFile the File to recursively scan as a directory
118 public static void addClassesFromDir(Vector classList,
122 String [] files = currentDir.list();
123 for (int i = 0; i < files.length; i++) {
124 String current = files[i];
125 if (current.toLowerCase().endsWith(".class")) {
126 current = current.substring(0, current.length() - 6);
127 String rootPath = rootDir.getPath();
128 String currentPath = currentDir.getPath();
129 if (currentPath.indexOf(rootPath) != 0) {
130 System.err.println("currentPath doesn't start with rootPath!\n"
131 + "rootPath: " + rootPath + "\n"
132 + "currentPath: " + currentPath + "\n");
134 String packageName = currentPath.substring(rootPath.length());
135 if (packageName.length() > 0) {
136 // Not the current directory
137 packageName = packageName.replace('\\', '.');
138 packageName = packageName.replace('/', '.');
139 classList.addElement(packageName.substring(1) + '.' + current);
141 // The current directory
142 classList.addElement(current);
146 // Check if it's a directory to recurse into
147 File currentFile = new File(currentDir, current);
148 if (currentFile.isDirectory()) {
149 addClassesFromDir(classList, rootDir, currentFile);
157 * Looks up an unqualified class name in the class path to find possible
158 * fully qualified matches.
160 * @param className a value of type 'String'
162 public static void makeImportStatement(String className) {
164 String importList = "(list";
166 for (int i = 0; i < CLASS_LIST.size(); i++) {
167 String testName = (String) CLASS_LIST.elementAt(i);
169 if ((testName.length() > className.length() && testName.endsWith(className) &&
170 testName.charAt(testName.length() - className.length() - 1) == '.') ||
171 (testName.length() == className.length()) && testName.equals(className)) {
174 testName = " \"" + testName + "\"";
175 if (importList.indexOf(testName) == -1)
176 importList += testName;
183 System.out.println(importList);
190 * Tests the ImportWizard from the command line
192 * @param args an array of strings containing class names to look up
194 public static void main(String[] args) {
196 if (args.length == 0) {
197 System.out.println("Give class names as arguments to look up");
199 for (int i = 0; i < args.length; i++) {
200 System.out.println("=== " + args[i] + " ===");
201 makeImportStatement(args[i]);
208 * $Log: ImportWizard.java,v $
209 * Revision 1.4 1999/06/17 17:49:27 paulk
210 * Added change log to end of file.
214 // End of ImportWizard.java