Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/FileOps.java @ 1:0ff3ede32060
Initial revision
author | robcast |
---|---|
date | Thu, 17 Jan 2002 15:25:46 +0100 |
parents | |
children | 2ea78a56ecf8 6d2032b6121d 9cedd170b581 |
comparison
equal
deleted
inserted
replaced
0:ffd2df307e81 | 1:0ff3ede32060 |
---|---|
1 /* FileOps -- Utility class for file operations | |
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 | |
29 public class FileOps { | |
30 | |
31 private Utils util = null; | |
32 public static String[] fileTypes = { | |
33 "jpg", "image/jpeg", | |
34 "jpeg", "image/jpeg", | |
35 "png", "image/png", | |
36 "gif", "image/gif", | |
37 "tif", "image/tiff", | |
38 "tiff", "image/tiff"}; | |
39 | |
40 public FileOps() { | |
41 util = new Utils(); | |
42 } | |
43 | |
44 public FileOps(Utils u) { | |
45 util = u; | |
46 } | |
47 | |
48 public void setUtils(Utils u) { | |
49 util = u; | |
50 } | |
51 | |
52 | |
53 /** | |
54 * get the mime type for a file format (by extension) | |
55 */ | |
56 public static String mimeForFile(File f) { | |
57 String fn = f.getName(); | |
58 for (int i = 0; i < fileTypes.length; i += 2) { | |
59 if (fn.toLowerCase().endsWith(fileTypes[i])) { | |
60 return fileTypes[i+1]; | |
61 } | |
62 } | |
63 return null; | |
64 } | |
65 | |
66 /** | |
67 * get a filehandle for a file or directory name | |
68 * returns File number n if fn is directory (starts with 1) | |
69 */ | |
70 public File getFile(String fn, int n) throws FileOpException { | |
71 util.dprintln(4, "getFile ("+fn+", "+n+")"); | |
72 | |
73 File f = new File(fn); | |
74 // if fn is a file name then return file | |
75 if (f.isFile()) { | |
76 return f; | |
77 } | |
78 // if fn is a directory name then open directory | |
79 if (f.isDirectory()) { | |
80 File[] fl = f.listFiles(new ImgFileFilter()); | |
81 Arrays.sort(fl); | |
82 if ((n > 0) && (n <= fl.length)) { | |
83 return fl[n - 1]; | |
84 } | |
85 } | |
86 throw new FileOpException("Unable to find file: "+fn); | |
87 } | |
88 | |
89 /** | |
90 * get the number of files in a directory | |
91 * (almost the same as getFile) | |
92 * returns 0 in case of problems | |
93 */ | |
94 public int getNumFiles(String fn) throws FileOpException { | |
95 util.dprintln(4, "getNumFiles ("+fn+")"); | |
96 | |
97 File f = new File(fn); | |
98 // if fn is a file name then return 1 | |
99 if (f.isFile()) { | |
100 return 1; | |
101 } | |
102 // if fn is a directory name then return the number of files | |
103 if (f.isDirectory()) { | |
104 return f.listFiles(new ImgFileFilter()).length; | |
105 } | |
106 // then fn must be something strange... | |
107 return 0; | |
108 } | |
109 | |
110 | |
111 /** | |
112 * get a filehandle for a file or directory name out of a list | |
113 * dirs is a list of base directories, fn is the appended file/dirname | |
114 * searches dirs until fn exists (backwards if fwd is false) | |
115 * returns File number n if fn is directory (starts with 1) | |
116 */ | |
117 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) throws FileOpException { | |
118 util.dprintln(4, "getVariantFile ("+dirs+", "+fn+", "+n+")"); | |
119 | |
120 File f = null; | |
121 int start = 0; | |
122 int inc = 1; | |
123 int end = dirs.length; | |
124 if (fwd == false) { | |
125 start = dirs.length - 1; | |
126 inc = -1; | |
127 end = 0; | |
128 } | |
129 | |
130 for (int i = start; i != end; i += inc) { | |
131 try { | |
132 f = getFile(dirs[i]+fn, n); | |
133 } catch (FileOpException e) { | |
134 f = null; | |
135 } | |
136 if (f != null) { | |
137 return f; | |
138 } | |
139 } | |
140 throw new FileOpException("Unable to find file: "+fn); | |
141 } | |
142 | |
143 /** | |
144 * get the number of files in a directory | |
145 * (almost the same as getFileVariant) | |
146 * returns 0 in case of problems | |
147 */ | |
148 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) throws FileOpException { | |
149 util.dprintln(4, "getNumFilesVariant ("+dirs+", "+fn+")"); | |
150 | |
151 int nf = 0; | |
152 int start = 0; | |
153 int inc = 1; | |
154 int end = dirs.length; | |
155 if (fwd == false) { | |
156 start = dirs.length - 1; | |
157 inc = -1; | |
158 end = 0; | |
159 } | |
160 | |
161 for (int i = start; i != end; i += inc) { | |
162 try { | |
163 nf = getNumFiles(dirs[i]+fn); | |
164 } catch (FileOpException e) { | |
165 nf = 0; | |
166 } | |
167 if (nf > 0) { | |
168 return nf; | |
169 } | |
170 } | |
171 return 0; | |
172 } | |
173 | |
174 /** | |
175 * FileFilter for image types (helper class for getFile) | |
176 */ | |
177 private class ImgFileFilter implements FileFilter { | |
178 | |
179 public boolean accept(File f) { | |
180 if (f.isFile()) { | |
181 return (mimeForFile(f) != null); | |
182 } else { | |
183 return false; | |
184 } | |
185 } | |
186 } | |
187 | |
188 } |