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;
|
|
26 import java.util.Arrays;
|
|
27 import java.util.Iterator;
|
|
28 import java.util.StringTokenizer;
|
1
|
29
|
|
30 public class FileOps {
|
|
31
|
91
|
32 public static String[] fileTypes =
|
|
33 {
|
|
34 "jpg",
|
|
35 "image/jpeg",
|
|
36 "jpeg",
|
|
37 "image/jpeg",
|
|
38 "jp2",
|
|
39 "image/jp2",
|
|
40 "png",
|
|
41 "image/png",
|
|
42 "gif",
|
|
43 "image/gif",
|
|
44 "tif",
|
|
45 "image/tiff",
|
|
46 "tiff",
|
159
|
47 "image/tiff",
|
|
48 "txt",
|
|
49 "text/plain",
|
|
50 "html",
|
|
51 "text/html",
|
|
52 "htm",
|
|
53 "text/html",
|
|
54 "xml",
|
187
|
55 "text/xml",
|
|
56 "svg",
|
|
57 "image/svg+xml" };
|
1
|
58
|
159
|
59 public static String[] imageExtensions =
|
91
|
60 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
|
|
61
|
187
|
62 public static String[] textExtensions = { "txt", "html", "htm", "xml" };
|
|
63
|
|
64 public static String[] svgExtensions = { "svg" };
|
|
65
|
159
|
66 public static final int CLASS_NONE = -1;
|
|
67 public static final int CLASS_IMAGE = 0;
|
|
68 public static final int CLASS_TEXT = 1;
|
187
|
69 public static final int CLASS_SVG = 2;
|
|
70 public static final int NUM_CLASSES = 3;
|
159
|
71
|
91
|
72 /**
|
187
|
73 * get the mime type for a file format (by extension)
|
91
|
74 */
|
|
75 public static String mimeForFile(File f) {
|
|
76 String fn = f.getName();
|
|
77 for (int i = 0; i < fileTypes.length; i += 2) {
|
|
78 if (fn.toLowerCase().endsWith(fileTypes[i])) {
|
|
79 return fileTypes[i + 1];
|
|
80 }
|
|
81 }
|
|
82 return null;
|
|
83 }
|
1
|
84
|
159
|
85 /**
|
|
86 * get the file class for the filename (by extension)
|
187
|
87 *
|
159
|
88 * @param fn
|
|
89 * @return
|
|
90 */
|
|
91 public static int classForFilename(String fn) {
|
|
92 int n = imageExtensions.length;
|
187
|
93 for (int i = 0; i < n; i++) {
|
159
|
94 if (fn.toLowerCase().endsWith(imageExtensions[i])) {
|
|
95 return CLASS_IMAGE;
|
|
96 }
|
|
97 }
|
|
98 n = textExtensions.length;
|
187
|
99 for (int i = 0; i < n; i++) {
|
159
|
100 if (fn.toLowerCase().endsWith(textExtensions[i])) {
|
|
101 return CLASS_TEXT;
|
|
102 }
|
|
103 }
|
|
104 return CLASS_NONE;
|
187
|
105
|
91
|
106 }
|
1
|
107
|
159
|
108 public static Iterator getImageExtensionIterator() {
|
|
109 return Arrays.asList(imageExtensions).iterator();
|
|
110 }
|
|
111
|
|
112 public static Iterator getTextExtensionIterator() {
|
|
113 return Arrays.asList(textExtensions).iterator();
|
|
114 }
|
187
|
115
|
|
116 public static Iterator getSVGExtensionIterator() {
|
|
117 return Arrays.asList(svgExtensions).iterator();
|
|
118 }
|
|
119
|
91
|
120 /**
|
187
|
121 * convert a string with a list of pathnames into an array of strings using
|
|
122 * the system's path separator string
|
152
|
123 */
|
|
124 public static String[] pathToArray(String paths) {
|
|
125 // split list into directories
|
159
|
126 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
127 int n = dirs.countTokens();
|
|
128 if (n < 1) {
|
|
129 return null;
|
|
130 }
|
|
131 // add directories into array
|
|
132 String[] pathArray = new String[n];
|
|
133 for (int i = 0; i < n; i++) {
|
|
134 String s = dirs.nextToken();
|
|
135 // make shure the dir name ends with a directory separator
|
|
136 if (s.endsWith(File.separator)) {
|
|
137 pathArray[i] = s;
|
|
138 } else {
|
|
139 pathArray[i] = s + File.separator;
|
|
140 }
|
|
141 }
|
|
142 return pathArray;
|
|
143 }
|
187
|
144
|
|
145 /**
|
|
146 * Extract the base of a file name (sans extension).
|
176
|
147 *
|
187
|
148 * Returns the filename without the extension. The extension is the part
|
|
149 * behind the last dot in the filename. If the filename has no dot the full
|
|
150 * file name is returned.
|
|
151 *
|
176
|
152 * @param fn
|
|
153 * @return
|
|
154 */
|
|
155 public static String basename(String fn) {
|
|
156 int i = fn.lastIndexOf('.');
|
|
157 if (i > 0) {
|
|
158 return fn.substring(0, i);
|
|
159 }
|
|
160 return fn;
|
|
161 }
|
|
162
|
187
|
163 /**
|
|
164 * Extract the extension of a file name.
|
176
|
165 *
|
|
166 * Returns the extension of a file name. The extension is the part behind
|
187
|
167 * the last dot in the filename. If the filename has no dot the empty
|
|
168 * string is returned.
|
|
169 *
|
176
|
170 * @param fn
|
|
171 * @return
|
|
172 */
|
|
173 public static String extname(String fn) {
|
|
174 int i = fn.lastIndexOf('.');
|
|
175 if (i > 0) {
|
187
|
176 return fn.substring(i + 1);
|
176
|
177 }
|
|
178 return "";
|
|
179 }
|
152
|
180
|
246
|
181 /**
|
|
182 * Extract the parent directory of a (digilib) path name.
|
|
183 *
|
|
184 * Returns the parent directory of a path name. The parent is the part before
|
|
185 * the last slash in the path name. If the path name has no slash the empty
|
|
186 * string is returned.
|
|
187 *
|
|
188 * @param fn
|
|
189 * @return
|
|
190 */
|
|
191 public static String parent(String fn) {
|
|
192 int i = fn.lastIndexOf('/');
|
|
193 if (i > 0) {
|
|
194 return fn.substring(0, i);
|
|
195 }
|
|
196 return "";
|
|
197 }
|
|
198
|
197
|
199 /** Normalize a path name.
|
|
200 *
|
|
201 * Removes leading and trailing slashes. Returns null if there is other
|
|
202 * unwanted stuff in the path name.
|
|
203 *
|
|
204 * @param pathname
|
|
205 * @return
|
|
206 */
|
|
207 public static String normalName(String pathname) {
|
|
208 // upper-dir references are unwanted
|
|
209 if (pathname.indexOf("../") >= 0) {
|
|
210 return null;
|
|
211 }
|
|
212 int a = 0;
|
209
|
213 int e = pathname.length() - 1;
|
|
214 if (e < 0) {
|
|
215 return pathname;
|
|
216 }
|
197
|
217 // leading and trailing "/" are removed
|
209
|
218 while ((a <= e) && (pathname.charAt(a) == '/')) {
|
197
|
219 a++;
|
|
220 }
|
209
|
221 while ((a < e) && (pathname.charAt(e) == '/')) {
|
197
|
222 e--;
|
|
223 }
|
209
|
224 return pathname.substring(a, e + 1);
|
197
|
225 }
|
|
226
|
|
227
|
152
|
228 /**
|
187
|
229 * FileFilter for image types (helper class for getFile)
|
91
|
230 */
|
|
231 static class ImageFileFilter implements FileFilter {
|
1
|
232
|
91
|
233 public boolean accept(File f) {
|
|
234 if (f.isFile()) {
|
187
|
235 return (
|
|
236 (mimeForFile(f) != null)
|
|
237 && (mimeForFile(f).startsWith("image")));
|
|
238 } else {
|
|
239 return false;
|
|
240 }
|
|
241 }
|
|
242 }
|
|
243
|
|
244 /**
|
|
245 * FileFilter for text types (helper class for getFile)
|
|
246 */
|
|
247 static class TextFileFilter implements FileFilter {
|
|
248
|
|
249 public boolean accept(File f) {
|
|
250 if (f.isFile()) {
|
|
251 return (
|
|
252 (mimeForFile(f) != null)
|
|
253 && (mimeForFile(f).startsWith("text")));
|
91
|
254 } else {
|
|
255 return false;
|
|
256 }
|
|
257 }
|
|
258 }
|
1
|
259
|
159
|
260 /**
|
187
|
261 * FileFilter for svg types (helper class for getFile).
|
|
262 *
|
159
|
263 */
|
187
|
264 static class SVGFileFilter implements FileFilter {
|
159
|
265
|
|
266 public boolean accept(File f) {
|
|
267 if (f.isFile()) {
|
187
|
268 return (
|
|
269 (mimeForFile(f) != null)
|
|
270 && (mimeForFile(f).startsWith("image/svg")));
|
159
|
271 } else {
|
|
272 return false;
|
|
273 }
|
|
274 }
|
|
275 }
|
|
276
|
187
|
277 /**
|
|
278 * Factory for FileFilters (image or text).
|
159
|
279 *
|
|
280 * @param fileClass
|
|
281 * @return
|
187
|
282 */
|
159
|
283 public static FileFilter filterForClass(int fileClass) {
|
|
284 if (fileClass == CLASS_IMAGE) {
|
|
285 return new ImageFileFilter();
|
|
286 }
|
|
287 if (fileClass == CLASS_TEXT) {
|
|
288 return new TextFileFilter();
|
|
289 }
|
187
|
290 if (fileClass == CLASS_SVG) {
|
|
291 return new SVGFileFilter();
|
|
292 }
|
159
|
293 return null;
|
|
294 }
|
|
295
|
187
|
296 /** Factory for DocuDirents based on file class.
|
|
297 *
|
|
298 * Returns an ImageFileset, TextFile or SVGFile.
|
|
299 * baseDirs and scalext are only for ImageFilesets.
|
|
300 *
|
|
301 * @param fileClass
|
|
302 * @param file
|
|
303 * @param baseDirs array of base directories (for ImageFileset)
|
|
304 * @param scalext first extension to try for scaled images (for ImageFileset)
|
|
305 * @return
|
|
306 */
|
|
307 public static DocuDirent fileForClass(
|
|
308 int fileClass,
|
|
309 File file,
|
|
310 Directory[] baseDirs,
|
|
311 String scalext) {
|
|
312 // what class of file do we have?
|
|
313 if (fileClass == CLASS_IMAGE) {
|
|
314 // image file
|
|
315 return new ImageFileset(baseDirs, file, scalext);
|
|
316 } else if (fileClass == CLASS_TEXT) {
|
|
317 // text file
|
|
318 return new TextFile(file);
|
|
319 } else if (fileClass == CLASS_SVG) {
|
|
320 // text file
|
|
321 return new SVGFile(file);
|
|
322 }
|
|
323 return null;
|
|
324 }
|
1
|
325 }
|