1 package com.quadcap.io.dir; 2 3 40 41 import java.io.File ; 42 import java.io.FileFilter ; 43 import java.io.IOException ; 44 45 import java.util.Enumeration ; 46 import java.util.Vector ; 47 48 import java.net.MalformedURLException ; 49 import java.net.URL ; 50 51 56 public class FileDirectory extends Directory { 57 URL base; 58 File root; 59 Vector entries = null; 60 61 public FileDirectory(File root) throws IOException { 62 this.root = root; 63 this.base = new URL ("file", "", root.getCanonicalPath()); 64 } 65 66 final void getEntries() { 67 entries = new Vector (); 68 root.listFiles(makeFilter(null)); 69 } 70 71 FileFilter makeFilter(final String path) { 72 return new FileFilter () { 73 public boolean accept(File f) { 74 StringBuffer sb = new StringBuffer (); 75 if (path != null) { 76 sb.append(path); 77 sb.append('/'); 78 } 79 sb.append(f.getName()); 80 if (f.isDirectory()) { 81 f.listFiles(makeFilter(sb.toString())); 82 } else { 83 entries.addElement(sb.toString()); 84 } 85 return false; 86 } 87 }; 88 } 89 90 public Enumeration entries() { 91 getEntries(); 92 return entries.elements(); 93 } 94 95 public Entry getEntry(String name) { 96 FileEntry fe = null; 97 if (name.length() > 0 && name.charAt(0) == '/') { 98 name = name.substring(1); 99 } 100 File f = new File (root, name); 101 if (f.exists()) { 102 fe = new FileEntry(f, name); 103 } 104 return fe; 105 } 106 107 public URL getURL(String name) throws MalformedURLException { 108 URL url = null; 109 if (name.length() > 0 && name.charAt(0) == '/') { 110 name = name.substring(1); 111 } 112 File f = new File (root, name); 113 if (f.exists()) { 114 try { 115 url = new URL ("file", "", f.getCanonicalPath()); 116 } catch (IOException e) { 117 throw new MalformedURLException (e.toString()); 118 } 119 } 120 return url; 121 } 122 123 public static String safePath(String path) { 124 String opath = path; 125 int state = -1; 126 int last = -1; 127 int prev = -1; 128 for (int i = 0; i < path.length(); i++) { 129 if (state == 0) { 130 prev = last; 131 last = i; 132 } 133 if (state == -1) state = 0; 134 char c = path.charAt(i); 135 if (c == '\\') c = '/'; 136 switch (state) { 137 case 0: 138 if (c == '.') state = 10; 139 else if (c == '/') state = 0; 140 else state = 1; 141 break; 142 case 1: 143 if (c == '/') state = 0; 144 break; 145 case 10: 146 if (c == '.') state = 11; 147 else if (c == '/') state = 0; 148 else state = 1; 149 break; 150 case 11: 151 if (c == '/') { 152 if (prev < 0) return null; 153 int cut = i - prev + 1; 154 path = path.substring(0, prev) + path.substring(i+1); 155 i += cut; 156 state = 0; 157 last = prev; 158 prev = -1; 159 } else { 160 state = 1; 161 } 162 break; 163 } 164 } 165 return path; 166 } 167 168 public String getRealPath(String name) { 169 String ret = null; 170 String path = safePath(name); 171 if (path != null) { 172 File f = new File (root, path); 173 ret = f.getAbsolutePath(); 174 } 175 return ret; 176 } 177 178 public String getRootPath() { 179 return root.getAbsolutePath(); 180 } 181 182 public void close() {} 183 184 public boolean isFile() { return true; } 185 } 186 | Popular Tags |