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