Mercurial > hg > digilib-old
changeset 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 | 8058d3b3466a |
children | 757068ff7a26 |
files | servlet/src/digilib/io/DocuDirCache.java servlet/src/digilib/io/DocuDirectory.java servlet/src/digilib/io/DocuFileset.java servlet/src/digilib/io/FileOps.java |
diffstat | 4 files changed, 386 insertions(+), 264 deletions(-) [+] |
line wrap: on
line diff
--- a/servlet/src/digilib/io/DocuDirCache.java Mon Mar 17 15:57:54 2003 +0100 +++ b/servlet/src/digilib/io/DocuDirCache.java Mon Mar 17 15:59:12 2003 +0100 @@ -22,48 +22,38 @@ package digilib.io; import java.io.File; -import java.util.Hashtable; +import java.util.HashMap; /** * @author casties */ -public class DocuDirCache extends Hashtable { +public class DocuDirCache { + // HashMap of directories + private HashMap map = null; // names of base directories private String[] baseDirNames = null; - // number of files in the whole cache (not reliable) + // number of files in the whole cache (approximate) private long numFiles = 0; // number of cache hits private long hits = 0; // number of cache misses private long misses = 0; - /* - * inherited constructors - */ - public DocuDirCache(int initialCapacity, float loadFactor) { - super(initialCapacity, loadFactor); - } - - public DocuDirCache(int initialCapacity) { - super(initialCapacity); - } - - public DocuDirCache() { - super(); - } - - /* - * new and exiting stuff - */ - /** Constructor with array of base directory names. * * @param bd base directory names */ public DocuDirCache(String[] bd) { - super(); baseDirNames = bd; + map = new HashMap(); + } + + /** The number of directories in the cache. + * @return + */ + public int size() { + return (map != null) ? map.size() : 0; } /** Add a DocuDirectory to the cache. @@ -72,20 +62,91 @@ */ public void put(DocuDirectory newdir) { String s = newdir.getDirName(); - if (containsKey(s)) { + if (map.containsKey(s)) { System.out.println("Baah, duplicate key in DocuDirectory.put!"); } else { - super.put(s, newdir); + map.put(s, newdir); numFiles += newdir.size(); } } + /** Returns the DocuFileset with the pathname <code>fn</code> and the + * index <code>in</code>. + * + * If <code>fn</code> is a file then the corresponding Fileset is + * returned and the index is ignored. + * + * @param fn digilib pathname + * @param in file index + * @return + */ public DocuFileset getFileset(String fn, int in) { DocuDirectory dd; // file number is 1-based, vector index is 0-based int n = in - 1; // first, assume fn is a directory and look in the cache - dd = (DocuDirectory) get(fn); + dd = (DocuDirectory) map.get(fn); + if (dd == null) { + // cache miss + misses++; + // see if it's a directory + File f = new File(baseDirNames[0] + fn); + if (f.isDirectory()) { + dd = new DocuDirectory(fn, baseDirNames); + if (dd.isValid()) { + // add to the cache + put(dd); + } + } else { + // maybe it's a file + if (f.canRead()) { + // get the parent directory + String d = fn.substring(0, fn.lastIndexOf(File.separator)); + // try it in the cache + dd = (DocuDirectory) map.get(d); + if (dd == null) { + // try to read from disk + dd = new DocuDirectory(d, baseDirNames); + if (dd.isValid()) { + // add to the cache + put(dd); + } else { + // invalid path + return null; + } + } else { + // not a real cache miss then + misses--; + } + // get the file's index + n = dd.indexOf(f.getName()); + } + } + } else { + // cache hit + hits++; + } + dd.refresh(); + if (dd.isValid()) { + try { + return dd.get(n); + } catch (ArrayIndexOutOfBoundsException e) { + } + } + return null; + } + + /** Returns the DocuDirectory indicated by the pathname <code>fn</code>. + * + * If <code>fn</code> is a file then its parent directory is returned. + * + * @param fn digilib pathname + * @return + */ + public DocuDirectory getDirectory(String fn) { + DocuDirectory dd; + // first, assume fn is a directory and look in the cache + dd = (DocuDirectory) map.get(fn); if (dd == null) { // cache miss misses++; @@ -101,7 +162,7 @@ // maybe it's a file if (f.canRead()) { // try the parent directory in the cache - dd = (DocuDirectory) get(f.getParent()); + dd = (DocuDirectory) map.get(f.getParent()); if (dd == null) { // try to read from disk dd = new DocuDirectory(f.getParent(), baseDirNames); @@ -112,55 +173,9 @@ // invalid path return null; } - } - // get the file's index - n = dd.indexOf(f.getName()); - } - } - } else { - // cache hit - hits++; - } - dd.refresh(); - if (dd.isValid()) { - try { - return (DocuFileset) dd.elementAt(n); - } catch (ArrayIndexOutOfBoundsException e) { - } - } - return null; - } - - public DocuDirectory getDirectory(String fn) { - DocuDirectory dd; - // first, assume fn is a directory and look in the cache - dd = (DocuDirectory) get(fn); - if (dd == null) { - // cache miss - misses++; - // see if it's a directory - File f = new File(baseDirNames[0] + fn); - if (f.isDirectory()) { - dd = new DocuDirectory(fn, baseDirNames); - if (dd.isValid()) { - // add to the cache - put(dd); - } - } else { - // maybe it's a file - if (f.canRead()) { - // try the parent directory in the cache - dd = (DocuDirectory) get(f.getParent()); - if (dd == null) { - // try to read from disk - dd = new DocuDirectory(f.getParent(), baseDirNames); - if (dd.isValid()) { - // add to the cache - put(dd); - } else { - // invalid path - return null; - } + } else { + // not a real cache miss then + misses--; } } }
--- a/servlet/src/digilib/io/DocuDirectory.java Mon Mar 17 15:57:54 2003 +0100 +++ b/servlet/src/digilib/io/DocuDirectory.java Mon Mar 17 15:59:12 2003 +0100 @@ -22,16 +22,18 @@ package digilib.io; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; -import java.util.Hashtable; -import java.util.Vector; +import java.util.HashMap; +import java.util.Iterator; /** * @author casties */ -public class DocuDirectory extends Vector { +public class DocuDirectory { + // list of files + private ArrayList list = null; // directory object is valid (has been read) private boolean isValid = false; // names of base directories @@ -41,30 +43,14 @@ // default/hires directory private File dir = null; // directory metadata - private Hashtable dirMeta = null; + private HashMap dirMeta = null; // time of last access of this object (not the filesystem) private long objectATime = 0; // time the file system directory was last modified private long dirMTime = 0; /* - * inherited stuff - */ - - public DocuDirectory(int initialCapacity, int capacityIncrement) { - super(initialCapacity, capacityIncrement); - } - - public DocuDirectory(int initialCapacity) { - super(initialCapacity); - } - - public DocuDirectory(Collection c) { - super(c); - } - - /* - * new stuff + * constructors */ /** Constructor with directory path and set of base directories. @@ -77,12 +63,23 @@ * @param bd array of base directory names */ public DocuDirectory(String path, String[] bd) { - super(); dirName = path; baseDirNames = bd; readDir(); } + /* + * other stuff + */ + + public int size() { + return (list != null) ? list.size() : 0; + } + + public DocuFileset get(int index) { + return (list != null) ? (DocuFileset) list.get(index) : null; + } + /** Read the directory and fill this object. * * Clears the Vector and (re)reads all files. @@ -90,8 +87,10 @@ * @return boolean the directory exists */ public boolean readDir() { + // first file extension to try for scaled directories + String fext = null; // clear directory first - clear(); + list = null; isValid = false; // number of base dirs int nb = baseDirNames.length; @@ -114,16 +113,18 @@ // not a directory return false; } - // number of image files + // number of image files in the directory int nf = fl.length; if (nf > 0) { - // resize Vector - this.ensureCapacity(nf); + // create new list + list = new ArrayList(nf); // sort the file names alphabetically and iterate the list Arrays.sort(fl); for (int i = 0; i < nf; i++) { String fn = fl[i].getName(); + String fnx = + fn.substring(0, fn.lastIndexOf('.') + 1); // add the first DocuFile to a new DocuFileset DocuFileset fs = new DocuFileset(nb); fs.add(new DocuFile(fl[i])); @@ -132,14 +133,37 @@ if (dirs[j] == null) { continue; } - File f = new File(dirs[j], fn); + File f; + if (fext != null) { + // use the last extension + f = new File(dirs[j], fnx + fext); + } else { + // try the same filename as the original + f = new File(dirs[j], fn); + } // if the file exists, add to the DocuFileset if (f.canRead()) { fs.add(new DocuFile(f)); + } else { + // try other file extensions + Iterator exts = FileOps.getImageExtensionIterator(); + while (exts.hasNext()) { + String s = (String) exts.next(); + f = + new File( + dirs[j], + fnx + s); + // if the file exists, add to the DocuFileset + if (f.canRead()) { + fs.add(new DocuFile(f)); + fext = s; + break; + } + } } } - // add the fileset to our Vector - add(fs); + // add the fileset to our list + list.add(fs); fs.setParent(this); } } @@ -192,8 +216,9 @@ */ public int indexOf(String fn) { // linear search -> worst performance - for (int i = 0; i < elementCount; i++) { - DocuFileset fs = (DocuFileset) get(i); + int n = list.size(); + for (int i = 0; i < n; i++) { + DocuFileset fs = (DocuFileset) list.get(i); if (fs.getName().equals(fn)) { return i; } @@ -212,7 +237,7 @@ public DocuFileset find(String fn) { int i = indexOf(fn); if (i >= 0) { - return (DocuFileset) get(i); + return (DocuFileset) list.get(i); } return null; } @@ -241,7 +266,7 @@ /** * @return Hashtable */ - public Hashtable getDirMeta() { + public HashMap getDirMeta() { return dirMeta; } @@ -256,7 +281,7 @@ * Sets the dirMeta. * @param dirMeta The dirMeta to set */ - public void setDirMeta(Hashtable dirMeta) { + public void setDirMeta(HashMap dirMeta) { this.dirMeta = dirMeta; }
--- a/servlet/src/digilib/io/DocuFileset.java Mon Mar 17 15:57:54 2003 +0100 +++ b/servlet/src/digilib/io/DocuFileset.java Mon Mar 17 15:59:12 2003 +0100 @@ -19,55 +19,105 @@ */ package digilib.io; -import java.util.Collection; -import java.util.Hashtable; -import java.util.Vector; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.ListIterator; /** * @author casties */ -public class DocuFileset extends Vector { +public class DocuFileset { + // list of files + private ArrayList list = null; // metadata - private Hashtable fileMeta = null; + private HashMap fileMeta = null; // parent directory private DocuDirectory parent = null; - public DocuFileset(int initialCapacity, int capacityIncrement) { - super(initialCapacity, capacityIncrement); - } + /* + * constructors + */ public DocuFileset(int initialCapacity) { - super(initialCapacity); - } - - public DocuFileset() { - super(); + list = new ArrayList(initialCapacity); } - public DocuFileset(Collection c) { - super(c); + /* + * other stuff + */ + + /** Adds a DocuFile to this Fileset. + * + * The files should be added in the order of lower resolutions. The first + * file is considered the hires "original". + * + * @param f file to add + * @return true (always) + */ + public boolean add(DocuFile f) { + f.setParent(this); + return list.add(f); } - - /* (non-Javadoc) - * @see java.util.Collection#add(java.lang.Object) + + /** The number of image files in this Fileset. + * + * @return number of image files + */ + public int size() { + return (list != null) ? list.size() : 0; + } + + /** Get the DocuFile at the index. + * + * @param index + * @return */ - public synchronized boolean add(DocuFile f) { - f.setParent(this); - return super.add(f); + public DocuFile get(int index) { + return (DocuFile) list.get(index); + } + + + /** Get an Iterator for this Fileset starting at the highest resolution + * images. + * + * @return + */ + public ListIterator getHiresIterator() { + return list.listIterator(); } - - public void readMeta() { + + /** Get an Iterator for this Fileset starting at the lowest resolution + * images. + * + * The Iterator starts at the last element, so you have to use it backwards + * with hasPrevious() and previous(). + * + * @return + */ + public ListIterator getLoresIterator() { + return list.listIterator(list.size()); + } + + /** Reads meta-data for this Fileset if there is any. + * (not yet implemented) + */ + public void checkMeta() { // check for file metadata... } + /** The name of the (original) image file. + * + * @return + */ public String getName() { - if (this.elementCount > 0) { - return ((DocuFile) firstElement()).getName(); + if (!list.isEmpty()) { + return ((DocuFile) list.get(0)).getName(); } return null; } - /** + + /** Returns the parent DocuDirectory. * @return DocuDirectory */ public DocuDirectory getParent() { @@ -82,4 +132,20 @@ this.parent = parent; } + /** Returns the meta-data for this fileset. + * + * @return HashMap + */ + public HashMap getFileMeta() { + return fileMeta; + } + + /** + * Sets the fileMeta. + * @param fileMeta The fileMeta to set + */ + public void setFileMeta(HashMap fileMeta) { + this.fileMeta = fileMeta; + } + }
--- a/servlet/src/digilib/io/FileOps.java Mon Mar 17 15:57:54 2003 +0100 +++ b/servlet/src/digilib/io/FileOps.java Mon Mar 17 15:59:12 2003 +0100 @@ -25,151 +25,167 @@ import digilib.*; - public class FileOps { - private Utils util = null; - public static String[] fileTypes = { - "jpg", "image/jpeg", - "jpeg", "image/jpeg", - "jp2", "image/jp2", - "png", "image/png", - "gif", "image/gif", - "tif", "image/tiff", - "tiff", "image/tiff"}; + private Utils util = null; + public static String[] fileTypes = + { + "jpg", + "image/jpeg", + "jpeg", + "image/jpeg", + "jp2", + "image/jp2", + "png", + "image/png", + "gif", + "image/gif", + "tif", + "image/tiff", + "tiff", + "image/tiff" }; - public FileOps() { - util = new Utils(); - } + public static String[] fileExtensions = + { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" }; + + public FileOps() { + util = new Utils(); + } - public FileOps(Utils u) { - util = u; - } + public FileOps(Utils u) { + util = u; + } - public void setUtils(Utils u) { - util = u; - } - + public void setUtils(Utils u) { + util = u; + } - /** - * get the mime type for a file format (by extension) - */ - public static String mimeForFile(File f) { - String fn = f.getName(); - for (int i = 0; i < fileTypes.length; i += 2) { - if (fn.toLowerCase().endsWith(fileTypes[i])) { - return fileTypes[i+1]; - } - } - return null; - } + /** + * get the mime type for a file format (by extension) + */ + public static String mimeForFile(File f) { + String fn = f.getName(); + for (int i = 0; i < fileTypes.length; i += 2) { + if (fn.toLowerCase().endsWith(fileTypes[i])) { + return fileTypes[i + 1]; + } + } + return null; + } - /** - * get a filehandle for a file or directory name - * returns File number n if fn is directory (starts with 1) - */ - public File getFile(String fn, int n) throws FileOpException { - util.dprintln(4, "getFile ("+fn+", "+n+")"); + public static Iterator getImageExtensionIterator() { + return Arrays.asList(fileExtensions).iterator(); + } - File f = new File(fn); - // if fn is a file name then return file - if (f.isFile()) { - return f; - } - // if fn is a directory name then open directory - if (f.isDirectory()) { - File[] fl = f.listFiles(new ImageFileFilter()); - Arrays.sort(fl); - if ((n > 0) && (n <= fl.length)) { - return fl[n - 1]; - } - } - throw new FileOpException("Unable to find file: "+fn); - } + /** + * get a filehandle for a file or directory name + * returns File number n if fn is directory (starts with 1) + */ + public File getFile(String fn, int n) throws FileOpException { + util.dprintln(4, "getFile (" + fn + ", " + n + ")"); - /** - * get the number of files in a directory - * (almost the same as getFile) - * returns 0 in case of problems - */ - public int getNumFiles(String fn) throws FileOpException { - util.dprintln(4, "getNumFiles ("+fn+")"); + File f = new File(fn); + // if fn is a file name then return file + if (f.isFile()) { + return f; + } + // if fn is a directory name then open directory + if (f.isDirectory()) { + File[] fl = f.listFiles(new ImageFileFilter()); + Arrays.sort(fl); + if ((n > 0) && (n <= fl.length)) { + return fl[n - 1]; + } + } + throw new FileOpException("Unable to find file: " + fn); + } - File f = new File(fn); - // if fn is a file name then return 1 - if (f.isFile()) { - return 1; - } - // if fn is a directory name then return the number of files - if (f.isDirectory()) { - return f.listFiles(new ImageFileFilter()).length; - } - // then fn must be something strange... - return 0; - } + /** + * get the number of files in a directory + * (almost the same as getFile) + * returns 0 in case of problems + */ + public int getNumFiles(String fn) throws FileOpException { + util.dprintln(4, "getNumFiles (" + fn + ")"); + File f = new File(fn); + // if fn is a file name then return 1 + if (f.isFile()) { + return 1; + } + // if fn is a directory name then return the number of files + if (f.isDirectory()) { + return f.listFiles(new ImageFileFilter()).length; + } + // then fn must be something strange... + return 0; + } - /** - * get a filehandle for a file or directory name out of a list - * dirs is a list of base directories, fn is the appended file/dirname - * searches dirs until fn exists (backwards if fwd is false) - * returns File number n if fn is directory (starts with 1) - */ - public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) throws FileOpException { - util.dprintln(4, "getVariantFile ("+dirs+", "+fn+", "+n+")"); + /** + * get a filehandle for a file or directory name out of a list + * dirs is a list of base directories, fn is the appended file/dirname + * searches dirs until fn exists (backwards if fwd is false) + * returns File number n if fn is directory (starts with 1) + */ + public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) + throws FileOpException { + util.dprintln( + 4, + "getVariantFile (" + dirs + ", " + fn + ", " + n + ")"); - File f = null; - int nvar = dirs.length; + File f = null; + int nvar = dirs.length; - for (int i = 0; i < nvar; i++) { - try { - f = getFile(dirs[(fwd) ? i : (nvar-i-1)]+fn, n); - } catch (FileOpException e) { - f = null; - } - if (f != null) { - return f; - } - } - throw new FileOpException("Unable to find file: "+fn); - } + for (int i = 0; i < nvar; i++) { + try { + f = getFile(dirs[(fwd) ? i : (nvar - i - 1)] + fn, n); + } catch (FileOpException e) { + f = null; + } + if (f != null) { + return f; + } + } + throw new FileOpException("Unable to find file: " + fn); + } - /** - * get the number of files in a directory - * (almost the same as getFileVariant) - * returns 0 in case of problems - */ - public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) throws FileOpException { - util.dprintln(4, "getNumFilesVariant ("+dirs+", "+fn+")"); + /** + * get the number of files in a directory + * (almost the same as getFileVariant) + * returns 0 in case of problems + */ + public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) + throws FileOpException { + util.dprintln(4, "getNumFilesVariant (" + dirs + ", " + fn + ")"); - int nf = 0; - int nvar = dirs.length; + int nf = 0; + int nvar = dirs.length; - for (int i = 0; i < nvar; i++) { - try { - nf = getNumFiles(dirs[(fwd) ? i : (nvar-i-1)]+fn); - } catch (FileOpException e) { - nf = 0; - } - if (nf > 0) { - return nf; - } - } - return 0; - } + for (int i = 0; i < nvar; i++) { + try { + nf = getNumFiles(dirs[(fwd) ? i : (nvar - i - 1)] + fn); + } catch (FileOpException e) { + nf = 0; + } + if (nf > 0) { + return nf; + } + } + return 0; + } - /** - * FileFilter for image types (helper class for getFile) - */ - static class ImageFileFilter implements FileFilter { + /** + * FileFilter for image types (helper class for getFile) + */ + static class ImageFileFilter implements FileFilter { - public boolean accept(File f) { - if (f.isFile()) { - return (mimeForFile(f) != null); - } else { - return false; - } - } - } + public boolean accept(File f) { + if (f.isFile()) { + return (mimeForFile(f) != null); + } else { + return false; + } + } + } }