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
|
|
29 public class FileOps {
|
|
30
|
91
|
31 public static String[] fileTypes =
|
|
32 {
|
|
33 "jpg",
|
|
34 "image/jpeg",
|
|
35 "jpeg",
|
|
36 "image/jpeg",
|
|
37 "jp2",
|
|
38 "image/jp2",
|
|
39 "png",
|
|
40 "image/png",
|
|
41 "gif",
|
|
42 "image/gif",
|
|
43 "tif",
|
|
44 "image/tiff",
|
|
45 "tiff",
|
159
|
46 "image/tiff",
|
|
47 "txt",
|
|
48 "text/plain",
|
|
49 "html",
|
|
50 "text/html",
|
|
51 "htm",
|
|
52 "text/html",
|
|
53 "xml",
|
|
54 "text/xml" };
|
1
|
55
|
159
|
56 public static String[] imageExtensions =
|
91
|
57 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
|
|
58
|
159
|
59 public static String[] textExtensions =
|
|
60 { "txt", "html", "htm", "xml"};
|
|
61
|
|
62 public static final int CLASS_NONE = -1;
|
|
63 public static final int CLASS_IMAGE = 0;
|
|
64 public static final int CLASS_TEXT = 1;
|
|
65 public static final int NUM_CLASSES = 2;
|
|
66
|
|
67
|
91
|
68 /**
|
|
69 * get the mime type for a file format (by extension)
|
|
70 */
|
|
71 public static String mimeForFile(File f) {
|
|
72 String fn = f.getName();
|
|
73 for (int i = 0; i < fileTypes.length; i += 2) {
|
|
74 if (fn.toLowerCase().endsWith(fileTypes[i])) {
|
|
75 return fileTypes[i + 1];
|
|
76 }
|
|
77 }
|
|
78 return null;
|
|
79 }
|
1
|
80
|
159
|
81 /**
|
|
82 * get the file class for the filename (by extension)
|
|
83 * @param fn
|
|
84 * @return
|
|
85 */
|
|
86 public static int classForFilename(String fn) {
|
|
87 int n = imageExtensions.length;
|
|
88 for (int i = 0; i < n; i ++) {
|
|
89 if (fn.toLowerCase().endsWith(imageExtensions[i])) {
|
|
90 return CLASS_IMAGE;
|
|
91 }
|
|
92 }
|
|
93 n = textExtensions.length;
|
|
94 for (int i = 0; i < n; i ++) {
|
|
95 if (fn.toLowerCase().endsWith(textExtensions[i])) {
|
|
96 return CLASS_TEXT;
|
|
97 }
|
|
98 }
|
|
99 return CLASS_NONE;
|
|
100
|
91
|
101 }
|
1
|
102
|
159
|
103 public static Iterator getImageExtensionIterator() {
|
|
104 return Arrays.asList(imageExtensions).iterator();
|
|
105 }
|
|
106
|
|
107 public static Iterator getTextExtensionIterator() {
|
|
108 return Arrays.asList(textExtensions).iterator();
|
|
109 }
|
|
110
|
91
|
111 /**
|
152
|
112 * convert a string with a list of pathnames into an array of strings
|
|
113 * using the system's path separator string
|
|
114 */
|
|
115 public static String[] pathToArray(String paths) {
|
|
116 // split list into directories
|
159
|
117 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
|
152
|
118 int n = dirs.countTokens();
|
|
119 if (n < 1) {
|
|
120 return null;
|
|
121 }
|
|
122 // add directories into array
|
|
123 String[] pathArray = new String[n];
|
|
124 for (int i = 0; i < n; i++) {
|
|
125 String s = dirs.nextToken();
|
|
126 // make shure the dir name ends with a directory separator
|
|
127 if (s.endsWith(File.separator)) {
|
|
128 pathArray[i] = s;
|
|
129 } else {
|
|
130 pathArray[i] = s + File.separator;
|
|
131 }
|
|
132 }
|
|
133 return pathArray;
|
|
134 }
|
176
|
135
|
|
136 /** Extract the base of a file name (sans extension).
|
|
137 *
|
|
138 * Returns the filename without the extension. The extension is the part behind
|
|
139 * the last dot in the filename. If the filename has no dot the full file name
|
|
140 * is returned.
|
|
141 *
|
|
142 * @param fn
|
|
143 * @return
|
|
144 */
|
|
145 public static String basename(String fn) {
|
|
146 int i = fn.lastIndexOf('.');
|
|
147 if (i > 0) {
|
|
148 return fn.substring(0, i);
|
|
149 }
|
|
150 return fn;
|
|
151 }
|
|
152
|
|
153 /** Extract the extension of a file name.
|
|
154 *
|
|
155 * Returns the extension of a file name. The extension is the part behind
|
|
156 * the last dot in the filename. If the filename has no dot the empty string
|
|
157 * is returned.
|
|
158 *
|
|
159 * @param fn
|
|
160 * @return
|
|
161 */
|
|
162 public static String extname(String fn) {
|
|
163 int i = fn.lastIndexOf('.');
|
|
164 if (i > 0) {
|
|
165 return fn.substring(i+1);
|
|
166 }
|
|
167 return "";
|
|
168 }
|
152
|
169
|
|
170 /**
|
91
|
171 * FileFilter for image types (helper class for getFile)
|
|
172 */
|
|
173 static class ImageFileFilter implements FileFilter {
|
1
|
174
|
91
|
175 public boolean accept(File f) {
|
|
176 if (f.isFile()) {
|
159
|
177 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("image")));
|
91
|
178 } else {
|
|
179 return false;
|
|
180 }
|
|
181 }
|
|
182 }
|
1
|
183
|
159
|
184 /**
|
|
185 * FileFilter for text types (helper class for getFile)
|
|
186 */
|
|
187 static class TextFileFilter implements FileFilter {
|
|
188
|
|
189 public boolean accept(File f) {
|
|
190 if (f.isFile()) {
|
|
191 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("text")));
|
|
192 } else {
|
|
193 return false;
|
|
194 }
|
|
195 }
|
|
196 }
|
|
197
|
|
198 /** Factory for FileFilters (image or text).
|
|
199 *
|
|
200 * @param fileClass
|
|
201 * @return
|
|
202 */
|
|
203 public static FileFilter filterForClass(int fileClass) {
|
|
204 if (fileClass == CLASS_IMAGE) {
|
|
205 return new ImageFileFilter();
|
|
206 }
|
|
207 if (fileClass == CLASS_TEXT) {
|
|
208 return new TextFileFilter();
|
|
209 }
|
|
210 return null;
|
|
211 }
|
|
212
|
1
|
213 }
|