Initial Commit
[packages] / xemacs-packages / jde / java / src / jde / wizards / ImportWizard.java
1 /*
2  *    ImportWizard.java
3  *    Copyright (C) 1999 Len Trigg (trigg@cs.waikato.ac.nz)
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package jde.wizards;
21
22 import java.io.File;
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;
29
30 /**
31  * Converts an unqualified class name to import statements by scanning
32  * through the classpath.
33  *
34  * @author Len Trigg (trigg@cs.waikato.ac.nz)
35  * @version 1.0 - 6 May 1999
36  */
37 public class ImportWizard {
38
39   /** Stores the list of all classes in the classpath */
40   public static Vector CLASS_LIST = new Vector(500);
41   
42   /** Build the list of classes */
43   static {
44
45     // System.err.println("Making class list");
46     buildClassList();
47
48     //    System.err.println("Done (" + CLASS_LIST.size() + " classes)");
49
50   }
51
52   public static void buildClassList() {
53     String classPath = System.getProperty("java.class.path");
54     // System.out.println("classpath = " + classPath);
55     
56     String classPathSeparator = File.pathSeparator;
57
58     // For Java 2!
59     String classPath2 = System.getProperty("sun.boot.class.path");
60     if (classPath2 != null)
61       classPath += classPathSeparator + classPath2;
62
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);
74         }
75       }
76     }
77
78     
79   }
80   
81     
82   
83   /**
84    * Adds the classes from the supplied Zip file to the class list.
85    *
86    * @param classList the Vector to add the classes to
87    * @param classPathFile the File to scan as a zip file
88    */
89   public static void addClassesFromZip(Vector classList,
90                                        File classPathFile) {
91     // System.out.println("Processing jar/zip file: " + classPathFile);
92     
93     try {
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);
104         }
105       }
106     } catch (Exception ex) {
107       System.err.println("Problem opening " + classPathFile + " with zip.");
108     }
109   }
110
111   
112   /**
113    * Adds the classes from the supplied directory to the class list.
114    *
115    * @param classList the Vector to add the classes to
116    * @param classPathFile the File to recursively scan as a directory
117    */
118   public static void addClassesFromDir(Vector classList,
119                                        File rootDir,
120                                        File currentDir) {
121     
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");
133         } else {
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);
140           } else {
141             // The current directory
142             classList.addElement(current);
143           }
144         }
145       } else {
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);
150         }
151       }
152     }
153   }
154
155   
156   /**
157    * Looks up an unqualified class name in the class path to find possible
158    * fully qualified matches.
159    *
160    * @param className a value of type 'String'
161    */
162   public static void makeImportStatement(String className) {
163
164     String importList = "(list";
165     
166     for (int i = 0; i < CLASS_LIST.size(); i++) {
167       String testName = (String) CLASS_LIST.elementAt(i);
168       
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)) {
172         
173         // Avoid duplicates!
174         testName = " \"" +  testName + "\"";
175         if (importList.indexOf(testName) == -1)
176           importList += testName;
177       }
178     }
179
180     importList += ")";
181     
182
183     System.out.println(importList);
184     System.out.flush();
185
186   }
187
188
189   /**
190    * Tests the ImportWizard from the command line
191    *
192    * @param args an array of strings containing class names to look up
193    */
194   public static void main(String[] args) {
195
196     if (args.length == 0) {
197       System.out.println("Give class names as arguments to look up");
198     } else {
199       for (int i = 0; i < args.length; i++) {
200         System.out.println("=== " + args[i] + " ===");
201         makeImportStatement(args[i]);
202       }
203     }
204   }  
205 } // ImportWizard
206
207 /*
208  * $Log: ImportWizard.java,v $
209  * Revision 1.4  1999/06/17 17:49:27  paulk
210  * Added change log to end of file.
211  *
212  */
213
214 // End of ImportWizard.java