001: // Copyright 2000 Samuele Pedroni
002:
003: package jxxload_help;
004:
005: import java.util.Vector;
006: import java.util.Hashtable;
007: import java.util.Enumeration;
008: import java.util.zip.ZipFile;
009: import java.util.zip.ZipEntry;
010: import java.io.*;
011:
012: public class PathVFS extends Object {
013:
014: public interface VFS {
015:
016: public InputStream open(String id);
017:
018: }
019:
020: public static class JarVFS implements VFS {
021: private ZipFile zipfile;
022:
023: public JarVFS(String fname) throws IOException {
024: zipfile = new ZipFile(fname);
025: }
026:
027: public InputStream open(String id) {
028: ZipEntry ent = zipfile.getEntry(id);
029: if (ent == null)
030: return null;
031: try {
032: return zipfile.getInputStream(ent);
033: } catch (IOException e) {
034: return null;
035: }
036: }
037:
038: }
039:
040: public static class DirVFS implements VFS {
041: private String prefix;
042:
043: public DirVFS(String dir) {
044: if (dir.length() == 0)
045: prefix = null;
046: else
047: prefix = dir;
048: }
049:
050: public InputStream open(String id) {
051: File file = new File(prefix, id.replace('/',
052: File.separatorChar));
053: if (file.isFile()) {
054: try {
055: return new BufferedInputStream(new FileInputStream(
056: file));
057: } catch (IOException e) {
058: return null;
059: }
060: }
061: return null;
062: }
063: }
064:
065: private Vector vfs = new Vector();
066: private Hashtable once = new Hashtable();
067:
068: private final static Object PRESENT = new Object();
069:
070: public void addVFS(String fname) {
071: if (fname.length() == 0) {
072: if (!once.containsKey("")) {
073: once.put("", PRESENT);
074: vfs.addElement(new DirVFS(""));
075: }
076: return;
077: }
078: try {
079: File file = new File(fname);
080: String canon = file.getCanonicalPath().toString();
081: if (!once.containsKey(canon)) {
082: once.put(canon, PRESENT);
083: if (file.isDirectory())
084: vfs.addElement(new DirVFS(fname));
085: else if (file.exists()
086: && (fname.endsWith(".jar") || fname
087: .endsWith(".zip"))) {
088: vfs.addElement(new JarVFS(fname));
089: }
090: }
091:
092: } catch (IOException e) {
093: }
094: }
095:
096: public InputStream open(String id) {
097: for (Enumeration enumm = vfs.elements(); enumm
098: .hasMoreElements();) {
099: VFS v = (VFS) enumm.nextElement();
100: InputStream stream = v.open(id);
101: if (stream != null)
102: return stream;
103: }
104: return null;
105: }
106:
107: }
|