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