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