comparison servlet/src/digilib/io/DocuDirectory.java @ 151:bc8df0133c04

Servlet version 1.15b1 - information in index.meta works finally for files in subdirectories.
author robcast
date Mon, 01 Sep 2003 18:21:27 +0200
parents 04ad64b2137a
children f4a5cfe37469
comparison
equal deleted inserted replaced
150:84ddd05c95d5 151:bc8df0133c04
37 37
38 // list of files (DocuFileSet) 38 // list of files (DocuFileSet)
39 private ArrayList list = null; 39 private ArrayList list = null;
40 // directory object is valid (has been read) 40 // directory object is valid (has been read)
41 private boolean isValid = false; 41 private boolean isValid = false;
42 // names of base directories 42 // reference of the parent DocuDirCache
43 private String[] baseDirNames = null; 43 private DocuDirCache cache = null;
44 // directory name (digilib canonical form) 44 // directory name (digilib canonical form)
45 private String dirName = null; 45 private String dirName = null;
46 // directory metadata 46 // directory metadata
47 private HashMap dirMeta = null; 47 private HashMap dirMeta = null;
48 // unresolved file metadata
49 private HashMap unresolvedFileMeta = null;
48 // time of last access of this object (not the filesystem) 50 // time of last access of this object (not the filesystem)
49 private long objectATime = 0; 51 private long objectATime = 0;
50 // time the file system directory was last modified 52 // time the file system directory was last modified
51 private long dirMTime = 0; 53 private long dirMTime = 0;
54
52 55
53 /* 56 /*
54 * constructors 57 * constructors
55 */ 58 */
56 59
61 * @see readDir 64 * @see readDir
62 * 65 *
63 * @param path digilib directory path name 66 * @param path digilib directory path name
64 * @param bd array of base directory names 67 * @param bd array of base directory names
65 */ 68 */
66 public DocuDirectory(String path, String[] bd) { 69 public DocuDirectory(String path, DocuDirCache cache) {
67 dirName = path; 70 this.dirName = path;
68 baseDirNames = bd; 71 this.cache = cache;
69 readDir(); 72 readDir();
70 } 73 }
71 74
72 /* 75 /*
73 * other stuff 76 * other stuff
74 */ 77 */
75 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
92 /** number of DocuFiles in this directory.
93 *
94 */
76 public int size() { 95 public int size() {
77 return (list != null) ? list.size() : 0; 96 return (list != null) ? list.size() : 0;
78 } 97 }
79 98
99 /** Returns the DocuFile at the index.
100 *
101 * @param index
102 * @return
103 */
80 public DocuFileset get(int index) { 104 public DocuFileset get(int index) {
81 if ((list == null)||(index >= list.size())) { 105 if ((list == null) || (index >= list.size())) {
82 return null; 106 return null;
83 } 107 }
84 return (DocuFileset)list.get(index); 108 return (DocuFileset) list.get(index);
85 } 109 }
86 110
87 /** Read the directory and fill this object. 111 /** Read the filesystem directory and fill this object.
88 * 112 *
89 * Clears the List and (re)reads all files. 113 * Clears the List and (re)reads all files.
90 * 114 *
91 * @return boolean the directory exists 115 * @return boolean the directory exists
92 */ 116 */
93 public boolean readDir() { 117 public boolean readDir() {
118 String[] baseDirNames = cache.getBaseDirNames();
94 // first file extension to try for scaled directories 119 // first file extension to try for scaled directories
95 String fext = null; 120 String fext = null;
96 // clear directory first 121 // clear directory first
97 list = null; 122 list = null;
98 isValid = false; 123 isValid = false;
125 150
126 // sort the file names alphabetically and iterate the list 151 // sort the file names alphabetically and iterate the list
127 Arrays.sort(fl); 152 Arrays.sort(fl);
128 for (int i = 0; i < nf; i++) { 153 for (int i = 0; i < nf; i++) {
129 String fn = fl[i].getName(); 154 String fn = fl[i].getName();
130 String fnx = 155 String fnx = fn.substring(0, fn.lastIndexOf('.') + 1);
131 fn.substring(0, fn.lastIndexOf('.') + 1);
132 // add the first DocuFile to a new DocuFileset 156 // add the first DocuFile to a new DocuFileset
133 DocuFileset fs = new DocuFileset(nb); 157 DocuFileset fs = new DocuFileset(nb);
134 fs.add(new DocuFile(fn, fs, this)); 158 fs.add(new DocuFile(fn, fs, this));
135 // iterate the remaining base directories 159 // iterate the remaining base directories
136 for (int j = 1; j < nb; j++) { 160 for (int j = 1; j < nb; j++) {
151 } else { 175 } else {
152 // try other file extensions 176 // try other file extensions
153 Iterator exts = FileOps.getImageExtensionIterator(); 177 Iterator exts = FileOps.getImageExtensionIterator();
154 while (exts.hasNext()) { 178 while (exts.hasNext()) {
155 String s = (String) exts.next(); 179 String s = (String) exts.next();
156 f = 180 f = new File(dirs[j].getDir(), fnx + s);
157 new File(
158 dirs[j].getDir(),
159 fnx + s);
160 // if the file exists, add to the DocuFileset 181 // if the file exists, add to the DocuFileset
161 if (f.canRead()) { 182 if (f.canRead()) {
162 fs.add(new DocuFile(f.getName(), fs, dirs[j])); 183 fs.add(
184 new DocuFile(f.getName(), fs, dirs[j]));
163 fext = s; 185 fext = s;
164 break; 186 break;
165 } 187 }
166 } 188 }
167 } 189 }
208 HashMap fileMeta = ml.loadURL(mf.getAbsolutePath()); 230 HashMap fileMeta = ml.loadURL(mf.getAbsolutePath());
209 if (fileMeta == null) { 231 if (fileMeta == null) {
210 return; 232 return;
211 } 233 }
212 // meta for the directory itself is in the "" bin 234 // meta for the directory itself is in the "" bin
213 dirMeta = (HashMap)fileMeta.remove(""); 235 dirMeta = (HashMap) fileMeta.remove("");
214 // is there meta for other files? 236 // read meta for files in this directory
237 readFileMeta(fileMeta, null);
238 // is there meta for other files left?
215 if (fileMeta.size() > 0) { 239 if (fileMeta.size() > 0) {
216 // iterate through the list of files 240 unresolvedFileMeta = fileMeta;
217 for (Iterator i = list.iterator(); i.hasNext();) {
218 DocuFileset df = (DocuFileset)i.next();
219 // look up meta for this file
220 HashMap meta = (HashMap)fileMeta.get(df.getName());
221 if (meta != null) {
222 df.setFileMeta(meta);
223 }
224 }
225 } 241 }
226 } catch (SAXException e) { 242 } catch (SAXException e) {
227 // TODO Auto-generated catch block 243 // TODO Auto-generated catch block
228 e.printStackTrace(); 244 e.printStackTrace();
229 } catch (IOException e) { 245 } catch (IOException e) {
230 // TODO Auto-generated catch block 246 // TODO Auto-generated catch block
231 e.printStackTrace(); 247 e.printStackTrace();
232 } 248 }
233 249 }
250 readParentMeta();
251 }
252
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 }
234 } 296 }
235 } 297 }
236 298
237 /** Update access time. 299 /** Update access time.
238 * 300 *
321 */ 383 */
322 public void setDirMeta(HashMap dirMeta) { 384 public void setDirMeta(HashMap dirMeta) {
323 this.dirMeta = dirMeta; 385 this.dirMeta = dirMeta;
324 } 386 }
325 387
388 public boolean hasUnresolvedFileMeta() {
389 return (this.unresolvedFileMeta != null);
390 }
391
326 } 392 }