1 7 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 ; 40 import java.util.HashMap ; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 import java.util.Iterator ; 44 import java.util.StringTokenizer ; 45 import java.io.*; 46 47 class SourceMapper { 48 49 private final String [] dirs; 50 51 SourceMapper(List sourcepath) { 52 56 List dirList = new ArrayList (); 57 Iterator iter = sourcepath.iterator(); 58 while (iter.hasNext()) { 59 String element = (String )iter.next(); 60 if ( ! (element.endsWith(".jar") || 63 element.endsWith(".zip"))) { 64 dirList.add(element); 65 } 66 } 67 dirs = (String [])dirList.toArray(new String [0]); 68 } 69 70 SourceMapper(String sourcepath) { 71 79 StringTokenizer st = new StringTokenizer (sourcepath, 80 File.pathSeparator); 81 List dirList = new ArrayList (); 82 while (st.hasMoreTokens()) { 83 String s = st.nextToken(); 84 if ( ! (s.endsWith(".jar") || 87 s.endsWith(".zip"))) { 88 dirList.add(s); 89 } 90 } 91 dirs = (String [])dirList.toArray(new String [0]); 92 } 93 94 97 String getSourcePath() { 98 int i = 0; 99 StringBuffer sp; 100 if (dirs.length < 1) { 101 return ""; } else { 103 sp = new StringBuffer (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 116 File sourceFile(Location loc) { 117 try { 118 String filename = loc.sourceName(); 119 String refName = loc.declaringType().name(); 120 int iDot = refName.lastIndexOf('.'); 121 String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : ""; 122 String 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 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 |