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 /**
|
|
81 * get a filehandle for a file or directory name
|
|
82 * returns File number n if fn is directory (starts with 1)
|
|
83 */
|
|
84 public File getFile(String fn, int n) throws FileOpException {
|
|
85 util.dprintln(4, "getFile (" + fn + ", " + n + ")");
|
1
|
86
|
91
|
87 File f = new File(fn);
|
|
88 // if fn is a file name then return file
|
|
89 if (f.isFile()) {
|
|
90 return f;
|
|
91 }
|
|
92 // if fn is a directory name then open directory
|
|
93 if (f.isDirectory()) {
|
|
94 File[] fl = f.listFiles(new ImageFileFilter());
|
|
95 Arrays.sort(fl);
|
|
96 if ((n > 0) && (n <= fl.length)) {
|
|
97 return fl[n - 1];
|
|
98 }
|
|
99 }
|
|
100 throw new FileOpException("Unable to find file: " + fn);
|
|
101 }
|
1
|
102
|
91
|
103 /**
|
|
104 * get the number of files in a directory
|
|
105 * (almost the same as getFile)
|
|
106 * returns 0 in case of problems
|
|
107 */
|
|
108 public int getNumFiles(String fn) throws FileOpException {
|
|
109 util.dprintln(4, "getNumFiles (" + fn + ")");
|
1
|
110
|
91
|
111 File f = new File(fn);
|
|
112 // if fn is a file name then return 1
|
|
113 if (f.isFile()) {
|
|
114 return 1;
|
|
115 }
|
|
116 // if fn is a directory name then return the number of files
|
|
117 if (f.isDirectory()) {
|
|
118 return f.listFiles(new ImageFileFilter()).length;
|
|
119 }
|
|
120 // then fn must be something strange...
|
|
121 return 0;
|
|
122 }
|
1
|
123
|
91
|
124 /**
|
|
125 * get a filehandle for a file or directory name out of a list
|
|
126 * dirs is a list of base directories, fn is the appended file/dirname
|
|
127 * searches dirs until fn exists (backwards if fwd is false)
|
|
128 * returns File number n if fn is directory (starts with 1)
|
|
129 */
|
|
130 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd)
|
|
131 throws FileOpException {
|
|
132 util.dprintln(
|
|
133 4,
|
|
134 "getVariantFile (" + dirs + ", " + fn + ", " + n + ")");
|
1
|
135
|
91
|
136 File f = null;
|
|
137 int nvar = dirs.length;
|
1
|
138
|
91
|
139 for (int i = 0; i < nvar; i++) {
|
|
140 try {
|
|
141 f = getFile(dirs[(fwd) ? i : (nvar - i - 1)] + fn, n);
|
|
142 } catch (FileOpException e) {
|
|
143 f = null;
|
|
144 }
|
|
145 if (f != null) {
|
|
146 return f;
|
|
147 }
|
|
148 }
|
|
149 throw new FileOpException("Unable to find file: " + fn);
|
|
150 }
|
1
|
151
|
91
|
152 /**
|
|
153 * get the number of files in a directory
|
|
154 * (almost the same as getFileVariant)
|
|
155 * returns 0 in case of problems
|
|
156 */
|
|
157 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd)
|
|
158 throws FileOpException {
|
|
159 util.dprintln(4, "getNumFilesVariant (" + dirs + ", " + fn + ")");
|
1
|
160
|
91
|
161 int nf = 0;
|
|
162 int nvar = dirs.length;
|
1
|
163
|
91
|
164 for (int i = 0; i < nvar; i++) {
|
|
165 try {
|
|
166 nf = getNumFiles(dirs[(fwd) ? i : (nvar - i - 1)] + fn);
|
|
167 } catch (FileOpException e) {
|
|
168 nf = 0;
|
|
169 }
|
|
170 if (nf > 0) {
|
|
171 return nf;
|
|
172 }
|
|
173 }
|
|
174 return 0;
|
|
175 }
|
1
|
176
|
91
|
177 /**
|
|
178 * FileFilter for image types (helper class for getFile)
|
|
179 */
|
|
180 static class ImageFileFilter implements FileFilter {
|
1
|
181
|
91
|
182 public boolean accept(File f) {
|
|
183 if (f.isFile()) {
|
|
184 return (mimeForFile(f) != null);
|
|
185 } else {
|
|
186 return false;
|
|
187 }
|
|
188 }
|
|
189 }
|
1
|
190
|
|
191 }
|