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
|
531
|
47 public static Map<String, String> fileTypes;
|
259
|
48
|
531
|
49 public static List<String> imageExtensions;
|
91
|
50
|
531
|
51 public static List<String> textExtensions;
|
187
|
52
|
531
|
53 public static List<String> 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 {
|
531
|
75 fileTypes = new HashMap<String, String>();
|
|
76 imageExtensions = new ArrayList<String>();
|
|
77 textExtensions = new ArrayList<String>();
|
|
78 svgExtensions = new ArrayList<String>();
|
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
|
531
|
132 public static Iterator<String> getImageExtensionIterator() {
|
259
|
133 return imageExtensions.iterator();
|
159
|
134 }
|
|
135
|
531
|
136 public static List<String> getImageExtensions() {
|
|
137 return imageExtensions;
|
|
138 }
|
|
139
|
|
140 public static Iterator<String> getTextExtensionIterator() {
|
259
|
141 return textExtensions.iterator();
|
159
|
142 }
|
187
|
143
|
531
|
144 public static List<String> getTextExtensions() {
|
|
145 return textExtensions;
|
|
146 }
|
|
147
|
|
148 public static Iterator<String> getSVGExtensionIterator() {
|
259
|
149 return svgExtensions.iterator();
|
187
|
150 }
|
|
151
|
531
|
152 public static List<String> getSvgExtensions() {
|
|
153 return svgExtensions;
|
|
154 }
|
1
|
155
|
|
156
|
91
|
157 /**
|
187
|
158 * convert a string with a list of pathnames into an array of strings using
|
|
159 * the system's path separator string
|
152
|
160 */
|
|
161 public static String[] pathToArray(String paths) {
|
|
162 // split list into directories
|
159
|
163 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
164 int n = dirs.countTokens();
|
|
165 if (n < 1) {
|
|
166 return null;
|
|
167 }
|
|
168 // add directories into array
|
|
169 String[] pathArray = new String[n];
|
|
170 for (int i = 0; i < n; i++) {
|
|
171 String s = dirs.nextToken();
|
|
172 // make shure the dir name ends with a directory separator
|
|
173 if (s.endsWith(File.separator)) {
|
|
174 pathArray[i] = s;
|
|
175 } else {
|
|
176 pathArray[i] = s + File.separator;
|
|
177 }
|
|
178 }
|
|
179 return pathArray;
|
|
180 }
|
187
|
181
|
|
182 /**
|
|
183 * Extract the base of a file name (sans extension).
|
176
|
184 *
|
187
|
185 * Returns the filename without the extension. The extension is the part
|
|
186 * behind the last dot in the filename. If the filename has no dot the full
|
|
187 * file name is returned.
|
|
188 *
|
176
|
189 * @param fn
|
|
190 * @return
|
|
191 */
|
|
192 public static String basename(String fn) {
|
271
|
193 if (fn == null) {
|
|
194 return null;
|
|
195 }
|
176
|
196 int i = fn.lastIndexOf('.');
|
|
197 if (i > 0) {
|
|
198 return fn.substring(0, i);
|
|
199 }
|
|
200 return fn;
|
|
201 }
|
|
202
|
187
|
203 /**
|
|
204 * Extract the extension of a file name.
|
176
|
205 *
|
|
206 * Returns the extension of a file name. The extension is the part behind
|
259
|
207 * the last dot in the filename. If the filename has no dot the empty string
|
|
208 * is returned.
|
187
|
209 *
|
176
|
210 * @param fn
|
|
211 * @return
|
|
212 */
|
|
213 public static String extname(String fn) {
|
271
|
214 if (fn == null) {
|
|
215 return null;
|
|
216 }
|
176
|
217 int i = fn.lastIndexOf('.');
|
|
218 if (i > 0) {
|
187
|
219 return fn.substring(i + 1);
|
176
|
220 }
|
|
221 return "";
|
|
222 }
|
152
|
223
|
246
|
224 /**
|
|
225 * Extract the parent directory of a (digilib) path name.
|
|
226 *
|
259
|
227 * Returns the parent directory of a path name. The parent is the part
|
|
228 * before the last slash in the path name. If the path name has no slash the
|
|
229 * empty string is returned.
|
246
|
230 *
|
|
231 * @param fn
|
|
232 * @return
|
|
233 */
|
|
234 public static String parent(String fn) {
|
271
|
235 if (fn == null) {
|
|
236 return null;
|
|
237 }
|
246
|
238 int i = fn.lastIndexOf('/');
|
|
239 if (i > 0) {
|
|
240 return fn.substring(0, i);
|
|
241 }
|
|
242 return "";
|
|
243 }
|
|
244
|
259
|
245 /**
|
|
246 * Normalize a path name.
|
197
|
247 *
|
|
248 * Removes leading and trailing slashes. Returns null if there is other
|
259
|
249 * unwanted stuff in the path name.
|
197
|
250 *
|
|
251 * @param pathname
|
|
252 * @return
|
|
253 */
|
|
254 public static String normalName(String pathname) {
|
271
|
255 if (pathname == null) {
|
|
256 return null;
|
|
257 }
|
197
|
258 // upper-dir references are unwanted
|
|
259 if (pathname.indexOf("../") >= 0) {
|
|
260 return null;
|
|
261 }
|
|
262 int a = 0;
|
209
|
263 int e = pathname.length() - 1;
|
|
264 if (e < 0) {
|
|
265 return pathname;
|
|
266 }
|
197
|
267 // leading and trailing "/" are removed
|
209
|
268 while ((a <= e) && (pathname.charAt(a) == '/')) {
|
197
|
269 a++;
|
|
270 }
|
209
|
271 while ((a < e) && (pathname.charAt(e) == '/')) {
|
259
|
272 e--;
|
197
|
273 }
|
209
|
274 return pathname.substring(a, e + 1);
|
197
|
275 }
|
259
|
276
|
|
277 /**
|
|
278 * FileFilter for general files
|
|
279 */
|
|
280 static class ReadableFileFilter implements FileFilter {
|
|
281
|
|
282 public boolean accept(File f) {
|
|
283 return f.canRead();
|
|
284 }
|
|
285 }
|
|
286
|
152
|
287 /**
|
187
|
288 * FileFilter for image types (helper class for getFile)
|
91
|
289 */
|
|
290 static class ImageFileFilter implements FileFilter {
|
1
|
291
|
91
|
292 public boolean accept(File f) {
|
259
|
293 return (classForFilename(f.getName()) == CLASS_IMAGE);
|
187
|
294 }
|
|
295 }
|
|
296
|
|
297 /**
|
|
298 * FileFilter for text types (helper class for getFile)
|
|
299 */
|
|
300 static class TextFileFilter implements FileFilter {
|
|
301
|
|
302 public boolean accept(File f) {
|
259
|
303 return (classForFilename(f.getName()) == CLASS_TEXT);
|
91
|
304 }
|
|
305 }
|
1
|
306
|
159
|
307 /**
|
187
|
308 * FileFilter for svg types (helper class for getFile).
|
|
309 *
|
159
|
310 */
|
187
|
311 static class SVGFileFilter implements FileFilter {
|
159
|
312
|
|
313 public boolean accept(File f) {
|
259
|
314 return (classForFilename(f.getName()) == CLASS_SVG);
|
159
|
315 }
|
|
316 }
|
|
317
|
187
|
318 /**
|
|
319 * Factory for FileFilters (image or text).
|
159
|
320 *
|
|
321 * @param fileClass
|
|
322 * @return
|
187
|
323 */
|
159
|
324 public static FileFilter filterForClass(int fileClass) {
|
|
325 if (fileClass == CLASS_IMAGE) {
|
|
326 return new ImageFileFilter();
|
|
327 }
|
|
328 if (fileClass == CLASS_TEXT) {
|
|
329 return new TextFileFilter();
|
|
330 }
|
187
|
331 if (fileClass == CLASS_SVG) {
|
|
332 return new SVGFileFilter();
|
|
333 }
|
159
|
334 return null;
|
|
335 }
|
|
336
|
259
|
337 /**
|
|
338 * Factory for DocuDirents based on file class.
|
187
|
339 *
|
259
|
340 * Returns an ImageFileset, TextFile or SVGFile. baseDirs and scalext are
|
|
341 * only for ImageFilesets.
|
187
|
342 *
|
|
343 * @param fileClass
|
|
344 * @param file
|
259
|
345 * @param hints
|
|
346 * optional additional parameters
|
187
|
347 * @return
|
|
348 */
|
531
|
349 public static DocuDirent fileForClass(int fileClass, File file, Map<Integer,Object> hints) {
|
187
|
350 // what class of file do we have?
|
|
351 if (fileClass == CLASS_IMAGE) {
|
|
352 // image file
|
259
|
353 return new ImageFileset(file, hints);
|
187
|
354 } else if (fileClass == CLASS_TEXT) {
|
|
355 // text file
|
259
|
356 return new TextFile(file);
|
187
|
357 } else if (fileClass == CLASS_SVG) {
|
|
358 // text file
|
259
|
359 return new SVGFile(file);
|
187
|
360 }
|
|
361 return null;
|
|
362 }
|
259
|
363
|
|
364 /**
|
|
365 * Filters a list of Files through a FileFilter.
|
|
366 *
|
|
367 * @param files
|
|
368 * @param filter
|
|
369 * @return
|
|
370 */
|
|
371 public static File[] listFiles(File[] files, FileFilter filter) {
|
|
372 if (files == null) {
|
|
373 return null;
|
|
374 }
|
|
375 File[] ff = new File[files.length];
|
|
376 int ffi = 0;
|
|
377 for (int i = 0; i < files.length; i++) {
|
|
378 if (filter.accept(files[i])) {
|
|
379 ff[ffi] = files[i];
|
|
380 ffi++;
|
|
381 }
|
|
382 }
|
|
383 File[] fff = new File[ffi];
|
|
384 System.arraycopy(ff, 0, fff, 0, ffi);
|
|
385 return fff;
|
|
386 }
|
|
387
|
|
388 /**
|
|
389 * Creates a new hints Map with the given first element.
|
|
390 *
|
|
391 * @param type
|
|
392 * @param value
|
|
393 * @return
|
|
394 */
|
531
|
395 public static Map<Integer, Object> newHints(Integer type, Object value) {
|
|
396 Map<Integer, Object> m = new HashMap<Integer, Object>();
|
259
|
397 if (type != null) {
|
|
398 m.put(type, value);
|
|
399 }
|
|
400 return m;
|
|
401 }
|
|
402
|
1
|
403 }
|