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++) {
|
187
|
199 DocuDirent f = FileOps.fileForClass(fc, fl[i], dirs, scalext);
|
159
|
200 // add the file to our list
|
|
201 list[fc].add(f);
|
|
202 f.setParent(this);
|
86
|
203 }
|
|
204 }
|
|
205 }
|
159
|
206 dirMTime = dir.lastModified();
|
|
207 // read metadata as well
|
|
208 readMeta();
|
86
|
209 return isValid;
|
|
210 }
|
|
211
|
|
212 /** Check to see if the directory has been modified and reread if necessary.
|
|
213 *
|
|
214 * @return boolean the directory is valid
|
|
215 */
|
|
216 public boolean refresh() {
|
|
217 if (isValid) {
|
|
218 if (dir.lastModified() > dirMTime) {
|
|
219 // on-disk modification time is more recent
|
|
220 readDir();
|
|
221 }
|
|
222 touch();
|
|
223 }
|
|
224 return isValid;
|
|
225 }
|
|
226
|
|
227 /** Read directory metadata.
|
|
228 *
|
|
229 */
|
|
230 public void readMeta() {
|
|
231 // check for directory metadata...
|
130
|
232 File mf = new File(dir, "index.meta");
|
|
233 if (mf.canRead()) {
|
|
234 XMLMetaLoader ml = new XMLMetaLoader();
|
|
235 try {
|
|
236 // read directory meta file
|
|
237 HashMap fileMeta = ml.loadURL(mf.getAbsolutePath());
|
|
238 if (fileMeta == null) {
|
|
239 return;
|
|
240 }
|
|
241 // meta for the directory itself is in the "" bin
|
151
|
242 dirMeta = (HashMap) fileMeta.remove("");
|
|
243 // read meta for files in this directory
|
|
244 readFileMeta(fileMeta, null);
|
|
245 // is there meta for other files left?
|
130
|
246 if (fileMeta.size() > 0) {
|
151
|
247 unresolvedFileMeta = fileMeta;
|
130
|
248 }
|
|
249 } catch (SAXException e) {
|
181
|
250 if (cache != null) {
|
|
251 cache.logger.warn("error parsing index.meta", e);
|
|
252 }
|
130
|
253 } catch (IOException e) {
|
181
|
254 if (cache != null) {
|
|
255 cache.logger.warn("error reading index.meta", e);
|
|
256 }
|
130
|
257 }
|
151
|
258 }
|
|
259 readParentMeta();
|
|
260 }
|
130
|
261
|
151
|
262 /** Read metadata from all known parent directories.
|
|
263 *
|
|
264 */
|
|
265 public void readParentMeta() {
|
|
266 // check the parent directories for additional file meta
|
|
267 Directory dd = parent;
|
156
|
268 String path = dir.getName();
|
151
|
269 while (dd != null) {
|
156
|
270 if (((DocuDirectory) dd).hasUnresolvedFileMeta()) {
|
|
271 readFileMeta(((DocuDirectory) dd).unresolvedFileMeta, path);
|
151
|
272 }
|
|
273 // prepend parent dir path
|
|
274 path = dd.dir.getName() + "/" + path;
|
|
275 // become next parent
|
|
276 dd = dd.parent;
|
|
277 }
|
|
278 }
|
|
279
|
|
280 /** Read metadata for the files in this directory.
|
|
281 *
|
|
282 * Takes a HashMap with meta-information, adding the relative path
|
|
283 * before the lookup.
|
|
284 *
|
|
285 * @param fileMeta
|
|
286 * @param relPath
|
159
|
287 * @param fc fileClass
|
151
|
288 */
|
159
|
289 protected void readFileMeta(HashMap fileMeta, String relPath) {
|
151
|
290 if (list == null) {
|
|
291 // there are no files
|
|
292 return;
|
|
293 }
|
|
294 String path = (relPath != null) ? (relPath + "/") : "";
|
159
|
295 // go through all file classes
|
|
296 for (int nc = 0; nc < list.length; nc++) {
|
|
297 int fc = cache.getFileClasses()[nc];
|
|
298 if (list[fc] == null) {
|
|
299 continue;
|
|
300 }
|
|
301 // iterate through the list of files in this directory
|
|
302 for (Iterator i = list[fc].iterator(); i.hasNext();) {
|
|
303 DocuDirent f = (DocuDirent) i.next();
|
|
304 // prepend path to the filename
|
|
305 String fn = path + f.getName();
|
|
306 // look up meta for this file and remove from dir
|
|
307 HashMap meta = (HashMap) fileMeta.remove(fn);
|
|
308 if (meta != null) {
|
|
309 // store meta in file
|
|
310 f.setFileMeta(meta);
|
|
311 }
|
|
312 }
|
|
313 }
|
|
314 }
|
|
315
|
|
316 protected void notifyChildMeta(HashMap childmeta) {
|
|
317 List children = cache.getChildren(this.dirName, true);
|
|
318 if (children.size() > 0) {
|
|
319 for (Iterator i = children.iterator(); i.hasNext();) {
|
|
320 // TODO: finish this!
|
|
321 //((DocuDirectory) i.next()).readFileMeta()
|
151
|
322 }
|
130
|
323 }
|
86
|
324 }
|
|
325
|
|
326 /** Update access time.
|
|
327 *
|
|
328 * @return long time of last access.
|
|
329 */
|
|
330 public long touch() {
|
|
331 long t = objectATime;
|
|
332 objectATime = System.currentTimeMillis();
|
|
333 return t;
|
|
334 }
|
|
335
|
|
336 /** Searches for the file with the name <code>fn</code>.
|
|
337 *
|
|
338 * Searches the directory for the file with the name <code>fn</code> and returns
|
|
339 * its index. Returns -1 if the file cannot be found.
|
|
340 *
|
|
341 * @param fn filename
|
159
|
342 * @param fc file class
|
86
|
343 * @return int index of file <code>fn</code>
|
|
344 */
|
|
345 public int indexOf(String fn) {
|
159
|
346 int fc = FileOps.classForFilename(fn);
|
|
347 return indexOf(fn, fc);
|
|
348 }
|
|
349
|
|
350 /** Searches for the file with the name <code>fn</code> and class fc.
|
|
351 *
|
|
352 * Searches the directory for the file with the name <code>fn</code> and returns
|
|
353 * its index. Returns -1 if the file cannot be found.
|
|
354 *
|
|
355 * @param fn filename
|
|
356 * @return int index of file <code>fn</code>
|
|
357 */
|
|
358 public int indexOf(String fn, int fc) {
|
176
|
359 if (!isRead()) {
|
|
360 // read directory now
|
|
361 if (!readDir()) {
|
|
362 return -1;
|
|
363 }
|
|
364 }
|
86
|
365 // linear search -> worst performance
|
159
|
366 int n = list[fc].size();
|
91
|
367 for (int i = 0; i < n; i++) {
|
187
|
368 DocuDirent fs = (DocuDirent) list[fc].get(i);
|
86
|
369 if (fs.getName().equals(fn)) {
|
176
|
370 // filename matches
|
|
371 return i;
|
|
372 }
|
|
373 }
|
|
374 // try again without extension
|
|
375 for (int i = 0; i < n; i++) {
|
187
|
376 DocuDirent fs = (DocuDirent) list[fc].get(i);
|
176
|
377 if (fs.getBasename().equals(FileOps.basename(fn))) {
|
|
378 // basename matches
|
86
|
379 return i;
|
|
380 }
|
|
381 }
|
|
382 return -1;
|
|
383 }
|
|
384
|
187
|
385 /** Finds the DocuDirent with the name <code>fn</code>.
|
86
|
386 *
|
187
|
387 * Searches the directory for the DocuDirent with the name <code>fn</code> and returns
|
86
|
388 * it. Returns null if the file cannot be found.
|
|
389 *
|
|
390 * @param fn filename
|
187
|
391 * @return DocuDirent
|
86
|
392 */
|
187
|
393 public DocuDirent find(String fn) {
|
159
|
394 int fc = FileOps.classForFilename(fn);
|
|
395 int i = indexOf(fn, fc);
|
86
|
396 if (i >= 0) {
|
187
|
397 return (DocuDirent) list[0].get(i);
|
159
|
398 }
|
|
399 return null;
|
|
400 }
|
|
401
|
187
|
402 /** Finds the DocuDirent with the name <code>fn</code> and class <code>fc</code>.
|
159
|
403 *
|
187
|
404 * Searches the directory for the DocuDirent with the name <code>fn</code> and returns
|
159
|
405 * it. Returns null if the file cannot be found.
|
|
406 *
|
|
407 * @param fn filename
|
187
|
408 * @return DocuDirent
|
159
|
409 */
|
187
|
410 public DocuDirent find(String fn, int fc) {
|
159
|
411 int i = indexOf(fn, fc);
|
|
412 if (i >= 0) {
|
187
|
413 return (DocuDirent) list[fc].get(i);
|
86
|
414 }
|
|
415 return null;
|
|
416 }
|
|
417
|
|
418 /**
|
|
419 * @return String
|
|
420 */
|
|
421 public String getDirName() {
|
|
422 return dirName;
|
|
423 }
|
|
424
|
159
|
425 /** The directory is valid (exists on disk).
|
|
426 *
|
86
|
427 * @return boolean
|
|
428 */
|
|
429 public boolean isValid() {
|
|
430 return isValid;
|
|
431 }
|
|
432
|
159
|
433 /** The directory has been read from disk.
|
|
434 *
|
|
435 * @return
|
|
436 */
|
|
437 public boolean isRead() {
|
|
438 return (dirMTime != 0);
|
|
439 }
|
|
440
|
86
|
441 /**
|
|
442 * @return long
|
|
443 */
|
|
444 public long getAccessTime() {
|
|
445 return objectATime;
|
|
446 }
|
|
447
|
|
448 /**
|
|
449 * @return Hashtable
|
|
450 */
|
91
|
451 public HashMap getDirMeta() {
|
86
|
452 return dirMeta;
|
|
453 }
|
|
454
|
|
455 /**
|
|
456 * @return long
|
|
457 */
|
|
458 public long getDirMTime() {
|
|
459 return dirMTime;
|
|
460 }
|
|
461
|
|
462 /**
|
|
463 * Sets the dirMeta.
|
|
464 * @param dirMeta The dirMeta to set
|
|
465 */
|
91
|
466 public void setDirMeta(HashMap dirMeta) {
|
86
|
467 this.dirMeta = dirMeta;
|
|
468 }
|
|
469
|
151
|
470 public boolean hasUnresolvedFileMeta() {
|
|
471 return (this.unresolvedFileMeta != null);
|
|
472 }
|
|
473
|
86
|
474 }
|