KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > base > SourceManager


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.base;
24
25 import org.aspectj.debugger.request.*;
26 import java.io.*;
27 import java.util.*;
28
29 /**
30  * SourceManager.java
31  *
32  *
33  * Created: Tue Aug 29 11:38:31 2000
34  *
35  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
36  */

37
38 public class SourceManager {
39
40     private Debugger debugger;
41     private String JavaDoc sourcePath;
42
43     public SourceManager(Debugger debugger) {
44         this(debugger, ".");
45     }
46
47     public SourceManager(Debugger debugger, String JavaDoc sourcePath) {
48         this.debugger = debugger;
49         try {
50             this.sourcePath = new File(sourcePath).getCanonicalPath();
51         } catch (IOException ioe) {
52         } //FILE
53
// this.sourcePath = new File(sourcePath).getAbsolutePath();
54
}
55
56     public void setSourcePath(String JavaDoc sourcePath) {
57         try {
58             File file = new File(sourcePath);
59             if (file.exists()) {
60                 this.sourcePath = file.getCanonicalPath();
61                 //FILE
62
// this.sourcePath = file.getAbsolutePath();
63
return;
64             }
65         } catch (IOException ioe) {
66         }
67         //!!! Do we still want to assign it
68
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 JavaDoc sourcePath) {
76         Iterator iter = getPossiblePaths(sourcePath).iterator();
77         if (!iter.hasNext()) {
78             debugger.error("There are no possible paths");
79             return;
80         }
81         String JavaDoc 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 JavaDoc 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 JavaDoc getSourcePath() {
124         return sourcePath;
125     }
126
127     public List getSourceLines(String JavaDoc sourceName) {
128         Vector list = new Vector();
129         LineNumberReader in = getReader(sourceName);
130         if (in == null) {
131             return list;
132         }
133         try {
134             String JavaDoc 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 JavaDoc getLine(String JavaDoc sourceName, int lineNumber) {
150         return getSourceLine(sourceName, lineNumber).lineString;
151     }
152
153     public List getSourceLines(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc relativeFileName) {
218         String JavaDoc fullName = resolve(relativeFileName);
219         File file = new File(fullName);
220         return file;
221     }
222
223     private String JavaDoc resolve(String JavaDoc fileName) {
224         return resolve(fileName, debugger.getFilePaths());
225     }
226
227     private String JavaDoc resolve(String JavaDoc fileName, List filePaths) {
228         Iterator iter = filePaths.iterator();
229         while (iter.hasNext()) {
230             String JavaDoc filePath = iter.next() + "";
231             String JavaDoc 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                     //FILE
240
return file.getCanonicalPath();
241 // return file.getAbsolutePath();
242
}
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 JavaDoc sourceName = "";
259         public String JavaDoc lineString = "";
260         public int lineNumber = -1;
261         public final static String JavaDoc 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 JavaDoc 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 JavaDoc sourceName, String JavaDoc lineString, int lineNumber) {
286             this.sourceName = sourceName;
287             this.lineNumber = lineNumber + 1;
288             this.lineString = expandTabs(lineString);
289         }
290
291         public String JavaDoc toString() {
292             return sourceName + ":" + lineNumber + ": " + lineString;
293         }
294
295         public String JavaDoc getSourceName() {
296             return sourceName;
297         }
298
299         public String JavaDoc 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 JavaDoc getClassName() {
317             return className;
318         }
319
320         public void setClassName(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc expandTabs(String JavaDoc line) {
373             char[] chars = line.toCharArray();
374             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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