1 22 23 package org.aspectj.debugger.base; 24 25 import org.aspectj.debugger.request.*; 26 import java.io.*; 27 import java.util.*; 28 29 37 38 public class SourceManager { 39 40 private Debugger debugger; 41 private String sourcePath; 42 43 public SourceManager(Debugger debugger) { 44 this(debugger, "."); 45 } 46 47 public SourceManager(Debugger debugger, String sourcePath) { 48 this.debugger = debugger; 49 try { 50 this.sourcePath = new File(sourcePath).getCanonicalPath(); 51 } catch (IOException ioe) { 52 } } 55 56 public void setSourcePath(String sourcePath) { 57 try { 58 File file = new File(sourcePath); 59 if (file.exists()) { 60 this.sourcePath = file.getCanonicalPath(); 61 return; 64 } 65 } catch (IOException ioe) { 66 } 67 this.sourcePath = sourcePath; 69 if (debugger.getOptions().isSet("extra")) { 70 debugger.error("The path " + sourcePath + " does not exist"); 71 showPossiblePaths(sourcePath); 72 } 73 } 74 75 private void showPossiblePaths(String sourcePath) { 76 Iterator iter = getPossiblePaths(sourcePath).iterator(); 77 if (!iter.hasNext()) { 78 debugger.error("There are no possible paths"); 79 return; 80 } 81 String error = "Possible paths are:\n"; 82 while (iter.hasNext()) { 83 error += "\n " + iter.next(); 84 } 85 debugger.error(error); 86 } 87 88 private List getPossiblePaths(String sourcePath) { 89 List completions = new Vector(); 90 char c; 91 int isep1 = sourcePath.lastIndexOf(File.separatorChar); 92 int isep2 = sourcePath.lastIndexOf('/'); 93 int isep3 = sourcePath.lastIndexOf('\\'); 94 int ilastSep = -1; 95 if ((isep1 >= isep2) && (isep1 >= isep3)) { 96 ilastSep = isep1; 97 } else if ((isep2 >= isep1) && (isep2 >= isep3)) { 98 ilastSep = isep2; 99 } else if ((isep3 >= isep2) && (isep3 >= isep2)) { 100 ilastSep = isep3; 101 } else { 102 ilastSep = isep1; 103 } 104 if (ilastSep == -1) { 105 return completions; 106 } 107 File file = new File(sourcePath.substring(0, ilastSep+1) + "."); 108 File parent = file.getParentFile(); 109 if (!parent.exists()) { 110 return completions; 111 } 112 File[] files = parent.listFiles(new FileFilter() { 113 public boolean accept(File _file) { 114 return _file.isDirectory(); 115 } 116 }); 117 for (int i = 0; i < files.length; i++) { 118 completions.add(files[i]); 119 } 120 return completions; 121 } 122 123 public String getSourcePath() { 124 return sourcePath; 125 } 126 127 public List getSourceLines(String sourceName) { 128 Vector list = new Vector(); 129 LineNumberReader in = getReader(sourceName); 130 if (in == null) { 131 return list; 132 } 133 try { 134 String lineString = ""; 135 int lineNumber = -1; 136 while ((lineNumber = in.getLineNumber()) != -1 && 137 (lineString = in.readLine()) != null) { 138 list.add(new SourceLine(sourceName, lineString, lineNumber)); 139 } 140 } catch (IOException ioe) { 141 } 142 try { 143 in.close(); 144 } catch (IOException ioe) { 145 } 146 return list; 147 } 148 149 public String getLine(String sourceName, int lineNumber) { 150 return getSourceLine(sourceName, lineNumber).lineString; 151 } 152 153 public List getSourceLines(String sourceName, int startLine, int endLine) throws DebuggerException { 154 Vector lines = new Vector(); 155 lines.add(emptySourceLine()); 156 LineNumberReader in = getReader(sourceName); 157 if (in == null) { 158 return lines; 159 } 160 if (startLine > endLine) { 161 throw new DebuggerException("Start line must be greater than the end line."); 162 } 163 while (in.getLineNumber() < startLine) { 164 try { 165 in.readLine(); 166 } catch (IOException ioe) { 167 return lines; 168 } 169 } 170 lines = new Vector(); 171 int num = -1; 172 String lineStr = ""; 173 try { 174 while ((lineStr = in.readLine()) != null && 175 (num = in.getLineNumber()) <= endLine) { 176 lines.add(new SourceLine(sourceName, lineStr, num-1)); 177 } 178 } catch (IOException ioe) { 179 } 180 return lines; 181 } 182 183 public SourceLine getSourceLine(String sourceName, int lineNumber) { 184 SourceLine sl = emptySourceLine(); 185 LineNumberReader in = getReader(sourceName); 186 if (in == null) { 187 return null; 188 } 189 while (in.getLineNumber() < lineNumber) { 190 try { 191 in.readLine(); 192 } catch (IOException ioe) { 193 return null; 194 } 195 } 196 String line = ""; 197 try { 198 line = in.readLine(); 199 sl = new SourceLine(sourceName, line, lineNumber); 200 in.close(); 201 } catch (IOException ioe) { 202 } 203 return sl; 204 } 205 206 private LineNumberReader getReader(String relativeFileName) { 207 LineNumberReader in = null; 208 try { 209 if (relativeFileName != null) { 210 in = new LineNumberReader(new FileReader(resolve(relativeFileName))); 211 } 212 } catch (FileNotFoundException fnfe) { 213 } 214 return in; 215 } 216 217 public File getFile(String relativeFileName) { 218 String fullName = resolve(relativeFileName); 219 File file = new File(fullName); 220 return file; 221 } 222 223 private String resolve(String fileName) { 224 return resolve(fileName, debugger.getFilePaths()); 225 } 226 227 private String resolve(String fileName, List filePaths) { 228 Iterator iter = filePaths.iterator(); 229 while (iter.hasNext()) { 230 String filePath = iter.next() + ""; 231 String fullName = fileName; 232 if (!fullName.startsWith(getSourcePath())) { 233 fullName = sourcePath + File.separator + 234 (filePath.equals("") ? "" : File.separator) + fileName; 235 } 236 try { 237 File file = new File(fullName); 238 if (file.exists()) { 239 return file.getCanonicalPath(); 241 } 243 } catch (IOException ioe) { 244 } 245 } 246 return fileName; 247 } 248 249 private SourceLine emptySourceLine() { 250 return new SourceLine("<not available>", "", -1); 251 } 252 253 public interface LineUpdatable { 254 public void update(int line); 255 } 256 257 public static class SourceLine { 258 public String sourceName = ""; 259 public String lineString = ""; 260 public int lineNumber = -1; 261 public final static String tab = " "; 262 private static char c; 263 private boolean hasBreakpoint = false; 264 private boolean isBreakpoint = false; 265 private BreakpointRequestAction request; 266 private boolean isExecutable = false; 267 private String className = null; 268 public final static SourceLine emptyLine = 269 new SourceLine("<not-available>", "", 0); 270 271 public final static int NONE = 0; 272 public final static int UNVERIFIED = 1; 273 public final static int VERIFIED = 2; 274 275 private int status = NONE; 276 277 private LineUpdatable updater; 278 public void setUpdater(LineUpdatable updater) { 279 this.updater = updater; 280 } 281 private void update() { 282 if (updater != null) updater.update(getLineNumber()); 283 } 284 285 public SourceLine(String sourceName, String lineString, int lineNumber) { 286 this.sourceName = sourceName; 287 this.lineNumber = lineNumber + 1; 288 this.lineString = expandTabs(lineString); 289 } 290 291 public String toString() { 292 return sourceName + ":" + lineNumber + ": " + lineString; 293 } 294 295 public String getSourceName() { 296 return sourceName; 297 } 298 299 public String getLineString() { 300 return lineString; 301 } 302 303 public int getLineNumber() { 304 return lineNumber; 305 } 306 307 public void setStatus(int status) { 308 this.status = status; 309 update(); 310 } 311 312 public int getStatus() { 313 return status; 314 } 315 316 public String getClassName() { 317 return className; 318 } 319 320 public void setClassName(String className) { 321 this.className = className; 322 } 323 324 public void setRequest(BreakpointRequestAction request) { 325 this.request = request; 326 request.setSourceLine(this); 327 } 328 329 public boolean hasBreakpoint() { 330 return status != NONE; 331 } 332 333 public void clear() { 334 setStatus(NONE); 335 } 336 337 public void unverify(BreakpointRequestAction request) { 338 setRequest(request); 339 setStatus(UNVERIFIED); 340 } 341 342 public void verify(BreakpointRequestAction request) { 343 setRequest(request); 344 setStatus(VERIFIED); 345 } 346 347 public void ex(String className) { 348 setExecutable(true, className); 349 } 350 351 public void unex() { 352 setExecutable(false); 353 } 354 355 public void setExecutable(boolean isExecutable) { 356 setExecutable(isExecutable, null); 357 } 358 359 public void setExecutable(boolean isExecutable, String className) { 360 this.isExecutable = isExecutable; 361 setClassName(className); 362 } 363 364 public boolean isExecutable() { 365 return isExecutable; 366 } 367 368 public BreakpointRequestAction getRequest() { 369 return this.request; 370 } 371 372 private String expandTabs(String line) { 373 char[] chars = line.toCharArray(); 374 StringBuffer buf = new StringBuffer (); 375 for (int i = 0; i < chars.length; i++) { 376 buf.append(((c = chars[i]) == '\t') ? tab : c+""); 377 } 378 return buf.toString(); 379 } 380 } 381 } 382 | Popular Tags |