Quadcap Embeddable Server

com/quadcap/io/dir/FileDirectory.java

Go to the documentation of this file.
00001 package com.quadcap.io.dir; 00002 00003 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * @author Stan Bailes 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 }