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