package net.sourceforge.javautil.classloader.source;
import java.io.File;
import java.net.URL;
import net.sourceforge.javautil.common.ArchiveUtil;
import net.sourceforge.javautil.common.io.IVirtualArtifact;
import net.sourceforge.javautil.common.io.VirtualArtifactSystem;
import net.sourceforge.javautil.common.io.VirtualArtifactWrapped;
import net.sourceforge.javautil.common.io.IVirtualDirectory;
import net.sourceforge.javautil.common.io.impl.DirectoryInternal;
import net.sourceforge.javautil.common.io.impl.ISystemArtifact;
import net.sourceforge.javautil.common.io.impl.SystemDirectory;
import net.sourceforge.javautil.common.io.impl.SystemFile;
import net.sourceforge.javautil.common.io.impl.ZippedDirectory;
/**
* Utilities in relation to {@link ClassSource}'s.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class ClassSourceUtil {
/**
* @param url The URL in question
* @return An appropriate class source for this URL, or null if the URL cannot be translated into a class source
*/
public static ClassSource getFor (URL url) {
IVirtualArtifact va = VirtualArtifactSystem.getArtifactFor(url);
if (va == null) {
if (url.getProtocol().equals("jar")) {
String path = url.getPath();
if (path.startsWith("file://")) path = path.substring(7);
va = new ZippedDirectory(new SystemFile(path));
} else if (url.getProtocol().equals("file")) {
String path = url.getPath();
File target = new File(path);
if (target.isDirectory()) {
va = new SystemDirectory(target);
} else {
if (ArchiveUtil.isArchive(target)) {
va = new ZippedDirectory(new SystemFile(target));
}
}
}
} else {
if (va instanceof VirtualArtifactWrapped) {
va = ((VirtualArtifactWrapped)va).unwrap(ISystemArtifact.class);
if (va instanceof SystemFile && ArchiveUtil.isArchive( (SystemFile) va )) {
va = new ZippedDirectory( (SystemFile) va );
}
}
}
if (!(va instanceof IVirtualDirectory)) return null;
return new VirtualDirectoryClassSource( (IVirtualDirectory) va );
}
}