comparison servlet/src/main/java/digilib/io/FileOps.java @ 892:ba1eb2d821a2 mvnify

rearrange sources to maven directory standard
author robcast
date Tue, 19 Apr 2011 18:44:25 +0200
parents servlet/src/digilib/io/FileOps.java@6c752969f9e8
children
comparison
equal deleted inserted replaced
891:6584af320296 892:ba1eb2d821a2
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 */
21
22 package digilib.io;
23
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.StringTokenizer;
32
33 public class FileOps {
34
35 /**
36 * Array of file extensions and corresponding mime-types.
37 */
38 private static final 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 { "fpx", "image/fpx" },
43 { "txt", "text/plain" }, { "html", "text/html" },
44 { "htm", "text/html" }, { "xml", "text/xml" },
45 { "svg", "image/svg+xml" }, { "meta", "text/xml" } };
46
47 public static Map<String, String> fileTypes;
48
49 public static List<String> imageExtensions;
50
51 public static List<String> textExtensions;
52
53 public static List<String> svgExtensions;
54
55 public static enum FileClass {NONE, IMAGE, TEXT, SVG}
56
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 {
67 fileTypes = new HashMap<String, String>();
68 imageExtensions = new ArrayList<String>();
69 textExtensions = new ArrayList<String>();
70 svgExtensions = new ArrayList<String>();
71 // iterate through file types in ft and fill the Map and Lists
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);
76 if (classForMimetype(mt) == FileClass.IMAGE) {
77 imageExtensions.add(ext);
78 } else if (classForMimetype(mt) == FileClass.TEXT) {
79 textExtensions.add(ext);
80 } else if (classForMimetype(mt) == FileClass.SVG) {
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 */
92 public static FileClass classForMimetype(String mt) {
93 if (mt == null) {
94 return FileClass.NONE;
95 }
96 if (mt.startsWith("image/svg")) {
97 return FileClass.SVG;
98 } else if (mt.startsWith("image")) {
99 return FileClass.IMAGE;
100 } else if (mt.startsWith("text")) {
101 return FileClass.TEXT;
102 }
103 return FileClass.NONE;
104 }
105
106 /**
107 * get the mime type for a file format (by extension)
108 */
109 public static String mimeForFile(File f) {
110 if (f == null) {
111 return null;
112 }
113 return fileTypes.get(extname(f.getName().toLowerCase()));
114 }
115
116 /**
117 * get the file class for the filename (by extension)
118 *
119 * @param fn
120 * @return
121 */
122 public static FileClass classForFilename(String fn) {
123 String mt = (String) fileTypes.get(extname(fn).toLowerCase());
124 return classForMimetype(mt);
125 }
126
127 public static Iterator<String> getImageExtensionIterator() {
128 return imageExtensions.iterator();
129 }
130
131 public static List<String> getImageExtensions() {
132 return imageExtensions;
133 }
134
135 public static Iterator<String> getTextExtensionIterator() {
136 return textExtensions.iterator();
137 }
138
139 public static List<String> getTextExtensions() {
140 return textExtensions;
141 }
142
143 public static Iterator<String> getSVGExtensionIterator() {
144 return svgExtensions.iterator();
145 }
146
147 public static List<String> getSvgExtensions() {
148 return svgExtensions;
149 }
150
151
152 /**
153 * convert a string with a list of pathnames into an array of strings using
154 * the system's path separator string
155 */
156 public static String[] pathToArray(String paths) {
157 // split list into directories
158 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
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 }
176
177 /**
178 * Extract the base of a file name (sans extension).
179 *
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 *
184 * @param fn
185 * @return
186 */
187 public static String basename(String fn) {
188 if (fn == null) {
189 return null;
190 }
191 int i = fn.lastIndexOf('.');
192 if (i > 0) {
193 return fn.substring(0, i);
194 }
195 return fn;
196 }
197
198 /**
199 * Extract the extension of a file name.
200 *
201 * Returns the extension of a file name. The extension is the part behind
202 * the last dot in the filename. If the filename has no dot the empty string
203 * is returned.
204 *
205 * @param fn
206 * @return
207 */
208 public static String extname(String fn) {
209 if (fn == null) {
210 return null;
211 }
212 int i = fn.lastIndexOf('.');
213 if (i > 0) {
214 return fn.substring(i + 1);
215 }
216 return "";
217 }
218
219 /**
220 * Extract the parent directory of a (digilib) path name.
221 *
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.
225 *
226 * @param fn
227 * @return
228 */
229 public static String parent(String fn) {
230 if (fn == null) {
231 return null;
232 }
233 int i = fn.lastIndexOf('/');
234 if (i > 0) {
235 return fn.substring(0, i);
236 }
237 return "";
238 }
239
240 /**
241 * Normalize a path name.
242 *
243 * Removes leading and trailing slashes. Returns null if there is other
244 * unwanted stuff in the path name.
245 *
246 * @param pathname
247 * @return
248 */
249 public static String normalName(String pathname) {
250 if (pathname == null) {
251 return null;
252 }
253 // upper-dir references are unwanted
254 if (pathname.indexOf("../") >= 0) {
255 return null;
256 }
257 int a = 0;
258 int e = pathname.length() - 1;
259 if (e < 0) {
260 return pathname;
261 }
262 // leading and trailing "/" are removed
263 while ((a <= e) && (pathname.charAt(a) == '/')) {
264 a++;
265 }
266 while ((a < e) && (pathname.charAt(e) == '/')) {
267 e--;
268 }
269 return pathname.substring(a, e + 1);
270 }
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
282 /**
283 * FileFilter for image types (helper class for getFile)
284 */
285 static class ImageFileFilter implements FileFilter {
286
287 public boolean accept(File f) {
288 return (classForFilename(f.getName()) == FileClass.IMAGE);
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) {
298 return (classForFilename(f.getName()) == FileClass.TEXT);
299 }
300 }
301
302 /**
303 * FileFilter for svg types (helper class for getFile).
304 *
305 */
306 static class SVGFileFilter implements FileFilter {
307
308 public boolean accept(File f) {
309 return (classForFilename(f.getName()) == FileClass.SVG);
310 }
311 }
312
313 /**
314 * Factory for FileFilters (image or text).
315 *
316 * @param fileClass
317 * @return
318 */
319 public static FileFilter filterForClass(FileClass fileClass) {
320 if (fileClass == FileClass.IMAGE) {
321 return new ImageFileFilter();
322 }
323 if (fileClass == FileClass.TEXT) {
324 return new TextFileFilter();
325 }
326 if (fileClass == FileClass.SVG) {
327 return new SVGFileFilter();
328 }
329 return null;
330 }
331
332 /**
333 * Factory for DocuDirents based on file class.
334 *
335 * Returns an ImageSet, TextFile or SVGFile. scaleDirs are
336 * only for ImageFilesets.
337 *
338 * @param fileClass
339 * @param file
340 * @param scaleDirs
341 * optional additional parameters
342 * @return
343 */
344 public static DocuDirent fileForClass(FileClass fileClass, File file, Directory[] scaleDirs) {
345 // what class of file do we have?
346 if (fileClass == FileClass.IMAGE) {
347 // image file
348 return new ImageFileSet(file, scaleDirs);
349 } else if (fileClass == FileClass.TEXT) {
350 // text file
351 return new TextFile(file);
352 } else if (fileClass == FileClass.SVG) {
353 // text file
354 return new SVGFile(file);
355 }
356 return null;
357 }
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 */
390 public static Map<Integer, Object> newHints(Integer type, Object value) {
391 Map<Integer, Object> m = new HashMap<Integer, Object>();
392 if (type != null) {
393 m.put(type, value);
394 }
395 return m;
396 }
397
398 }