Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/DocuDirectory.java @ 91:a398fc09ba71
New version 1.8b4.
DocuFile classes use new Collection classes and has-a instead of is-a
list and map relations.
| author | robcast |
|---|---|
| date | Mon, 17 Mar 2003 15:59:12 +0100 |
| parents | 997ba69afb81 |
| children | a32e8c80e2f2 |
comparison
equal
deleted
inserted
replaced
| 90:8058d3b3466a | 91:a398fc09ba71 |
|---|---|
| 20 */ | 20 */ |
| 21 | 21 |
| 22 package digilib.io; | 22 package digilib.io; |
| 23 | 23 |
| 24 import java.io.File; | 24 import java.io.File; |
| 25 import java.util.ArrayList; | |
| 25 import java.util.Arrays; | 26 import java.util.Arrays; |
| 26 import java.util.Collection; | 27 import java.util.HashMap; |
| 27 import java.util.Hashtable; | 28 import java.util.Iterator; |
| 28 import java.util.Vector; | |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * @author casties | 31 * @author casties |
| 32 */ | 32 */ |
| 33 public class DocuDirectory extends Vector { | 33 public class DocuDirectory { |
| 34 | 34 |
| 35 // list of files | |
| 36 private ArrayList list = null; | |
| 35 // directory object is valid (has been read) | 37 // directory object is valid (has been read) |
| 36 private boolean isValid = false; | 38 private boolean isValid = false; |
| 37 // names of base directories | 39 // names of base directories |
| 38 private String[] baseDirNames = null; | 40 private String[] baseDirNames = null; |
| 39 // directory name (digilib canonical form) | 41 // directory name (digilib canonical form) |
| 40 private String dirName = null; | 42 private String dirName = null; |
| 41 // default/hires directory | 43 // default/hires directory |
| 42 private File dir = null; | 44 private File dir = null; |
| 43 // directory metadata | 45 // directory metadata |
| 44 private Hashtable dirMeta = null; | 46 private HashMap dirMeta = null; |
| 45 // time of last access of this object (not the filesystem) | 47 // time of last access of this object (not the filesystem) |
| 46 private long objectATime = 0; | 48 private long objectATime = 0; |
| 47 // time the file system directory was last modified | 49 // time the file system directory was last modified |
| 48 private long dirMTime = 0; | 50 private long dirMTime = 0; |
| 49 | 51 |
| 50 /* | 52 /* |
| 51 * inherited stuff | 53 * constructors |
| 52 */ | |
| 53 | |
| 54 public DocuDirectory(int initialCapacity, int capacityIncrement) { | |
| 55 super(initialCapacity, capacityIncrement); | |
| 56 } | |
| 57 | |
| 58 public DocuDirectory(int initialCapacity) { | |
| 59 super(initialCapacity); | |
| 60 } | |
| 61 | |
| 62 public DocuDirectory(Collection c) { | |
| 63 super(c); | |
| 64 } | |
| 65 | |
| 66 /* | |
| 67 * new stuff | |
| 68 */ | 54 */ |
| 69 | 55 |
| 70 /** Constructor with directory path and set of base directories. | 56 /** Constructor with directory path and set of base directories. |
| 71 * | 57 * |
| 72 * Reads the directory at the given path appended to the base directories. | 58 * Reads the directory at the given path appended to the base directories. |
| 75 * | 61 * |
| 76 * @param path digilib directory path name | 62 * @param path digilib directory path name |
| 77 * @param bd array of base directory names | 63 * @param bd array of base directory names |
| 78 */ | 64 */ |
| 79 public DocuDirectory(String path, String[] bd) { | 65 public DocuDirectory(String path, String[] bd) { |
| 80 super(); | |
| 81 dirName = path; | 66 dirName = path; |
| 82 baseDirNames = bd; | 67 baseDirNames = bd; |
| 83 readDir(); | 68 readDir(); |
| 84 } | 69 } |
| 85 | 70 |
| 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 /** Read the directory and fill this object. | 83 /** Read the directory and fill this object. |
| 87 * | 84 * |
| 88 * Clears the Vector and (re)reads all files. | 85 * Clears the Vector and (re)reads all files. |
| 89 * | 86 * |
| 90 * @return boolean the directory exists | 87 * @return boolean the directory exists |
| 91 */ | 88 */ |
| 92 public boolean readDir() { | 89 public boolean readDir() { |
| 90 // first file extension to try for scaled directories | |
| 91 String fext = null; | |
| 93 // clear directory first | 92 // clear directory first |
| 94 clear(); | 93 list = null; |
| 95 isValid = false; | 94 isValid = false; |
| 96 // number of base dirs | 95 // number of base dirs |
| 97 int nb = baseDirNames.length; | 96 int nb = baseDirNames.length; |
| 98 // array of base dirs | 97 // array of base dirs |
| 99 File[] dirs = new File[nb]; | 98 File[] dirs = new File[nb]; |
| 112 File[] fl = dir.listFiles(new FileOps.ImageFileFilter()); | 111 File[] fl = dir.listFiles(new FileOps.ImageFileFilter()); |
| 113 if (fl == null) { | 112 if (fl == null) { |
| 114 // not a directory | 113 // not a directory |
| 115 return false; | 114 return false; |
| 116 } | 115 } |
| 117 // number of image files | 116 // number of image files in the directory |
| 118 int nf = fl.length; | 117 int nf = fl.length; |
| 119 if (nf > 0) { | 118 if (nf > 0) { |
| 120 // resize Vector | 119 // create new list |
| 121 this.ensureCapacity(nf); | 120 list = new ArrayList(nf); |
| 122 | 121 |
| 123 // sort the file names alphabetically and iterate the list | 122 // sort the file names alphabetically and iterate the list |
| 124 Arrays.sort(fl); | 123 Arrays.sort(fl); |
| 125 for (int i = 0; i < nf; i++) { | 124 for (int i = 0; i < nf; i++) { |
| 126 String fn = fl[i].getName(); | 125 String fn = fl[i].getName(); |
| 126 String fnx = | |
| 127 fn.substring(0, fn.lastIndexOf('.') + 1); | |
| 127 // add the first DocuFile to a new DocuFileset | 128 // add the first DocuFile to a new DocuFileset |
| 128 DocuFileset fs = new DocuFileset(nb); | 129 DocuFileset fs = new DocuFileset(nb); |
| 129 fs.add(new DocuFile(fl[i])); | 130 fs.add(new DocuFile(fl[i])); |
| 130 // iterate the remaining base directories | 131 // iterate the remaining base directories |
| 131 for (int j = 1; j < nb; j++) { | 132 for (int j = 1; j < nb; j++) { |
| 132 if (dirs[j] == null) { | 133 if (dirs[j] == null) { |
| 133 continue; | 134 continue; |
| 134 } | 135 } |
| 135 File f = new File(dirs[j], fn); | 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 } | |
| 136 // if the file exists, add to the DocuFileset | 144 // if the file exists, add to the DocuFileset |
| 137 if (f.canRead()) { | 145 if (f.canRead()) { |
| 138 fs.add(new DocuFile(f)); | 146 fs.add(new DocuFile(f)); |
| 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 } | |
| 139 } | 163 } |
| 140 } | 164 } |
| 141 // add the fileset to our Vector | 165 // add the fileset to our list |
| 142 add(fs); | 166 list.add(fs); |
| 143 fs.setParent(this); | 167 fs.setParent(this); |
| 144 } | 168 } |
| 145 } | 169 } |
| 146 dirMTime = dir.lastModified(); | 170 dirMTime = dir.lastModified(); |
| 147 isValid = true; | 171 isValid = true; |
| 190 * @param fn filename | 214 * @param fn filename |
| 191 * @return int index of file <code>fn</code> | 215 * @return int index of file <code>fn</code> |
| 192 */ | 216 */ |
| 193 public int indexOf(String fn) { | 217 public int indexOf(String fn) { |
| 194 // linear search -> worst performance | 218 // linear search -> worst performance |
| 195 for (int i = 0; i < elementCount; i++) { | 219 int n = list.size(); |
| 196 DocuFileset fs = (DocuFileset) get(i); | 220 for (int i = 0; i < n; i++) { |
| 221 DocuFileset fs = (DocuFileset) list.get(i); | |
| 197 if (fs.getName().equals(fn)) { | 222 if (fs.getName().equals(fn)) { |
| 198 return i; | 223 return i; |
| 199 } | 224 } |
| 200 } | 225 } |
| 201 return -1; | 226 return -1; |
| 210 * @return DocuFileset | 235 * @return DocuFileset |
| 211 */ | 236 */ |
| 212 public DocuFileset find(String fn) { | 237 public DocuFileset find(String fn) { |
| 213 int i = indexOf(fn); | 238 int i = indexOf(fn); |
| 214 if (i >= 0) { | 239 if (i >= 0) { |
| 215 return (DocuFileset) get(i); | 240 return (DocuFileset) list.get(i); |
| 216 } | 241 } |
| 217 return null; | 242 return null; |
| 218 } | 243 } |
| 219 | 244 |
| 220 /** | 245 /** |
| 239 } | 264 } |
| 240 | 265 |
| 241 /** | 266 /** |
| 242 * @return Hashtable | 267 * @return Hashtable |
| 243 */ | 268 */ |
| 244 public Hashtable getDirMeta() { | 269 public HashMap getDirMeta() { |
| 245 return dirMeta; | 270 return dirMeta; |
| 246 } | 271 } |
| 247 | 272 |
| 248 /** | 273 /** |
| 249 * @return long | 274 * @return long |
| 254 | 279 |
| 255 /** | 280 /** |
| 256 * Sets the dirMeta. | 281 * Sets the dirMeta. |
| 257 * @param dirMeta The dirMeta to set | 282 * @param dirMeta The dirMeta to set |
| 258 */ | 283 */ |
| 259 public void setDirMeta(Hashtable dirMeta) { | 284 public void setDirMeta(HashMap dirMeta) { |
| 260 this.dirMeta = dirMeta; | 285 this.dirMeta = dirMeta; |
| 261 } | 286 } |
| 262 | 287 |
| 263 } | 288 } |
