Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/FileOps.java @ 72:300d5ba8b33b
New servlet version 1.5b.
Mostly cleanup. Global parameters for digilib now in DigilibConfiguration,
per request parameters are now all in DigilibRequest. The DocuImage implementation
can be selected by the configuration docuimage-class.
Pixel-by-pixel view implemented with "mo=clip".
author | robcast |
---|---|
date | Fri, 24 Jan 2003 21:40:59 +0100 |
parents | 5ea1999befd4 |
children | 4e6757e8ccd4 |
rev | line source |
---|---|
1 | 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", | |
64
5ea1999befd4
New JAI ImageLoader plugin. Currently uses first beta version of the plugin.
robcast
parents:
56
diff
changeset
|
35 "jp2", "image/jp2", |
1 | 36 "png", "image/png", |
37 "gif", "image/gif", | |
38 "tif", "image/tiff", | |
39 "tiff", "image/tiff"}; | |
40 | |
41 public FileOps() { | |
42 util = new Utils(); | |
43 } | |
44 | |
45 public FileOps(Utils u) { | |
46 util = u; | |
47 } | |
48 | |
49 public void setUtils(Utils u) { | |
50 util = u; | |
51 } | |
52 | |
53 | |
54 /** | |
55 * get the mime type for a file format (by extension) | |
56 */ | |
57 public static String mimeForFile(File f) { | |
58 String fn = f.getName(); | |
59 for (int i = 0; i < fileTypes.length; i += 2) { | |
60 if (fn.toLowerCase().endsWith(fileTypes[i])) { | |
61 return fileTypes[i+1]; | |
62 } | |
63 } | |
64 return null; | |
65 } | |
66 | |
67 /** | |
68 * get a filehandle for a file or directory name | |
69 * returns File number n if fn is directory (starts with 1) | |
70 */ | |
71 public File getFile(String fn, int n) throws FileOpException { | |
72 util.dprintln(4, "getFile ("+fn+", "+n+")"); | |
73 | |
74 File f = new File(fn); | |
75 // if fn is a file name then return file | |
76 if (f.isFile()) { | |
77 return f; | |
78 } | |
79 // if fn is a directory name then open directory | |
80 if (f.isDirectory()) { | |
81 File[] fl = f.listFiles(new ImgFileFilter()); | |
82 Arrays.sort(fl); | |
83 if ((n > 0) && (n <= fl.length)) { | |
84 return fl[n - 1]; | |
85 } | |
86 } | |
87 throw new FileOpException("Unable to find file: "+fn); | |
88 } | |
89 | |
90 /** | |
91 * get the number of files in a directory | |
92 * (almost the same as getFile) | |
93 * returns 0 in case of problems | |
94 */ | |
95 public int getNumFiles(String fn) throws FileOpException { | |
96 util.dprintln(4, "getNumFiles ("+fn+")"); | |
97 | |
98 File f = new File(fn); | |
99 // if fn is a file name then return 1 | |
100 if (f.isFile()) { | |
101 return 1; | |
102 } | |
103 // if fn is a directory name then return the number of files | |
104 if (f.isDirectory()) { | |
105 return f.listFiles(new ImgFileFilter()).length; | |
106 } | |
107 // then fn must be something strange... | |
108 return 0; | |
109 } | |
110 | |
111 | |
112 /** | |
113 * get a filehandle for a file or directory name out of a list | |
114 * dirs is a list of base directories, fn is the appended file/dirname | |
115 * searches dirs until fn exists (backwards if fwd is false) | |
116 * returns File number n if fn is directory (starts with 1) | |
117 */ | |
118 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) throws FileOpException { | |
119 util.dprintln(4, "getVariantFile ("+dirs+", "+fn+", "+n+")"); | |
120 | |
121 File f = null; | |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
122 int nvar = dirs.length; |
1 | 123 |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
124 for (int i = 0; i < nvar; i++) { |
1 | 125 try { |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
126 f = getFile(dirs[(fwd) ? i : (nvar-i-1)]+fn, n); |
1 | 127 } catch (FileOpException e) { |
128 f = null; | |
129 } | |
130 if (f != null) { | |
131 return f; | |
132 } | |
133 } | |
134 throw new FileOpException("Unable to find file: "+fn); | |
135 } | |
136 | |
137 /** | |
138 * get the number of files in a directory | |
139 * (almost the same as getFileVariant) | |
140 * returns 0 in case of problems | |
141 */ | |
142 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) throws FileOpException { | |
143 util.dprintln(4, "getNumFilesVariant ("+dirs+", "+fn+")"); | |
144 | |
145 int nf = 0; | |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
146 int nvar = dirs.length; |
1 | 147 |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
148 for (int i = 0; i < nvar; i++) { |
1 | 149 try { |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
150 nf = getNumFiles(dirs[(fwd) ? i : (nvar-i-1)]+fn); |
1 | 151 } catch (FileOpException e) { |
152 nf = 0; | |
153 } | |
154 if (nf > 0) { | |
155 return nf; | |
156 } | |
157 } | |
158 return 0; | |
159 } | |
160 | |
161 /** | |
162 * FileFilter for image types (helper class for getFile) | |
163 */ | |
164 private class ImgFileFilter implements FileFilter { | |
165 | |
166 public boolean accept(File f) { | |
167 if (f.isFile()) { | |
168 return (mimeForFile(f) != null); | |
169 } else { | |
170 return false; | |
171 } | |
172 } | |
173 } | |
174 | |
175 } |