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 /**
|
|
73 * get the mime type for a file format (by extension)
|
|
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)
|
|
87 * @param fn
|
|
88 * @return
|
|
89 */
|
|
90 public static int classForFilename(String fn) {
|
|
91 int n = imageExtensions.length;
|
|
92 for (int i = 0; i < n; i ++) {
|
|
93 if (fn.toLowerCase().endsWith(imageExtensions[i])) {
|
|
94 return CLASS_IMAGE;
|
|
95 }
|
|
96 }
|
|
97 n = textExtensions.length;
|
|
98 for (int i = 0; i < n; i ++) {
|
|
99 if (fn.toLowerCase().endsWith(textExtensions[i])) {
|
|
100 return CLASS_TEXT;
|
|
101 }
|
|
102 }
|
|
103 return CLASS_NONE;
|
|
104
|
91
|
105 }
|
1
|
106
|
159
|
107 public static Iterator getImageExtensionIterator() {
|
|
108 return Arrays.asList(imageExtensions).iterator();
|
|
109 }
|
|
110
|
|
111 public static Iterator getTextExtensionIterator() {
|
|
112 return Arrays.asList(textExtensions).iterator();
|
|
113 }
|
|
114
|
91
|
115 /**
|
152
|
116 * convert a string with a list of pathnames into an array of strings
|
|
117 * using the system's path separator string
|
|
118 */
|
|
119 public static String[] pathToArray(String paths) {
|
|
120 // split list into directories
|
159
|
121 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
122 int n = dirs.countTokens();
|
|
123 if (n < 1) {
|
|
124 return null;
|
|
125 }
|
|
126 // add directories into array
|
|
127 String[] pathArray = new String[n];
|
|
128 for (int i = 0; i < n; i++) {
|
|
129 String s = dirs.nextToken();
|
|
130 // make shure the dir name ends with a directory separator
|
|
131 if (s.endsWith(File.separator)) {
|
|
132 pathArray[i] = s;
|
|
133 } else {
|
|
134 pathArray[i] = s + File.separator;
|
|
135 }
|
|
136 }
|
|
137 return pathArray;
|
|
138 }
|
176
|
139
|
|
140 /** Extract the base of a file name (sans extension).
|
|
141 *
|
|
142 * Returns the filename without the extension. The extension is the part behind
|
|
143 * the last dot in the filename. If the filename has no dot the full file name
|
|
144 * is returned.
|
|
145 *
|
|
146 * @param fn
|
|
147 * @return
|
|
148 */
|
|
149 public static String basename(String fn) {
|
|
150 int i = fn.lastIndexOf('.');
|
|
151 if (i > 0) {
|
|
152 return fn.substring(0, i);
|
|
153 }
|
|
154 return fn;
|
|
155 }
|
|
156
|
|
157 /** Extract the extension of a file name.
|
|
158 *
|
|
159 * Returns the extension of a file name. The extension is the part behind
|
|
160 * the last dot in the filename. If the filename has no dot the empty string
|
|
161 * is returned.
|
|
162 *
|
|
163 * @param fn
|
|
164 * @return
|
|
165 */
|
|
166 public static String extname(String fn) {
|
|
167 int i = fn.lastIndexOf('.');
|
|
168 if (i > 0) {
|
|
169 return fn.substring(i+1);
|
|
170 }
|
|
171 return "";
|
|
172 }
|
152
|
173
|
|
174 /**
|
91
|
175 * FileFilter for image types (helper class for getFile)
|
|
176 */
|
|
177 static class ImageFileFilter implements FileFilter {
|
1
|
178
|
91
|
179 public boolean accept(File f) {
|
|
180 if (f.isFile()) {
|
159
|
181 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("image")));
|
91
|
182 } else {
|
|
183 return false;
|
|
184 }
|
|
185 }
|
|
186 }
|
1
|
187
|
159
|
188 /**
|
|
189 * FileFilter for text types (helper class for getFile)
|
|
190 */
|
|
191 static class TextFileFilter implements FileFilter {
|
|
192
|
|
193 public boolean accept(File f) {
|
|
194 if (f.isFile()) {
|
|
195 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("text")));
|
|
196 } else {
|
|
197 return false;
|
|
198 }
|
|
199 }
|
|
200 }
|
|
201
|
|
202 /** Factory for FileFilters (image or text).
|
|
203 *
|
|
204 * @param fileClass
|
|
205 * @return
|
|
206 */
|
|
207 public static FileFilter filterForClass(int fileClass) {
|
|
208 if (fileClass == CLASS_IMAGE) {
|
|
209 return new ImageFileFilter();
|
|
210 }
|
|
211 if (fileClass == CLASS_TEXT) {
|
|
212 return new TextFileFilter();
|
|
213 }
|
|
214 return null;
|
|
215 }
|
|
216
|
1
|
217 }
|