Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/DocuDirectory.java @ 86:997ba69afb81
New version 1.8b1.
With directory and file information cache.
With enhanceRGB method for color correction.
author | robcast |
---|---|
date | Sun, 09 Mar 2003 21:37:27 +0100 |
parents | |
children | a398fc09ba71 |
comparison
equal
deleted
inserted
replaced
85:4e6757e8ccd4 | 86:997ba69afb81 |
---|---|
1 /* DocuDirectory -- Directory of DocuFilesets. | |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 * Created on 25.02.2003 | |
20 */ | |
21 | |
22 package digilib.io; | |
23 | |
24 import java.io.File; | |
25 import java.util.Arrays; | |
26 import java.util.Collection; | |
27 import java.util.Hashtable; | |
28 import java.util.Vector; | |
29 | |
30 /** | |
31 * @author casties | |
32 */ | |
33 public class DocuDirectory extends Vector { | |
34 | |
35 // directory object is valid (has been read) | |
36 private boolean isValid = false; | |
37 // names of base directories | |
38 private String[] baseDirNames = null; | |
39 // directory name (digilib canonical form) | |
40 private String dirName = null; | |
41 // default/hires directory | |
42 private File dir = null; | |
43 // directory metadata | |
44 private Hashtable dirMeta = null; | |
45 // time of last access of this object (not the filesystem) | |
46 private long objectATime = 0; | |
47 // time the file system directory was last modified | |
48 private long dirMTime = 0; | |
49 | |
50 /* | |
51 * inherited stuff | |
52 */ | |
53 | |
54 public DocuDirectory(int initialCapacity, int capacityIncrement) { | |
55 super(initialCapacity, capacityIncrement); | |
56 } | |
57 | |
58 public DocuDirectory(int initialCapacity) { | |
59 super(initialCapacity); | |
60 } | |
61 | |
62 public DocuDirectory(Collection c) { | |
63 super(c); | |
64 } | |
65 | |
66 /* | |
67 * new stuff | |
68 */ | |
69 | |
70 /** Constructor with directory path and set of base directories. | |
71 * | |
72 * Reads the directory at the given path appended to the base directories. | |
73 * | |
74 * @see readDir | |
75 * | |
76 * @param path digilib directory path name | |
77 * @param bd array of base directory names | |
78 */ | |
79 public DocuDirectory(String path, String[] bd) { | |
80 super(); | |
81 dirName = path; | |
82 baseDirNames = bd; | |
83 readDir(); | |
84 } | |
85 | |
86 /** Read the directory and fill this object. | |
87 * | |
88 * Clears the Vector and (re)reads all files. | |
89 * | |
90 * @return boolean the directory exists | |
91 */ | |
92 public boolean readDir() { | |
93 // clear directory first | |
94 clear(); | |
95 isValid = false; | |
96 // number of base dirs | |
97 int nb = baseDirNames.length; | |
98 // array of base dirs | |
99 File[] dirs = new File[nb]; | |
100 // the first directory has to exist | |
101 dir = new File(baseDirNames[0] + dirName); | |
102 | |
103 if (dir.isDirectory()) { | |
104 // fill array with the remaining directories | |
105 for (int j = 1; j < nb; j++) { | |
106 File d = new File(baseDirNames[j] + dirName); | |
107 if (d.isDirectory()) { | |
108 dirs[j] = d; | |
109 } | |
110 } | |
111 | |
112 File[] fl = dir.listFiles(new FileOps.ImageFileFilter()); | |
113 if (fl == null) { | |
114 // not a directory | |
115 return false; | |
116 } | |
117 // number of image files | |
118 int nf = fl.length; | |
119 if (nf > 0) { | |
120 // resize Vector | |
121 this.ensureCapacity(nf); | |
122 | |
123 // sort the file names alphabetically and iterate the list | |
124 Arrays.sort(fl); | |
125 for (int i = 0; i < nf; i++) { | |
126 String fn = fl[i].getName(); | |
127 // add the first DocuFile to a new DocuFileset | |
128 DocuFileset fs = new DocuFileset(nb); | |
129 fs.add(new DocuFile(fl[i])); | |
130 // iterate the remaining base directories | |
131 for (int j = 1; j < nb; j++) { | |
132 if (dirs[j] == null) { | |
133 continue; | |
134 } | |
135 File f = new File(dirs[j], fn); | |
136 // if the file exists, add to the DocuFileset | |
137 if (f.canRead()) { | |
138 fs.add(new DocuFile(f)); | |
139 } | |
140 } | |
141 // add the fileset to our Vector | |
142 add(fs); | |
143 fs.setParent(this); | |
144 } | |
145 } | |
146 dirMTime = dir.lastModified(); | |
147 isValid = true; | |
148 } | |
149 return isValid; | |
150 | |
151 } | |
152 | |
153 /** Check to see if the directory has been modified and reread if necessary. | |
154 * | |
155 * @return boolean the directory is valid | |
156 */ | |
157 public boolean refresh() { | |
158 if (isValid) { | |
159 if (dir.lastModified() > dirMTime) { | |
160 // on-disk modification time is more recent | |
161 readDir(); | |
162 } | |
163 touch(); | |
164 } | |
165 return isValid; | |
166 } | |
167 | |
168 /** Read directory metadata. | |
169 * | |
170 */ | |
171 public void readMeta() { | |
172 // check for directory metadata... | |
173 } | |
174 | |
175 /** Update access time. | |
176 * | |
177 * @return long time of last access. | |
178 */ | |
179 public long touch() { | |
180 long t = objectATime; | |
181 objectATime = System.currentTimeMillis(); | |
182 return t; | |
183 } | |
184 | |
185 /** Searches for the file with the name <code>fn</code>. | |
186 * | |
187 * Searches the directory for the file with the name <code>fn</code> and returns | |
188 * its index. Returns -1 if the file cannot be found. | |
189 * | |
190 * @param fn filename | |
191 * @return int index of file <code>fn</code> | |
192 */ | |
193 public int indexOf(String fn) { | |
194 // linear search -> worst performance | |
195 for (int i = 0; i < elementCount; i++) { | |
196 DocuFileset fs = (DocuFileset) get(i); | |
197 if (fs.getName().equals(fn)) { | |
198 return i; | |
199 } | |
200 } | |
201 return -1; | |
202 } | |
203 | |
204 /** Finds the DocuFileset with the name <code>fn</code>. | |
205 * | |
206 * Searches the directory for the DocuFileset with the name <code>fn</code> and returns | |
207 * it. Returns null if the file cannot be found. | |
208 * | |
209 * @param fn filename | |
210 * @return DocuFileset | |
211 */ | |
212 public DocuFileset find(String fn) { | |
213 int i = indexOf(fn); | |
214 if (i >= 0) { | |
215 return (DocuFileset) get(i); | |
216 } | |
217 return null; | |
218 } | |
219 | |
220 /** | |
221 * @return String | |
222 */ | |
223 public String getDirName() { | |
224 return dirName; | |
225 } | |
226 | |
227 /** | |
228 * @return boolean | |
229 */ | |
230 public boolean isValid() { | |
231 return isValid; | |
232 } | |
233 | |
234 /** | |
235 * @return long | |
236 */ | |
237 public long getAccessTime() { | |
238 return objectATime; | |
239 } | |
240 | |
241 /** | |
242 * @return Hashtable | |
243 */ | |
244 public Hashtable getDirMeta() { | |
245 return dirMeta; | |
246 } | |
247 | |
248 /** | |
249 * @return long | |
250 */ | |
251 public long getDirMTime() { | |
252 return dirMTime; | |
253 } | |
254 | |
255 /** | |
256 * Sets the dirMeta. | |
257 * @param dirMeta The dirMeta to set | |
258 */ | |
259 public void setDirMeta(Hashtable dirMeta) { | |
260 this.dirMeta = dirMeta; | |
261 } | |
262 | |
263 } |