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