changeset 187:b3f9a7f646c5

Servlet version 1.18b3 new Raster servlet - new Raster servlet for rastering SVG graphics into PNG - new SVGFile class and accompanying changes - fixes in DocuDirectory for better handling of other file classes
author robcast
date Fri, 28 Nov 2003 13:19:37 +0100
parents 26b2a74e2fe5
children dde98eaf97ff
files servlet/src/digilib/io/DocuDirCache.java servlet/src/digilib/io/DocuDirectory.java servlet/src/digilib/io/FileOps.java
diffstat 3 files changed, 131 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/io/DocuDirCache.java	Tue Nov 25 19:26:46 2003 +0100
+++ b/servlet/src/digilib/io/DocuDirCache.java	Fri Nov 28 13:19:37 2003 +0100
@@ -149,10 +149,10 @@
 	}
 
 	/**
-	 * Returns the ImageFileset with the pathname <code>fn</code> and the
+	 * Returns the DocuDirent with the pathname <code>fn</code> and the
 	 * index <code>in</code> and the class <code>fc</code>.
 	 * 
-	 * If <code>fn</code> is a file then the corresponding Fileset is
+	 * If <code>fn</code> is a file then the corresponding DocuDirent is
 	 * returned and the index is ignored.
 	 * 
 	 * @param fn
@@ -214,7 +214,7 @@
 		if (dd.isValid()) {
 			try {
 				return dd.get(n, fc);
-			} catch (ArrayIndexOutOfBoundsException e) {
+			} catch (IndexOutOfBoundsException e) {
 			}
 		}
 		return null;
--- a/servlet/src/digilib/io/DocuDirectory.java	Tue Nov 25 19:26:46 2003 +0100
+++ b/servlet/src/digilib/io/DocuDirectory.java	Fri Nov 28 13:19:37 2003 +0100
@@ -196,15 +196,7 @@
 				// sort the file names alphabetically and iterate the list
 				Arrays.sort(fl);
 				for (int i = 0; i < nf; i++) {
-					DocuDirent f = null;
-					// what class of file do we have?
-					if (fc == FileOps.CLASS_IMAGE) {
-						// image file
-						f = new ImageFileset(dirs, fl[i], scalext);
-					} else if (fc == FileOps.CLASS_TEXT) {
-						// text file
-						f = new TextFile(fl[i]);
-					}
+					DocuDirent f = FileOps.fileForClass(fc, fl[i], dirs, scalext);
 					// add the file to our list
 					list[fc].add(f);
 					f.setParent(this);
@@ -373,7 +365,7 @@
 		// linear search -> worst performance
 		int n = list[fc].size();
 		for (int i = 0; i < n; i++) {
-			ImageFileset fs = (ImageFileset) list[fc].get(i);
+			DocuDirent fs = (DocuDirent) list[fc].get(i);
 			if (fs.getName().equals(fn)) {
 				// filename matches
 				return i;
@@ -381,7 +373,7 @@
 		}
 		// try again without extension
 		for (int i = 0; i < n; i++) {
-			ImageFileset fs = (ImageFileset) list[fc].get(i);
+			DocuDirent fs = (DocuDirent) list[fc].get(i);
 			if (fs.getBasename().equals(FileOps.basename(fn))) {
 				// basename matches
 				return i;
@@ -390,35 +382,35 @@
 		return -1;
 	}
 
-	/** Finds the ImageFileset with the name <code>fn</code>.
+	/** Finds the DocuDirent with the name <code>fn</code>.
 	 * 
-	 * Searches the directory for the ImageFileset with the name <code>fn</code> and returns 
+	 * Searches the directory for the DocuDirent with the name <code>fn</code> and returns 
 	 * it. Returns null if the file cannot be found. 
 	 *  
 	 * @param fn filename
-	 * @return ImageFileset
+	 * @return DocuDirent
 	 */
-	public ImageFileset find(String fn) {
+	public DocuDirent find(String fn) {
 		int fc = FileOps.classForFilename(fn);
 		int i = indexOf(fn, fc);
 		if (i >= 0) {
-			return (ImageFileset) list[0].get(i);
+			return (DocuDirent) list[0].get(i);
 		}
 		return null;
 	}
 
-	/** Finds the ImageFileset with the name <code>fn</code> and class <code>fc</code>.
+	/** Finds the DocuDirent with the name <code>fn</code> and class <code>fc</code>.
 	 * 
-	 * Searches the directory for the ImageFileset with the name <code>fn</code> and returns 
+	 * Searches the directory for the DocuDirent with the name <code>fn</code> and returns 
 	 * it. Returns null if the file cannot be found. 
 	 *  
 	 * @param fn filename
-	 * @return ImageFileset
+	 * @return DocuDirent
 	 */
-	public ImageFileset find(String fn, int fc) {
+	public DocuDirent find(String fn, int fc) {
 		int i = indexOf(fn, fc);
 		if (i >= 0) {
-			return (ImageFileset) list[fc].get(i);
+			return (DocuDirent) list[fc].get(i);
 		}
 		return null;
 	}
--- a/servlet/src/digilib/io/FileOps.java	Tue Nov 25 19:26:46 2003 +0100
+++ b/servlet/src/digilib/io/FileOps.java	Fri Nov 28 13:19:37 2003 +0100
@@ -1,22 +1,23 @@
-/* FileOps -- Utility class for file operations
-
-  Digital Image Library servlet components
-
-  Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
-
-  This program is free software; you can redistribute  it and/or modify it
-  under  the terms of  the GNU General  Public License as published by the
-  Free Software Foundation;  either version 2 of the  License, or (at your
-  option) any later version.
-   
-  Please read license.txt for the full details. A copy of the GPL
-  may be found at http://www.gnu.org/copyleft/lgpl.html
-
-  You should have received a copy of the GNU General Public License
-  along with this program; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-
-*/
+/*
+ * FileOps -- Utility class for file operations
+ * 
+ * Digital Image Library servlet components
+ * 
+ * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
+ * 
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ * 
+ * Please read license.txt for the full details. A copy of the GPL may be found
+ * at http://www.gnu.org/copyleft/lgpl.html
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ *  
+ */
 
 package digilib.io;
 
@@ -51,22 +52,25 @@
 			"htm",
 			"text/html",
 			"xml",
-			"text/xml" };
+			"text/xml",
+			"svg",
+			"image/svg+xml" };
 
 	public static String[] imageExtensions =
 		{ "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
 
-	public static String[] textExtensions =
-		{ "txt", "html", "htm", "xml"};
-		
+	public static String[] textExtensions = { "txt", "html", "htm", "xml" };
+
+	public static String[] svgExtensions = { "svg" };
+
 	public static final int CLASS_NONE = -1;
 	public static final int CLASS_IMAGE = 0;
 	public static final int CLASS_TEXT = 1;
-	public static final int NUM_CLASSES = 2;
-	
+	public static final int CLASS_SVG = 2;
+	public static final int NUM_CLASSES = 3;
 
 	/**
-	 *  get the mime type for a file format (by extension)
+	 * get the mime type for a file format (by extension)
 	 */
 	public static String mimeForFile(File f) {
 		String fn = f.getName();
@@ -80,24 +84,25 @@
 
 	/**
 	 * get the file class for the filename (by extension)
+	 * 
 	 * @param fn
 	 * @return
 	 */
 	public static int classForFilename(String fn) {
 		int n = imageExtensions.length;
-		for (int i = 0; i < n; i ++) {
+		for (int i = 0; i < n; i++) {
 			if (fn.toLowerCase().endsWith(imageExtensions[i])) {
 				return CLASS_IMAGE;
 			}
 		}
 		n = textExtensions.length;
-		for (int i = 0; i < n; i ++) {
+		for (int i = 0; i < n; i++) {
 			if (fn.toLowerCase().endsWith(textExtensions[i])) {
 				return CLASS_TEXT;
 			}
 		}
 		return CLASS_NONE;
-		
+
 	}
 
 	public static Iterator getImageExtensionIterator() {
@@ -107,10 +112,14 @@
 	public static Iterator getTextExtensionIterator() {
 		return Arrays.asList(textExtensions).iterator();
 	}
-	
+
+	public static Iterator getSVGExtensionIterator() {
+		return Arrays.asList(svgExtensions).iterator();
+	}
+
 	/**
-	 * convert a string with a list of pathnames into an array of strings
-	 * using the system's path separator string
+	 * convert a string with a list of pathnames into an array of strings using
+	 * the system's path separator string
 	 */
 	public static String[] pathToArray(String paths) {
 		// split list into directories
@@ -132,13 +141,14 @@
 		}
 		return pathArray;
 	}
-	
-	/** Extract the base of a file name (sans extension).
+
+	/**
+	 * Extract the base of a file name (sans extension).
 	 * 
-	 * Returns the filename without the extension. The extension is the part behind
-	 * the last dot in the filename. If the filename has no dot the full file name 
-	 * is returned.
-	 *  
+	 * Returns the filename without the extension. The extension is the part
+	 * behind the last dot in the filename. If the filename has no dot the full
+	 * file name is returned.
+	 * 
 	 * @param fn
 	 * @return
 	 */
@@ -150,31 +160,50 @@
 		return fn;
 	}
 
-	/** Extract the extension of a file name.
+	/**
+	 * Extract the extension of a file name.
 	 * 
 	 * Returns the extension of a file name. The extension is the part behind
-	 * the last dot in the filename. If the filename has no dot the empty string 
-	 * is returned.
-	 *  
+	 * the last dot in the filename. If the filename has no dot the empty
+	 * string is returned.
+	 * 
 	 * @param fn
 	 * @return
 	 */
 	public static String extname(String fn) {
 		int i = fn.lastIndexOf('.');
 		if (i > 0) {
-			return fn.substring(i+1);
+			return fn.substring(i + 1);
 		}
 		return "";
 	}
 
 	/**
-	 *  FileFilter for image types (helper class for getFile)
+	 * 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)&&(mimeForFile(f).startsWith("image")));
+				return (
+					(mimeForFile(f) != null)
+						&& (mimeForFile(f).startsWith("image")));
+			} else {
+				return false;
+			}
+		}
+	}
+
+	/**
+	 * FileFilter for text types (helper class for getFile)
+	 */
+	static class TextFileFilter implements FileFilter {
+
+		public boolean accept(File f) {
+			if (f.isFile()) {
+				return (
+					(mimeForFile(f) != null)
+						&& (mimeForFile(f).startsWith("text")));
 			} else {
 				return false;
 			}
@@ -182,24 +211,28 @@
 	}
 
 	/**
-	 *  FileFilter for text types (helper class for getFile)
+	 * FileFilter for svg types (helper class for getFile).
+	 *  
 	 */
-	static class TextFileFilter implements FileFilter {
+	static class SVGFileFilter implements FileFilter {
 
 		public boolean accept(File f) {
 			if (f.isFile()) {
-				return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("text")));
+				return (
+					(mimeForFile(f) != null)
+						&& (mimeForFile(f).startsWith("image/svg")));
 			} else {
 				return false;
 			}
 		}
 	}
 
-	/** Factory for FileFilters (image or text).
+	/**
+	 * Factory for FileFilters (image or text).
 	 * 
 	 * @param fileClass
 	 * @return
-	 */ 
+	 */
 	public static FileFilter filterForClass(int fileClass) {
 		if (fileClass == CLASS_IMAGE) {
 			return new ImageFileFilter();
@@ -207,7 +240,39 @@
 		if (fileClass == CLASS_TEXT) {
 			return new TextFileFilter();
 		}
+		if (fileClass == CLASS_SVG) {
+			return new SVGFileFilter();
+		}
 		return null;
 	}
 
+	/** Factory for DocuDirents based on file class.
+	 * 
+	 * Returns an ImageFileset, TextFile or SVGFile. 
+	 * baseDirs and scalext are only for ImageFilesets.
+	 * 
+	 * @param fileClass
+	 * @param file
+	 * @param baseDirs array of base directories (for ImageFileset)
+	 * @param scalext first extension to try for scaled images (for ImageFileset)
+	 * @return
+	 */
+	public static DocuDirent fileForClass(
+		int fileClass,
+		File file,
+		Directory[] baseDirs,
+		String scalext) {
+		// what class of file do we have?
+		if (fileClass == CLASS_IMAGE) {
+			// image file
+			return new ImageFileset(baseDirs, file, scalext);
+		} else if (fileClass == CLASS_TEXT) {
+			// text file
+			return  new TextFile(file);
+		} else if (fileClass == CLASS_SVG) {
+			// text file
+			return  new SVGFile(file);
+		}
+		return null;
+	}
 }