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