1 21 22 package org.armedbear.j; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 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 className, String sourcePath) 33 { 34 final List dirNames = Utilities.getDirectoriesInPath(sourcePath); 35 if (dirNames != null) { 36 String fileName = 37 className.replace('.', SEPARATOR_CHAR).concat(".java"); 38 Iterator iter = dirNames.iterator(); 39 while (iter.hasNext()) { 40 File dir = File.getInstance((String )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 int index = fileName.lastIndexOf(SEPARATOR_CHAR); 49 if (index >= 0) { 50 String shortName = fileName.substring(index+1); 51 Log.debug("shortName = |" + shortName + "|"); 52 iter = dirNames.iterator(); 53 while (iter.hasNext()) { 54 File dir = File.getInstance((String )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 public static File findSource(Buffer buffer, String className, boolean exact) 70 { 71 String [] candidates; 72 if (exact) { 73 candidates = new String [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 [] candidates) 81 { 82 final String [] imports = getImports(buffer); 83 84 final String jdkSourcePath = 86 buffer.getStringProperty(Property.JDK_SOURCE_PATH); 87 if (jdkSourcePath != null) { 88 final List dirNames = Utilities.getDirectoriesInPath(jdkSourcePath); 89 if (dirNames != null) { 90 for (Iterator it = dirNames.iterator(); it.hasNext();) { 92 String name = (String ) it.next(); 93 File dir = File.getInstance(name); 94 String 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 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 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 [] getCandidates(String s) 137 { 138 ArrayList list = new ArrayList (); 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 [] array = new String [list.size()]; 146 return (String []) list.toArray(array); 147 } 148 149 public static String getPackageName(Buffer buffer) 150 { 151 String packageName = null; 152 for (Line line = buffer.getFirstLine(); line != null; line = line.next()) { 153 String 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 packageName = getPackageName(buffer); 187 if (packageName == null) 188 return null; 189 final String packagePrefix = 190 packageName.replace('.', LocalFile.getSeparatorChar()); 191 final String dirName = parentDir.canonicalPath(); 192 if (dirName.endsWith(packagePrefix)) { 193 String packageRootDirName = 194 dirName.substring(0, dirName.length() - packagePrefix.length()); 195 return File.getInstance(packageRootDirName); 196 } 197 return null; 198 } 199 200 public static String [] getImports(Buffer buffer) 201 { 202 if (buffer.getModeId() != JAVA_MODE) 203 return null; 204 ArrayList list = new ArrayList (); 205 list.add("java.lang.*"); 206 for (Line line = buffer.getFirstLine(); line != null; line = line.next()) { 207 String 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 [] array = new String [list.size()]; 226 return (String []) list.toArray(array); 227 } 228 229 private static File findImport(String className, String [] imports, 231 List dirNames, String extension) 232 { 233 final int size = dirNames.size(); 235 for (int i = 0; i < size; i++) { 236 final String dirName = (String ) 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 public static File findImport(String className, String [] imports, 249 File dir, String extension) 250 { 251 if (dir == null) 252 return null; 253 if (className.indexOf('.') >= 0) { 254 String 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 suffix = ".".concat(className); 262 for (int i = 0; i < imports.length; i++) { 263 String s = imports[i]; 264 if (s.endsWith(suffix)) { 265 String 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 prefix = s.substring(0, s.length() - 1); 273 String canonicalName = prefix.concat(className); 274 String 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 |