86
|
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;
|
130
|
25 import java.io.IOException;
|
91
|
26 import java.util.ArrayList;
|
86
|
27 import java.util.Arrays;
|
91
|
28 import java.util.HashMap;
|
|
29 import java.util.Iterator;
|
159
|
30 import java.util.List;
|
86
|
31
|
130
|
32 import org.xml.sax.SAXException;
|
|
33
|
86
|
34 /**
|
|
35 * @author casties
|
|
36 */
|
149
|
37 public class DocuDirectory extends Directory {
|
86
|
38
|
159
|
39 // list of files (DocuDirent)
|
|
40 private ArrayList[] list = null;
|
|
41 // directory object is valid (exists on disk)
|
86
|
42 private boolean isValid = false;
|
151
|
43 // reference of the parent DocuDirCache
|
|
44 private DocuDirCache cache = null;
|
86
|
45 // directory name (digilib canonical form)
|
|
46 private String dirName = null;
|
|
47 // directory metadata
|
91
|
48 private HashMap dirMeta = null;
|
233
|
49 // state of metadata is valid
|
|
50 private boolean metaChecked = false;
|
151
|
51 // unresolved file metadata
|
|
52 private HashMap unresolvedFileMeta = null;
|
86
|
53 // time of last access of this object (not the filesystem)
|
|
54 private long objectATime = 0;
|
|
55 // time the file system directory was last modified
|
|
56 private long dirMTime = 0;
|
|
57
|
159
|
58 /** Constructor with digilib directory path and a parent DocuDirCache.
|
86
|
59 *
|
159
|
60 * Directory names at the given path are appended to the base directories
|
|
61 * from the cache. The directory is checked on disk and isValid is set.
|
86
|
62 *
|
|
63 * @see readDir
|
|
64 *
|
|
65 * @param path digilib directory path name
|
176
|
66 * @param cache parent DocuDirCache
|
86
|
67 */
|
151
|
68 public DocuDirectory(String path, DocuDirCache cache) {
|
|
69 this.dirName = path;
|
|
70 this.cache = cache;
|
159
|
71 initDir();
|
|
72 checkDir();
|
86
|
73 }
|
|
74
|
159
|
75 /** Sets and checks the dir object.
|
|
76 *
|
91
|
77 */
|
159
|
78 protected void initDir() {
|
|
79 String baseDirName = cache.getBaseDirNames()[0];
|
|
80 // clear directory first
|
|
81 list = new ArrayList[FileOps.NUM_CLASSES];
|
|
82 isValid = false;
|
|
83 dirMTime = 0;
|
|
84 // the first directory has to exist
|
|
85 dir = new File(baseDirName, dirName);
|
|
86 }
|
156
|
87
|
151
|
88 /** The digilib name of the parent directory.
|
|
89 *
|
|
90 * Returns null if there is no parent.
|
|
91 */
|
|
92 public String getParentDirName() {
|
|
93 String s = null;
|
|
94 int lastidx = dirName.lastIndexOf("/");
|
|
95 if (lastidx > 0) {
|
|
96 s = dirName.substring(0, lastidx);
|
|
97 }
|
|
98 return s;
|
|
99 }
|
91
|
100
|
151
|
101 /** number of DocuFiles in this directory.
|
|
102 *
|
|
103 */
|
91
|
104 public int size() {
|
159
|
105 return ((list != null)&&(list[0] != null)) ? list[0].size() : 0;
|
91
|
106 }
|
|
107
|
159
|
108 /** number of files of this class in this directory.
|
|
109 *
|
|
110 * @param fc fileClass
|
|
111 */
|
|
112 public int size(int fc) {
|
|
113 return ((list != null)&&(list[fc] != null)) ? list[fc].size() : 0;
|
|
114 }
|
|
115
|
|
116 /** Returns the ImageFile at the index.
|
151
|
117 *
|
|
118 * @param index
|
|
119 * @return
|
|
120 */
|
159
|
121 public ImageFileset get(int index) {
|
220
|
122 if ((list == null) || (list[0] == null) || (index >= list[0].size())) {
|
122
|
123 return null;
|
151
|
124 }
|
159
|
125 return (ImageFileset) list[0].get(index);
|
|
126 }
|
|
127
|
|
128 /** Returns the file of the class at the index.
|
|
129 *
|
|
130 * @param index
|
|
131 * @param fc fileClass
|
|
132 * @return
|
|
133 */
|
|
134 public DocuDirent get(int index, int fc) {
|
|
135 if ((list == null) || (list[fc] == null) || (index >= list[fc].size())) {
|
|
136 return null;
|
|
137 }
|
|
138 return (DocuDirent) list[fc].get(index);
|
|
139 }
|
|
140
|
|
141 /** Checks if the directory exists on the filesystem.
|
|
142 *
|
|
143 * Sets isValid.
|
|
144 *
|
|
145 * @return
|
|
146 */
|
|
147 public boolean checkDir() {
|
|
148 if (dir == null) {
|
|
149 initDir();
|
|
150 }
|
|
151 isValid = dir.isDirectory();
|
|
152 return isValid;
|
91
|
153 }
|
|
154
|
151
|
155 /** Read the filesystem directory and fill this object.
|
86
|
156 *
|
130
|
157 * Clears the List and (re)reads all files.
|
86
|
158 *
|
|
159 * @return boolean the directory exists
|
|
160 */
|
|
161 public boolean readDir() {
|
159
|
162 // list of base dirs from the parent cache
|
151
|
163 String[] baseDirNames = cache.getBaseDirNames();
|
91
|
164 // first file extension to try for scaled directories
|
159
|
165 String scalext = null;
|
86
|
166 // number of base dirs
|
|
167 int nb = baseDirNames.length;
|
|
168 // array of base dirs
|
149
|
169 Directory[] dirs = new Directory[nb];
|
159
|
170 // check directory first
|
|
171 checkDir();
|
|
172 if (!isValid) {
|
|
173 return false;
|
|
174 }
|
|
175 // first entry is this directory
|
|
176 dirs[0] = this;
|
|
177 // fill array with the remaining directories
|
|
178 for (int j = 1; j < nb; j++) {
|
|
179 File d = new File(baseDirNames[j], dirName);
|
|
180 if (d.isDirectory()) {
|
|
181 dirs[j] = new Directory(d);
|
|
182 }
|
|
183 }
|
86
|
184
|
159
|
185 // go through all file classes
|
|
186 for (int nc = 0; nc < FileOps.NUM_CLASSES; nc++) {
|
|
187 int fc = cache.getFileClasses()[nc];
|
|
188 File[] fl = dir.listFiles(FileOps.filterForClass(fc));
|
86
|
189 if (fl == null) {
|
|
190 // not a directory
|
|
191 return false;
|
|
192 }
|
159
|
193 // number of files in the directory
|
86
|
194 int nf = fl.length;
|
|
195 if (nf > 0) {
|
91
|
196 // create new list
|
159
|
197 list[fc] = new ArrayList(nf);
|
86
|
198 // sort the file names alphabetically and iterate the list
|
|
199 Arrays.sort(fl);
|
|
200 for (int i = 0; i < nf; i++) {
|
187
|
201 DocuDirent f = FileOps.fileForClass(fc, fl[i], dirs, scalext);
|
159
|
202 // add the file to our list
|
|
203 list[fc].add(f);
|
|
204 f.setParent(this);
|
86
|
205 }
|
|
206 }
|
|
207 }
|
159
|
208 dirMTime = dir.lastModified();
|
|
209 // read metadata as well
|
|
210 readMeta();
|
86
|
211 return isValid;
|
|
212 }
|
|
213
|
|
214 /** Check to see if the directory has been modified and reread if necessary.
|
|
215 *
|
|
216 * @return boolean the directory is valid
|
|
217 */
|
|
218 public boolean refresh() {
|
|
219 if (isValid) {
|
|
220 if (dir.lastModified() > dirMTime) {
|
|
221 // on-disk modification time is more recent
|
|
222 readDir();
|
|
223 }
|
|
224 touch();
|
|
225 }
|
|
226 return isValid;
|
|
227 }
|
|
228
|
|
229 /** Read directory metadata.
|
|
230 *
|
|
231 */
|
|
232 public void readMeta() {
|
|
233 // check for directory metadata...
|
130
|
234 File mf = new File(dir, "index.meta");
|
|
235 if (mf.canRead()) {
|
|
236 XMLMetaLoader ml = new XMLMetaLoader();
|
|
237 try {
|
|
238 // read directory meta file
|
|
239 HashMap fileMeta = ml.loadURL(mf.getAbsolutePath());
|
|
240 if (fileMeta == null) {
|
|
241 return;
|
|
242 }
|
|
243 // meta for the directory itself is in the "" bin
|
151
|
244 dirMeta = (HashMap) fileMeta.remove("");
|
|
245 // read meta for files in this directory
|
|
246 readFileMeta(fileMeta, null);
|
|
247 // is there meta for other files left?
|
130
|
248 if (fileMeta.size() > 0) {
|
151
|
249 unresolvedFileMeta = fileMeta;
|
130
|
250 }
|
|
251 } catch (SAXException e) {
|
181
|
252 if (cache != null) {
|
|
253 cache.logger.warn("error parsing index.meta", e);
|
|
254 }
|
130
|
255 } catch (IOException e) {
|
181
|
256 if (cache != null) {
|
|
257 cache.logger.warn("error reading index.meta", e);
|
|
258 }
|
130
|
259 }
|
151
|
260 }
|
|
261 readParentMeta();
|
233
|
262 metaChecked = true;
|
151
|
263 }
|
130
|
264
|
151
|
265 /** Read metadata from all known parent directories.
|
|
266 *
|
|
267 */
|
|
268 public void readParentMeta() {
|
|
269 // check the parent directories for additional file meta
|
|
270 Directory dd = parent;
|
156
|
271 String path = dir.getName();
|
151
|
272 while (dd != null) {
|
156
|
273 if (((DocuDirectory) dd).hasUnresolvedFileMeta()) {
|
|
274 readFileMeta(((DocuDirectory) dd).unresolvedFileMeta, path);
|
151
|
275 }
|
|
276 // prepend parent dir path
|
|
277 path = dd.dir.getName() + "/" + path;
|
|
278 // become next parent
|
|
279 dd = dd.parent;
|
|
280 }
|
|
281 }
|
|
282
|
|
283 /** Read metadata for the files in this directory.
|
|
284 *
|
|
285 * Takes a HashMap with meta-information, adding the relative path
|
|
286 * before the lookup.
|
|
287 *
|
|
288 * @param fileMeta
|
|
289 * @param relPath
|
159
|
290 * @param fc fileClass
|
151
|
291 */
|
159
|
292 protected void readFileMeta(HashMap fileMeta, String relPath) {
|
151
|
293 if (list == null) {
|
|
294 // there are no files
|
|
295 return;
|
|
296 }
|
|
297 String path = (relPath != null) ? (relPath + "/") : "";
|
159
|
298 // go through all file classes
|
|
299 for (int nc = 0; nc < list.length; nc++) {
|
|
300 int fc = cache.getFileClasses()[nc];
|
|
301 if (list[fc] == null) {
|
|
302 continue;
|
|
303 }
|
|
304 // iterate through the list of files in this directory
|
|
305 for (Iterator i = list[fc].iterator(); i.hasNext();) {
|
|
306 DocuDirent f = (DocuDirent) i.next();
|
|
307 // prepend path to the filename
|
|
308 String fn = path + f.getName();
|
|
309 // look up meta for this file and remove from dir
|
|
310 HashMap meta = (HashMap) fileMeta.remove(fn);
|
|
311 if (meta != null) {
|
|
312 // store meta in file
|
|
313 f.setFileMeta(meta);
|
|
314 }
|
|
315 }
|
|
316 }
|
|
317 }
|
|
318
|
|
319 protected void notifyChildMeta(HashMap childmeta) {
|
|
320 List children = cache.getChildren(this.dirName, true);
|
|
321 if (children.size() > 0) {
|
|
322 for (Iterator i = children.iterator(); i.hasNext();) {
|
|
323 // TODO: finish this!
|
|
324 //((DocuDirectory) i.next()).readFileMeta()
|
151
|
325 }
|
130
|
326 }
|
86
|
327 }
|
|
328
|
|
329 /** Update access time.
|
|
330 *
|
|
331 * @return long time of last access.
|
|
332 */
|
|
333 public long touch() {
|
|
334 long t = objectATime;
|
|
335 objectATime = System.currentTimeMillis();
|
|
336 return t;
|
|
337 }
|
|
338
|
|
339 /** Searches for the file with the name <code>fn</code>.
|
|
340 *
|
|
341 * Searches the directory for the file with the name <code>fn</code> and returns
|
|
342 * its index. Returns -1 if the file cannot be found.
|
|
343 *
|
|
344 * @param fn filename
|
159
|
345 * @param fc file class
|
86
|
346 * @return int index of file <code>fn</code>
|
|
347 */
|
|
348 public int indexOf(String fn) {
|
159
|
349 int fc = FileOps.classForFilename(fn);
|
|
350 return indexOf(fn, fc);
|
|
351 }
|
|
352
|
|
353 /** Searches for the file with the name <code>fn</code> and class fc.
|
|
354 *
|
|
355 * Searches the directory for the file with the name <code>fn</code> and returns
|
|
356 * its index. Returns -1 if the file cannot be found.
|
|
357 *
|
|
358 * @param fn filename
|
|
359 * @return int index of file <code>fn</code>
|
|
360 */
|
|
361 public int indexOf(String fn, int fc) {
|
176
|
362 if (!isRead()) {
|
|
363 // read directory now
|
|
364 if (!readDir()) {
|
|
365 return -1;
|
|
366 }
|
|
367 }
|
86
|
368 // linear search -> worst performance
|
159
|
369 int n = list[fc].size();
|
91
|
370 for (int i = 0; i < n; i++) {
|
187
|
371 DocuDirent fs = (DocuDirent) list[fc].get(i);
|
86
|
372 if (fs.getName().equals(fn)) {
|
176
|
373 // filename matches
|
|
374 return i;
|
|
375 }
|
|
376 }
|
|
377 // try again without extension
|
|
378 for (int i = 0; i < n; i++) {
|
187
|
379 DocuDirent fs = (DocuDirent) list[fc].get(i);
|
176
|
380 if (fs.getBasename().equals(FileOps.basename(fn))) {
|
|
381 // basename matches
|
86
|
382 return i;
|
|
383 }
|
|
384 }
|
|
385 return -1;
|
|
386 }
|
|
387
|
187
|
388 /** Finds the DocuDirent with the name <code>fn</code>.
|
86
|
389 *
|
187
|
390 * Searches the directory for the DocuDirent with the name <code>fn</code> and returns
|
86
|
391 * it. Returns null if the file cannot be found.
|
|
392 *
|
|
393 * @param fn filename
|
187
|
394 * @return DocuDirent
|
86
|
395 */
|
187
|
396 public DocuDirent find(String fn) {
|
159
|
397 int fc = FileOps.classForFilename(fn);
|
|
398 int i = indexOf(fn, fc);
|
86
|
399 if (i >= 0) {
|
187
|
400 return (DocuDirent) list[0].get(i);
|
159
|
401 }
|
|
402 return null;
|
|
403 }
|
|
404
|
187
|
405 /** Finds the DocuDirent with the name <code>fn</code> and class <code>fc</code>.
|
159
|
406 *
|
187
|
407 * Searches the directory for the DocuDirent with the name <code>fn</code> and returns
|
159
|
408 * it. Returns null if the file cannot be found.
|
|
409 *
|
|
410 * @param fn filename
|
187
|
411 * @return DocuDirent
|
159
|
412 */
|
187
|
413 public DocuDirent find(String fn, int fc) {
|
159
|
414 int i = indexOf(fn, fc);
|
|
415 if (i >= 0) {
|
187
|
416 return (DocuDirent) list[fc].get(i);
|
86
|
417 }
|
|
418 return null;
|
|
419 }
|
|
420
|
|
421 /**
|
|
422 * @return String
|
|
423 */
|
|
424 public String getDirName() {
|
|
425 return dirName;
|
|
426 }
|
|
427
|
159
|
428 /** The directory is valid (exists on disk).
|
|
429 *
|
86
|
430 * @return boolean
|
|
431 */
|
|
432 public boolean isValid() {
|
|
433 return isValid;
|
|
434 }
|
|
435
|
159
|
436 /** The directory has been read from disk.
|
|
437 *
|
|
438 * @return
|
|
439 */
|
|
440 public boolean isRead() {
|
|
441 return (dirMTime != 0);
|
|
442 }
|
|
443
|
86
|
444 /**
|
|
445 * @return long
|
|
446 */
|
|
447 public long getAccessTime() {
|
|
448 return objectATime;
|
|
449 }
|
|
450
|
|
451 /**
|
|
452 * @return Hashtable
|
|
453 */
|
91
|
454 public HashMap getDirMeta() {
|
86
|
455 return dirMeta;
|
|
456 }
|
233
|
457
|
|
458 /**
|
|
459 * Checks metadata
|
|
460 *
|
|
461 */
|
|
462 public void checkMeta() {
|
|
463 if (metaChecked) {
|
|
464 return;
|
|
465 } else {
|
|
466 readMeta();
|
|
467 }
|
|
468 }
|
86
|
469
|
|
470 /**
|
|
471 * @return long
|
|
472 */
|
|
473 public long getDirMTime() {
|
|
474 return dirMTime;
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * Sets the dirMeta.
|
|
479 * @param dirMeta The dirMeta to set
|
|
480 */
|
91
|
481 public void setDirMeta(HashMap dirMeta) {
|
86
|
482 this.dirMeta = dirMeta;
|
|
483 }
|
|
484
|
151
|
485 public boolean hasUnresolvedFileMeta() {
|
|
486 return (this.unresolvedFileMeta != null);
|
|
487 }
|
|
488
|
86
|
489 }
|