diff servlet/src/digilib/io/FileOps.java @ 159:e743b853efca

servlet version 1.16a4 - rather experimental - new Texter servlet for sending text - reads and caches text files in DocuDirCache - DocuFile renamed to ImageFile (-Set) - new TextFile
author robcast
date Tue, 16 Sep 2003 18:32:00 +0200
parents f4a5cfe37469
children 67ff8c7fecb9
line wrap: on
line diff
--- a/servlet/src/digilib/io/FileOps.java	Tue Sep 16 18:26:31 2003 +0200
+++ b/servlet/src/digilib/io/FileOps.java	Tue Sep 16 18:32:00 2003 +0200
@@ -20,14 +20,18 @@
 
 package digilib.io;
 
-import java.io.*;
-import java.util.*;
+import java.io.File;
+import java.io.FileFilter;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.StringTokenizer;
 
-import digilib.*;
+import digilib.Utils;
 
 public class FileOps {
 
 	private Utils util = null;
+	
 	public static String[] fileTypes =
 		{
 			"jpg",
@@ -43,11 +47,28 @@
 			"tif",
 			"image/tiff",
 			"tiff",
-			"image/tiff" };
+			"image/tiff",
+			"txt",
+			"text/plain",
+			"html",
+			"text/html",
+			"htm",
+			"text/html",
+			"xml",
+			"text/xml" };
 
-	public static String[] fileExtensions =
+	public static String[] imageExtensions =
 		{ "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
 
+	public static String[] textExtensions =
+		{ "txt", "html", "htm", "xml"};
+		
+	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 FileOps() {
 		util = new Utils();
 	}
@@ -73,18 +94,43 @@
 		return null;
 	}
 
-	public static Iterator getImageExtensionIterator() {
-		return Arrays.asList(fileExtensions).iterator();
+	/**
+	 * 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 ++) {
+			if (fn.toLowerCase().endsWith(imageExtensions[i])) {
+				return CLASS_IMAGE;
+			}
+		}
+		n = textExtensions.length;
+		for (int i = 0; i < n; i ++) {
+			if (fn.toLowerCase().endsWith(textExtensions[i])) {
+				return CLASS_TEXT;
+			}
+		}
+		return CLASS_NONE;
+		
 	}
 
+	public static Iterator getImageExtensionIterator() {
+		return Arrays.asList(imageExtensions).iterator();
+	}
+
+	public static Iterator getTextExtensionIterator() {
+		return Arrays.asList(textExtensions).iterator();
+	}
+	
 	/**
 	 * 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
-		StringTokenizer dirs =
-			new StringTokenizer(paths, File.pathSeparator);
+		StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
 		int n = dirs.countTokens();
 		if (n < 1) {
 			return null;
@@ -104,114 +150,46 @@
 	}
 
 	/**
-	 *  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 + ")");
-
-		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 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 + ")");
-
-		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);
-	}
-
-	/**
-	 *  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;
-
-		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 {
 
 		public boolean accept(File f) {
 			if (f.isFile()) {
-				return (mimeForFile(f) != null);
+				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;
+			}
+		}
+	}
+
+	/** Factory for FileFilters (image or text).
+	 * 
+	 * @param fileClass
+	 * @return
+	 */ 
+	public static FileFilter filterForClass(int fileClass) {
+		if (fileClass == CLASS_IMAGE) {
+			return new ImageFileFilter();
+		}
+		if (fileClass == CLASS_TEXT) {
+			return new TextFileFilter();
+		}
+		return null;
+	}
+
 }