85
|
1 /* FileOps -- Utility class for file operations
|
1
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 */
|
|
20
|
|
21 package digilib.io;
|
|
22
|
159
|
23 import java.io.File;
|
|
24 import java.io.FileFilter;
|
|
25 import java.util.Arrays;
|
|
26 import java.util.Iterator;
|
|
27 import java.util.StringTokenizer;
|
1
|
28
|
159
|
29 import digilib.Utils;
|
1
|
30
|
|
31 public class FileOps {
|
|
32
|
91
|
33 private Utils util = null;
|
159
|
34
|
91
|
35 public static String[] fileTypes =
|
|
36 {
|
|
37 "jpg",
|
|
38 "image/jpeg",
|
|
39 "jpeg",
|
|
40 "image/jpeg",
|
|
41 "jp2",
|
|
42 "image/jp2",
|
|
43 "png",
|
|
44 "image/png",
|
|
45 "gif",
|
|
46 "image/gif",
|
|
47 "tif",
|
|
48 "image/tiff",
|
|
49 "tiff",
|
159
|
50 "image/tiff",
|
|
51 "txt",
|
|
52 "text/plain",
|
|
53 "html",
|
|
54 "text/html",
|
|
55 "htm",
|
|
56 "text/html",
|
|
57 "xml",
|
|
58 "text/xml" };
|
1
|
59
|
159
|
60 public static String[] imageExtensions =
|
91
|
61 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
|
|
62
|
159
|
63 public static String[] textExtensions =
|
|
64 { "txt", "html", "htm", "xml"};
|
|
65
|
|
66 public static final int CLASS_NONE = -1;
|
|
67 public static final int CLASS_IMAGE = 0;
|
|
68 public static final int CLASS_TEXT = 1;
|
|
69 public static final int NUM_CLASSES = 2;
|
|
70
|
|
71
|
91
|
72 public FileOps() {
|
|
73 util = new Utils();
|
|
74 }
|
1
|
75
|
91
|
76 public FileOps(Utils u) {
|
|
77 util = u;
|
|
78 }
|
1
|
79
|
91
|
80 public void setUtils(Utils u) {
|
|
81 util = u;
|
|
82 }
|
1
|
83
|
91
|
84 /**
|
|
85 * get the mime type for a file format (by extension)
|
|
86 */
|
|
87 public static String mimeForFile(File f) {
|
|
88 String fn = f.getName();
|
|
89 for (int i = 0; i < fileTypes.length; i += 2) {
|
|
90 if (fn.toLowerCase().endsWith(fileTypes[i])) {
|
|
91 return fileTypes[i + 1];
|
|
92 }
|
|
93 }
|
|
94 return null;
|
|
95 }
|
1
|
96
|
159
|
97 /**
|
|
98 * get the file class for the filename (by extension)
|
|
99 * @param fn
|
|
100 * @return
|
|
101 */
|
|
102 public static int classForFilename(String fn) {
|
|
103 int n = imageExtensions.length;
|
|
104 for (int i = 0; i < n; i ++) {
|
|
105 if (fn.toLowerCase().endsWith(imageExtensions[i])) {
|
|
106 return CLASS_IMAGE;
|
|
107 }
|
|
108 }
|
|
109 n = textExtensions.length;
|
|
110 for (int i = 0; i < n; i ++) {
|
|
111 if (fn.toLowerCase().endsWith(textExtensions[i])) {
|
|
112 return CLASS_TEXT;
|
|
113 }
|
|
114 }
|
|
115 return CLASS_NONE;
|
|
116
|
91
|
117 }
|
1
|
118
|
159
|
119 public static Iterator getImageExtensionIterator() {
|
|
120 return Arrays.asList(imageExtensions).iterator();
|
|
121 }
|
|
122
|
|
123 public static Iterator getTextExtensionIterator() {
|
|
124 return Arrays.asList(textExtensions).iterator();
|
|
125 }
|
|
126
|
91
|
127 /**
|
152
|
128 * convert a string with a list of pathnames into an array of strings
|
|
129 * using the system's path separator string
|
|
130 */
|
|
131 public static String[] pathToArray(String paths) {
|
|
132 // split list into directories
|
159
|
133 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
134 int n = dirs.countTokens();
|
|
135 if (n < 1) {
|
|
136 return null;
|
|
137 }
|
|
138 // add directories into array
|
|
139 String[] pathArray = new String[n];
|
|
140 for (int i = 0; i < n; i++) {
|
|
141 String s = dirs.nextToken();
|
|
142 // make shure the dir name ends with a directory separator
|
|
143 if (s.endsWith(File.separator)) {
|
|
144 pathArray[i] = s;
|
|
145 } else {
|
|
146 pathArray[i] = s + File.separator;
|
|
147 }
|
|
148 }
|
|
149 return pathArray;
|
|
150 }
|
176
|
151
|
|
152 /** Extract the base of a file name (sans extension).
|
|
153 *
|
|
154 * Returns the filename without the extension. The extension is the part behind
|
|
155 * the last dot in the filename. If the filename has no dot the full file name
|
|
156 * is returned.
|
|
157 *
|
|
158 * @param fn
|
|
159 * @return
|
|
160 */
|
|
161 public static String basename(String fn) {
|
|
162 int i = fn.lastIndexOf('.');
|
|
163 if (i > 0) {
|
|
164 return fn.substring(0, i);
|
|
165 }
|
|
166 return fn;
|
|
167 }
|
|
168
|
|
169 /** Extract the extension of a file name.
|
|
170 *
|
|
171 * Returns the extension of a file name. The extension is the part behind
|
|
172 * the last dot in the filename. If the filename has no dot the empty string
|
|
173 * is returned.
|
|
174 *
|
|
175 * @param fn
|
|
176 * @return
|
|
177 */
|
|
178 public static String extname(String fn) {
|
|
179 int i = fn.lastIndexOf('.');
|
|
180 if (i > 0) {
|
|
181 return fn.substring(i+1);
|
|
182 }
|
|
183 return "";
|
|
184 }
|
152
|
185
|
|
186 /**
|
91
|
187 * FileFilter for image types (helper class for getFile)
|
|
188 */
|
|
189 static class ImageFileFilter implements FileFilter {
|
1
|
190
|
91
|
191 public boolean accept(File f) {
|
|
192 if (f.isFile()) {
|
159
|
193 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("image")));
|
91
|
194 } else {
|
|
195 return false;
|
|
196 }
|
|
197 }
|
|
198 }
|
1
|
199
|
159
|
200 /**
|
|
201 * FileFilter for text types (helper class for getFile)
|
|
202 */
|
|
203 static class TextFileFilter implements FileFilter {
|
|
204
|
|
205 public boolean accept(File f) {
|
|
206 if (f.isFile()) {
|
|
207 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("text")));
|
|
208 } else {
|
|
209 return false;
|
|
210 }
|
|
211 }
|
|
212 }
|
|
213
|
|
214 /** Factory for FileFilters (image or text).
|
|
215 *
|
|
216 * @param fileClass
|
|
217 * @return
|
|
218 */
|
|
219 public static FileFilter filterForClass(int fileClass) {
|
|
220 if (fileClass == CLASS_IMAGE) {
|
|
221 return new ImageFileFilter();
|
|
222 }
|
|
223 if (fileClass == CLASS_TEXT) {
|
|
224 return new TextFileFilter();
|
|
225 }
|
|
226 return null;
|
|
227 }
|
|
228
|
1
|
229 }
|