summaryrefslogtreecommitdiffstats
path: root/lib/jython/Lib/jxxload_help/PathVFS.java
blob: 0be51e3cd115485064b8874264f6a0c96e7845cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2000 Samuele Pedroni

package jxxload_help;

import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.*;

public class PathVFS extends Object {
    
    public interface VFS {
        
       public InputStream open(String id);
        
    }
        
    public static class JarVFS implements VFS {
        private ZipFile zipfile;
        
        public JarVFS(String fname) throws IOException {
            zipfile = new ZipFile(fname);
        }
        
        public InputStream open(String id) {
            ZipEntry ent = zipfile.getEntry(id);
            if (ent == null) return null;
            try {
                return zipfile.getInputStream(ent);
            } catch(IOException e) {
                return null;
            }
        }
        
    }
    
    public static class DirVFS implements VFS {
        private String prefix;
        
        public DirVFS(String dir) {
            if (dir.length() == 0)
                prefix = null;
            else
                prefix = dir;
        }
        
        public InputStream open(String id) {
            File file = new File(prefix,id.replace('/',File.separatorChar));
            if (file.isFile()) {
                try {
                    return new BufferedInputStream(new FileInputStream(file));
                } catch(IOException e) {
                    return null;
                }
            }           
            return null;
        }
    }
    
    private Vector vfs = new Vector();
    private Hashtable once = new Hashtable();
    
    private final static Object PRESENT = new Object();
    
    public void addVFS(String fname) {
        if (fname.length() == 0) {
            if (!once.containsKey("")) {
                once.put("",PRESENT);
                vfs.addElement(new DirVFS(""));
            }
            return;
        }
        try {
            File file = new File(fname);
            String canon = file.getCanonicalPath().toString();
            if (!once.containsKey(canon)) {
                once.put(canon,PRESENT);
                if (file.isDirectory()) vfs.addElement(new DirVFS(fname));
                else if (file.exists() &&  (fname.endsWith(".jar") || fname.endsWith(".zip"))) {
                    vfs.addElement(new JarVFS(fname));
                }
            }
            
        } catch(IOException e) {}
    }
    
    public InputStream open(String id) {
        for(Enumeration enum = vfs.elements(); enum.hasMoreElements();) {
            VFS v = (VFS)enum.nextElement();
            InputStream stream = v.open(id);
            if (stream != null) return stream;
        }
        return null;
    }
      
}