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