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