187
|
1 /*
|
|
2 * FileOps -- Utility class for file operations
|
|
3 *
|
|
4 * Digital Image Library servlet components
|
|
5 *
|
|
6 * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify it
|
|
9 * under the terms of the GNU General Public License as published by the Free
|
|
10 * Software Foundation; either version 2 of the License, or (at your option)
|
|
11 * any later version.
|
|
12 *
|
|
13 * Please read license.txt for the full details. A copy of the GPL may be found
|
|
14 * at http://www.gnu.org/copyleft/lgpl.html
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License along with
|
|
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
18 * Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 */
|
1
|
21
|
|
22 package digilib.io;
|
|
23
|
159
|
24 import java.io.File;
|
|
25 import java.io.FileFilter;
|
259
|
26 import java.util.ArrayList;
|
|
27 import java.util.HashMap;
|
159
|
28 import java.util.Iterator;
|
259
|
29 import java.util.List;
|
|
30 import java.util.Map;
|
159
|
31 import java.util.StringTokenizer;
|
1
|
32
|
|
33 public class FileOps {
|
|
34
|
259
|
35 /**
|
|
36 * Array of file extensions and corresponding mime-types.
|
|
37 */
|
459
|
38 private static final String[][] ft = { { "jpg", "image/jpeg" },
|
259
|
39 { "jpeg", "image/jpeg" }, { "jp2", "image/jp2" },
|
|
40 { "png", "image/png" }, { "gif", "image/gif" },
|
|
41 { "tif", "image/tiff" }, { "tiff", "image/tiff" },
|
459
|
42 { "fpx", "image/fpx" },
|
259
|
43 { "txt", "text/plain" }, { "html", "text/html" },
|
|
44 { "htm", "text/html" }, { "xml", "text/xml" },
|
459
|
45 { "svg", "image/svg+xml" }, { "meta", "text/xml" } };
|
1
|
46
|
259
|
47 public static Map fileTypes;
|
|
48
|
|
49 public static List imageExtensions;
|
91
|
50
|
259
|
51 public static List textExtensions;
|
187
|
52
|
259
|
53 public static List svgExtensions;
|
187
|
54
|
159
|
55 public static final int CLASS_NONE = -1;
|
259
|
56
|
159
|
57 public static final int CLASS_IMAGE = 0;
|
259
|
58
|
159
|
59 public static final int CLASS_TEXT = 1;
|
259
|
60
|
187
|
61 public static final int CLASS_SVG = 2;
|
259
|
62
|
187
|
63 public static final int NUM_CLASSES = 3;
|
159
|
64
|
259
|
65 public static final Integer HINT_BASEDIRS = new Integer(1);
|
|
66
|
|
67 public static final Integer HINT_FILEEXT = new Integer(2);
|
|
68
|
|
69 public static final Integer HINT_DIRS = new Integer(3);
|
|
70
|
|
71 /**
|
|
72 * static initializer for FileOps
|
|
73 */
|
|
74 static {
|
|
75 fileTypes = new HashMap();
|
|
76 imageExtensions = new ArrayList();
|
|
77 textExtensions = new ArrayList();
|
|
78 svgExtensions = new ArrayList();
|
271
|
79 // iterate through file types in ft and fill the Map and Lists
|
259
|
80 for (int i = 0; i < ft.length; i++) {
|
|
81 String ext = ft[i][0];
|
|
82 String mt = ft[i][1];
|
|
83 fileTypes.put(ext, mt);
|
|
84 if (classForMimetype(mt) == CLASS_IMAGE) {
|
|
85 imageExtensions.add(ext);
|
|
86 } else if (classForMimetype(mt) == CLASS_TEXT) {
|
|
87 textExtensions.add(ext);
|
|
88 } else if (classForMimetype(mt) == CLASS_SVG) {
|
|
89 svgExtensions.add(ext);
|
|
90 }
|
|
91 }
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * returns the file class for a mime-type
|
|
96 *
|
|
97 * @param mt
|
|
98 * @return
|
|
99 */
|
|
100 public static int classForMimetype(String mt) {
|
|
101 if (mt == null) {
|
|
102 return CLASS_NONE;
|
|
103 }
|
|
104 if (mt.startsWith("image/svg")) {
|
|
105 return CLASS_SVG;
|
|
106 } else if (mt.startsWith("image")) {
|
|
107 return CLASS_IMAGE;
|
|
108 } else if (mt.startsWith("text")) {
|
|
109 return CLASS_TEXT;
|
|
110 }
|
|
111 return CLASS_NONE;
|
|
112 }
|
|
113
|
91
|
114 /**
|
187
|
115 * get the mime type for a file format (by extension)
|
91
|
116 */
|
|
117 public static String mimeForFile(File f) {
|
259
|
118 return (String) fileTypes.get(extname(f.getName().toLowerCase()));
|
91
|
119 }
|
1
|
120
|
159
|
121 /**
|
|
122 * get the file class for the filename (by extension)
|
187
|
123 *
|
159
|
124 * @param fn
|
|
125 * @return
|
|
126 */
|
|
127 public static int classForFilename(String fn) {
|
259
|
128 String mt = (String) fileTypes.get(extname(fn).toLowerCase());
|
|
129 return classForMimetype(mt);
|
91
|
130 }
|
1
|
131
|
159
|
132 public static Iterator getImageExtensionIterator() {
|
259
|
133 return imageExtensions.iterator();
|
159
|
134 }
|
|
135
|
|
136 public static Iterator getTextExtensionIterator() {
|
259
|
137 return textExtensions.iterator();
|
159
|
138 }
|
187
|
139
|
|
140 public static Iterator getSVGExtensionIterator() {
|
259
|
141 return svgExtensions.iterator();
|
187
|
142 }
|
|
143
|
91
|
144 /**
|
187
|
145 * convert a string with a list of pathnames into an array of strings using
|
|
146 * the system's path separator string
|
152
|
147 */
|
|
148 public static String[] pathToArray(String paths) {
|
|
149 // split list into directories
|
159
|
150 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
151 int n = dirs.countTokens();
|
|
152 if (n < 1) {
|
|
153 return null;
|
|
154 }
|
|
155 // add directories into array
|
|
156 String[] pathArray = new String[n];
|
|
157 for (int i = 0; i < n; i++) {
|
|
158 String s = dirs.nextToken();
|
|
159 // make shure the dir name ends with a directory separator
|
|
160 if (s.endsWith(File.separator)) {
|
|
161 pathArray[i] = s;
|
|
162 } else {
|
|
163 pathArray[i] = s + File.separator;
|
|
164 }
|
|
165 }
|
|
166 return pathArray;
|
|
167 }
|
187
|
168
|
|
169 /**
|
|
170 * Extract the base of a file name (sans extension).
|
176
|
171 *
|
187
|
172 * Returns the filename without the extension. The extension is the part
|
|
173 * behind the last dot in the filename. If the filename has no dot the full
|
|
174 * file name is returned.
|
|
175 *
|
176
|
176 * @param fn
|
|
177 * @return
|
|
178 */
|
|
179 public static String basename(String fn) {
|
271
|
180 if (fn == null) {
|
|
181 return null;
|
|
182 }
|
176
|
183 int i = fn.lastIndexOf('.');
|
|
184 if (i > 0) {
|
|
185 return fn.substring(0, i);
|
|
186 }
|
|
187 return fn;
|
|
188 }
|
|
189
|
187
|
190 /**
|
|
191 * Extract the extension of a file name.
|
176
|
192 *
|
|
193 * Returns the extension of a file name. The extension is the part behind
|
259
|
194 * the last dot in the filename. If the filename has no dot the empty string
|
|
195 * is returned.
|
187
|
196 *
|
176
|
197 * @param fn
|
|
198 * @return
|
|
199 */
|
|
200 public static String extname(String fn) {
|
271
|
201 if (fn == null) {
|
|
202 return null;
|
|
203 }
|
176
|
204 int i = fn.lastIndexOf('.');
|
|
205 if (i > 0) {
|
187
|
206 return fn.substring(i + 1);
|
176
|
207 }
|
|
208 return "";
|
|
209 }
|
152
|
210
|
246
|
211 /**
|
|
212 * Extract the parent directory of a (digilib) path name.
|
|
213 *
|
259
|
214 * Returns the parent directory of a path name. The parent is the part
|
|
215 * before the last slash in the path name. If the path name has no slash the
|
|
216 * empty string is returned.
|
246
|
217 *
|
|
218 * @param fn
|
|
219 * @return
|
|
220 */
|
|
221 public static String parent(String fn) {
|
271
|
222 if (fn == null) {
|
|
223 return null;
|
|
224 }
|
246
|
225 int i = fn.lastIndexOf('/');
|
|
226 if (i > 0) {
|
|
227 return fn.substring(0, i);
|
|
228 }
|
|
229 return "";
|
|
230 }
|
|
231
|
259
|
232 /**
|
|
233 * Normalize a path name.
|
197
|
234 *
|
|
235 * Removes leading and trailing slashes. Returns null if there is other
|
259
|
236 * unwanted stuff in the path name.
|
197
|
237 *
|
|
238 * @param pathname
|
|
239 * @return
|
|
240 */
|
|
241 public static String normalName(String pathname) {
|
271
|
242 if (pathname == null) {
|
|
243 return null;
|
|
244 }
|
197
|
245 // upper-dir references are unwanted
|
|
246 if (pathname.indexOf("../") >= 0) {
|
|
247 return null;
|
|
248 }
|
|
249 int a = 0;
|
209
|
250 int e = pathname.length() - 1;
|
|
251 if (e < 0) {
|
|
252 return pathname;
|
|
253 }
|
197
|
254 // leading and trailing "/" are removed
|
209
|
255 while ((a <= e) && (pathname.charAt(a) == '/')) {
|
197
|
256 a++;
|
|
257 }
|
209
|
258 while ((a < e) && (pathname.charAt(e) == '/')) {
|
259
|
259 e--;
|
197
|
260 }
|
209
|
261 return pathname.substring(a, e + 1);
|
197
|
262 }
|
259
|
263
|
|
264 /**
|
|
265 * FileFilter for general files
|
|
266 */
|
|
267 static class ReadableFileFilter implements FileFilter {
|
|
268
|
|
269 public boolean accept(File f) {
|
|
270 return f.canRead();
|
|
271 }
|
|
272 }
|
|
273
|
152
|
274 /**
|
187
|
275 * FileFilter for image types (helper class for getFile)
|
91
|
276 */
|
|
277 static class ImageFileFilter implements FileFilter {
|
1
|
278
|
91
|
279 public boolean accept(File f) {
|
259
|
280 return (classForFilename(f.getName()) == CLASS_IMAGE);
|
187
|
281 }
|
|
282 }
|
|
283
|
|
284 /**
|
|
285 * FileFilter for text types (helper class for getFile)
|
|
286 */
|
|
287 static class TextFileFilter implements FileFilter {
|
|
288
|
|
289 public boolean accept(File f) {
|
259
|
290 return (classForFilename(f.getName()) == CLASS_TEXT);
|
91
|
291 }
|
|
292 }
|
1
|
293
|
159
|
294 /**
|
187
|
295 * FileFilter for svg types (helper class for getFile).
|
|
296 *
|
159
|
297 */
|
187
|
298 static class SVGFileFilter implements FileFilter {
|
159
|
299
|
|
300 public boolean accept(File f) {
|
259
|
301 return (classForFilename(f.getName()) == CLASS_SVG);
|
159
|
302 }
|
|
303 }
|
|
304
|
187
|
305 /**
|
|
306 * Factory for FileFilters (image or text).
|
159
|
307 *
|
|
308 * @param fileClass
|
|
309 * @return
|
187
|
310 */
|
159
|
311 public static FileFilter filterForClass(int fileClass) {
|
|
312 if (fileClass == CLASS_IMAGE) {
|
|
313 return new ImageFileFilter();
|
|
314 }
|
|
315 if (fileClass == CLASS_TEXT) {
|
|
316 return new TextFileFilter();
|
|
317 }
|
187
|
318 if (fileClass == CLASS_SVG) {
|
|
319 return new SVGFileFilter();
|
|
320 }
|
159
|
321 return null;
|
|
322 }
|
|
323
|
259
|
324 /**
|
|
325 * Factory for DocuDirents based on file class.
|
187
|
326 *
|
259
|
327 * Returns an ImageFileset, TextFile or SVGFile. baseDirs and scalext are
|
|
328 * only for ImageFilesets.
|
187
|
329 *
|
|
330 * @param fileClass
|
|
331 * @param file
|
259
|
332 * @param hints
|
|
333 * optional additional parameters
|
187
|
334 * @return
|
|
335 */
|
259
|
336 public static DocuDirent fileForClass(int fileClass, File file, Map hints) {
|
187
|
337 // what class of file do we have?
|
|
338 if (fileClass == CLASS_IMAGE) {
|
|
339 // image file
|
259
|
340 return new ImageFileset(file, hints);
|
187
|
341 } else if (fileClass == CLASS_TEXT) {
|
|
342 // text file
|
259
|
343 return new TextFile(file);
|
187
|
344 } else if (fileClass == CLASS_SVG) {
|
|
345 // text file
|
259
|
346 return new SVGFile(file);
|
187
|
347 }
|
|
348 return null;
|
|
349 }
|
259
|
350
|
|
351 /**
|
|
352 * Filters a list of Files through a FileFilter.
|
|
353 *
|
|
354 * @param files
|
|
355 * @param filter
|
|
356 * @return
|
|
357 */
|
|
358 public static File[] listFiles(File[] files, FileFilter filter) {
|
|
359 if (files == null) {
|
|
360 return null;
|
|
361 }
|
|
362 File[] ff = new File[files.length];
|
|
363 int ffi = 0;
|
|
364 for (int i = 0; i < files.length; i++) {
|
|
365 if (filter.accept(files[i])) {
|
|
366 ff[ffi] = files[i];
|
|
367 ffi++;
|
|
368 }
|
|
369 }
|
|
370 File[] fff = new File[ffi];
|
|
371 System.arraycopy(ff, 0, fff, 0, ffi);
|
|
372 return fff;
|
|
373 }
|
|
374
|
|
375 /**
|
|
376 * Creates a new hints Map with the given first element.
|
|
377 *
|
|
378 * @param type
|
|
379 * @param value
|
|
380 * @return
|
|
381 */
|
|
382 public static Map newHints(Integer type, Object value) {
|
271
|
383 Map m = new HashMap();
|
259
|
384 if (type != null) {
|
|
385 m.put(type, value);
|
|
386 }
|
|
387 return m;
|
|
388 }
|
|
389
|
1
|
390 }
|