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.
|
|
60 * If read is true the directory is read and filled.
|
86
|
61 *
|
|
62 * @see readDir
|
|
63 *
|
|
64 * @param path digilib directory path name
|
|
65 * @param bd array of base directory names
|
159
|
66 * @param read the directory is read and filled
|
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) {
|
|
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++) {
|
159
|
201 DocuDirent f = null;
|
|
202 // what class of file do we have?
|
|
203 if (fc == FileOps.CLASS_IMAGE) {
|
|
204 // image file
|
|
205 f = new ImageFileset(dirs, fl[i], scalext);
|
|
206 } else if (fc == FileOps.CLASS_TEXT) {
|
|
207 // text file
|
|
208 f = new TextFile(fl[i]);
|
86
|
209 }
|
159
|
210 // add the file to our list
|
|
211 list[fc].add(f);
|
|
212 f.setParent(this);
|
86
|
213 }
|
|
214 }
|
|
215 }
|
159
|
216 dirMTime = dir.lastModified();
|
|
217 // read metadata as well
|
|
218 readMeta();
|
86
|
219 return isValid;
|
|
220 }
|
|
221
|
|
222 /** Check to see if the directory has been modified and reread if necessary.
|
|
223 *
|
|
224 * @return boolean the directory is valid
|
|
225 */
|
|
226 public boolean refresh() {
|
|
227 if (isValid) {
|
|
228 if (dir.lastModified() > dirMTime) {
|
|
229 // on-disk modification time is more recent
|
|
230 readDir();
|
|
231 }
|
|
232 touch();
|
|
233 }
|
|
234 return isValid;
|
|
235 }
|
|
236
|
|
237 /** Read directory metadata.
|
|
238 *
|
|
239 */
|
|
240 public void readMeta() {
|
|
241 // check for directory metadata...
|
130
|
242 File mf = new File(dir, "index.meta");
|
|
243 if (mf.canRead()) {
|
|
244 XMLMetaLoader ml = new XMLMetaLoader();
|
|
245 try {
|
|
246 // read directory meta file
|
|
247 HashMap fileMeta = ml.loadURL(mf.getAbsolutePath());
|
|
248 if (fileMeta == null) {
|
|
249 return;
|
|
250 }
|
|
251 // meta for the directory itself is in the "" bin
|
151
|
252 dirMeta = (HashMap) fileMeta.remove("");
|
|
253 // read meta for files in this directory
|
|
254 readFileMeta(fileMeta, null);
|
|
255 // is there meta for other files left?
|
130
|
256 if (fileMeta.size() > 0) {
|
151
|
257 unresolvedFileMeta = fileMeta;
|
130
|
258 }
|
|
259 } catch (SAXException e) {
|
|
260 // TODO Auto-generated catch block
|
|
261 e.printStackTrace();
|
|
262 } catch (IOException e) {
|
|
263 // TODO Auto-generated catch block
|
|
264 e.printStackTrace();
|
|
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) {
|
86
|
367 // linear search -> worst performance
|
159
|
368 int n = list[fc].size();
|
91
|
369 for (int i = 0; i < n; i++) {
|
159
|
370 ImageFileset fs = (ImageFileset) list[fc].get(i);
|
86
|
371 if (fs.getName().equals(fn)) {
|
|
372 return i;
|
|
373 }
|
|
374 }
|
|
375 return -1;
|
|
376 }
|
|
377
|
159
|
378 /** Finds the ImageFileset with the name <code>fn</code>.
|
86
|
379 *
|
159
|
380 * Searches the directory for the ImageFileset with the name <code>fn</code> and returns
|
86
|
381 * it. Returns null if the file cannot be found.
|
|
382 *
|
|
383 * @param fn filename
|
159
|
384 * @return ImageFileset
|
86
|
385 */
|
159
|
386 public ImageFileset find(String fn) {
|
|
387 int fc = FileOps.classForFilename(fn);
|
|
388 int i = indexOf(fn, fc);
|
86
|
389 if (i >= 0) {
|
159
|
390 return (ImageFileset) list[0].get(i);
|
|
391 }
|
|
392 return null;
|
|
393 }
|
|
394
|
|
395 /** Finds the ImageFileset with the name <code>fn</code> and class <code>fc</code>.
|
|
396 *
|
|
397 * Searches the directory for the ImageFileset with the name <code>fn</code> and returns
|
|
398 * it. Returns null if the file cannot be found.
|
|
399 *
|
|
400 * @param fn filename
|
|
401 * @return ImageFileset
|
|
402 */
|
|
403 public ImageFileset find(String fn, int fc) {
|
|
404 int i = indexOf(fn, fc);
|
|
405 if (i >= 0) {
|
|
406 return (ImageFileset) list[fc].get(i);
|
86
|
407 }
|
|
408 return null;
|
|
409 }
|
|
410
|
|
411 /**
|
|
412 * @return String
|
|
413 */
|
|
414 public String getDirName() {
|
|
415 return dirName;
|
|
416 }
|
|
417
|
159
|
418 /** The directory is valid (exists on disk).
|
|
419 *
|
86
|
420 * @return boolean
|
|
421 */
|
|
422 public boolean isValid() {
|
|
423 return isValid;
|
|
424 }
|
|
425
|
159
|
426 /** The directory has been read from disk.
|
|
427 *
|
|
428 * @return
|
|
429 */
|
|
430 public boolean isRead() {
|
|
431 return (dirMTime != 0);
|
|
432 }
|
|
433
|
86
|
434 /**
|
|
435 * @return long
|
|
436 */
|
|
437 public long getAccessTime() {
|
|
438 return objectATime;
|
|
439 }
|
|
440
|
|
441 /**
|
|
442 * @return Hashtable
|
|
443 */
|
91
|
444 public HashMap getDirMeta() {
|
86
|
445 return dirMeta;
|
|
446 }
|
|
447
|
|
448 /**
|
|
449 * @return long
|
|
450 */
|
|
451 public long getDirMTime() {
|
|
452 return dirMTime;
|
|
453 }
|
|
454
|
|
455 /**
|
|
456 * Sets the dirMeta.
|
|
457 * @param dirMeta The dirMeta to set
|
|
458 */
|
91
|
459 public void setDirMeta(HashMap dirMeta) {
|
86
|
460 this.dirMeta = dirMeta;
|
|
461 }
|
|
462
|
151
|
463 public boolean hasUnresolvedFileMeta() {
|
|
464 return (this.unresolvedFileMeta != null);
|
|
465 }
|
|
466
|
86
|
467 }
|