001: package com.quadcap.io.dir;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.File;
042: import java.io.FileFilter;
043: import java.io.IOException;
044:
045: import java.util.Enumeration;
046: import java.util.Vector;
047:
048: import java.net.MalformedURLException;
049: import java.net.URL;
050:
051: /**
052: *
053: *
054: * @author Stan Bailes
055: */
056: public class FileDirectory extends Directory {
057: URL base;
058: File root;
059: Vector entries = null;
060:
061: public FileDirectory(File root) throws IOException {
062: this .root = root;
063: this .base = new URL("file", "", root.getCanonicalPath());
064: }
065:
066: final void getEntries() {
067: entries = new Vector();
068: root.listFiles(makeFilter(null));
069: }
070:
071: FileFilter makeFilter(final String path) {
072: return new FileFilter() {
073: public boolean accept(File f) {
074: StringBuffer sb = new StringBuffer();
075: if (path != null) {
076: sb.append(path);
077: sb.append('/');
078: }
079: sb.append(f.getName());
080: if (f.isDirectory()) {
081: f.listFiles(makeFilter(sb.toString()));
082: } else {
083: entries.addElement(sb.toString());
084: }
085: return false;
086: }
087: };
088: }
089:
090: public Enumeration entries() {
091: getEntries();
092: return entries.elements();
093: }
094:
095: public Entry getEntry(String name) {
096: FileEntry fe = null;
097: if (name.length() > 0 && name.charAt(0) == '/') {
098: name = name.substring(1);
099: }
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)
134: state = 0;
135: char c = path.charAt(i);
136: if (c == '\\')
137: c = '/';
138: switch (state) {
139: case 0:
140: if (c == '.')
141: state = 10;
142: else if (c == '/')
143: state = 0;
144: else
145: state = 1;
146: break;
147: case 1:
148: if (c == '/')
149: state = 0;
150: break;
151: case 10:
152: if (c == '.')
153: state = 11;
154: else if (c == '/')
155: state = 0;
156: else
157: state = 1;
158: break;
159: case 11:
160: if (c == '/') {
161: if (prev < 0)
162: return null;
163: int cut = i - prev + 1;
164: path = path.substring(0, prev)
165: + path.substring(i + 1);
166: i += cut;
167: state = 0;
168: last = prev;
169: prev = -1;
170: } else {
171: state = 1;
172: }
173: break;
174: }
175: }
176: return path;
177: }
178:
179: public String getRealPath(String name) {
180: String ret = null;
181: String path = safePath(name);
182: if (path != null) {
183: File f = new File(root, path);
184: ret = f.getAbsolutePath();
185: }
186: return ret;
187: }
188:
189: public String getRootPath() {
190: return root.getAbsolutePath();
191: }
192:
193: public void close() {
194: }
195:
196: public boolean isFile() {
197: return true;
198: }
199: }
|