KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uni_hamburg > eggink > autojar > FilePath


1 package de.uni_hamburg.eggink.autojar;
2
3 import java.util.*;
4 import java.util.zip.*;
5 import java.io.*;
6
7 /** Class representing a lookup path for non-class files.
8  * Essentially a simplified ClassPath.
9  * @author Bernd Eggink, RRZ Uni Hamburg (Bernd.Eggink@rrz.uni-hamburg.de)
10  */

11   
12 class FilePath
13 {
14     private PathEntry[] paths;
15     private int nPaths;
16
17     //----------------------------------------------------------------------
18

19     /** A regular file in a directory */
20
21     private class RegularNormalFile
22         implements NormalFile
23     {
24         File file;
25
26         //----------------------------------------------------------------------
27

28         RegularNormalFile(File file)
29         {
30             this.file = file;
31         }
32
33         //----------------------------------------------------------------------
34

35         public InputStream getInputStream()
36             throws IOException
37         {
38             return new FileInputStream(file);
39         }
40
41         //----------------------------------------------------------------------
42

43         public String JavaDoc getBase()
44         {
45             return file.getParent();
46         }
47
48         //----------------------------------------------------------------------
49

50         public String JavaDoc getPath()
51         {
52             return file.getPath();
53         }
54     }
55
56     //----------------------------------------------------------------------
57

58     /** An entry in a Zip or Jar file */
59
60     private class ZipNormalFile
61         implements NormalFile
62     {
63         ZipFile file;
64         ZipEntry entry;
65
66         //----------------------------------------------------------------------
67

68         ZipNormalFile(ZipFile file, ZipEntry entry)
69         {
70             this.file = file;
71             this.entry = entry;
72         }
73
74         //----------------------------------------------------------------------
75

76         public InputStream getInputStream()
77             throws IOException
78         {
79             return file.getInputStream(entry);
80         }
81
82         //----------------------------------------------------------------------
83

84         public String JavaDoc getBase()
85         {
86             return file.getName();
87         }
88
89         //----------------------------------------------------------------------
90

91         public String JavaDoc getPath()
92         {
93             return entry.getName();
94         }
95     }
96
97     //----------------------------------------------------------------------
98

99     /** Path component interface. May be a directory or zip file. */
100
101     private interface PathEntry
102     {
103         /** Lookup a file in this entry (dir or zip file) */
104
105         NormalFile getFile(String JavaDoc name) throws IOException;
106     }
107
108     //----------------------------------------------------------------------
109

110     /** Directory as Path Entry */
111     
112     private class Dir
113         implements PathEntry
114     {
115         private String JavaDoc dir;
116
117         //----------------------------------------------------------------------
118

119         Dir(String JavaDoc dir)
120         {
121             this.dir = dir;
122         }
123
124         //----------------------------------------------------------------------
125

126         /** Look for file in directory */
127         
128         public NormalFile getFile(String JavaDoc name)
129             throws IOException
130         {
131             File file = new File(dir, name);
132
133             if (file.exists())
134                 return new RegularNormalFile(file);
135
136             return null;
137         }
138     }
139
140     //----------------------------------------------------------------------
141

142     /** Zip file as path entry */
143
144     private class Zip
145         implements PathEntry
146     {
147         private ZipFile zipFile;
148
149         //----------------------------------------------------------------------
150

151         Zip(ZipFile zipFile)
152         {
153             this.zipFile = zipFile;
154         }
155  
156         //----------------------------------------------------------------------
157

158         /** Look for correspoding zip entry */
159
160         public NormalFile getFile(String JavaDoc name)
161         {
162             ZipEntry entry = zipFile.getEntry(name);
163
164             if (entry != null)
165                 return new ZipNormalFile(zipFile, entry);
166
167             return null;
168         }
169     }
170     
171     //----------------------------------------------------------------------
172

173     /** Set up file path from an external path string like a:b:z.zip */
174     
175     FilePath(String JavaDoc filepath)
176     {
177         StringTokenizer tok = new StringTokenizer(filepath, System.getProperty("path.separator"));
178         int n = 0;
179         
180         paths = new PathEntry[tok.countTokens()];
181
182         while (tok.hasMoreTokens())
183         {
184             String JavaDoc path = tok.nextToken();
185
186             if (! path.equals(""))
187             {
188                 File file = new File(path);
189
190                 // next file, may be directory, .jar or .zip
191

192                 try
193                 {
194                     if (file.exists())
195                     {
196                         if (file.isDirectory())
197                             paths[nPaths] = new Dir(path);
198                         else
199                             paths[nPaths] = new Zip(new ZipFile(file));
200                     }
201
202                     ++nPaths;
203                 }
204                 catch (IOException ex)
205                 {
206                     System.err.println("FILEPATH component " + file + ": e");
207                 }
208             }
209         }
210     }
211
212     //----------------------------------------------------------------------
213

214     /** Scan file path components for filename */
215
216     NormalFile getFile(String JavaDoc name)
217         throws IOException
218     {
219         NormalFile file;
220         
221         if (nPaths > 0)
222             for (int i = 0; i < nPaths; ++i)
223                 if ((file = paths[i].getFile(name)) != null)
224                     return file;
225
226         throw new IOException("Couldn't find: " + name);
227     }
228     
229 }
230
231
Popular Tags