KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > SourcePath


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.debugger.jpda;
20
21 import com.sun.jdi.AbsentInformationException;
22 import com.sun.jdi.Location;
23 import com.sun.jdi.StackFrame;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.spi.debugger.ContextProvider;
28
29 import org.netbeans.api.debugger.jpda.CallStackFrame;
30 import org.netbeans.api.debugger.jpda.Field;
31 import org.netbeans.api.debugger.jpda.JPDADebugger;
32 import org.netbeans.api.debugger.jpda.JPDAThread;
33 import org.netbeans.spi.debugger.jpda.SourcePathProvider;
34 import org.netbeans.spi.debugger.jpda.EditorContext;
35 import org.openide.ErrorManager;
36
37 /**
38  * Utility methods for sources.
39  *
40  * @see Similar class in debuggerjpda/ui when modifying this.
41  *
42  * @author Jan Jancura
43  */

44 public class SourcePath {
45
46     private ContextProvider lookupProvider;
47     private SourcePathProvider contextProvider;
48     private JPDADebugger debugger;
49     
50
51     public SourcePath (ContextProvider lookupProvider) {
52         this.lookupProvider = lookupProvider;
53         debugger = (JPDADebugger) lookupProvider.lookupFirst
54             (null, JPDADebugger.class);
55     }
56
57     private SourcePathProvider getContext () {
58         if (contextProvider == null) {
59             List JavaDoc l = lookupProvider.lookup (null, SourcePathProvider.class);
60             contextProvider = (SourcePathProvider) l.get (0);
61             int i, k = l.size ();
62             for (i = 1; i < k; i++) {
63                 contextProvider = new CompoundContextProvider (
64                     (SourcePathProvider) l.get (i),
65                     contextProvider
66                 );
67             }
68         }
69         return contextProvider;
70     }
71
72     
73     // ContextProvider methods .................................................
74

75     /**
76      * Returns relative path for given url.
77      *
78      * @param url a url of resource file
79      * @param directorySeparator a directory separator character
80      * @param includeExtension whether the file extension should be included
81      * in the result
82      *
83      * @return relative path
84      */

85     public String JavaDoc getRelativePath (
86         String JavaDoc url,
87         char directorySeparator,
88         boolean includeExtension
89     ) {
90         return getContext ().getRelativePath
91             (url, directorySeparator, includeExtension);
92     }
93     
94     /**
95      * Returns the source root (if any) for given url.
96      *
97      * @param url a url of resource file
98      *
99      * @return the source root or <code>null</code> when no source root was found.
100      */

101     public String JavaDoc getSourceRoot(String JavaDoc url) {
102         return getContext().getSourceRoot(url);
103     }
104
105     /**
106      * Translates a relative path ("java/lang/Thread.java") to url
107      * ("file:///C:/Sources/java/lang/Thread.java"). Uses GlobalPathRegistry
108      * if global == true.
109      *
110      * @param relativePath a relative path (java/lang/Thread.java)
111      * @param global true if global path should be used
112      * @return url
113      */

114     public String JavaDoc getURL (String JavaDoc relativePath, boolean global) {
115         return getContext ().getURL (relativePath, global);
116     }
117     
118     public String JavaDoc getURL (
119         StackFrame sf,
120         String JavaDoc stratumn
121     ) {
122         try {
123             return getURL (
124                 convertSlash (sf.location ().sourcePath (stratumn)),
125                 true
126             );
127         } catch (AbsentInformationException e) {
128             return getURL (
129                 convertClassNameToRelativePath (
130                     sf.location ().declaringType ().name ()
131                 ),
132                 true
133             );
134         }
135     }
136     
137     public String JavaDoc getURL (
138         Location loc,
139         String JavaDoc stratumn
140     ) {
141         try {
142             return getURL (
143                 convertSlash(loc.sourcePath(stratumn)),
144                 true
145             );
146         } catch (AbsentInformationException e) {
147             return getURL (
148                 convertClassNameToRelativePath (
149                     loc.declaringType().name()
150                 ),
151                 true
152             );
153         }
154     }
155     
156     /**
157      * Returns array of source roots.
158      */

159     public String JavaDoc[] getSourceRoots () {
160         return getContext ().getSourceRoots ();
161     }
162     
163     /**
164      * Sets array of source roots.
165      *
166      * @param sourceRoots a new array of sourceRoots
167      */

168     public void setSourceRoots (String JavaDoc[] sourceRoots) {
169         getContext ().setSourceRoots (sourceRoots);
170     }
171     
172     /**
173      * Returns set of original source roots.
174      *
175      * @return set of original source roots
176      */

177     public String JavaDoc[] getOriginalSourceRoots () {
178         return getContext ().getOriginalSourceRoots ();
179     }
180     
181     /**
182      * Adds property change listener.
183      *
184      * @param l new listener.
185      */

186     public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
187         getContext ().addPropertyChangeListener (l);
188     }
189
190     /**
191      * Removes property change listener.
192      *
193      * @param l removed listener.
194      */

195     public void removePropertyChangeListener (
196         PropertyChangeListener JavaDoc l
197     ) {
198         getContext ().removePropertyChangeListener (l);
199     }
200     
201     
202     // utility methods .........................................................
203

204     public boolean sourceAvailable (
205         String JavaDoc relativePath
206     ) {
207         return getURL (relativePath, true) != null;
208     }
209
210     public boolean sourceAvailable (
211         JPDAThread t,
212         String JavaDoc stratumn
213     ) {
214         try {
215             return sourceAvailable (convertSlash (t.getSourcePath (stratumn)));
216         } catch (AbsentInformationException e) {
217             return sourceAvailable (convertClassNameToRelativePath (t.getClassName ()));
218         }
219     }
220
221     public boolean sourceAvailable (
222         Field f
223     ) {
224         String JavaDoc className = f.getClassName ();
225         return sourceAvailable (className);
226     }
227
228     public boolean sourceAvailable (
229         CallStackFrame csf,
230         String JavaDoc stratumn
231     ) {
232         try {
233             return sourceAvailable (convertSlash (csf.getSourcePath (stratumn)));
234         } catch (AbsentInformationException e) {
235             return sourceAvailable (convertClassNameToRelativePath (csf.getClassName ()));
236         }
237     }
238
239     public String JavaDoc getURL (
240         CallStackFrame csf,
241         String JavaDoc stratumn
242     ) {
243         try {
244             return getURL (convertSlash (csf.getSourcePath (stratumn)), true);
245         } catch (AbsentInformationException e) {
246             return getURL (
247                 convertClassNameToRelativePath (csf.getClassName ()),
248                 true
249             );
250         }
251     }
252
253     public boolean showSource (
254         JPDAThread t,
255         String JavaDoc stratumn
256     ) {
257         int lineNumber = t.getLineNumber (stratumn);
258         if (lineNumber < 1) lineNumber = 1;
259         try {
260             return EditorContextBridge.showSource (
261                 getURL (convertSlash (t.getSourcePath (stratumn)), true),
262                 lineNumber,
263                 debugger
264             );
265         } catch (AbsentInformationException e) {
266             return EditorContextBridge.showSource (
267                 getURL (
268                     convertClassNameToRelativePath (t.getClassName ()),
269                     true
270                 ),
271                 lineNumber,
272                 debugger
273             );
274         }
275     }
276
277     public boolean showSource (Field v) {
278         String JavaDoc fieldName = ((Field) v).getName ();
279         String JavaDoc className = className = ((Field) v).getClassName ();
280         String JavaDoc url = getURL (
281             EditorContextBridge.getRelativePath (className), true
282         );
283         if (url == null) return false;
284         int lineNumber = lineNumber = EditorContextBridge.getFieldLineNumber (
285             url,
286             className,
287             fieldName
288         );
289         if (lineNumber < 1) lineNumber = 1;
290         return EditorContextBridge.showSource (
291             url,
292             lineNumber,
293             debugger
294         );
295     }
296
297     private static String JavaDoc convertSlash (String JavaDoc original) {
298         return original.replace (File.separatorChar, '/');
299     }
300
301     public static String JavaDoc convertClassNameToRelativePath (
302         String JavaDoc className
303     ) {
304         int i = className.indexOf ('$');
305         if (i > 0) className = className.substring (0, i);
306         String JavaDoc sourceName = className.replace
307             ('.', '/') + ".java";
308         return sourceName;
309     }
310
311     public Object JavaDoc annotate (
312         JPDAThread t,
313         String JavaDoc stratumn
314     ) {
315         int lineNumber = t.getLineNumber (stratumn);
316         if (lineNumber < 1) return null;
317         try {
318             return EditorContextBridge.annotate (
319                 getURL (convertSlash (t.getSourcePath (stratumn)), true),
320                 lineNumber,
321                 EditorContext.CURRENT_LINE_ANNOTATION_TYPE,
322                 debugger
323             );
324         } catch (AbsentInformationException e) {
325             return EditorContextBridge.annotate (
326                 getURL (
327                     convertClassNameToRelativePath (t.getClassName ()), true
328                 ),
329                 lineNumber,
330                 EditorContext.CURRENT_LINE_ANNOTATION_TYPE,
331                 debugger
332             );
333         }
334     }
335
336     public Object JavaDoc annotate (
337         CallStackFrame csf,
338         String JavaDoc stratumn
339     ) {
340         int lineNumber = csf.getLineNumber (stratumn);
341         if (lineNumber < 1) return null;
342         try {
343             return EditorContextBridge.annotate (
344                 getURL (convertSlash (csf.getSourcePath (stratumn)), true),
345                 lineNumber,
346                 EditorContext.CALL_STACK_FRAME_ANNOTATION_TYPE,
347                 debugger
348             );
349         } catch (AbsentInformationException e) {
350             return EditorContextBridge.annotate (
351                 getURL (
352                     convertClassNameToRelativePath (csf.getClassName ()), true
353                 ),
354                 lineNumber,
355                 EditorContext.CALL_STACK_FRAME_ANNOTATION_TYPE,
356                 debugger
357             );
358         }
359     }
360
361     
362     // innerclasses ............................................................
363

364     private static class CompoundContextProvider extends SourcePathProvider {
365
366         private SourcePathProvider cp1, cp2;
367
368         CompoundContextProvider (
369             SourcePathProvider cp1,
370             SourcePathProvider cp2
371         ) {
372             this.cp1 = cp1;
373             this.cp2 = cp2;
374         }
375
376         public String JavaDoc getURL (String JavaDoc relativePath, boolean global) {
377             String JavaDoc p1 = cp1.getURL (relativePath, global);
378             if (p1 != null) return p1;
379             return cp2.getURL (relativePath, global);
380         }
381
382         public String JavaDoc getRelativePath (
383             String JavaDoc url,
384             char directorySeparator,
385             boolean includeExtension
386         ) {
387             String JavaDoc p1 = cp1.getRelativePath (
388                 url,
389                 directorySeparator,
390                 includeExtension
391             );
392             if (p1 != null) return p1;
393             return cp2.getRelativePath (
394                 url,
395                 directorySeparator,
396                 includeExtension
397             );
398         }
399
400         public String JavaDoc getSourceRoot(String JavaDoc url) {
401             String JavaDoc sourceRoot = cp1.getSourceRoot(url);
402             if (sourceRoot == null) {
403                 sourceRoot = cp2.getSourceRoot(url);
404             }
405             return sourceRoot;
406         }
407         
408         public String JavaDoc[] getSourceRoots () {
409             String JavaDoc[] fs1 = cp1.getSourceRoots ();
410             String JavaDoc[] fs2 = cp2.getSourceRoots ();
411             String JavaDoc[] fs = new String JavaDoc [fs1.length + fs2.length];
412             System.arraycopy (fs1, 0, fs, 0, fs1.length);
413             System.arraycopy (fs2, 0, fs, fs1.length, fs2.length);
414             return fs;
415         }
416     
417         public String JavaDoc[] getOriginalSourceRoots () {
418             String JavaDoc[] fs1 = cp1.getOriginalSourceRoots ();
419             String JavaDoc[] fs2 = cp2.getOriginalSourceRoots ();
420             String JavaDoc[] fs = new String JavaDoc [fs1.length + fs2.length];
421             System.arraycopy (fs1, 0, fs, 0, fs1.length);
422             System.arraycopy (fs2, 0, fs, fs1.length, fs2.length);
423             return fs;
424         }
425
426         public void setSourceRoots (String JavaDoc[] sourceRoots) {
427             cp1.setSourceRoots (sourceRoots);
428             cp2.setSourceRoots (sourceRoots);
429         }
430
431         public void addPropertyChangeListener (PropertyChangeListener JavaDoc l) {
432             cp1.addPropertyChangeListener (l);
433             cp2.addPropertyChangeListener (l);
434         }
435
436         public void removePropertyChangeListener (PropertyChangeListener JavaDoc l) {
437             cp1.removePropertyChangeListener (l);
438             cp2.removePropertyChangeListener (l);
439         }
440     }
441
442     private static class CompoundAnnotation {
443         CompoundAnnotation () {}
444         
445         Object JavaDoc annotation1;
446         Object JavaDoc annotation2;
447     }
448 }
449
450
Popular Tags