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;
|
86
|
30
|
130
|
31 import org.xml.sax.SAXException;
|
|
32
|
86
|
33 /**
|
|
34 * @author casties
|
|
35 */
|
149
|
36 public class DocuDirectory extends Directory {
|
86
|
37
|
130
|
38 // list of files (DocuFileSet)
|
91
|
39 private ArrayList list = null;
|
86
|
40 // directory object is valid (has been read)
|
|
41 private boolean isValid = false;
|
151
|
42 // reference of the parent DocuDirCache
|
|
43 private DocuDirCache cache = null;
|
86
|
44 // directory name (digilib canonical form)
|
|
45 private String dirName = null;
|
|
46 // directory metadata
|
91
|
47 private HashMap dirMeta = null;
|
151
|
48 // unresolved file metadata
|
|
49 private HashMap unresolvedFileMeta = null;
|
86
|
50 // time of last access of this object (not the filesystem)
|
|
51 private long objectATime = 0;
|
|
52 // time the file system directory was last modified
|
|
53 private long dirMTime = 0;
|
151
|
54
|
86
|
55
|
|
56 /*
|
91
|
57 * constructors
|
86
|
58 */
|
|
59
|
|
60 /** Constructor with directory path and set of base directories.
|
|
61 *
|
|
62 * Reads the directory at the given path appended to the base directories.
|
|
63 *
|
|
64 * @see readDir
|
|
65 *
|
|
66 * @param path digilib directory path name
|
|
67 * @param bd array of base directory names
|
|
68 */
|
151
|
69 public DocuDirectory(String path, DocuDirCache cache) {
|
|
70 this.dirName = path;
|
|
71 this.cache = cache;
|
86
|
72 readDir();
|
|
73 }
|
|
74
|
91
|
75 /*
|
|
76 * other stuff
|
|
77 */
|
151
|
78
|
|
79 /** The digilib name of the parent directory.
|
|
80 *
|
|
81 * Returns null if there is no parent.
|
|
82 */
|
|
83 public String getParentDirName() {
|
|
84 String s = null;
|
|
85 int lastidx = dirName.lastIndexOf("/");
|
|
86 if (lastidx > 0) {
|
|
87 s = dirName.substring(0, lastidx);
|
|
88 }
|
|
89 return s;
|
|
90 }
|
91
|
91
|
151
|
92 /** number of DocuFiles in this directory.
|
|
93 *
|
|
94 */
|
91
|
95 public int size() {
|
|
96 return (list != null) ? list.size() : 0;
|
|
97 }
|
|
98
|
151
|
99 /** Returns the DocuFile at the index.
|
|
100 *
|
|
101 * @param index
|
|
102 * @return
|
|
103 */
|
91
|
104 public DocuFileset get(int index) {
|
151
|
105 if ((list == null) || (index >= list.size())) {
|
122
|
106 return null;
|
151
|
107 }
|
|
108 return (DocuFileset) list.get(index);
|
91
|
109 }
|
|
110
|
151
|
111 /** Read the filesystem directory and fill this object.
|
86
|
112 *
|
130
|
113 * Clears the List and (re)reads all files.
|
86
|
114 *
|
|
115 * @return boolean the directory exists
|
|
116 */
|
|
117 public boolean readDir() {
|
151
|
118 String[] baseDirNames = cache.getBaseDirNames();
|
91
|
119 // first file extension to try for scaled directories
|
|
120 String fext = null;
|
86
|
121 // clear directory first
|
91
|
122 list = null;
|
86
|
123 isValid = false;
|
|
124 // number of base dirs
|
|
125 int nb = baseDirNames.length;
|
|
126 // array of base dirs
|
149
|
127 Directory[] dirs = new Directory[nb];
|
86
|
128 // the first directory has to exist
|
152
|
129 dir = new File(baseDirNames[0], dirName);
|
86
|
130
|
|
131 if (dir.isDirectory()) {
|
|
132 // fill array with the remaining directories
|
|
133 for (int j = 1; j < nb; j++) {
|
152
|
134 File d = new File(baseDirNames[j], dirName);
|
86
|
135 if (d.isDirectory()) {
|
149
|
136 dirs[j] = new Directory(d);
|
86
|
137 }
|
|
138 }
|
|
139
|
|
140 File[] fl = dir.listFiles(new FileOps.ImageFileFilter());
|
|
141 if (fl == null) {
|
|
142 // not a directory
|
|
143 return false;
|
|
144 }
|
91
|
145 // number of image files in the directory
|
86
|
146 int nf = fl.length;
|
|
147 if (nf > 0) {
|
91
|
148 // create new list
|
|
149 list = new ArrayList(nf);
|
86
|
150
|
|
151 // sort the file names alphabetically and iterate the list
|
|
152 Arrays.sort(fl);
|
|
153 for (int i = 0; i < nf; i++) {
|
|
154 String fn = fl[i].getName();
|
151
|
155 String fnx = fn.substring(0, fn.lastIndexOf('.') + 1);
|
86
|
156 // add the first DocuFile to a new DocuFileset
|
|
157 DocuFileset fs = new DocuFileset(nb);
|
149
|
158 fs.add(new DocuFile(fn, fs, this));
|
86
|
159 // iterate the remaining base directories
|
|
160 for (int j = 1; j < nb; j++) {
|
|
161 if (dirs[j] == null) {
|
|
162 continue;
|
|
163 }
|
91
|
164 File f;
|
|
165 if (fext != null) {
|
|
166 // use the last extension
|
149
|
167 f = new File(dirs[j].getDir(), fnx + fext);
|
91
|
168 } else {
|
|
169 // try the same filename as the original
|
149
|
170 f = new File(dirs[j].getDir(), fn);
|
91
|
171 }
|
86
|
172 // if the file exists, add to the DocuFileset
|
|
173 if (f.canRead()) {
|
149
|
174 fs.add(new DocuFile(f.getName(), fs, dirs[j]));
|
91
|
175 } else {
|
|
176 // try other file extensions
|
|
177 Iterator exts = FileOps.getImageExtensionIterator();
|
|
178 while (exts.hasNext()) {
|
|
179 String s = (String) exts.next();
|
151
|
180 f = new File(dirs[j].getDir(), fnx + s);
|
91
|
181 // if the file exists, add to the DocuFileset
|
|
182 if (f.canRead()) {
|
151
|
183 fs.add(
|
|
184 new DocuFile(f.getName(), fs, dirs[j]));
|
91
|
185 fext = s;
|
|
186 break;
|
|
187 }
|
|
188 }
|
86
|
189 }
|
|
190 }
|
91
|
191 // add the fileset to our list
|
|
192 list.add(fs);
|
86
|
193 fs.setParent(this);
|
|
194 }
|
|
195 }
|
|
196 dirMTime = dir.lastModified();
|
|
197 isValid = true;
|
130
|
198 // read metadata as well
|
|
199 readMeta();
|
86
|
200 }
|
|
201 return isValid;
|
|
202
|
|
203 }
|
|
204
|
|
205 /** Check to see if the directory has been modified and reread if necessary.
|
|
206 *
|
|
207 * @return boolean the directory is valid
|
|
208 */
|
|
209 public boolean refresh() {
|
|
210 if (isValid) {
|
|
211 if (dir.lastModified() > dirMTime) {
|
|
212 // on-disk modification time is more recent
|
|
213 readDir();
|
|
214 }
|
|
215 touch();
|
|
216 }
|
|
217 return isValid;
|
|
218 }
|
|
219
|
|
220 /** Read directory metadata.
|
|
221 *
|
|
222 */
|
|
223 public void readMeta() {
|
|
224 // check for directory metadata...
|
130
|
225 File mf = new File(dir, "index.meta");
|
|
226 if (mf.canRead()) {
|
|
227 XMLMetaLoader ml = new XMLMetaLoader();
|
|
228 try {
|
|
229 // read directory meta file
|
|
230 HashMap fileMeta = ml.loadURL(mf.getAbsolutePath());
|
|
231 if (fileMeta == null) {
|
|
232 return;
|
|
233 }
|
|
234 // meta for the directory itself is in the "" bin
|
151
|
235 dirMeta = (HashMap) fileMeta.remove("");
|
|
236 // read meta for files in this directory
|
|
237 readFileMeta(fileMeta, null);
|
|
238 // is there meta for other files left?
|
130
|
239 if (fileMeta.size() > 0) {
|
151
|
240 unresolvedFileMeta = fileMeta;
|
130
|
241 }
|
|
242 } catch (SAXException e) {
|
|
243 // TODO Auto-generated catch block
|
|
244 e.printStackTrace();
|
|
245 } catch (IOException e) {
|
|
246 // TODO Auto-generated catch block
|
|
247 e.printStackTrace();
|
|
248 }
|
151
|
249 }
|
|
250 readParentMeta();
|
|
251 }
|
130
|
252
|
151
|
253 /** Read metadata from all known parent directories.
|
|
254 *
|
|
255 */
|
|
256 public void readParentMeta() {
|
|
257 // check the parent directories for additional file meta
|
|
258 Directory dd = parent;
|
|
259 String path = dir.getName() ;
|
|
260 while (dd != null) {
|
|
261 if (((DocuDirectory)dd).hasUnresolvedFileMeta()) {
|
|
262 readFileMeta(((DocuDirectory)dd).unresolvedFileMeta, path);
|
|
263 }
|
|
264 // prepend parent dir path
|
|
265 path = dd.dir.getName() + "/" + path;
|
|
266 // become next parent
|
|
267 dd = dd.parent;
|
|
268 }
|
|
269 }
|
|
270
|
|
271 /** Read metadata for the files in this directory.
|
|
272 *
|
|
273 * Takes a HashMap with meta-information, adding the relative path
|
|
274 * before the lookup.
|
|
275 *
|
|
276 * @param fileMeta
|
|
277 * @param relPath
|
|
278 */
|
|
279 public void readFileMeta(HashMap fileMeta, String relPath) {
|
|
280 if (list == null) {
|
|
281 // there are no files
|
|
282 return;
|
|
283 }
|
|
284 String path = (relPath != null) ? (relPath + "/") : "";
|
|
285 // iterate through the list of files in this directory
|
|
286 for (Iterator i = list.iterator(); i.hasNext();) {
|
|
287 DocuFileset df = (DocuFileset) i.next();
|
|
288 // prepend path to the filename
|
|
289 String fn = path + df.getName();
|
|
290 // look up meta for this file and remove from dir
|
|
291 HashMap meta = (HashMap) fileMeta.remove(fn);
|
|
292 if (meta != null) {
|
|
293 // store meta in file
|
|
294 df.setFileMeta(meta);
|
|
295 }
|
130
|
296 }
|
86
|
297 }
|
|
298
|
|
299 /** Update access time.
|
|
300 *
|
|
301 * @return long time of last access.
|
|
302 */
|
|
303 public long touch() {
|
|
304 long t = objectATime;
|
|
305 objectATime = System.currentTimeMillis();
|
|
306 return t;
|
|
307 }
|
|
308
|
|
309 /** Searches for the file with the name <code>fn</code>.
|
|
310 *
|
|
311 * Searches the directory for the file with the name <code>fn</code> and returns
|
|
312 * its index. Returns -1 if the file cannot be found.
|
|
313 *
|
|
314 * @param fn filename
|
|
315 * @return int index of file <code>fn</code>
|
|
316 */
|
|
317 public int indexOf(String fn) {
|
|
318 // linear search -> worst performance
|
91
|
319 int n = list.size();
|
|
320 for (int i = 0; i < n; i++) {
|
|
321 DocuFileset fs = (DocuFileset) list.get(i);
|
86
|
322 if (fs.getName().equals(fn)) {
|
|
323 return i;
|
|
324 }
|
|
325 }
|
|
326 return -1;
|
|
327 }
|
|
328
|
|
329 /** Finds the DocuFileset with the name <code>fn</code>.
|
|
330 *
|
|
331 * Searches the directory for the DocuFileset with the name <code>fn</code> and returns
|
|
332 * it. Returns null if the file cannot be found.
|
|
333 *
|
|
334 * @param fn filename
|
|
335 * @return DocuFileset
|
|
336 */
|
|
337 public DocuFileset find(String fn) {
|
|
338 int i = indexOf(fn);
|
|
339 if (i >= 0) {
|
91
|
340 return (DocuFileset) list.get(i);
|
86
|
341 }
|
|
342 return null;
|
|
343 }
|
|
344
|
|
345 /**
|
|
346 * @return String
|
|
347 */
|
|
348 public String getDirName() {
|
|
349 return dirName;
|
|
350 }
|
|
351
|
|
352 /**
|
|
353 * @return boolean
|
|
354 */
|
|
355 public boolean isValid() {
|
|
356 return isValid;
|
|
357 }
|
|
358
|
|
359 /**
|
|
360 * @return long
|
|
361 */
|
|
362 public long getAccessTime() {
|
|
363 return objectATime;
|
|
364 }
|
|
365
|
|
366 /**
|
|
367 * @return Hashtable
|
|
368 */
|
91
|
369 public HashMap getDirMeta() {
|
86
|
370 return dirMeta;
|
|
371 }
|
|
372
|
|
373 /**
|
|
374 * @return long
|
|
375 */
|
|
376 public long getDirMTime() {
|
|
377 return dirMTime;
|
|
378 }
|
|
379
|
|
380 /**
|
|
381 * Sets the dirMeta.
|
|
382 * @param dirMeta The dirMeta to set
|
|
383 */
|
91
|
384 public void setDirMeta(HashMap dirMeta) {
|
86
|
385 this.dirMeta = dirMeta;
|
|
386 }
|
|
387
|
151
|
388 public boolean hasUnresolvedFileMeta() {
|
|
389 return (this.unresolvedFileMeta != null);
|
|
390 }
|
|
391
|
86
|
392 }
|