/home/gshute/netbeans/Toolkit/src/vfs/VFSRoot.java |
package vfs;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
@author
public class VFSRoot {
private VFSDirectory root;
private URL getResourceURL(Class c, String nm)
throws IOException {
String className = c.getSimpleName();
URL classURL = c.getResource(className + ".class");
if (classURL == null) {
String message = "Resource " + className +
".class not found";
throw new VFSError(message, null);
}
String qualifiedClassName = c.getName();
qualifiedClassName = qualifiedClassName.replace('.', '/');
String classURLString = classURL.toString();
int packageNamePosition =
classURLString.lastIndexOf(qualifiedClassName);
String baseURLString =
classURLString.substring(0, packageNamePosition);
return new URL(baseURLString + nm + "/");
}
private URL getResourceURL(Class c)
throws IOException {
String className = c.getSimpleName();
URL classURL = c.getResource(className + ".class");
if (classURL == null) {
String message = "Resource " + className +
".class not found";
throw new VFSError(message, null);
}
String qualifiedClassName = c.getName();
qualifiedClassName = qualifiedClassName.replace('.', '/');
String classURLString = classURL.toString();
int classNamePosition =
classURLString.lastIndexOf(className);
String baseURLString =
classURLString.substring(0, classNamePosition);
return new URL(baseURLString);
}
@param
@param
public VFSRoot(Class c, String nm) {
root = null;
try {
URL url = getResourceURL(c, nm);
String protocol = url.getProtocol();
if (protocol.equals("jar")) {
root = new JarDirectory(url);
} else if (protocol.equals("file")) {
root = new NativeDirectory(url);
} else {
throw new IllegalArgumentException("Illegal protocol");
}
} catch (IOException ex) {
throw new VFSError(ex);
}
}
@param
public VFSRoot(Class c) {
root = null;
try {
URL url = getResourceURL(c);
String protocol = url.getProtocol();
if (protocol.equals("jar")) {
root = new JarDirectory(url);
} else if (protocol.equals("file")) {
root = new NativeDirectory(url);
} else {
throw new IllegalArgumentException("Illegal protocol");
}
} catch (IOException ex) {
throw new VFSError(ex);
}
}
public VFSDirectory getRootDirectory() {
return root;
}
public void escort(VFSVisitor v) {
v.start();
Iterator<VFSDirectory> subIterator = root.getSubdirectories();
while (subIterator.hasNext()) {
subIterator.next().escort(v);
}
Iterator<VFSFile> fileIterator = root.getFiles();
while (fileIterator.hasNext()) {
v.visit(fileIterator.next());
}
v.finish();
}
}