1 11 package org.eclipse.jdt.internal.compiler.batch; 12 13 import java.io.File ; 14 import java.io.FilenameFilter ; 15 import java.io.IOException ; 16 import java.util.Hashtable ; 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 path; 29 private Hashtable directoryCache; 30 private String [] missingPackageHolder = new String [1]; 31 private int mode; private String encoding; 34 ClasspathDirectory(File directory, String encoding, int mode, 35 AccessRuleSet accessRuleSet, String 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 (11); 42 this.encoding = encoding; 43 } 44 String [] directoryList(String qualifiedPackageName) { 45 String [] dirList = (String []) this.directoryCache.get(qualifiedPackageName); 46 if (dirList == this.missingPackageHolder) return null; if (dirList != null) return dirList; 48 49 File dir = new File (this.path + qualifiedPackageName); 50 notFound : if (dir.isDirectory()) { 51 int index = qualifiedPackageName.length(); 54 int last = qualifiedPackageName.lastIndexOf(File.separatorChar); 55 while (--index > last && !ScannerHelper.isUpperCase(qualifiedPackageName.charAt(index))){} 56 if (index > last) { 57 if (last == -1) { 58 if (!doesFileExist(qualifiedPackageName, Util.EMPTY_STRING)) 59 break notFound; 60 } else { 61 String packageName = qualifiedPackageName.substring(last + 1); 62 String 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 fileName, String qualifiedPackageName) { 76 String [] dirList = directoryList(qualifiedPackageName); 77 if (dirList == null) return false; 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 qualifiedPackageName, String qualifiedBinaryFileName) { 85 return findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName, false); 86 } 87 public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName, boolean asBinaryOnly) { 88 if (!isPackage(qualifiedPackageName)) return null; 90 String fileName = new String (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 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 fullBinaryPath = this.path + qualifiedBinaryFileName; 100 long binaryModified = new File (fullBinaryPath).lastModified(); 101 long sourceModified = new File (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 e) { 115 } 117 } 118 return null; 119 } 120 public char[][][] findTypeNames(String qualifiedPackageName) { 121 if (!isPackage(qualifiedPackageName)) { 122 return null; } 124 File dir = new File (this.path + qualifiedPackageName); 125 if (!dir.exists() || !dir.isDirectory()) { 126 return null; 127 } 128 String [] listFiles = dir.list(new FilenameFilter () { 129 public boolean accept(File directory, String name) { 130 String fileName = name.toLowerCase(); 131 return fileName.endsWith(".class") || fileName.endsWith(".java"); } 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 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 { 148 } 150 public boolean isPackage(String qualifiedPackageName) { 151 return directoryList(qualifiedPackageName) != null; 152 } 153 public void reset() { 154 this.directoryCache = new Hashtable (11); 155 } 156 public String toString() { 157 return "ClasspathDirectory " + this.path; } 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 getPath() { 169 return this.path; 170 } 171 } 172 | Popular Tags |