comparison servlet/src/digilib/io/FileOps.java @ 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 91f28e4fee8f
children bb4ed821d06e
comparison
equal deleted inserted replaced
186:26b2a74e2fe5 187:b3f9a7f646c5
1 /* FileOps -- Utility class for file operations 1 /*
2 2 * FileOps -- Utility class for file operations
3 Digital Image Library servlet components 3 *
4 4 * Digital Image Library servlet components
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) 5 *
6 6 * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
7 This program is free software; you can redistribute it and/or modify it 7 *
8 under the terms of the GNU General Public License as published by the 8 * This program is free software; you can redistribute it and/or modify it
9 Free Software Foundation; either version 2 of the License, or (at your 9 * under the terms of the GNU General Public License as published by the Free
10 option) any later version. 10 * Software Foundation; either version 2 of the License, or (at your option)
11 11 * any later version.
12 Please read license.txt for the full details. A copy of the GPL 12 *
13 may be found at http://www.gnu.org/copyleft/lgpl.html 13 * Please read license.txt for the full details. A copy of the GPL may be found
14 14 * at http://www.gnu.org/copyleft/lgpl.html
15 You should have received a copy of the GNU General Public License 15 *
16 along with this program; if not, write to the Free Software 16 * You should have received a copy of the GNU General Public License along with
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 18 * Place, Suite 330, Boston, MA 02111-1307 USA
19 */ 19 *
20 */
20 21
21 package digilib.io; 22 package digilib.io;
22 23
23 import java.io.File; 24 import java.io.File;
24 import java.io.FileFilter; 25 import java.io.FileFilter;
49 "html", 50 "html",
50 "text/html", 51 "text/html",
51 "htm", 52 "htm",
52 "text/html", 53 "text/html",
53 "xml", 54 "xml",
54 "text/xml" }; 55 "text/xml",
56 "svg",
57 "image/svg+xml" };
55 58
56 public static String[] imageExtensions = 59 public static String[] imageExtensions =
57 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" }; 60 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
58 61
59 public static String[] textExtensions = 62 public static String[] textExtensions = { "txt", "html", "htm", "xml" };
60 { "txt", "html", "htm", "xml"}; 63
61 64 public static String[] svgExtensions = { "svg" };
65
62 public static final int CLASS_NONE = -1; 66 public static final int CLASS_NONE = -1;
63 public static final int CLASS_IMAGE = 0; 67 public static final int CLASS_IMAGE = 0;
64 public static final int CLASS_TEXT = 1; 68 public static final int CLASS_TEXT = 1;
65 public static final int NUM_CLASSES = 2; 69 public static final int CLASS_SVG = 2;
66 70 public static final int NUM_CLASSES = 3;
67 71
68 /** 72 /**
69 * get the mime type for a file format (by extension) 73 * get the mime type for a file format (by extension)
70 */ 74 */
71 public static String mimeForFile(File f) { 75 public static String mimeForFile(File f) {
72 String fn = f.getName(); 76 String fn = f.getName();
73 for (int i = 0; i < fileTypes.length; i += 2) { 77 for (int i = 0; i < fileTypes.length; i += 2) {
74 if (fn.toLowerCase().endsWith(fileTypes[i])) { 78 if (fn.toLowerCase().endsWith(fileTypes[i])) {
78 return null; 82 return null;
79 } 83 }
80 84
81 /** 85 /**
82 * get the file class for the filename (by extension) 86 * get the file class for the filename (by extension)
87 *
83 * @param fn 88 * @param fn
84 * @return 89 * @return
85 */ 90 */
86 public static int classForFilename(String fn) { 91 public static int classForFilename(String fn) {
87 int n = imageExtensions.length; 92 int n = imageExtensions.length;
88 for (int i = 0; i < n; i ++) { 93 for (int i = 0; i < n; i++) {
89 if (fn.toLowerCase().endsWith(imageExtensions[i])) { 94 if (fn.toLowerCase().endsWith(imageExtensions[i])) {
90 return CLASS_IMAGE; 95 return CLASS_IMAGE;
91 } 96 }
92 } 97 }
93 n = textExtensions.length; 98 n = textExtensions.length;
94 for (int i = 0; i < n; i ++) { 99 for (int i = 0; i < n; i++) {
95 if (fn.toLowerCase().endsWith(textExtensions[i])) { 100 if (fn.toLowerCase().endsWith(textExtensions[i])) {
96 return CLASS_TEXT; 101 return CLASS_TEXT;
97 } 102 }
98 } 103 }
99 return CLASS_NONE; 104 return CLASS_NONE;
100 105
101 } 106 }
102 107
103 public static Iterator getImageExtensionIterator() { 108 public static Iterator getImageExtensionIterator() {
104 return Arrays.asList(imageExtensions).iterator(); 109 return Arrays.asList(imageExtensions).iterator();
105 } 110 }
106 111
107 public static Iterator getTextExtensionIterator() { 112 public static Iterator getTextExtensionIterator() {
108 return Arrays.asList(textExtensions).iterator(); 113 return Arrays.asList(textExtensions).iterator();
109 } 114 }
110 115
111 /** 116 public static Iterator getSVGExtensionIterator() {
112 * convert a string with a list of pathnames into an array of strings 117 return Arrays.asList(svgExtensions).iterator();
113 * using the system's path separator string 118 }
119
120 /**
121 * convert a string with a list of pathnames into an array of strings using
122 * the system's path separator string
114 */ 123 */
115 public static String[] pathToArray(String paths) { 124 public static String[] pathToArray(String paths) {
116 // split list into directories 125 // split list into directories
117 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator); 126 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
118 int n = dirs.countTokens(); 127 int n = dirs.countTokens();
130 pathArray[i] = s + File.separator; 139 pathArray[i] = s + File.separator;
131 } 140 }
132 } 141 }
133 return pathArray; 142 return pathArray;
134 } 143 }
135 144
136 /** Extract the base of a file name (sans extension). 145 /**
137 * 146 * Extract the base of a file name (sans extension).
138 * Returns the filename without the extension. The extension is the part behind 147 *
139 * the last dot in the filename. If the filename has no dot the full file name 148 * Returns the filename without the extension. The extension is the part
140 * is returned. 149 * behind the last dot in the filename. If the filename has no dot the full
141 * 150 * file name is returned.
151 *
142 * @param fn 152 * @param fn
143 * @return 153 * @return
144 */ 154 */
145 public static String basename(String fn) { 155 public static String basename(String fn) {
146 int i = fn.lastIndexOf('.'); 156 int i = fn.lastIndexOf('.');
148 return fn.substring(0, i); 158 return fn.substring(0, i);
149 } 159 }
150 return fn; 160 return fn;
151 } 161 }
152 162
153 /** Extract the extension of a file name. 163 /**
164 * Extract the extension of a file name.
154 * 165 *
155 * Returns the extension of a file name. The extension is the part behind 166 * Returns the extension of a file name. The extension is the part behind
156 * the last dot in the filename. If the filename has no dot the empty string 167 * the last dot in the filename. If the filename has no dot the empty
157 * is returned. 168 * string is returned.
158 * 169 *
159 * @param fn 170 * @param fn
160 * @return 171 * @return
161 */ 172 */
162 public static String extname(String fn) { 173 public static String extname(String fn) {
163 int i = fn.lastIndexOf('.'); 174 int i = fn.lastIndexOf('.');
164 if (i > 0) { 175 if (i > 0) {
165 return fn.substring(i+1); 176 return fn.substring(i + 1);
166 } 177 }
167 return ""; 178 return "";
168 } 179 }
169 180
170 /** 181 /**
171 * FileFilter for image types (helper class for getFile) 182 * FileFilter for image types (helper class for getFile)
172 */ 183 */
173 static class ImageFileFilter implements FileFilter { 184 static class ImageFileFilter implements FileFilter {
174 185
175 public boolean accept(File f) { 186 public boolean accept(File f) {
176 if (f.isFile()) { 187 if (f.isFile()) {
177 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("image"))); 188 return (
189 (mimeForFile(f) != null)
190 && (mimeForFile(f).startsWith("image")));
178 } else { 191 } else {
179 return false; 192 return false;
180 } 193 }
181 } 194 }
182 } 195 }
183 196
184 /** 197 /**
185 * FileFilter for text types (helper class for getFile) 198 * FileFilter for text types (helper class for getFile)
186 */ 199 */
187 static class TextFileFilter implements FileFilter { 200 static class TextFileFilter implements FileFilter {
188 201
189 public boolean accept(File f) { 202 public boolean accept(File f) {
190 if (f.isFile()) { 203 if (f.isFile()) {
191 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("text"))); 204 return (
205 (mimeForFile(f) != null)
206 && (mimeForFile(f).startsWith("text")));
192 } else { 207 } else {
193 return false; 208 return false;
194 } 209 }
195 } 210 }
196 } 211 }
197 212
198 /** Factory for FileFilters (image or text). 213 /**
214 * FileFilter for svg types (helper class for getFile).
215 *
216 */
217 static class SVGFileFilter implements FileFilter {
218
219 public boolean accept(File f) {
220 if (f.isFile()) {
221 return (
222 (mimeForFile(f) != null)
223 && (mimeForFile(f).startsWith("image/svg")));
224 } else {
225 return false;
226 }
227 }
228 }
229
230 /**
231 * Factory for FileFilters (image or text).
199 * 232 *
200 * @param fileClass 233 * @param fileClass
201 * @return 234 * @return
202 */ 235 */
203 public static FileFilter filterForClass(int fileClass) { 236 public static FileFilter filterForClass(int fileClass) {
204 if (fileClass == CLASS_IMAGE) { 237 if (fileClass == CLASS_IMAGE) {
205 return new ImageFileFilter(); 238 return new ImageFileFilter();
206 } 239 }
207 if (fileClass == CLASS_TEXT) { 240 if (fileClass == CLASS_TEXT) {
208 return new TextFileFilter(); 241 return new TextFileFilter();
209 } 242 }
243 if (fileClass == CLASS_SVG) {
244 return new SVGFileFilter();
245 }
210 return null; 246 return null;
211 } 247 }
212 248
249 /** Factory for DocuDirents based on file class.
250 *
251 * Returns an ImageFileset, TextFile or SVGFile.
252 * baseDirs and scalext are only for ImageFilesets.
253 *
254 * @param fileClass
255 * @param file
256 * @param baseDirs array of base directories (for ImageFileset)
257 * @param scalext first extension to try for scaled images (for ImageFileset)
258 * @return
259 */
260 public static DocuDirent fileForClass(
261 int fileClass,
262 File file,
263 Directory[] baseDirs,
264 String scalext) {
265 // what class of file do we have?
266 if (fileClass == CLASS_IMAGE) {
267 // image file
268 return new ImageFileset(baseDirs, file, scalext);
269 } else if (fileClass == CLASS_TEXT) {
270 // text file
271 return new TextFile(file);
272 } else if (fileClass == CLASS_SVG) {
273 // text file
274 return new SVGFile(file);
275 }
276 return null;
277 }
213 } 278 }