1 19 20 package org.netbeans.modules.debugger.jpda.projects; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.*; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.TreeSet ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import java.util.regex.Pattern ; 34 import java.util.regex.Matcher ; 35 36 import org.netbeans.api.java.platform.JavaPlatformManager; 37 38 import org.netbeans.spi.debugger.jpda.SourcePathProvider; 39 import org.netbeans.api.debugger.Session; 40 import org.netbeans.spi.debugger.ContextProvider; 41 42 import org.netbeans.api.java.classpath.ClassPath; 43 import org.netbeans.api.java.classpath.GlobalPathRegistry; 44 import org.netbeans.api.java.platform.JavaPlatform; 45 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 46 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileStateInvalidException; 49 import org.openide.filesystems.FileSystem; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.filesystems.JarFileSystem; 52 import org.openide.filesystems.Repository; 53 import org.openide.filesystems.URLMapper; 54 55 56 60 public class SourcePathProviderImpl extends SourcePathProvider { 61 62 private static boolean verbose = 63 System.getProperty ("netbeans.debugger.sourcepathproviderimpl") != null; 64 65 private static Logger logger = Logger.getLogger("org.netbeans.modules.debugger.jpda.projects"); 66 67 private static final Pattern thisDirectoryPattern = Pattern.compile("(/|\\A)\\./"); 68 private static final Pattern parentDirectoryPattern = Pattern.compile("(/|\\A)([^/]+?)/\\.\\./"); 69 70 private Session session; 71 private ClassPath originalSourcePath; 73 private ClassPath smartSteppingSourcePath; 74 private String [] sourceRoots; 75 private PropertyChangeSupport pcs; 76 77 78 public SourcePathProviderImpl (ContextProvider contextProvider) { 79 pcs = new PropertyChangeSupport (this); 80 this.session = (Session) contextProvider.lookupFirst 81 (null, Session.class); 82 Map properties = (Map) contextProvider.lookupFirst 83 (null, Map.class); 84 85 if (properties != null) { 87 smartSteppingSourcePath = (ClassPath) properties.get ("sourcepath"); 88 ClassPath jdkCP = (ClassPath) properties.get ("jdksources"); 89 if ( (jdkCP == null) && (JavaPlatform.getDefault () != null) ) 90 jdkCP = JavaPlatform.getDefault ().getSourceFolders (); 91 originalSourcePath = jdkCP == null ? 92 smartSteppingSourcePath : 93 ClassPathSupport.createProxyClassPath ( 94 new ClassPath[] { 95 smartSteppingSourcePath, 96 jdkCP 97 } 98 ); 99 } else { 100 Set allSourceRoots = new HashSet ( 101 GlobalPathRegistry.getDefault ().getSourceRoots () 102 ); 103 originalSourcePath = ClassPathSupport.createClassPath ( 104 (FileObject[]) allSourceRoots.toArray 105 (new FileObject [allSourceRoots.size()]) 106 ); 107 108 JavaPlatform[] platforms = JavaPlatformManager.getDefault (). 109 getInstalledPlatforms (); 110 int i, k = platforms.length; 111 for (i = 0; i < k; i++) { 112 FileObject[] roots = platforms [i].getSourceFolders (). 113 getRoots (); 114 int j, jj = roots.length; 115 for (j = 0; j < jj; j++) 116 allSourceRoots.remove (roots [j]); 117 } 118 smartSteppingSourcePath = ClassPathSupport.createClassPath ( 119 (FileObject[]) allSourceRoots.toArray 120 (new FileObject [allSourceRoots.size()]) 121 ); 122 } 123 sourceRoots = getRoots ( 124 Arrays.asList (smartSteppingSourcePath.getRoots ()).iterator () 125 ); 126 127 if (verbose) 128 System.out.println 129 ("SPPI: init originalSourcePath " + originalSourcePath); 130 if (verbose) 131 System.out.println ( 132 "SPPI: init smartSteppingSourcePath " + smartSteppingSourcePath 133 ); 134 } 135 136 145 public String getURL (String relativePath, boolean global) { if (verbose) System.out.println ("SPPI: getURL " + relativePath + " global " + global); 146 FileObject fo = null; 147 relativePath = normalize(relativePath); 148 if (!global) { 149 fo = smartSteppingSourcePath.findResource 150 (relativePath); if (verbose) System.out.println ("SPPI: fo " + fo); 151 } else { 152 fo = originalSourcePath.findResource 153 (relativePath); if (verbose) System.out.println ("SPPI: fo " + fo); 154 if (fo == null) 155 fo = GlobalPathRegistry.getDefault ().findResource (relativePath); if (verbose) System.out.println ("SPPI: fo2 " + fo); 156 } 157 if (fo == null) return null; 158 try { 159 return fo.getURL ().toString (); 160 } catch (FileStateInvalidException e) { if (verbose) System.out.println ("SPPI: FileStateInvalidException"); 161 return null; 162 } 163 } 164 165 175 public String getRelativePath ( 176 String url, 177 char directorySeparator, 178 boolean includeExtension 179 ) { 180 FileObject fo = null; if (verbose) System.out.println ("SPPI: getRelativePath " + url); 182 try { 183 fo = URLMapper.findFileObject (new URL (url)); if (verbose) System.out.println ("SPPI: fo " + fo); 184 } catch (MalformedURLException e) { 185 return null; 187 } 188 ClassPath cp = ClassPath.getClassPath (fo, ClassPath.SOURCE); 189 if (cp == null) 190 cp = ClassPath.getClassPath (fo, ClassPath.COMPILE); 191 if (cp == null) return null; 192 return cp.getResourceName ( 193 fo, 194 directorySeparator, 195 includeExtension 196 ); 197 } 198 199 206 public String getSourceRoot(String url) { 207 Iterator it = GlobalPathRegistry.getDefault().getSourceRoots().iterator(); 208 while (it.hasNext()) { 209 FileObject fileObject = (FileObject) it.next (); 210 try { 211 String rootURL = fileObject.getURL().toString(); 212 if (url.startsWith(rootURL)) { 213 File f = null; 214 if (fileObject.getFileSystem () instanceof JarFileSystem) 215 f = ((JarFileSystem) fileObject.getFileSystem ()). 216 getJarFile (); 217 else 218 f = FileUtil.toFile (fileObject); 219 if (f != null) { 220 return f.getAbsolutePath (); 221 } 222 } 223 } catch (FileStateInvalidException ex) { 224 } 226 } 227 return null; } 229 230 235 public String [] getOriginalSourceRoots () { 236 return getRoots (GlobalPathRegistry.getDefault ().getSourceRoots (). 237 iterator ()); 238 } 239 240 245 public String [] getSourceRoots () { 246 return sourceRoots; 247 } 248 249 254 public void setSourceRoots (String [] sourceRoots) { 255 if (logger.isLoggable(Level.FINE)) { 256 logger.fine("SourcePathProviderImpl.setSourceRoots("+java.util.Arrays.asList(sourceRoots)+")"); 257 } 258 int i, k = sourceRoots.length; 259 FileObject[] fos = new FileObject [k]; 260 for (i = 0; i < k; i++) 261 fos [i] = getFileObject (sourceRoots [i]); 262 Object old = smartSteppingSourcePath; 263 smartSteppingSourcePath = ClassPathSupport.createClassPath (fos); 264 this.sourceRoots = sourceRoots; 265 pcs.firePropertyChange ( 266 PROP_SOURCE_ROOTS, old, smartSteppingSourcePath 267 ); 268 } 269 270 275 public void addPropertyChangeListener (PropertyChangeListener l) { 276 pcs.addPropertyChangeListener (l); 277 } 278 279 284 public void removePropertyChangeListener ( 285 PropertyChangeListener l 286 ) { 287 pcs.removePropertyChangeListener (l); 288 } 289 290 291 293 302 private static String normalize(String path) { 303 for (Matcher m = thisDirectoryPattern.matcher(path); m.find(); ) 304 { 305 path = m.replaceAll("$1"); 306 m = thisDirectoryPattern.matcher(path); 307 } 308 for (Matcher m = parentDirectoryPattern.matcher(path); m.find(); ) 309 { 310 if (!m.group(2).equals("..")) { 311 path = path.substring(0, m.start()) + m.group(1) + path.substring(m.end()); 312 m = parentDirectoryPattern.matcher(path); 313 } 314 } 315 return path; 316 } 317 318 321 private static String [] getRoots (Iterator it) { 322 Set roots = new TreeSet (); 323 while (it.hasNext ()) { 324 FileObject fileObject = (FileObject) it.next (); 325 File f = null; 326 try { 327 if (fileObject.getFileSystem () instanceof JarFileSystem) 328 f = ((JarFileSystem) fileObject.getFileSystem ()). 329 getJarFile (); 330 else 331 f = FileUtil.toFile (fileObject); 332 } catch (FileStateInvalidException ex) { 333 } 334 if (f != null) 335 roots.add (f.getAbsolutePath ()); 336 } 337 String [] fs = new String [roots.size ()]; 338 return (String []) roots.toArray (fs); 339 } 340 341 344 private FileObject getFileObject (String file) { 345 File f = new File (file); 346 FileObject fo = FileUtil.toFileObject (f); 347 if (FileUtil.isArchiveFile (fo)) 348 fo = FileUtil.getArchiveRoot (fo); 349 return fo; 350 } 351 } 352 | Popular Tags |