Mercurial > hg > digilib-old
changeset 583:7357ad8f9f42 stream
more ripping apart ImageFileSet
author | robcast |
---|---|
date | Wed, 05 Jan 2011 12:12:19 +0100 (2011-01-05) |
parents | c7034d166a24 |
children | 95417c4615b8 |
files | servlet/src/digilib/io/DocuDirectory.java servlet/src/digilib/io/FileOps.java servlet/src/digilib/io/ImageFile.java servlet/src/digilib/io/ImageFileSet.java |
diffstat | 4 files changed, 99 insertions(+), 90 deletions(-) [+] |
line wrap: on
line diff
--- a/servlet/src/digilib/io/DocuDirectory.java Tue Jan 04 11:56:12 2011 +0100 +++ b/servlet/src/digilib/io/DocuDirectory.java Wed Jan 05 12:12:19 2011 +0100 @@ -49,6 +49,9 @@ /** directory name (digilib canonical form) */ private String dirName = null; + /** array of parallel dirs for scaled images */ + private Directory[] dirs = null; + /** directory metadata */ private MetadataMap dirMeta = null; @@ -127,11 +130,11 @@ * @param index * @return */ - public ImageSet get(int index) { + public DocuDirent get(int index) { if ((list == null) || (list.get(0) == null) || (index >= list.get(0).size())) { return null; } - return (ImageSet) list.get(0).get(index); + return list.get(0).get(index); } /** @@ -171,7 +174,7 @@ * * @return boolean the directory exists */ - public synchronized boolean readDir() { + public boolean readDir() { // check directory first checkDir(); if (!isValid) { @@ -179,67 +182,67 @@ } // read all filenames logger.debug("reading directory " + dir.getPath()); + File[] allFiles = null; /* * using ReadableFileFilter is safer (we won't get directories with file * extensions) but slower. */ - File[] allFiles = null; - // allFiles = dir.listFiles(new FileOps.ReadableFileFilter()); + // allFiles = dir.listFiles(new FileOps.ReadableFileFilter()); allFiles = dir.listFiles(); - //logger.debug(" done"); if (allFiles == null) { // not a directory return false; } - // list of base dirs from the parent cache - String[] baseDirNames = cache.getBaseDirNames(); - // number of base dirs - int nb = baseDirNames.length; - // array of base dirs - Directory[] dirs = new Directory[nb]; - // first entry is this directory - dirs[0] = this; - // fill array with the remaining directories - for (int j = 1; j < nb; j++) { - File d = new File(baseDirNames[j], dirName); - if (d.isDirectory()) { - dirs[j] = new Directory(d); - logger.debug(" reading scaled directory " + d.getPath()); - dirs[j].readDir(); - //logger.debug(" done"); + // init parallel directories + if (dirs == null) { + // list of base dirs from the parent cache + String[] baseDirNames = cache.getBaseDirNames(); + // number of base dirs + int nb = baseDirNames.length; + // array of parallel dirs + dirs = new Directory[nb]; + // first entry is this directory + dirs[0] = this; + // fill array with the remaining directories + for (int j = 1; j < nb; j++) { + // add dirName to baseDirName + File d = new File(baseDirNames[j], dirName); + if (d.isDirectory()) { + dirs[j] = new Directory(d); + logger.debug(" reading scaled directory " + d.getPath()); + dirs[j].readDir(); + } } } // go through all file classes - //for (int classIdx = 0; classIdx < FileOps.NUM_CLASSES; classIdx++) { - for (FileClass fileClass: cache.getFileClasses()) { - //fileClass = cache.getFileClasses()[classIdx]; - File[] fileList = FileOps.listFiles(allFiles, FileOps - .filterForClass(fileClass)); - //logger.debug(" done"); + for (FileClass fileClass : cache.getFileClasses()) { + File[] fileList = FileOps.listFiles(allFiles, + FileOps.filterForClass(fileClass)); // number of files in the directory int numFiles = fileList.length; if (numFiles > 0) { // create new list - list.set(fileClass.ordinal(), new ArrayList<DocuDirent>(numFiles)); - // sort the file names alphabetically and iterate the list - // Arrays.sort(fileList); // not needed <hertzhaft> - for (int i = 0; i < numFiles; i++) { - DocuDirent f = FileOps.fileForClass(fileClass, fileList[i], - dirs); + ArrayList<DocuDirent> dl = new ArrayList<DocuDirent>(numFiles); + list.set(fileClass.ordinal(), dl); + for (File f : fileList) { + DocuDirent df = FileOps.fileForClass(fileClass, f, dirs); + df.setParent(this); // add the file to our list - list.get(fileClass.ordinal()).add(f); - f.setParent(this); + dl.add(df); } - // we sort the inner ArrayList (the list of files not the list of file types) - // for binarySearch to work (DocuDirent's natural sort order is by filename) - Collections.sort(list.get(fileClass.ordinal())); + /* + * we sort the inner ArrayList (the list of files not the list + * of file types) for binarySearch to work (DocuDirent's natural + * sort order is by filename) + */ + Collections.sort(dl); } } // clear the scaled directories - for (int j = 1; j < nb; j++) { - if (dirs[j] != null) { - dirs[j].clearFilenames(); + for (Directory d: dirs) { + if (d != null) { + d.clearFilenames(); } } // update number of cached files if this was the first time @@ -445,8 +448,7 @@ } private boolean isBasenameInList(List<DocuDirent> fileList, int idx, String fn) { - String dfn = FileOps.basename((fileList.get(idx)) - .getName()); + String dfn = FileOps.basename((fileList.get(idx)).getName()); return (dfn.equals(fn)||dfn.equals(FileOps.basename(fn))); } @@ -465,7 +467,7 @@ FileClass fc = FileOps.classForFilename(fn); int i = indexOf(fn, fc); if (i >= 0) { - return (DocuDirent) list.get(0).get(i); + return list.get(0).get(i); } return null; } @@ -484,7 +486,7 @@ public DocuDirent find(String fn, FileClass fc) { int i = indexOf(fn, fc); if (i >= 0) { - return (DocuDirent) list.get(fc.ordinal()).get(i); + return list.get(fc.ordinal()).get(i); } return null; }
--- a/servlet/src/digilib/io/FileOps.java Tue Jan 04 11:56:12 2011 +0100 +++ b/servlet/src/digilib/io/FileOps.java Wed Jan 05 12:12:19 2011 +0100 @@ -329,20 +329,20 @@ /** * Factory for DocuDirents based on file class. * - * Returns an ImageSet, TextFile or SVGFile. baseDirs and scalext are + * Returns an ImageSet, TextFile or SVGFile. scaleDirs are * only for ImageFilesets. * * @param fileClass * @param file - * @param baseDirs + * @param scaleDirs * optional additional parameters * @return */ - public static DocuDirent fileForClass(FileClass fileClass, File file, Directory[] baseDirs) { + public static DocuDirent fileForClass(FileClass fileClass, File file, Directory[] scaleDirs) { // what class of file do we have? if (fileClass == FileClass.IMAGE) { // image file - return new ImageFileSet(file, baseDirs); + return new ImageFileSet(file, scaleDirs); } else if (fileClass == FileClass.TEXT) { // text file return new TextFile(file);
--- a/servlet/src/digilib/io/ImageFile.java Tue Jan 04 11:56:12 2011 +0100 +++ b/servlet/src/digilib/io/ImageFile.java Wed Jan 05 12:12:19 2011 +0100 @@ -30,32 +30,46 @@ */ public class ImageFile extends ImageInput { + // file + private File file = null; // file name - private String filename = null; + private String name = null; // parent ImageSet private ImageSet parent = null; // parent directory private Directory dir = null; - public ImageFile(String fn, ImageSet parent, Directory dir) { - this.filename = fn; + /** Constructor with File. + * + * @param f + * @param parent + * @param dir + */ + public ImageFile(File f, ImageSet parent, Directory dir) { + this.file = f; + this.name = f.getName(); this.parent = parent; this.dir = dir; } - public ImageFile(String fn) { - File f = new File(fn); - this.dir = new Directory(f.getParentFile()); - this.filename = f.getName(); + /** Constructor with filename (without path). + * @param fn + * @param parent + * @param dir + */ + public ImageFile(String fn, ImageSet parent, Directory dir) { + this.name = fn; + this.dir = dir; + this.file = new File(this.dir.getDir(), fn); + this.parent = parent; } - /** Returns the file name (without path). * * @return */ public String getName() { - return filename; + return name; } @@ -63,11 +77,7 @@ * @return File */ public File getFile() { - if (dir == null) { - return null; - } - File f = new File(dir.getDir(), filename); - return f; + return file; } /** @@ -102,9 +112,8 @@ @Override public String toString() { // try to use File.toString - File f = getFile(); - if (f != null) { - return f.toString(); + if (file != null) { + return file.toString(); } return super.toString(); }
--- a/servlet/src/digilib/io/ImageFileSet.java Tue Jan 04 11:56:12 2011 +0100 +++ b/servlet/src/digilib/io/ImageFileSet.java Wed Jan 05 12:12:19 2011 +0100 @@ -29,23 +29,22 @@ /** Is the Metadata valid */ protected boolean metaChecked = false; /** the parent directory */ - protected Directory parent = null; + protected Directory parentDir = null; /** - * Constructor with a file and hints. - * - * The hints are expected to contain 'basedirs' and 'scaledfilext' keys. + * Constructor with a File and Directories. * * @param file - * @param baseDirs + * @param scaleDirs */ - public ImageFileSet(File file, Directory[] baseDirs) { - int nb = baseDirs.length; + public ImageFileSet(File file, Directory[] scaleDirs) { + int nb = scaleDirs.length; list = new ArrayList<ImageInput>(nb); - parent = baseDirs[0]; // TODO: is baseDir really our parent? + // first dir is our parent + parentDir = scaleDirs[0]; this.file = file; this.name = file.getName(); - fill(baseDirs, file); + fill(scaleDirs, file); } /* (non-Javadoc) @@ -59,14 +58,14 @@ * @see digilib.io.DocuDirent#getParent() */ public Directory getParent() { - return this.parent; + return this.parentDir; } /* (non-Javadoc) * @see digilib.io.DocuDirent#setParent(digilib.io.Directory) */ public void setParent(Directory parent) { - this.parent = parent; + this.parentDir = parent; } /* (non-Javadoc) @@ -136,21 +135,20 @@ * */ void fill(Directory[] dirs, File fl) { - int nb = dirs.length; String fn = fl.getName(); String baseFn = FileOps.basename(fn); // add the first ImageFile to the ImageSet - add(new ImageFile(fn, this, parent)); + add(new ImageFile(fl, this, parentDir)); // iterate the remaining base directories - for (int dirIdx = 1; dirIdx < nb; dirIdx++) { - if (dirs[dirIdx] == null) { + for (Directory dir: dirs) { + if (dir == null) { continue; } // read the directory - if (dirs[dirIdx].getFilenames() == null) { - dirs[dirIdx].readDir(); + if (dir.getFilenames() == null) { + dir.readDir(); } - String[] dirFiles = dirs[dirIdx].getFilenames(); + String[] dirFiles = dir.getFilenames(); // try the same filename as the original int fileIdx = Arrays.binarySearch(dirFiles, fn); if (fileIdx < 0) { @@ -178,7 +176,7 @@ if (FileOps.classForFilename(dirFiles[fileIdx]) == fileClass) { /* logger.debug("adding file " + dirFiles[fileIdx] + " to Fileset " + this.getName()); */ - add(new ImageFile(dirFiles[fileIdx], this, dirs[dirIdx])); + add(new ImageFile(dirFiles[fileIdx], this, dir)); } } } @@ -196,12 +194,12 @@ readMeta(); if (fileMeta == null) { // try directory metadata - ((DocuDirectory) parent).checkMeta(); - if (((DocuDirectory) parent).getDirMeta() != null) { - fileMeta = ((DocuDirectory) parent).getDirMeta(); + ((DocuDirectory) parentDir).checkMeta(); + if (((DocuDirectory) parentDir).getDirMeta() != null) { + fileMeta = ((DocuDirectory) parentDir).getDirMeta(); } else { // try parent directory metadata - DocuDirectory gp = (DocuDirectory) parent.getParent(); + DocuDirectory gp = (DocuDirectory) parentDir.getParent(); if (gp != null) { gp.checkMeta(); if (gp.getDirMeta() != null) {