Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/DocuFileset.java @ 139:11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
- fixed lots of bugs in metadata handling.
author | robcast |
---|---|
date | Thu, 31 Jul 2003 20:56:51 +0200 |
parents | c36944be0b58 |
children | bdd6789d21e0 |
rev | line source |
---|---|
86 | 1 /* DocuFileset -- digilib image file info class. |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 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 package digilib.io; | |
21 | |
122 | 22 import java.awt.Dimension; |
130 | 23 import java.io.File; |
91 | 24 import java.util.ArrayList; |
25 import java.util.HashMap; | |
122 | 26 import java.util.Iterator; |
91 | 27 import java.util.ListIterator; |
86 | 28 |
122 | 29 import digilib.image.DocuInfo; |
30 | |
86 | 31 /** |
32 * @author casties | |
33 */ | |
91 | 34 public class DocuFileset { |
86 | 35 |
130 | 36 // list of files (DocuFile) |
91 | 37 private ArrayList list = null; |
86 | 38 // metadata |
91 | 39 private HashMap fileMeta = null; |
130 | 40 // metadata has been checked |
41 private boolean metaChecked = false; | |
42 // resolution (DPI) | |
43 private double resX = 0; | |
44 private double resY = 0; | |
86 | 45 // parent directory |
46 private DocuDirectory parent = null; | |
47 | |
91 | 48 /* |
49 * constructors | |
50 */ | |
86 | 51 |
52 public DocuFileset(int initialCapacity) { | |
91 | 53 list = new ArrayList(initialCapacity); |
86 | 54 } |
55 | |
91 | 56 /* |
57 * other stuff | |
58 */ | |
59 | |
60 /** Adds a DocuFile to this Fileset. | |
61 * | |
122 | 62 * The files should be added in the order of higher to lower resolutions. |
63 * The first file is considered the hires "original". | |
91 | 64 * |
65 * @param f file to add | |
66 * @return true (always) | |
67 */ | |
68 public boolean add(DocuFile f) { | |
69 f.setParent(this); | |
70 return list.add(f); | |
86 | 71 } |
122 | 72 |
91 | 73 /** The number of image files in this Fileset. |
74 * | |
75 * @return number of image files | |
76 */ | |
77 public int size() { | |
78 return (list != null) ? list.size() : 0; | |
79 } | |
122 | 80 |
91 | 81 /** Get the DocuFile at the index. |
82 * | |
83 * @param index | |
84 * @return | |
86 | 85 */ |
91 | 86 public DocuFile get(int index) { |
87 return (DocuFile) list.get(index); | |
88 } | |
122 | 89 |
90 /** Get the next smaller DocuFile than the given size. | |
91 * | |
92 * Returns the DocuFile from the set that has a width and height | |
93 * smaller or equal the given size. | |
94 * Returns null if there isn't any smaller image. | |
95 * Needs DocuInfo instance to checkFile(). | |
96 * | |
97 * @param size | |
98 * @param info | |
99 * @return | |
100 */ | |
101 public DocuFile getNextSmaller(Dimension size, DocuInfo info) { | |
102 for (Iterator i = getHiresIterator(); i.hasNext();) { | |
103 DocuFile f = (DocuFile) i.next(); | |
104 if (!f.isChecked()) { | |
105 f.check(info); | |
106 } | |
107 if ((f.getSize().getHeight() <= size.getHeight()) | |
108 && (f.getSize().getWidth() <= size.getWidth())) { | |
109 return f; | |
110 } | |
111 } | |
112 return null; | |
113 } | |
114 | |
115 /** Get the next bigger DocuFile than the given size. | |
116 * | |
117 * Returns the DocuFile from the set that has a width and height | |
118 * bigger or equal the given size. | |
119 * Returns null if there isn't any bigger image. | |
120 * Needs DocuInfo instance to checkFile(). | |
121 * | |
122 * @param size | |
123 * @param info | |
124 * @return | |
125 */ | |
126 public DocuFile getNextBigger(Dimension size, DocuInfo info) { | |
127 for (ListIterator i = getLoresIterator(); i.hasPrevious();) { | |
128 DocuFile f = (DocuFile) i.previous(); | |
129 if (!f.isChecked()) { | |
130 f.check(info); | |
131 } | |
132 if ((f.getSize().getHeight() >= size.getHeight()) | |
133 && (f.getSize().getWidth() >= size.getWidth())) { | |
134 return f; | |
135 } | |
136 } | |
137 return null; | |
138 } | |
139 | |
91 | 140 /** Get an Iterator for this Fileset starting at the highest resolution |
141 * images. | |
142 * | |
143 * @return | |
144 */ | |
145 public ListIterator getHiresIterator() { | |
146 return list.listIterator(); | |
86 | 147 } |
122 | 148 |
91 | 149 /** Get an Iterator for this Fileset starting at the lowest resolution |
150 * images. | |
151 * | |
152 * The Iterator starts at the last element, so you have to use it backwards | |
153 * with hasPrevious() and previous(). | |
154 * | |
155 * @return | |
156 */ | |
157 public ListIterator getLoresIterator() { | |
158 return list.listIterator(list.size()); | |
159 } | |
122 | 160 |
91 | 161 /** Reads meta-data for this Fileset if there is any. |
130 | 162 * |
163 */ | |
164 public void readMeta() { | |
165 if ((fileMeta != null) || list.isEmpty()) { | |
166 // there is already metadata or there's no file | |
167 return; | |
168 } | |
169 // metadata is in the file {filename}.meta | |
170 String fn = ((DocuFile) list.get(0)).getFile().getAbsolutePath(); | |
171 File mf = new File(fn + ".meta"); | |
172 if (mf.canRead()) { | |
173 XMLMetaLoader ml = new XMLMetaLoader(); | |
174 try { | |
175 // read meta file | |
176 HashMap meta = ml.loadURL(mf.getAbsolutePath()); | |
177 if (meta == null) { | |
178 return; | |
179 } | |
180 fileMeta = (HashMap) meta.get(getName()); | |
181 } catch (Exception e) { | |
182 // TODO Auto-generated catch block | |
183 e.printStackTrace(); | |
184 } | |
185 } | |
186 } | |
187 | |
188 /** Checks metadata and sets resolution in resX and resY. | |
189 * | |
91 | 190 */ |
191 public void checkMeta() { | |
130 | 192 if (metaChecked) { |
193 return; | |
194 } | |
195 if (fileMeta == null) { | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
196 // try to read metadata file |
130 | 197 readMeta(); |
198 if (fileMeta == null) { | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
199 // try directory metadata |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
200 if (parent.getDirMeta() != null) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
201 fileMeta = parent.getDirMeta(); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
202 } else { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
203 // no metadata available |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
204 metaChecked = true; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
205 return; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
206 } |
130 | 207 } |
208 } | |
209 metaChecked = true; | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
210 String s; |
130 | 211 double dpi = 0; |
212 double dpix = 0; | |
213 double dpiy = 0; | |
214 double sizex = 0; | |
215 double sizey = 0; | |
216 double pixx = 0; | |
217 double pixy = 0; | |
218 // DPI is valid for X and Y | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
219 if (fileMeta.containsKey("original-dpi")) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
220 try { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
221 dpi = Double.parseDouble((String) fileMeta.get("original-dpi")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
222 } catch (NumberFormatException e) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
223 } |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
224 if (dpi != 0) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
225 resX = dpi; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
226 resY = dpi; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
227 return; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
228 } |
130 | 229 } |
230 // DPI-X and DPI-Y | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
231 if (fileMeta.containsKey("original-dpi-x") |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
232 && fileMeta.containsKey("original-dpi-y")) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
233 try { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
234 dpix = |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
235 Double.parseDouble((String) fileMeta.get("original-dpi-x")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
236 dpiy = |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
237 Double.parseDouble((String) fileMeta.get("original-dpi-y")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
238 } catch (NumberFormatException e) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
239 } |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
240 if ((dpix != 0) && (dpiy != 0)) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
241 resX = dpix; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
242 resY = dpiy; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
243 return; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
244 } |
130 | 245 } |
246 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
247 if (fileMeta.containsKey("original-size-x") |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
248 && fileMeta.containsKey("original-size-y") |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
249 && fileMeta.containsKey("original-pixel-x") |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
250 && fileMeta.containsKey("original-pixel-y")) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
251 try { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
252 sizex = |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
253 Double.parseDouble( |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
254 (String) fileMeta.get("original-size-x")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
255 sizey = |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
256 Double.parseDouble( |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
257 (String) fileMeta.get("original-size-y")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
258 pixx = |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
259 Double.parseDouble( |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
260 (String) fileMeta.get("original-pixel-x")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
261 pixy = |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
262 Double.parseDouble( |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
263 (String) fileMeta.get("original-pixel-y")); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
264 } catch (NumberFormatException e) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
265 } |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
266 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) { |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
267 resX = pixx / (sizex * 100 / 2.54); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
268 resY = pixy / (sizey * 100 / 2.54); |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
269 return; |
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
270 } |
130 | 271 } |
86 | 272 } |
273 | |
130 | 274 /** The name of the (hires) image file. |
91 | 275 * |
276 * @return | |
277 */ | |
86 | 278 public String getName() { |
91 | 279 if (!list.isEmpty()) { |
280 return ((DocuFile) list.get(0)).getName(); | |
86 | 281 } |
282 return null; | |
283 } | |
122 | 284 |
91 | 285 /** Returns the parent DocuDirectory. |
130 | 286 * |
86 | 287 * @return DocuDirectory |
288 */ | |
289 public DocuDirectory getParent() { | |
290 return parent; | |
291 } | |
292 | |
293 /** | |
294 * Sets the parent. | |
295 * @param parent The parent to set | |
296 */ | |
297 public void setParent(DocuDirectory parent) { | |
298 this.parent = parent; | |
299 } | |
300 | |
91 | 301 /** Returns the meta-data for this fileset. |
302 * | |
303 * @return HashMap | |
304 */ | |
305 public HashMap getFileMeta() { | |
306 return fileMeta; | |
307 } | |
308 | |
309 /** | |
310 * Sets the fileMeta. | |
311 * @param fileMeta The fileMeta to set | |
312 */ | |
313 public void setFileMeta(HashMap fileMeta) { | |
314 this.fileMeta = fileMeta; | |
315 } | |
316 | |
130 | 317 /** |
318 * @return | |
319 */ | |
320 public boolean isMetaChecked() { | |
321 return metaChecked; | |
322 } | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
323 |
130 | 324 /** |
325 * @return | |
326 */ | |
327 public double getResX() { | |
328 return resX; | |
329 } | |
330 | |
331 /** | |
332 * @return | |
333 */ | |
334 public double getResY() { | |
335 return resY; | |
336 } | |
139
11cfe4c89fdc
Servlet version 1.11b1 with improved original-size.
robcast
parents:
130
diff
changeset
|
337 |
86 | 338 } |