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
|
197
|
181 /** Normalize a path name.
|
|
182 *
|
|
183 * Removes leading and trailing slashes. Returns null if there is other
|
|
184 * unwanted stuff in the path name.
|
|
185 *
|
|
186 * @param pathname
|
|
187 * @return
|
|
188 */
|
|
189 public static String normalName(String pathname) {
|
|
190 // upper-dir references are unwanted
|
|
191 if (pathname.indexOf("../") >= 0) {
|
|
192 return null;
|
|
193 }
|
|
194 int a = 0;
|
|
195 int e = pathname.length();
|
|
196 // leading and trailing "/" are removed
|
|
197 if (pathname.startsWith("/")) {
|
|
198 a++;
|
|
199 }
|
|
200 if (pathname.endsWith("/")) {
|
|
201 e--;
|
|
202 }
|
|
203 return pathname.substring(a, e);
|
|
204 }
|
|
205
|
|
206
|
152
|
207 /**
|
187
|
208 * FileFilter for image types (helper class for getFile)
|
91
|
209 */
|
|
210 static class ImageFileFilter implements FileFilter {
|
1
|
211
|
91
|
212 public boolean accept(File f) {
|
|
213 if (f.isFile()) {
|
187
|
214 return (
|
|
215 (mimeForFile(f) != null)
|
|
216 && (mimeForFile(f).startsWith("image")));
|
|
217 } else {
|
|
218 return false;
|
|
219 }
|
|
220 }
|
|
221 }
|
|
222
|
|
223 /**
|
|
224 * FileFilter for text types (helper class for getFile)
|
|
225 */
|
|
226 static class TextFileFilter implements FileFilter {
|
|
227
|
|
228 public boolean accept(File f) {
|
|
229 if (f.isFile()) {
|
|
230 return (
|
|
231 (mimeForFile(f) != null)
|
|
232 && (mimeForFile(f).startsWith("text")));
|
91
|
233 } else {
|
|
234 return false;
|
|
235 }
|
|
236 }
|
|
237 }
|
1
|
238
|
159
|
239 /**
|
187
|
240 * FileFilter for svg types (helper class for getFile).
|
|
241 *
|
159
|
242 */
|
187
|
243 static class SVGFileFilter implements FileFilter {
|
159
|
244
|
|
245 public boolean accept(File f) {
|
|
246 if (f.isFile()) {
|
187
|
247 return (
|
|
248 (mimeForFile(f) != null)
|
|
249 && (mimeForFile(f).startsWith("image/svg")));
|
159
|
250 } else {
|
|
251 return false;
|
|
252 }
|
|
253 }
|
|
254 }
|
|
255
|
187
|
256 /**
|
|
257 * Factory for FileFilters (image or text).
|
159
|
258 *
|
|
259 * @param fileClass
|
|
260 * @return
|
187
|
261 */
|
159
|
262 public static FileFilter filterForClass(int fileClass) {
|
|
263 if (fileClass == CLASS_IMAGE) {
|
|
264 return new ImageFileFilter();
|
|
265 }
|
|
266 if (fileClass == CLASS_TEXT) {
|
|
267 return new TextFileFilter();
|
|
268 }
|
187
|
269 if (fileClass == CLASS_SVG) {
|
|
270 return new SVGFileFilter();
|
|
271 }
|
159
|
272 return null;
|
|
273 }
|
|
274
|
187
|
275 /** Factory for DocuDirents based on file class.
|
|
276 *
|
|
277 * Returns an ImageFileset, TextFile or SVGFile.
|
|
278 * baseDirs and scalext are only for ImageFilesets.
|
|
279 *
|
|
280 * @param fileClass
|
|
281 * @param file
|
|
282 * @param baseDirs array of base directories (for ImageFileset)
|
|
283 * @param scalext first extension to try for scaled images (for ImageFileset)
|
|
284 * @return
|
|
285 */
|
|
286 public static DocuDirent fileForClass(
|
|
287 int fileClass,
|
|
288 File file,
|
|
289 Directory[] baseDirs,
|
|
290 String scalext) {
|
|
291 // what class of file do we have?
|
|
292 if (fileClass == CLASS_IMAGE) {
|
|
293 // image file
|
|
294 return new ImageFileset(baseDirs, file, scalext);
|
|
295 } else if (fileClass == CLASS_TEXT) {
|
|
296 // text file
|
|
297 return new TextFile(file);
|
|
298 } else if (fileClass == CLASS_SVG) {
|
|
299 // text file
|
|
300 return new SVGFile(file);
|
|
301 }
|
|
302 return null;
|
|
303 }
|
1
|
304 }
|