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
|
563
|
55 public static enum FileClass {NONE, IMAGE, TEXT, SVG}
|
159
|
56
|
259
|
57 public static final Integer HINT_BASEDIRS = new Integer(1);
|
|
58
|
|
59 public static final Integer HINT_FILEEXT = new Integer(2);
|
|
60
|
|
61 public static final Integer HINT_DIRS = new Integer(3);
|
|
62
|
|
63 /**
|
|
64 * static initializer for FileOps
|
|
65 */
|
|
66 static {
|
531
|
67 fileTypes = new HashMap<String, String>();
|
|
68 imageExtensions = new ArrayList<String>();
|
|
69 textExtensions = new ArrayList<String>();
|
|
70 svgExtensions = new ArrayList<String>();
|
271
|
71 // iterate through file types in ft and fill the Map and Lists
|
259
|
72 for (int i = 0; i < ft.length; i++) {
|
|
73 String ext = ft[i][0];
|
|
74 String mt = ft[i][1];
|
|
75 fileTypes.put(ext, mt);
|
563
|
76 if (classForMimetype(mt) == FileClass.IMAGE) {
|
259
|
77 imageExtensions.add(ext);
|
563
|
78 } else if (classForMimetype(mt) == FileClass.TEXT) {
|
259
|
79 textExtensions.add(ext);
|
563
|
80 } else if (classForMimetype(mt) == FileClass.SVG) {
|
259
|
81 svgExtensions.add(ext);
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * returns the file class for a mime-type
|
|
88 *
|
|
89 * @param mt
|
|
90 * @return
|
|
91 */
|
563
|
92 public static FileClass classForMimetype(String mt) {
|
259
|
93 if (mt == null) {
|
563
|
94 return FileClass.NONE;
|
259
|
95 }
|
|
96 if (mt.startsWith("image/svg")) {
|
563
|
97 return FileClass.SVG;
|
259
|
98 } else if (mt.startsWith("image")) {
|
563
|
99 return FileClass.IMAGE;
|
259
|
100 } else if (mt.startsWith("text")) {
|
563
|
101 return FileClass.TEXT;
|
259
|
102 }
|
563
|
103 return FileClass.NONE;
|
259
|
104 }
|
|
105
|
91
|
106 /**
|
187
|
107 * get the mime type for a file format (by extension)
|
91
|
108 */
|
|
109 public static String mimeForFile(File f) {
|
821
|
110 if (f == null) {
|
|
111 return null;
|
|
112 }
|
|
113 return fileTypes.get(extname(f.getName().toLowerCase()));
|
91
|
114 }
|
1
|
115
|
159
|
116 /**
|
|
117 * get the file class for the filename (by extension)
|
187
|
118 *
|
159
|
119 * @param fn
|
|
120 * @return
|
|
121 */
|
563
|
122 public static FileClass classForFilename(String fn) {
|
259
|
123 String mt = (String) fileTypes.get(extname(fn).toLowerCase());
|
|
124 return classForMimetype(mt);
|
91
|
125 }
|
1
|
126
|
531
|
127 public static Iterator<String> getImageExtensionIterator() {
|
259
|
128 return imageExtensions.iterator();
|
159
|
129 }
|
|
130
|
531
|
131 public static List<String> getImageExtensions() {
|
|
132 return imageExtensions;
|
|
133 }
|
|
134
|
|
135 public static Iterator<String> getTextExtensionIterator() {
|
259
|
136 return textExtensions.iterator();
|
159
|
137 }
|
187
|
138
|
531
|
139 public static List<String> getTextExtensions() {
|
|
140 return textExtensions;
|
|
141 }
|
|
142
|
|
143 public static Iterator<String> getSVGExtensionIterator() {
|
259
|
144 return svgExtensions.iterator();
|
187
|
145 }
|
|
146
|
531
|
147 public static List<String> getSvgExtensions() {
|
|
148 return svgExtensions;
|
|
149 }
|
1
|
150
|
|
151
|
91
|
152 /**
|
187
|
153 * convert a string with a list of pathnames into an array of strings using
|
|
154 * the system's path separator string
|
152
|
155 */
|
|
156 public static String[] pathToArray(String paths) {
|
|
157 // split list into directories
|
159
|
158 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
159 int n = dirs.countTokens();
|
|
160 if (n < 1) {
|
|
161 return null;
|
|
162 }
|
|
163 // add directories into array
|
|
164 String[] pathArray = new String[n];
|
|
165 for (int i = 0; i < n; i++) {
|
|
166 String s = dirs.nextToken();
|
|
167 // make shure the dir name ends with a directory separator
|
|
168 if (s.endsWith(File.separator)) {
|
|
169 pathArray[i] = s;
|
|
170 } else {
|
|
171 pathArray[i] = s + File.separator;
|
|
172 }
|
|
173 }
|
|
174 return pathArray;
|
|
175 }
|
187
|
176
|
|
177 /**
|
|
178 * Extract the base of a file name (sans extension).
|
176
|
179 *
|
187
|
180 * Returns the filename without the extension. The extension is the part
|
|
181 * behind the last dot in the filename. If the filename has no dot the full
|
|
182 * file name is returned.
|
|
183 *
|
176
|
184 * @param fn
|
|
185 * @return
|
|
186 */
|
|
187 public static String basename(String fn) {
|
271
|
188 if (fn == null) {
|
|
189 return null;
|
|
190 }
|
176
|
191 int i = fn.lastIndexOf('.');
|
|
192 if (i > 0) {
|
|
193 return fn.substring(0, i);
|
|
194 }
|
|
195 return fn;
|
|
196 }
|
|
197
|
187
|
198 /**
|
|
199 * Extract the extension of a file name.
|
176
|
200 *
|
|
201 * Returns the extension of a file name. The extension is the part behind
|
259
|
202 * the last dot in the filename. If the filename has no dot the empty string
|
|
203 * is returned.
|
187
|
204 *
|
176
|
205 * @param fn
|
|
206 * @return
|
|
207 */
|
|
208 public static String extname(String fn) {
|
271
|
209 if (fn == null) {
|
|
210 return null;
|
|
211 }
|
176
|
212 int i = fn.lastIndexOf('.');
|
|
213 if (i > 0) {
|
187
|
214 return fn.substring(i + 1);
|
176
|
215 }
|
|
216 return "";
|
|
217 }
|
152
|
218
|
246
|
219 /**
|
|
220 * Extract the parent directory of a (digilib) path name.
|
|
221 *
|
259
|
222 * Returns the parent directory of a path name. The parent is the part
|
|
223 * before the last slash in the path name. If the path name has no slash the
|
|
224 * empty string is returned.
|
246
|
225 *
|
|
226 * @param fn
|
|
227 * @return
|
|
228 */
|
|
229 public static String parent(String fn) {
|
271
|
230 if (fn == null) {
|
|
231 return null;
|
|
232 }
|
246
|
233 int i = fn.lastIndexOf('/');
|
|
234 if (i > 0) {
|
|
235 return fn.substring(0, i);
|
|
236 }
|
|
237 return "";
|
|
238 }
|
|
239
|
259
|
240 /**
|
|
241 * Normalize a path name.
|
197
|
242 *
|
|
243 * Removes leading and trailing slashes. Returns null if there is other
|
259
|
244 * unwanted stuff in the path name.
|
197
|
245 *
|
|
246 * @param pathname
|
|
247 * @return
|
|
248 */
|
|
249 public static String normalName(String pathname) {
|
271
|
250 if (pathname == null) {
|
|
251 return null;
|
|
252 }
|
197
|
253 // upper-dir references are unwanted
|
|
254 if (pathname.indexOf("../") >= 0) {
|
|
255 return null;
|
|
256 }
|
|
257 int a = 0;
|
209
|
258 int e = pathname.length() - 1;
|
|
259 if (e < 0) {
|
|
260 return pathname;
|
|
261 }
|
197
|
262 // leading and trailing "/" are removed
|
209
|
263 while ((a <= e) && (pathname.charAt(a) == '/')) {
|
197
|
264 a++;
|
|
265 }
|
209
|
266 while ((a < e) && (pathname.charAt(e) == '/')) {
|
259
|
267 e--;
|
197
|
268 }
|
209
|
269 return pathname.substring(a, e + 1);
|
197
|
270 }
|
259
|
271
|
|
272 /**
|
|
273 * FileFilter for general files
|
|
274 */
|
|
275 static class ReadableFileFilter implements FileFilter {
|
|
276
|
|
277 public boolean accept(File f) {
|
|
278 return f.canRead();
|
|
279 }
|
|
280 }
|
|
281
|
152
|
282 /**
|
187
|
283 * FileFilter for image types (helper class for getFile)
|
91
|
284 */
|
|
285 static class ImageFileFilter implements FileFilter {
|
1
|
286
|
91
|
287 public boolean accept(File f) {
|
563
|
288 return (classForFilename(f.getName()) == FileClass.IMAGE);
|
187
|
289 }
|
|
290 }
|
|
291
|
|
292 /**
|
|
293 * FileFilter for text types (helper class for getFile)
|
|
294 */
|
|
295 static class TextFileFilter implements FileFilter {
|
|
296
|
|
297 public boolean accept(File f) {
|
563
|
298 return (classForFilename(f.getName()) == FileClass.TEXT);
|
91
|
299 }
|
|
300 }
|
1
|
301
|
159
|
302 /**
|
187
|
303 * FileFilter for svg types (helper class for getFile).
|
|
304 *
|
159
|
305 */
|
187
|
306 static class SVGFileFilter implements FileFilter {
|
159
|
307
|
|
308 public boolean accept(File f) {
|
563
|
309 return (classForFilename(f.getName()) == FileClass.SVG);
|
159
|
310 }
|
|
311 }
|
|
312
|
187
|
313 /**
|
|
314 * Factory for FileFilters (image or text).
|
159
|
315 *
|
|
316 * @param fileClass
|
|
317 * @return
|
187
|
318 */
|
563
|
319 public static FileFilter filterForClass(FileClass fileClass) {
|
|
320 if (fileClass == FileClass.IMAGE) {
|
159
|
321 return new ImageFileFilter();
|
|
322 }
|
563
|
323 if (fileClass == FileClass.TEXT) {
|
159
|
324 return new TextFileFilter();
|
|
325 }
|
563
|
326 if (fileClass == FileClass.SVG) {
|
187
|
327 return new SVGFileFilter();
|
|
328 }
|
159
|
329 return null;
|
|
330 }
|
|
331
|
259
|
332 /**
|
|
333 * Factory for DocuDirents based on file class.
|
187
|
334 *
|
583
|
335 * Returns an ImageSet, TextFile or SVGFile. scaleDirs are
|
259
|
336 * only for ImageFilesets.
|
187
|
337 *
|
|
338 * @param fileClass
|
|
339 * @param file
|
583
|
340 * @param scaleDirs
|
259
|
341 * optional additional parameters
|
187
|
342 * @return
|
|
343 */
|
583
|
344 public static DocuDirent fileForClass(FileClass fileClass, File file, Directory[] scaleDirs) {
|
187
|
345 // what class of file do we have?
|
563
|
346 if (fileClass == FileClass.IMAGE) {
|
187
|
347 // image file
|
583
|
348 return new ImageFileSet(file, scaleDirs);
|
563
|
349 } else if (fileClass == FileClass.TEXT) {
|
187
|
350 // text file
|
259
|
351 return new TextFile(file);
|
563
|
352 } else if (fileClass == FileClass.SVG) {
|
187
|
353 // text file
|
259
|
354 return new SVGFile(file);
|
187
|
355 }
|
|
356 return null;
|
|
357 }
|
259
|
358
|
|
359 /**
|
|
360 * Filters a list of Files through a FileFilter.
|
|
361 *
|
|
362 * @param files
|
|
363 * @param filter
|
|
364 * @return
|
|
365 */
|
|
366 public static File[] listFiles(File[] files, FileFilter filter) {
|
|
367 if (files == null) {
|
|
368 return null;
|
|
369 }
|
|
370 File[] ff = new File[files.length];
|
|
371 int ffi = 0;
|
|
372 for (int i = 0; i < files.length; i++) {
|
|
373 if (filter.accept(files[i])) {
|
|
374 ff[ffi] = files[i];
|
|
375 ffi++;
|
|
376 }
|
|
377 }
|
|
378 File[] fff = new File[ffi];
|
|
379 System.arraycopy(ff, 0, fff, 0, ffi);
|
|
380 return fff;
|
|
381 }
|
|
382
|
|
383 /**
|
|
384 * Creates a new hints Map with the given first element.
|
|
385 *
|
|
386 * @param type
|
|
387 * @param value
|
|
388 * @return
|
|
389 */
|
531
|
390 public static Map<Integer, Object> newHints(Integer type, Object value) {
|
|
391 Map<Integer, Object> m = new HashMap<Integer, Object>();
|
259
|
392 if (type != null) {
|
|
393 m.put(type, value);
|
|
394 }
|
|
395 return m;
|
|
396 }
|
|
397
|
1
|
398 }
|