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 }
|
|
151
|
|
152 /**
|
91
|
153 * FileFilter for image types (helper class for getFile)
|
|
154 */
|
|
155 static class ImageFileFilter implements FileFilter {
|
1
|
156
|
91
|
157 public boolean accept(File f) {
|
|
158 if (f.isFile()) {
|
159
|
159 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("image")));
|
91
|
160 } else {
|
|
161 return false;
|
|
162 }
|
|
163 }
|
|
164 }
|
1
|
165
|
159
|
166 /**
|
|
167 * FileFilter for text types (helper class for getFile)
|
|
168 */
|
|
169 static class TextFileFilter implements FileFilter {
|
|
170
|
|
171 public boolean accept(File f) {
|
|
172 if (f.isFile()) {
|
|
173 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("text")));
|
|
174 } else {
|
|
175 return false;
|
|
176 }
|
|
177 }
|
|
178 }
|
|
179
|
|
180 /** Factory for FileFilters (image or text).
|
|
181 *
|
|
182 * @param fileClass
|
|
183 * @return
|
|
184 */
|
|
185 public static FileFilter filterForClass(int fileClass) {
|
|
186 if (fileClass == CLASS_IMAGE) {
|
|
187 return new ImageFileFilter();
|
|
188 }
|
|
189 if (fileClass == CLASS_TEXT) {
|
|
190 return new TextFileFilter();
|
|
191 }
|
|
192 return null;
|
|
193 }
|
|
194
|
1
|
195 }
|