|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.debugger.projects;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.netbeans.spi.debugger.jpda.SourcePathProvider;
import org.netbeans.api.debugger.Session;
import org.netbeans.spi.debugger.ContextProvider;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.netbeans.api.java.platform.JavaPlatform;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.JarFileSystem;
import org.openide.filesystems.Repository;
import org.openide.filesystems.URLMapper;
/**
*
* @author Jan Jancura
*/
public class SourcePathProviderImpl extends SourcePathProvider {
private static boolean verbose =
System.getProperty ("netbeans.debugger.enginecontextproviderimpl") != null;
private Session session;
private ClassPath sourcePath;
private String[] originalSourceRoots;
private PropertyChangeSupport pcs;
{pcs = new PropertyChangeSupport (this);}
public SourcePathProviderImpl (ContextProvider contextProvider) {
this.session = (Session) contextProvider.lookupFirst (null, Session.class);
sourcePath = (ClassPath) contextProvider.lookupFirst (null, ClassPath.class);
if (sourcePath == null) {
Set s = GlobalPathRegistry.getDefault ().getPaths
(ClassPath.SOURCE);
sourcePath = ClassPathSupport.createProxyClassPath (
(ClassPath[]) s.toArray (new ClassPath [s.size ()])
);
}
HashSet s = new HashSet (Arrays.asList (getRoots (sourcePath)));
s.addAll (Arrays.asList (getRoots (JavaPlatform.getDefault ().getSourceFolders ())));
originalSourceRoots = (String[]) s.toArray (new String [s.size ()]);
}
/**
* Translates a relative path (java/lang/Thread.java) to url.
*
* @param relativePath a relative path (java/lang/Thread.java)
* @return url
*/
public String getURL (String relativePath) {
if (verbose) System.out.println ("ECPI: getURL " + relativePath);
FileObject fo = sourcePath.findResource (relativePath);
if (verbose) System.out.println ("ECPI: fo " + fo);
if (fo == null) return null;
try {
return fo.getURL ().toString ();
} catch (FileStateInvalidException e) {
if (verbose) System.out.println ("ECPI: FileStateInvalidException");
return null;
}
}
/**
* Returns relative path for given url.
*
* @param url a url of resource file
* @param directorySeparator a directory separator character
* @param includeExtension whether the file extension should be included
* in the result
*
* @return relative path
*/
public String getRelativePath (
String url,
char directorySeparator,
boolean includeExtension
) {
if (verbose) System.out.println ("ECPI: getRelativePath " + url);
FileObject fo = null;
try {
fo = URLMapper.findFileObject (new URL (url));
if (verbose) System.out.println ("ECPI: fo " + fo);
} catch (MalformedURLException e) {
//e.printStackTrace ();
return null;
}
ClassPath cp = sourcePath;
if (cp == null)
cp = ClassPath.getClassPath (fo, ClassPath.SOURCE);
if (verbose) System.out.println ("ECPI: cp " + cp);
return cp.getResourceName (
fo,
directorySeparator,
includeExtension
);
}
/**
* Returns set of original source roots.
*
* @return set of original source roots
*/
public String[] getOriginalSourceRoots () {
return originalSourceRoots;
}
/**
* Returns array of source roots.
*
* @return array of source roots
*/
public String[] getSourceRoots () {
return getRoots (sourcePath);
}
/**
* Sets array of source roots.
*
* @param sourceRoots a new array of sourceRoots
*/
public void setSourceRoots (String[] sourceRoots) {
int i, k = sourceRoots.length;
FileObject[] fos = new FileObject [k];
for (i = 0; i < k; i++)
fos [i] = getFileObject (sourceRoots [i]);
Object old = sourcePath;
sourcePath = ClassPathSupport.createClassPath (fos);
pcs.firePropertyChange (PROP_SOURCE_ROOTS, old, sourcePath);
}
/**
* Adds property change listener.
*
* @param l new listener.
*/
public void addPropertyChangeListener (PropertyChangeListener l) {
pcs.addPropertyChangeListener (l);
}
/**
* Removes property change listener.
*
* @param l removed listener.
*/
public void removePropertyChangeListener (
PropertyChangeListener l
) {
pcs.removePropertyChangeListener (l);
}
private static String[] getRoots (ClassPath cp) {
FileObject[] fos = cp.getRoots ();
int i, k = fos.length;
List l = new ArrayList ();
for (i = 0; i < k; i++) {
File f = null;
try {
if (fos [i].getFileSystem () instanceof JarFileSystem)
f = ((JarFileSystem) fos [i].getFileSystem ()).
getJarFile ();
else
f = FileUtil.toFile (fos [i]);
} catch (FileStateInvalidException ex) {
}
if (f != null)
l.add (f.getAbsolutePath ());
}
String[] fs = new String [l.size ()];
return (String[]) l.toArray (fs);
}
private FileObject getFileObject (String file) {
File f = new File (file);
FileObject fo = FileUtil.toFileObject (f);
if ( file.endsWith (".jar") ||
file.endsWith (".zip"))
fo = FileUtil.getArchiveRoot (fo);
return fo;
}
}
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.