annotate servlet/src/digilib/io/DigiDirectory.java @ 339:6d2032b6121d gen2_1

new directory and cache work
author robcast
date Wed, 17 Nov 2004 18:17:34 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
1 /* DigiDirectory.java -- digilib directory object
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
2 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
3 * Digital Image Library servlet components
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
4 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
5 * Copyright (C) 2004 Robert Casties (robcast@mail.berlios.de)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
6 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify it
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
8 * under the terms of the GNU General Public License as published by the Free
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
9 * Software Foundation; either version 2 of the License, or (at your option)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
10 * any later version.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
11 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
12 * Please read license.txt for the full details. A copy of the GPL may be found
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
13 * at http://www.gnu.org/copyleft/lgpl.html
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
14 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License along with
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
17 * Place, Suite 330, Boston, MA 02111-1307 USA
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
18 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
19 * Created on 03.11.2004
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
20 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
21 package digilib.io;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
22
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
23 import java.io.File;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
24 import java.io.IOException;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
25 import java.util.Arrays;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
26 import java.util.Map;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
27
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
28 import org.xml.sax.SAXException;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
29
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
30 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
31 * digilib directory object
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
32 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
33 * @author casties
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
34 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
35 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
36 public class DigiDirectory extends DigiDirent {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
37
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
38 protected static boolean isFile = false;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
39
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
40 protected static boolean isDirectory = true;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
41
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
42 protected DigiDirent[] entries;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
43
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
44 protected int[][] indexes;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
45
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
46 protected File dir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
47
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
48 /** directory name (digilib canonical form) */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
49 protected String dlPath = null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
50
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
51 protected long mtime = 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
52
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
53 protected boolean isDirRead = false;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
54
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
55 protected Map unresolvedMeta;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
56
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
57 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
58 * @param parent
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
59 * @param dir
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
60 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
61 public DigiDirectory(String dlPath) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
62 super(FileOps.dlName(dlPath), null);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
63 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
64
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
65 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
66 * @param dir
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
67 * @param parent
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
68 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
69 public DigiDirectory(File dir, DigiDirectory parent) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
70 super(dir.getName(), parent);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
71 this.dir = dir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
72 this.dlPath = parent.getDLPath() + "/" + dir.getName();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
73 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
74
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
75 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
76 * @param dir
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
77 * @param dlpath
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
78 * @param parent
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
79 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
80 public DigiDirectory(File dir, String dlpath, DigiDirectory parent) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
81 super(dir.getName(), parent);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
82 this.dlPath = dlpath;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
83 this.dir = dir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
84 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
85
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
86 public boolean exists() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
87 return ((dir != null) && (dir.isDirectory()));
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
88 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
89
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
90 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
91 * Returns the file of the class at the index.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
92 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
93 * @param index
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
94 * @param fc
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
95 * fileClass
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
96 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
97 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
98 public DigiDirent get(int index, int fc) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
99 if (!isDirRead) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
100 // read directory now
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
101 if (readDir() < 1) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
102 return null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
103 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
104 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
105 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
106 return (DigiDirent) entries[indexes[fc][index]];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
107 } catch (Exception e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
108 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
109 return null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
110 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
111
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
112 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
113 * Returns the file with the name <code>fn</code>.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
114 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
115 * @param fn
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
116 * filename
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
117 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
118 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
119 public DigiDirent get(String fn) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
120 if (!isDirRead) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
121 // read directory now
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
122 if (readDir() < 1) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
123 return null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
124 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
125 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
126 // search for exact match
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
127 int idx = Arrays.binarySearch(entries, fn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
128 if (idx >= 0) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
129 return entries[idx];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
130 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
131 // try closest matches without extension
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
132 idx = -idx - 1;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
133 int imax = entries.length;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
134 String fb = FileOps.basename(fn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
135 if ((idx < imax)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
136 && (FileOps.basename(entries[idx].getName()).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
137 // idx matches
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
138 return entries[idx];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
139 } else if ((idx > 0)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
140 && (FileOps.basename(entries[idx - 1].getName()).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
141 // idx-1 matches
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
142 return entries[idx - 1];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
143 } else if ((idx + 1 < imax)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
144 && (FileOps.basename(entries[idx + 1].getName()).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
145 // idx+1 matches
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
146 return entries[idx + 1];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
147 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
148 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
149 return null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
150 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
151
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
152
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
153 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
154 * Reads the names of the files in the directory. Fills the filenames array.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
155 * Returns the number of files.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
156 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
157 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
158 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
159 public int readDir() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
160 if (!exists()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
161 return -1;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
162 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
163 File[] allFiles = null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
164 // list all files in the directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
165 allFiles = dir.listFiles();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
166 if (allFiles == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
167 // not a directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
168 isDirRead = true;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
169 return -1;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
170 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
171 Arrays.sort(allFiles);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
172 int nfiles = allFiles.length;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
173 entries = new DigiDirent[nfiles];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
174 // create new index lists for all file classes
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
175 int nfc = FileOps.NUM_CLASSES;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
176 indexes = new int[nfc][nfiles];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
177 // index pointers for the file classes
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
178 int[] lastidx = new int[nfc];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
179 Map hints = FileOps.newHints();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
180 // go through all files
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
181 for (int dirIdx = 0; dirIdx < nfiles; dirIdx++) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
182 File f = allFiles[dirIdx];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
183 String fn = f.getName();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
184 int fc = FileOps.classForFilename(fn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
185 // create the right kind of Dirent
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
186 DigiDirent df = FileOps.fileForClass(fc, f, this, hints);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
187 // add the file to our list
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
188 entries[dirIdx] = df;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
189 // add to the indexes
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
190 if (fc >= 0) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
191 indexes[fc][lastidx[fc]++] = dirIdx;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
192 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
193 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
194 // copy out the index arrays
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
195 for (int i = 0; i < nfc; i++) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
196 int[] idxs = new int[lastidx[i]];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
197 System.arraycopy(indexes[i], 0, idxs, 0, lastidx[i]);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
198 indexes[i] = idxs;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
199 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
200 // update modification time
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
201 mtime = dir.lastModified();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
202 // read metadata as well
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
203 readMeta();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
204 isDirRead = true;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
205 return nfiles;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
206 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
207
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
208 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
209 * Check to see if the directory has been modified and reread if necessary.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
210 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
211 * @return boolean the directory is valid
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
212 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
213 public void check() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
214 if (isDirRead && (dir.lastModified() > mtime)) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
215 // on-disk modification time is more recent
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
216 readDir();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
217 } else if (!isDirRead) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
218 readDir();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
219 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
220 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
221
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
222 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
223 * Read directory metadata.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
224 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
225 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
226 public void readMeta() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
227 // check for directory metadata...
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
228 File mf = new File(dir, "index.meta");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
229 if (mf.canRead()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
230 XMLMetaLoader ml = new XMLMetaLoader();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
231 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
232 // read directory meta file
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
233 Map fileMeta = ml.loadURL(mf.getAbsolutePath());
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
234 if (fileMeta == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
235 throw new IOException("XMLMetaloader returned no data!");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
236 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
237 // meta for the directory itself is in the "" bin
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
238 meta = (Map) fileMeta.remove("");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
239 // read meta for files in this directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
240 storeFileMeta(fileMeta, null);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
241 // is there meta for other files left?
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
242 if (fileMeta.size() > 0) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
243 unresolvedMeta = fileMeta;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
244 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
245 } catch (SAXException e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
246 logger.warn("error parsing index.meta", e);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
247 } catch (IOException e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
248 logger.warn("error reading index.meta", e);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
249 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
250 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
251 readParentMeta();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
252 isMetaRead = true;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
253 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
254
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
255 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
256 * Read metadata from all known parent directories.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
257 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
258 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
259 public void readParentMeta() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
260 // check the parent directories for additional file meta
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
261 DigiDirectory dd = getParent();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
262 String path = dir.getName();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
263 while (dd != null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
264 if (dd.hasUnresolvedMeta()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
265 storeFileMeta(dd.getUnresolvedMeta(), path);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
266 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
267 // prepend parent dir path
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
268 path = dd.getDir().getName() + "/" + path;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
269 // become next parent
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
270 dd = dd.getParent();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
271 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
272 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
273
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
274 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
275 * Stores metadata in the files in this directory.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
276 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
277 * Takes a Map with meta-information, adding the relative path before the
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
278 * lookup.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
279 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
280 * @param fileMeta
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
281 * @param relPath
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
282 * @param fc
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
283 * fileClass
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
284 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
285 protected void storeFileMeta(Map fileMeta, String relPath) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
286 if (entries == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
287 // there are no files
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
288 return;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
289 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
290 String path = (relPath != null) ? (relPath + "/") : "";
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
291 // iterate through the list of files in this directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
292 for (int i = 0; i < entries.length; i++) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
293 DigiDirent f = entries[i];
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
294 // prepend path to the filename
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
295 String fn = path + f.getName();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
296 // look up meta for this file
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
297 if (relPath == null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
298 // remove from map the same directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
299 f.addMeta((Map) fileMeta.remove(fn));
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
300 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
301 // copy from map in other directories
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
302 f.addMeta((Map) fileMeta.get(fn));
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
303 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
304 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
305 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
306
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
307 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
308 * boring getters and setters
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
309 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
310
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
311 public boolean hasUnresolvedMeta() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
312 return ((unresolvedMeta != null) && unresolvedMeta.isEmpty());
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
313 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
314
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
315 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
316 * @return Returns the unresolvedMeta.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
317 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
318 public Map getUnresolvedMeta() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
319 return unresolvedMeta;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
320 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
321
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
322 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
323 * @return Returns the dir.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
324 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
325 public File getDir() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
326 return dir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
327 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
328
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
329 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
330 * @param dir
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
331 * The dir to set.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
332 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
333 public void setDir(File dir) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
334 this.dir = dir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
335 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
336
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
337 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
338 * @return Returns the dlPath.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
339 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
340 public String getDLPath() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
341 return dlPath;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
342 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
343
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
344 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
345 * @param dlPath
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
346 * The dlPath to set.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
347 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
348 public void setDlPath(String dlPath) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
349 this.dlPath = dlPath;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
350 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
351
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
352 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
353 * The number of files in this directory.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
354 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
355 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
356 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
357 public int getSize() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
358 return (entries != null) ? entries.length : 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
359 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
360
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
361 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
362 * The number of files of a file class in this directory.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
363 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
364 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
365 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
366 public int getSize(int fc) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
367 try {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
368 return indexes[fc].length;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
369 } catch (Exception e) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
370 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
371 return 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
372 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
373
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
374 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
375 * @return Returns the mtime.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
376 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
377 public long getMtime() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
378 return mtime;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
379 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
380 }