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
|
|
23 import java.io.*;
|
|
24 import java.util.*;
|
|
25
|
|
26 import digilib.*;
|
|
27
|
|
28 public class FileOps {
|
|
29
|
91
|
30 private Utils util = null;
|
|
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",
|
|
46 "image/tiff" };
|
1
|
47
|
91
|
48 public static String[] fileExtensions =
|
|
49 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" };
|
|
50
|
|
51 public FileOps() {
|
|
52 util = new Utils();
|
|
53 }
|
1
|
54
|
91
|
55 public FileOps(Utils u) {
|
|
56 util = u;
|
|
57 }
|
1
|
58
|
91
|
59 public void setUtils(Utils u) {
|
|
60 util = u;
|
|
61 }
|
1
|
62
|
91
|
63 /**
|
|
64 * get the mime type for a file format (by extension)
|
|
65 */
|
|
66 public static String mimeForFile(File f) {
|
|
67 String fn = f.getName();
|
|
68 for (int i = 0; i < fileTypes.length; i += 2) {
|
|
69 if (fn.toLowerCase().endsWith(fileTypes[i])) {
|
|
70 return fileTypes[i + 1];
|
|
71 }
|
|
72 }
|
|
73 return null;
|
|
74 }
|
1
|
75
|
91
|
76 public static Iterator getImageExtensionIterator() {
|
|
77 return Arrays.asList(fileExtensions).iterator();
|
|
78 }
|
1
|
79
|
91
|
80 /**
|
152
|
81 * convert a string with a list of pathnames into an array of strings
|
|
82 * using the system's path separator string
|
|
83 */
|
|
84 public static String[] pathToArray(String paths) {
|
|
85 // split list into directories
|
|
86 StringTokenizer dirs =
|
|
87 new StringTokenizer(paths, File.pathSeparator);
|
|
88 int n = dirs.countTokens();
|
|
89 if (n < 1) {
|
|
90 return null;
|
|
91 }
|
|
92 // add directories into array
|
|
93 String[] pathArray = new String[n];
|
|
94 for (int i = 0; i < n; i++) {
|
|
95 String s = dirs.nextToken();
|
|
96 // make shure the dir name ends with a directory separator
|
|
97 if (s.endsWith(File.separator)) {
|
|
98 pathArray[i] = s;
|
|
99 } else {
|
|
100 pathArray[i] = s + File.separator;
|
|
101 }
|
|
102 }
|
|
103 return pathArray;
|
|
104 }
|
|
105
|
|
106 /**
|
91
|
107 * get a filehandle for a file or directory name
|
|
108 * returns File number n if fn is directory (starts with 1)
|
|
109 */
|
|
110 public File getFile(String fn, int n) throws FileOpException {
|
|
111 util.dprintln(4, "getFile (" + fn + ", " + n + ")");
|
1
|
112
|
91
|
113 File f = new File(fn);
|
|
114 // if fn is a file name then return file
|
|
115 if (f.isFile()) {
|
|
116 return f;
|
|
117 }
|
|
118 // if fn is a directory name then open directory
|
|
119 if (f.isDirectory()) {
|
|
120 File[] fl = f.listFiles(new ImageFileFilter());
|
|
121 Arrays.sort(fl);
|
|
122 if ((n > 0) && (n <= fl.length)) {
|
|
123 return fl[n - 1];
|
|
124 }
|
|
125 }
|
|
126 throw new FileOpException("Unable to find file: " + fn);
|
|
127 }
|
1
|
128
|
91
|
129 /**
|
|
130 * get the number of files in a directory
|
|
131 * (almost the same as getFile)
|
|
132 * returns 0 in case of problems
|
|
133 */
|
|
134 public int getNumFiles(String fn) throws FileOpException {
|
|
135 util.dprintln(4, "getNumFiles (" + fn + ")");
|
1
|
136
|
91
|
137 File f = new File(fn);
|
|
138 // if fn is a file name then return 1
|
|
139 if (f.isFile()) {
|
|
140 return 1;
|
|
141 }
|
|
142 // if fn is a directory name then return the number of files
|
|
143 if (f.isDirectory()) {
|
|
144 return f.listFiles(new ImageFileFilter()).length;
|
|
145 }
|
|
146 // then fn must be something strange...
|
|
147 return 0;
|
|
148 }
|
1
|
149
|
91
|
150 /**
|
|
151 * get a filehandle for a file or directory name out of a list
|
|
152 * dirs is a list of base directories, fn is the appended file/dirname
|
|
153 * searches dirs until fn exists (backwards if fwd is false)
|
|
154 * returns File number n if fn is directory (starts with 1)
|
|
155 */
|
|
156 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd)
|
|
157 throws FileOpException {
|
|
158 util.dprintln(
|
|
159 4,
|
|
160 "getVariantFile (" + dirs + ", " + fn + ", " + n + ")");
|
1
|
161
|
91
|
162 File f = null;
|
|
163 int nvar = dirs.length;
|
1
|
164
|
91
|
165 for (int i = 0; i < nvar; i++) {
|
|
166 try {
|
|
167 f = getFile(dirs[(fwd) ? i : (nvar - i - 1)] + fn, n);
|
|
168 } catch (FileOpException e) {
|
|
169 f = null;
|
|
170 }
|
|
171 if (f != null) {
|
|
172 return f;
|
|
173 }
|
|
174 }
|
|
175 throw new FileOpException("Unable to find file: " + fn);
|
|
176 }
|
1
|
177
|
91
|
178 /**
|
|
179 * get the number of files in a directory
|
|
180 * (almost the same as getFileVariant)
|
|
181 * returns 0 in case of problems
|
|
182 */
|
|
183 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd)
|
|
184 throws FileOpException {
|
|
185 util.dprintln(4, "getNumFilesVariant (" + dirs + ", " + fn + ")");
|
1
|
186
|
91
|
187 int nf = 0;
|
|
188 int nvar = dirs.length;
|
1
|
189
|
91
|
190 for (int i = 0; i < nvar; i++) {
|
|
191 try {
|
|
192 nf = getNumFiles(dirs[(fwd) ? i : (nvar - i - 1)] + fn);
|
|
193 } catch (FileOpException e) {
|
|
194 nf = 0;
|
|
195 }
|
|
196 if (nf > 0) {
|
|
197 return nf;
|
|
198 }
|
|
199 }
|
|
200 return 0;
|
|
201 }
|
1
|
202
|
91
|
203 /**
|
|
204 * FileFilter for image types (helper class for getFile)
|
|
205 */
|
|
206 static class ImageFileFilter implements FileFilter {
|
1
|
207
|
91
|
208 public boolean accept(File f) {
|
|
209 if (f.isFile()) {
|
|
210 return (mimeForFile(f) != null);
|
|
211 } else {
|
|
212 return false;
|
|
213 }
|
|
214 }
|
|
215 }
|
1
|
216
|
|
217 }
|