KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > JavaSource


1 /*
2  * JavaSource.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: JavaSource.java,v 1.6 2003/05/18 19:26:02 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 public final class JavaSource implements Constants
29 {
30     private static final char SEPARATOR_CHAR = LocalFile.getSeparatorChar();
31
32     public static File findSource(String JavaDoc className, String JavaDoc sourcePath)
33     {
34         final List JavaDoc dirNames = Utilities.getDirectoriesInPath(sourcePath);
35         if (dirNames != null) {
36             String JavaDoc fileName =
37                 className.replace('.', SEPARATOR_CHAR).concat(".java");
38             Iterator JavaDoc iter = dirNames.iterator();
39             while (iter.hasNext()) {
40                 File dir = File.getInstance((String JavaDoc)iter.next());
41                 if (dir != null) {
42                     File file = File.getInstance(dir, fileName);
43                     if (file != null && file.isFile())
44                         return file;
45                 }
46             }
47             // Not found. Try looking for short name of file.
48
int index = fileName.lastIndexOf(SEPARATOR_CHAR);
49             if (index >= 0) {
50                 String JavaDoc shortName = fileName.substring(index+1);
51                 Log.debug("shortName = |" + shortName + "|");
52                 iter = dirNames.iterator();
53                 while (iter.hasNext()) {
54                     File dir = File.getInstance((String JavaDoc)iter.next());
55                     if (dir != null) {
56                         File file = File.getInstance(dir, shortName);
57                         if (file != null && file.isFile()) {
58                             Log.debug("file = " + file.canonicalPath());
59                             return file;
60                         }
61                     }
62                 }
63             }
64         }
65         return null;
66     }
67
68     // Returns file containing source for class referenced in buffer.
69
public static File findSource(Buffer buffer, String JavaDoc className, boolean exact)
70     {
71         String JavaDoc[] candidates;
72         if (exact) {
73             candidates = new String JavaDoc[1];
74             candidates[0] = className;
75         } else
76             candidates = getCandidates(className);
77         return findSource(buffer, candidates);
78     }
79
80     private static File findSource(Buffer buffer, String JavaDoc[] candidates)
81     {
82         final String JavaDoc[] imports = getImports(buffer);
83
84         // Look for import in JDK source path.
85
final String JavaDoc jdkSourcePath =
86             buffer.getStringProperty(Property.JDK_SOURCE_PATH);
87         if (jdkSourcePath != null) {
88             final List JavaDoc dirNames = Utilities.getDirectoriesInPath(jdkSourcePath);
89             if (dirNames != null) {
90                 // Tell the user if the JDK source path is bogus.
91
for (Iterator JavaDoc it = dirNames.iterator(); it.hasNext();) {
92                     String JavaDoc name = (String JavaDoc) it.next();
93                     File dir = File.getInstance(name);
94                     String JavaDoc message = null;
95                     if (dir == null) {
96                         message = "Invalid directory " + name;
97                     } else if (!dir.isDirectory()) {
98                         message = "The directory " + dir.canonicalPath() +
99                             " does not exist.";
100                     }
101                     if (message != null)
102                         MessageDialog.showMessageDialog(message, "Error");
103                 }
104                 for (int i = 0; i < candidates.length; i++) {
105                     File file = findImport(candidates[i], imports, dirNames,
106                         ".java");
107                     if (file != null)
108                         return file;
109                 }
110             }
111         }
112
113         // Look for import relative to package root directory.
114
File packageRootDir = JavaSource.getPackageRootDirectory(buffer);
115         if (packageRootDir != null) {
116             for (int i = 0; i < candidates.length; i++) {
117                 File file = findImport(candidates[i], imports, packageRootDir,
118                     ".java");
119                 if (file != null)
120                     return file;
121             }
122         }
123
124         // Look in current directory (i.e. current package).
125
File currentDir = buffer.getCurrentDirectory();
126         for (int i = 0; i < candidates.length; i++) {
127             File file =
128                 File.getInstance(currentDir, candidates[i].concat(".java"));
129             if (file != null && file.isFile())
130                 return file;
131         }
132
133         return null;
134     }
135
136     private static String JavaDoc[] getCandidates(String JavaDoc s)
137     {
138         ArrayList JavaDoc list = new ArrayList JavaDoc();
139         int index = s.indexOf('.');
140         while (index >= 0) {
141             list.add(s.substring(0, index));
142             index = s.indexOf('.', index + 1);
143         }
144         list.add(s);
145         String JavaDoc[] array = new String JavaDoc[list.size()];
146         return (String JavaDoc[]) list.toArray(array);
147     }
148
149     public static String JavaDoc getPackageName(Buffer buffer)
150     {
151         String JavaDoc packageName = null;
152         for (Line line = buffer.getFirstLine(); line != null; line = line.next()) {
153             String JavaDoc trim = line.trim();
154             if (!trim.startsWith("package"))
155                 continue;
156             trim = trim.substring(7);
157             if (trim.length() == 0)
158                 continue;
159             char c = trim.charAt(0);
160             if (c != ' ' && c != '\t')
161                 continue;
162             trim = trim.trim();
163             final int length = trim.length();
164             if (length == 0)
165                 continue;
166             FastStringBuffer sb = new FastStringBuffer();
167             for (int i = 0; i < length; i++) {
168                 c = trim.charAt(i);
169                 if (c == ' ' || c == '\t' || c == ';')
170                     break;
171                 sb.append(c);
172             }
173             packageName = sb.toString();
174         }
175         return packageName;
176     }
177
178     public static File getPackageRootDirectory(Buffer buffer)
179     {
180         final File file = buffer.getFile();
181         if (file == null || file.isRemote())
182             return null;
183         final File parentDir = file.getParentFile();
184         if (parentDir == null)
185             return null;
186         final String JavaDoc packageName = getPackageName(buffer);
187         if (packageName == null)
188             return null;
189         final String JavaDoc packagePrefix =
190             packageName.replace('.', LocalFile.getSeparatorChar());
191         final String JavaDoc dirName = parentDir.canonicalPath();
192         if (dirName.endsWith(packagePrefix)) {
193             String JavaDoc packageRootDirName =
194                 dirName.substring(0, dirName.length() - packagePrefix.length());
195             return File.getInstance(packageRootDirName);
196         }
197         return null;
198     }
199
200     public static String JavaDoc[] getImports(Buffer buffer)
201     {
202         if (buffer.getModeId() != JAVA_MODE)
203             return null;
204         ArrayList JavaDoc list = new ArrayList JavaDoc();
205         list.add("java.lang.*");
206         for (Line line = buffer.getFirstLine(); line != null; line = line.next()) {
207             String JavaDoc trim = line.trim();
208             if (!trim.startsWith("import"))
209                 continue;
210             trim = trim.substring(6);
211             if (trim.length() == 0)
212                 continue;
213             if (trim.charAt(0) != ' ' && trim.charAt(0) != '\t')
214                 continue;
215             trim = trim.trim();
216             FastStringBuffer sb = new FastStringBuffer();
217             for (int i = 0; i < trim.length(); i++) {
218                 char c = trim.charAt(i);
219                 if (c == ' ' || c == '\t' || c == ';')
220                     break;
221                 sb.append(c);
222             }
223             list.add(sb.toString());
224         }
225         String JavaDoc[] array = new String JavaDoc[list.size()];
226         return (String JavaDoc[]) list.toArray(array);
227     }
228
229     // Returns null if file not found.
230
private static File findImport(String JavaDoc className, String JavaDoc[] imports,
231         List JavaDoc dirNames, String JavaDoc extension)
232     {
233         // Iterate through directories.
234
final int size = dirNames.size();
235         for (int i = 0; i < size; i++) {
236             final String JavaDoc dirName = (String JavaDoc) dirNames.get(i);
237             final File dir = File.getInstance(dirName);
238             if (dir != null) {
239                 File file = findImport(className, imports, dir, ".java");
240                 if (file != null)
241                     return file;
242             }
243         }
244         return null;
245     }
246
247     // Returns null if file not found.
248
public static File findImport(String JavaDoc className, String JavaDoc[] imports,
249         File dir, String JavaDoc extension)
250     {
251         if (dir == null)
252             return null;
253         if (className.indexOf('.') >= 0) {
254             // Canonical class name.
255
String JavaDoc fileName =
256                 className.replace('.', SEPARATOR_CHAR).concat(extension);
257             File file = File.getInstance(dir, fileName);
258             return (file != null && file.isFile()) ? file : null;
259         }
260         if (imports != null) {
261             String JavaDoc suffix = ".".concat(className);
262             for (int i = 0; i < imports.length; i++) {
263                 String JavaDoc s = imports[i];
264                 if (s.endsWith(suffix)) {
265                     // Found it!
266
String JavaDoc fileName =
267                         s.replace('.', SEPARATOR_CHAR).concat(extension);
268                     File file = File.getInstance(dir, fileName);
269                     return (file != null && file.isFile()) ? file : null;
270                 }
271                 if (s.endsWith(".*")) {
272                     String JavaDoc prefix = s.substring(0, s.length() - 1);
273                     String JavaDoc canonicalName = prefix.concat(className);
274                     String JavaDoc filename =
275                         canonicalName.replace('.',
276                             SEPARATOR_CHAR).concat(extension);
277                     File file = File.getInstance(dir, filename);
278                     if (file != null && file.isFile())
279                         return file;
280                 }
281             }
282         }
283         return null;
284     }
285 }
286
Popular Tags