KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > io > dir > FileDirectory


1 package com.quadcap.io.dir;
2
3 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileFilter JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 import java.util.Enumeration JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 import java.net.MalformedURLException JavaDoc;
49 import java.net.URL JavaDoc;
50
51 /**
52  *
53  *
54  * @author Stan Bailes
55  */

56 public class FileDirectory extends Directory {
57     URL JavaDoc base;
58     File JavaDoc root;
59     Vector JavaDoc entries = null;
60     
61     public FileDirectory(File JavaDoc root) throws IOException JavaDoc {
62         this.root = root;
63         this.base = new URL JavaDoc("file", "", root.getCanonicalPath());
64     }
65
66     final void getEntries() {
67         entries = new Vector JavaDoc();
68         root.listFiles(makeFilter(null));
69     }
70
71     FileFilter JavaDoc makeFilter(final String JavaDoc path) {
72         return new FileFilter JavaDoc() {
73             public boolean accept(File JavaDoc f) {
74                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc entries() {
91         getEntries();
92         return entries.elements();
93     }
94
95     public Entry getEntry(String JavaDoc name) {
96         FileEntry fe = null;
97         if (name.length() > 0 && name.charAt(0) == '/') {
98             name = name.substring(1);
99         }
100         File JavaDoc f = new File JavaDoc(root, name);
101         if (f.exists()) {
102             fe = new FileEntry(f, name);
103         }
104         return fe;
105     }
106
107     public URL JavaDoc getURL(String JavaDoc name) throws MalformedURLException JavaDoc {
108         URL JavaDoc url = null;
109         if (name.length() > 0 && name.charAt(0) == '/') {
110             name = name.substring(1);
111         }
112         File JavaDoc f = new File JavaDoc(root, name);
113         if (f.exists()) {
114             try {
115                 url = new URL JavaDoc("file", "", f.getCanonicalPath());
116             } catch (IOException JavaDoc e) {
117                 throw new MalformedURLException JavaDoc(e.toString());
118             }
119         }
120         return url;
121     }
122
123     public static String JavaDoc safePath(String JavaDoc path) {
124         String JavaDoc 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 JavaDoc getRealPath(String JavaDoc name) {
169         String JavaDoc ret = null;
170         String JavaDoc path = safePath(name);
171         if (path != null) {
172             File JavaDoc f = new File JavaDoc(root, path);
173             ret = f.getAbsolutePath();
174         }
175         return ret;
176     }
177
178     public String JavaDoc getRootPath() {
179         return root.getAbsolutePath();
180     }
181
182     public void close() {}
183
184     public boolean isFile() { return true; }
185 }
186
Popular Tags