1 package de.uni_hamburg.eggink.autojar; 2 3 import java.util.*; 4 import java.util.zip.*; 5 import java.io.*; 6 7 11 12 class FilePath 13 { 14 private PathEntry[] paths; 15 private int nPaths; 16 17 19 20 21 private class RegularNormalFile 22 implements NormalFile 23 { 24 File file; 25 26 28 RegularNormalFile(File file) 29 { 30 this.file = file; 31 } 32 33 35 public InputStream getInputStream() 36 throws IOException 37 { 38 return new FileInputStream(file); 39 } 40 41 43 public String getBase() 44 { 45 return file.getParent(); 46 } 47 48 50 public String getPath() 51 { 52 return file.getPath(); 53 } 54 } 55 56 58 59 60 private class ZipNormalFile 61 implements NormalFile 62 { 63 ZipFile file; 64 ZipEntry entry; 65 66 68 ZipNormalFile(ZipFile file, ZipEntry entry) 69 { 70 this.file = file; 71 this.entry = entry; 72 } 73 74 76 public InputStream getInputStream() 77 throws IOException 78 { 79 return file.getInputStream(entry); 80 } 81 82 84 public String getBase() 85 { 86 return file.getName(); 87 } 88 89 91 public String getPath() 92 { 93 return entry.getName(); 94 } 95 } 96 97 99 100 101 private interface PathEntry 102 { 103 104 105 NormalFile getFile(String name) throws IOException; 106 } 107 108 110 111 112 private class Dir 113 implements PathEntry 114 { 115 private String dir; 116 117 119 Dir(String dir) 120 { 121 this.dir = dir; 122 } 123 124 126 127 128 public NormalFile getFile(String 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 142 143 144 private class Zip 145 implements PathEntry 146 { 147 private ZipFile zipFile; 148 149 151 Zip(ZipFile zipFile) 152 { 153 this.zipFile = zipFile; 154 } 155 156 158 159 160 public NormalFile getFile(String 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 173 174 175 FilePath(String 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 path = tok.nextToken(); 185 186 if (! path.equals("")) 187 { 188 File file = new File(path); 189 190 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 214 215 216 NormalFile getFile(String 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 |