KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > batch > ClasspathDirectory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.batch;
12
13 import java.io.File JavaDoc;
14 import java.io.FilenameFilter JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.Hashtable JavaDoc;
17
18 import org.eclipse.jdt.core.compiler.CharOperation;
19 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
20 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
21 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
22 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper;
23 import org.eclipse.jdt.internal.compiler.util.Util;
24
25 public class ClasspathDirectory extends ClasspathLocation {
26
27 private char[] normalizedPath;
28 private String JavaDoc path;
29 private Hashtable JavaDoc directoryCache;
30 private String JavaDoc[] missingPackageHolder = new String JavaDoc[1];
31 private int mode; // ability to only consider one kind of files (source vs. binaries), by default use both
32
private String JavaDoc encoding; // only useful if referenced in the source path
33

34 ClasspathDirectory(File JavaDoc directory, String JavaDoc encoding, int mode,
35         AccessRuleSet accessRuleSet, String JavaDoc destinationPath) {
36     super(accessRuleSet, destinationPath);
37     this.mode = mode;
38     this.path = directory.getAbsolutePath();
39     if (!this.path.endsWith(File.separator))
40         this.path += File.separator;
41     this.directoryCache = new Hashtable JavaDoc(11);
42     this.encoding = encoding;
43 }
44 String JavaDoc[] directoryList(String JavaDoc qualifiedPackageName) {
45     String JavaDoc[] dirList = (String JavaDoc[]) this.directoryCache.get(qualifiedPackageName);
46     if (dirList == this.missingPackageHolder) return null; // package exists in another classpath directory or jar
47
if (dirList != null) return dirList;
48
49     File JavaDoc dir = new File JavaDoc(this.path + qualifiedPackageName);
50     notFound : if (dir.isDirectory()) {
51         // must protect against a case insensitive File call
52
// walk the qualifiedPackageName backwards looking for an uppercase character before the '/'
53
int index = qualifiedPackageName.length();
54         int last = qualifiedPackageName.lastIndexOf(File.separatorChar);
55         while (--index > last && !ScannerHelper.isUpperCase(qualifiedPackageName.charAt(index))){/*empty*/}
56         if (index > last) {
57             if (last == -1) {
58                 if (!doesFileExist(qualifiedPackageName, Util.EMPTY_STRING))
59                     break notFound;
60             } else {
61                 String JavaDoc packageName = qualifiedPackageName.substring(last + 1);
62                 String JavaDoc parentPackage = qualifiedPackageName.substring(0, last);
63                 if (!doesFileExist(packageName, parentPackage))
64                     break notFound;
65             }
66         }
67         if ((dirList = dir.list()) == null)
68             dirList = CharOperation.NO_STRINGS;
69         this.directoryCache.put(qualifiedPackageName, dirList);
70         return dirList;
71     }
72     this.directoryCache.put(qualifiedPackageName, this.missingPackageHolder);
73     return null;
74 }
75 boolean doesFileExist(String JavaDoc fileName, String JavaDoc qualifiedPackageName) {
76     String JavaDoc[] dirList = directoryList(qualifiedPackageName);
77     if (dirList == null) return false; // most common case
78

79     for (int i = dirList.length; --i >= 0;)
80         if (fileName.equals(dirList[i]))
81             return true;
82     return false;
83 }
84 public NameEnvironmentAnswer findClass(char[] typeName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedBinaryFileName) {
85     return findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName, false);
86 }
87 public NameEnvironmentAnswer findClass(char[] typeName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedBinaryFileName, boolean asBinaryOnly) {
88     if (!isPackage(qualifiedPackageName)) return null; // most common case
89

90     String JavaDoc fileName = new String JavaDoc(typeName);
91     boolean binaryExists = ((this.mode & BINARY) != 0) && doesFileExist(fileName + SUFFIX_STRING_class, qualifiedPackageName);
92     boolean sourceExists = ((this.mode & SOURCE) != 0) && doesFileExist(fileName + SUFFIX_STRING_java, qualifiedPackageName);
93     if (sourceExists && !asBinaryOnly) {
94         String JavaDoc fullSourcePath = this.path + qualifiedBinaryFileName.substring(0, qualifiedBinaryFileName.length() - 6) + SUFFIX_STRING_java;
95         if (!binaryExists)
96             return new NameEnvironmentAnswer(new CompilationUnit(null,
97                     fullSourcePath, this.encoding, destinationPath),
98                     fetchAccessRestriction(qualifiedBinaryFileName));
99         String JavaDoc fullBinaryPath = this.path + qualifiedBinaryFileName;
100         long binaryModified = new File JavaDoc(fullBinaryPath).lastModified();
101         long sourceModified = new File JavaDoc(fullSourcePath).lastModified();
102         if (sourceModified > binaryModified)
103             return new NameEnvironmentAnswer(new CompilationUnit(null,
104                     fullSourcePath, this.encoding, destinationPath),
105                     fetchAccessRestriction(qualifiedBinaryFileName));
106     }
107     if (binaryExists) {
108         try {
109             ClassFileReader reader = ClassFileReader.read(this.path + qualifiedBinaryFileName);
110             if (reader != null)
111                 return new NameEnvironmentAnswer(
112                         reader,
113                         fetchAccessRestriction(qualifiedBinaryFileName));
114         } catch (Exception JavaDoc e) {
115             // treat as if file is missing
116
}
117     }
118     return null;
119 }
120 public char[][][] findTypeNames(String JavaDoc qualifiedPackageName) {
121     if (!isPackage(qualifiedPackageName)) {
122         return null; // most common case
123
}
124     File JavaDoc dir = new File JavaDoc(this.path + qualifiedPackageName);
125     if (!dir.exists() || !dir.isDirectory()) {
126         return null;
127     }
128     String JavaDoc[] listFiles = dir.list(new FilenameFilter JavaDoc() {
129         public boolean accept(File JavaDoc directory, String JavaDoc name) {
130             String JavaDoc fileName = name.toLowerCase();
131             return fileName.endsWith(".class") || fileName.endsWith(".java"); //$NON-NLS-1$ //$NON-NLS-2$
132
}
133     });
134     int length;
135     if (listFiles == null || (length = listFiles.length) == 0) {
136         return null;
137     }
138     char[][][] result = new char[length][][];
139     char[][] packageName = CharOperation.splitOn(File.separatorChar, qualifiedPackageName.toCharArray());
140     for (int i = 0; i < length; i++) {
141         String JavaDoc fileName = listFiles[i];
142         int indexOfLastDot = fileName.indexOf('.');
143         result[i] = CharOperation.arrayConcat(packageName, fileName.substring(0, indexOfLastDot).toCharArray());
144     }
145     return result;
146 }
147 public void initialize() throws IOException JavaDoc {
148     // nothing to do
149
}
150 public boolean isPackage(String JavaDoc qualifiedPackageName) {
151     return directoryList(qualifiedPackageName) != null;
152 }
153 public void reset() {
154     this.directoryCache = new Hashtable JavaDoc(11);
155 }
156 public String JavaDoc toString() {
157     return "ClasspathDirectory " + this.path; //$NON-NLS-1$
158
}
159 public char[] normalizedPath() {
160     if (this.normalizedPath == null) {
161         this.normalizedPath = this.path.toCharArray();
162         if (File.separatorChar == '\\') {
163             CharOperation.replace(this.normalizedPath, '\\', '/');
164         }
165     }
166     return this.normalizedPath;
167 }
168 public String JavaDoc getPath() {
169     return this.path;
170 }
171 }
172
Popular Tags