00001
package com.quadcap.io.dir;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.File;
00042
import java.io.FileFilter;
00043
import java.io.IOException;
00044
00045
import java.util.Enumeration;
00046
import java.util.Vector;
00047
00048
import java.net.MalformedURLException;
00049
import java.net.URL;
00050
00051
00052
00053
00054
00055
00056 public class FileDirectory extends Directory {
00057 URL
base;
00058 File
root;
00059 Vector
entries = null;
00060
00061 public FileDirectory(File root)
throws IOException {
00062
this.root =
root;
00063
this.base =
new URL(
"file",
"",
root.getCanonicalPath());
00064 }
00065
00066 final void getEntries() {
00067
entries =
new Vector();
00068
root.listFiles(
makeFilter(null));
00069 }
00070
00071 FileFilter
makeFilter(
final String path) {
00072
return new FileFilter() {
00073
public boolean accept(File f) {
00074 StringBuffer sb =
new StringBuffer();
00075
if (path != null) {
00076 sb.append(path);
00077 sb.append(
'/');
00078 }
00079 sb.append(f.getName());
00080
if (f.isDirectory()) {
00081 f.listFiles(makeFilter(sb.toString()));
00082 }
else {
00083
entries.addElement(sb.toString());
00084 }
00085
return false;
00086 }
00087 };
00088 }
00089
00090 public Enumeration
entries() {
00091
getEntries();
00092
return entries.elements();
00093 }
00094
00095 public Entry getEntry(String name) {
00096
FileEntry fe = null;
00097
if (name.length() > 0 && name.charAt(0) ==
'/') {
00098 name = name.substring(1);
00099 }
00100 File f =
new File(
root, name);
00101
if (f.exists()) {
00102 fe =
new FileEntry(f, name);
00103 }
00104
return fe;
00105 }
00106
00107 public URL
getURL(String name)
throws MalformedURLException {
00108 URL url = null;
00109
if (name.length() > 0 && name.charAt(0) ==
'/') {
00110 name = name.substring(1);
00111 }
00112 File f =
new File(
root, name);
00113
if (f.exists()) {
00114
try {
00115 url =
new URL(
"file",
"", f.getCanonicalPath());
00116 }
catch (IOException e) {
00117
throw new MalformedURLException(e.toString());
00118 }
00119 }
00120
return url;
00121 }
00122
00123 public static String
safePath(String path) {
00124 String opath = path;
00125
int state = -1;
00126
int last = -1;
00127
int prev = -1;
00128
for (
int i = 0; i < path.length(); i++) {
00129
if (state == 0) {
00130 prev = last;
00131 last = i;
00132 }
00133
if (state == -1) state = 0;
00134
char c = path.charAt(i);
00135
if (c ==
'\\') c =
'/';
00136
switch (state) {
00137
case 0:
00138
if (c ==
'.') state = 10;
00139
else if (c ==
'/') state = 0;
00140
else state = 1;
00141
break;
00142
case 1:
00143
if (c ==
'/') state = 0;
00144
break;
00145
case 10:
00146
if (c ==
'.') state = 11;
00147
else if (c ==
'/') state = 0;
00148
else state = 1;
00149
break;
00150
case 11:
00151
if (c ==
'/') {
00152
if (prev < 0)
return null;
00153
int cut = i - prev + 1;
00154 path = path.substring(0, prev) + path.substring(i+1);
00155 i += cut;
00156 state = 0;
00157 last = prev;
00158 prev = -1;
00159 }
else {
00160 state = 1;
00161 }
00162
break;
00163 }
00164 }
00165
return path;
00166 }
00167
00168 public String
getRealPath(String name) {
00169 String ret = null;
00170 String path = safePath(name);
00171
if (path != null) {
00172 File f =
new File(
root, path);
00173 ret = f.getAbsolutePath();
00174 }
00175
return ret;
00176 }
00177
00178 public String
getRootPath() {
00179
return root.getAbsolutePath();
00180 }
00181
00182 public void close() {}
00183
00184 public boolean isFile() {
return true; }
00185 }