KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > tty > SourceMapper


1 /*
2  * @(#)SourceMapper.java 1.19 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.tty;
36
37 import com.sun.jdi.Location;
38 import com.sun.jdi.AbsentInformationException;
39 import java.util.Map JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.StringTokenizer JavaDoc;
45 import java.io.*;
46
47 class SourceMapper {
48
49     private final String JavaDoc[] dirs;
50
51     SourceMapper(List JavaDoc sourcepath) {
52         /*
53          * sourcepath can arrive from the debugee as a List.
54          * (via PathSearchingVirtualMachine.classPath())
55          */

56         List JavaDoc dirList = new ArrayList JavaDoc();
57         Iterator JavaDoc iter = sourcepath.iterator();
58         while (iter.hasNext()) {
59             String JavaDoc element = (String JavaDoc)iter.next();
60             //XXX remove .jar and .zip files; we want only directories on
61
//the source path. (Bug ID 4186582)
62
if ( ! (element.endsWith(".jar") ||
63                     element.endsWith(".zip"))) {
64                 dirList.add(element);
65             }
66         }
67         dirs = (String JavaDoc[])dirList.toArray(new String JavaDoc[0]);
68     }
69
70     SourceMapper(String JavaDoc sourcepath) {
71         /*
72          * sourcepath can also arrive from the command line
73          * as a String. (via "-sourcepath")
74          *
75          * Using File.pathSeparator as delimiter below is OK
76          * because we are on the same machine as the command
77          * line originiated.
78          */

79         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sourcepath,
80                                                  File.pathSeparator);
81         List JavaDoc dirList = new ArrayList JavaDoc();
82         while (st.hasMoreTokens()) {
83             String JavaDoc s = st.nextToken();
84             //XXX remove .jar and .zip files; we want only directories on
85
//the source path. (Bug ID 4186582)
86
if ( ! (s.endsWith(".jar") ||
87                     s.endsWith(".zip"))) {
88                 dirList.add(s);
89             }
90         }
91         dirs = (String JavaDoc[])dirList.toArray(new String JavaDoc[0]);
92     }
93     
94     /*
95      * Return the current sourcePath as a String.
96      */

97     String JavaDoc getSourcePath() {
98         int i = 0;
99         StringBuffer JavaDoc sp;
100         if (dirs.length < 1) {
101             return ""; //The source path is empty.
102
} else {
103             sp = new StringBuffer JavaDoc(dirs[i++]);
104         }
105         for (; i < dirs.length; i++) {
106             sp.append(File.pathSeparator);
107             sp.append(dirs[i]);
108         }
109         return sp.toString();
110     }
111
112     /**
113      * Return a File cooresponding to the source of this location.
114      * Return null if not available.
115      */

116     File sourceFile(Location loc) {
117         try {
118             String JavaDoc filename = loc.sourceName();
119             String JavaDoc refName = loc.declaringType().name();
120             int iDot = refName.lastIndexOf('.');
121             String JavaDoc pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : "";
122             String JavaDoc full = pkgName.replace('.', File.separatorChar) + filename;
123             for (int i= 0; i < dirs.length; ++i) {
124                 File path = new File(dirs[i], full);
125                 if (path.exists()) {
126                     return path;
127                 }
128             }
129             return null;
130         } catch (AbsentInformationException e) {
131             return null;
132         }
133     }
134
135     /**
136      * Return a BufferedReader cooresponding to the source
137      * of this location.
138      * Return null if not available.
139      * Note: returned reader must be closed.
140      */

141     BufferedReader sourceReader(Location loc) {
142         File sourceFile = sourceFile(loc);
143         if (sourceFile == null) {
144             return null;
145         }
146         try {
147             return new BufferedReader(new FileReader(sourceFile));
148         } catch(IOException exc) {
149         }
150         return null;
151     }
152 }
153         
154
Popular Tags