Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/FileOps.java @ 159:e743b853efca
servlet version 1.16a4
- rather experimental
- new Texter servlet for sending text
- reads and caches text files in DocuDirCache
- DocuFile renamed to ImageFile (-Set)
- new TextFile
author | robcast |
---|---|
date | Tue, 16 Sep 2003 18:32:00 +0200 |
parents | f4a5cfe37469 |
children | 67ff8c7fecb9 |
comparison
equal
deleted
inserted
replaced
158:e9a81ac446cb | 159:e743b853efca |
---|---|
18 | 18 |
19 */ | 19 */ |
20 | 20 |
21 package digilib.io; | 21 package digilib.io; |
22 | 22 |
23 import java.io.*; | 23 import java.io.File; |
24 import java.util.*; | 24 import java.io.FileFilter; |
25 import java.util.Arrays; | |
26 import java.util.Iterator; | |
27 import java.util.StringTokenizer; | |
25 | 28 |
26 import digilib.*; | 29 import digilib.Utils; |
27 | 30 |
28 public class FileOps { | 31 public class FileOps { |
29 | 32 |
30 private Utils util = null; | 33 private Utils util = null; |
34 | |
31 public static String[] fileTypes = | 35 public static String[] fileTypes = |
32 { | 36 { |
33 "jpg", | 37 "jpg", |
34 "image/jpeg", | 38 "image/jpeg", |
35 "jpeg", | 39 "jpeg", |
41 "gif", | 45 "gif", |
42 "image/gif", | 46 "image/gif", |
43 "tif", | 47 "tif", |
44 "image/tiff", | 48 "image/tiff", |
45 "tiff", | 49 "tiff", |
46 "image/tiff" }; | 50 "image/tiff", |
51 "txt", | |
52 "text/plain", | |
53 "html", | |
54 "text/html", | |
55 "htm", | |
56 "text/html", | |
57 "xml", | |
58 "text/xml" }; | |
47 | 59 |
48 public static String[] fileExtensions = | 60 public static String[] imageExtensions = |
49 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" }; | 61 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" }; |
62 | |
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 | |
50 | 71 |
51 public FileOps() { | 72 public FileOps() { |
52 util = new Utils(); | 73 util = new Utils(); |
53 } | 74 } |
54 | 75 |
71 } | 92 } |
72 } | 93 } |
73 return null; | 94 return null; |
74 } | 95 } |
75 | 96 |
76 public static Iterator getImageExtensionIterator() { | 97 /** |
77 return Arrays.asList(fileExtensions).iterator(); | 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 | |
78 } | 117 } |
79 | 118 |
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 | |
80 /** | 127 /** |
81 * convert a string with a list of pathnames into an array of strings | 128 * convert a string with a list of pathnames into an array of strings |
82 * using the system's path separator string | 129 * using the system's path separator string |
83 */ | 130 */ |
84 public static String[] pathToArray(String paths) { | 131 public static String[] pathToArray(String paths) { |
85 // split list into directories | 132 // split list into directories |
86 StringTokenizer dirs = | 133 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator); |
87 new StringTokenizer(paths, File.pathSeparator); | |
88 int n = dirs.countTokens(); | 134 int n = dirs.countTokens(); |
89 if (n < 1) { | 135 if (n < 1) { |
90 return null; | 136 return null; |
91 } | 137 } |
92 // add directories into array | 138 // add directories into array |
102 } | 148 } |
103 return pathArray; | 149 return pathArray; |
104 } | 150 } |
105 | 151 |
106 /** | 152 /** |
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 + ")"); | |
112 | |
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 } | |
128 | |
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 + ")"); | |
136 | |
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 } | |
149 | |
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 + ")"); | |
161 | |
162 File f = null; | |
163 int nvar = dirs.length; | |
164 | |
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 } | |
177 | |
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 + ")"); | |
186 | |
187 int nf = 0; | |
188 int nvar = dirs.length; | |
189 | |
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 } | |
202 | |
203 /** | |
204 * FileFilter for image types (helper class for getFile) | 153 * FileFilter for image types (helper class for getFile) |
205 */ | 154 */ |
206 static class ImageFileFilter implements FileFilter { | 155 static class ImageFileFilter implements FileFilter { |
207 | 156 |
208 public boolean accept(File f) { | 157 public boolean accept(File f) { |
209 if (f.isFile()) { | 158 if (f.isFile()) { |
210 return (mimeForFile(f) != null); | 159 return ((mimeForFile(f) != null)&&(mimeForFile(f).startsWith("image"))); |
211 } else { | 160 } else { |
212 return false; | 161 return false; |
213 } | 162 } |
214 } | 163 } |
215 } | 164 } |
216 | 165 |
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 | |
217 } | 195 } |