KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxxload_help > PathVFS


1 // Copyright 2000 Samuele Pedroni
2

3 package jxxload_help;
4
5 import java.util.Vector JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.zip.ZipFile JavaDoc;
9 import java.util.zip.ZipEntry JavaDoc;
10 import java.io.*;
11
12 public class PathVFS extends Object JavaDoc {
13     
14     public interface VFS {
15         
16        public InputStream open(String JavaDoc id);
17         
18     }
19         
20     public static class JarVFS implements VFS {
21         private ZipFile JavaDoc zipfile;
22         
23         public JarVFS(String JavaDoc fname) throws IOException {
24             zipfile = new ZipFile JavaDoc(fname);
25         }
26         
27         public InputStream open(String JavaDoc id) {
28             ZipEntry JavaDoc ent = zipfile.getEntry(id);
29             if (ent == null) return null;
30             try {
31                 return zipfile.getInputStream(ent);
32             } catch(IOException e) {
33                 return null;
34             }
35         }
36         
37     }
38     
39     public static class DirVFS implements VFS {
40         private String JavaDoc prefix;
41         
42         public DirVFS(String JavaDoc dir) {
43             if (dir.length() == 0)
44                 prefix = null;
45             else
46                 prefix = dir;
47         }
48         
49         public InputStream open(String JavaDoc id) {
50             File JavaDoc file = new File JavaDoc(prefix,id.replace('/',File.separatorChar));
51             if (file.isFile()) {
52                 try {
53                     return new BufferedInputStream(new FileInputStream(file));
54                 } catch(IOException e) {
55                     return null;
56                 }
57             }
58             return null;
59         }
60     }
61     
62     private Vector JavaDoc vfs = new Vector JavaDoc();
63     private Hashtable JavaDoc once = new Hashtable JavaDoc();
64     
65     private final static Object JavaDoc PRESENT = new Object JavaDoc();
66     
67     public void addVFS(String JavaDoc fname) {
68         if (fname.length() == 0) {
69             if (!once.containsKey("")) {
70                 once.put("",PRESENT);
71                 vfs.addElement(new DirVFS(""));
72             }
73             return;
74         }
75         try {
76             File JavaDoc file = new File JavaDoc(fname);
77             String JavaDoc canon = file.getCanonicalPath().toString();
78             if (!once.containsKey(canon)) {
79                 once.put(canon,PRESENT);
80                 if (file.isDirectory()) vfs.addElement(new DirVFS(fname));
81                 else if (file.exists() && (fname.endsWith(".jar") || fname.endsWith(".zip"))) {
82                     vfs.addElement(new JarVFS(fname));
83                 }
84             }
85             
86         } catch(IOException e) {}
87     }
88     
89     public InputStream open(String JavaDoc id) {
90         for(Enumeration JavaDoc enumm = vfs.elements(); enumm.hasMoreElements();) {
91             VFS v = (VFS)enumm.nextElement();
92             InputStream stream = v.open(id);
93             if (stream != null) return stream;
94         }
95         return null;
96     }
97       
98 }
99
Popular Tags