Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/ImageFileset.java @ 284:c633e97cac12
Servlet version 1.22b1
- more fast searching (hopefully all working now)
- some simple synchronisation
- some reshuffling of methods to eliminate cruft
author | robcast |
---|---|
date | Fri, 15 Oct 2004 16:59:47 +0200 |
parents | beed92ee6022 |
children | 90bab835fc25 |
comparison
equal
deleted
inserted
replaced
283:b09009d7fcfc | 284:c633e97cac12 |
---|---|
23 import java.util.Arrays; | 23 import java.util.Arrays; |
24 import java.util.Iterator; | 24 import java.util.Iterator; |
25 import java.util.ListIterator; | 25 import java.util.ListIterator; |
26 import java.util.Map; | 26 import java.util.Map; |
27 | 27 |
28 import digilib.image.DocuInfo; | 28 import org.apache.log4j.Logger; |
29 | |
30 import digilib.image.ImageOps; | |
29 import digilib.image.ImageSize; | 31 import digilib.image.ImageSize; |
30 | 32 |
31 /** | 33 /** |
32 * @author casties | 34 * @author casties |
33 */ | 35 */ |
34 public class ImageFileset extends DocuDirent { | 36 public class ImageFileset extends DocuDirent { |
35 | 37 |
36 /** this is an image file */ | 38 /** this is an image file */ |
37 protected static int fileClass = FileOps.CLASS_IMAGE; | 39 protected static int fileClass = FileOps.CLASS_IMAGE; |
40 | |
41 /** logger for this class */ | |
42 private static Logger logger = Logger.getLogger(ImageFileset.class); | |
38 | 43 |
39 /** list of files (ImageFile) */ | 44 /** list of files (ImageFile) */ |
40 private ArrayList list = null; | 45 private ArrayList list = null; |
41 | 46 |
42 /** aspect ratio (width/height) */ | 47 /** aspect ratio (width/height) */ |
69 public ImageFileset(File file, Map hints) { | 74 public ImageFileset(File file, Map hints) { |
70 Directory[] dirs = (Directory[]) hints.get(FileOps.HINT_BASEDIRS); | 75 Directory[] dirs = (Directory[]) hints.get(FileOps.HINT_BASEDIRS); |
71 int nb = dirs.length; | 76 int nb = dirs.length; |
72 list = new ArrayList(nb); | 77 list = new ArrayList(nb); |
73 parent = dirs[0]; | 78 parent = dirs[0]; |
74 fill(dirs, file, hints); | 79 fill(dirs, file, hints); |
75 } | 80 } |
76 | 81 |
77 | |
78 /** | 82 /** |
79 * Adds an ImageFile to this Fileset. | 83 * Adds an ImageFile to this Fileset. |
80 * | 84 * |
81 * The files should be added in the order of higher to lower resolutions. | 85 * The files should be added in the order of higher to lower resolutions. |
82 * The first file is considered the hires "original". | 86 * The first file is considered the hires "original". |
130 * | 134 * |
131 * @param size | 135 * @param size |
132 * @param info | 136 * @param info |
133 * @return | 137 * @return |
134 */ | 138 */ |
135 public ImageFile getNextSmaller(ImageSize size, DocuInfo info) { | 139 public ImageFile getNextSmaller(ImageSize size) { |
136 for (Iterator i = getHiresIterator(); i.hasNext();) { | 140 for (Iterator i = getHiresIterator(); i.hasNext();) { |
137 ImageFile f = (ImageFile) i.next(); | 141 ImageFile f = (ImageFile) i.next(); |
138 try { | 142 try { |
139 if (!f.isChecked()) { | 143 if (!f.isChecked()) { |
140 info.checkFile(f); | 144 ImageOps.checkFile(f); |
141 } | 145 } |
142 if (f.getSize().isTotallySmallerThan(size)) { | 146 if (f.getSize().isTotallySmallerThan(size)) { |
143 return f; | 147 return f; |
144 } | 148 } |
145 } catch (IOException e) { | 149 } catch (IOException e) { |
158 * | 162 * |
159 * @param size | 163 * @param size |
160 * @param info | 164 * @param info |
161 * @return | 165 * @return |
162 */ | 166 */ |
163 public ImageFile getNextBigger(ImageSize size, DocuInfo info) { | 167 public ImageFile getNextBigger(ImageSize size) { |
164 for (ListIterator i = getLoresIterator(); i.hasPrevious();) { | 168 for (ListIterator i = getLoresIterator(); i.hasPrevious();) { |
165 ImageFile f = (ImageFile) i.previous(); | 169 ImageFile f = (ImageFile) i.previous(); |
166 try { | 170 try { |
167 if (!f.isChecked()) { | 171 if (!f.isChecked()) { |
168 info.checkFile(f); | 172 ImageOps.checkFile(f); |
169 } | 173 } |
170 if (f.getSize().isBiggerThan(size)) { | 174 if (f.getSize().isBiggerThan(size)) { |
171 return f; | 175 return f; |
172 } | 176 } |
173 } catch (IOException e) { | 177 } catch (IOException e) { |
228 * @param dirs | 232 * @param dirs |
229 * list of base directories | 233 * list of base directories |
230 * @param fl | 234 * @param fl |
231 * file (from first base dir) | 235 * file (from first base dir) |
232 * @param hints | 236 * @param hints |
233 * | 237 * |
234 */ | 238 */ |
235 void fill(Directory[] dirs, File fl, Map hints) { | 239 void fill(Directory[] dirs, File fl, Map hints) { |
236 String scalext = (String) hints.get(FileOps.HINT_FILEEXT); | 240 String scalext = (String) hints.get(FileOps.HINT_FILEEXT); |
237 int nb = dirs.length; | 241 int nb = dirs.length; |
238 String fn = fl.getName(); | 242 String fn = fl.getName(); |
239 String baseFn = FileOps.basename(fn); | 243 String baseFn = FileOps.basename(fn); |
240 // add the first ImageFile to the ImageFileset | 244 // add the first ImageFile to the ImageFileset |
241 add(new ImageFile(fn, this, parent)); | 245 add(new ImageFile(fn, this, parent)); |
242 // iterate the remaining base directories | 246 // iterate the remaining base directories |
243 for (int j = 1; j < nb; j++) { | 247 for (int dirIdx = 1; dirIdx < nb; dirIdx++) { |
244 if (dirs[j] == null) { | 248 if (dirs[dirIdx] == null) { |
245 continue; | 249 continue; |
246 } | 250 } |
247 // read the directories | 251 // read the directory |
248 if (dirs[j].getFilenames() == null) { | 252 if (dirs[dirIdx].getFilenames() == null) { |
249 dirs[j].readDir(); | 253 dirs[dirIdx].readDir(); |
250 } | 254 } |
251 File f = null; | 255 String[] dirFiles = dirs[dirIdx].getFilenames(); |
252 String[] dirFiles = dirs[j].getFilenames(); | 256 // try the same filename as the original |
253 if (scalext != null) { | 257 int fileIdx = Arrays.binarySearch(dirFiles, fn); |
254 // use the last extension | 258 if (fileIdx < 0) { |
255 int i = Arrays.binarySearch(dirFiles, baseFn + scalext); | 259 // try closest matches without extension |
256 if (i >= 0) { | 260 fileIdx = -fileIdx - 1; |
257 f = new File(dirs[j].getDir(), dirFiles[i]); | 261 // try idx |
258 } | 262 if ((fileIdx < dirFiles.length) |
259 } else { | 263 && (FileOps.basename(dirFiles[fileIdx]).equals(baseFn))) { |
260 // try the same filename as the original | 264 // idx ok |
261 int i = Arrays.binarySearch(dirFiles, fn); | 265 } else if ((fileIdx > 0) |
262 if (i >= 0) { | 266 && (FileOps.basename(dirFiles[fileIdx - 1]) |
263 f = new File(dirs[j].getDir(), dirFiles[i]); | 267 .equals(baseFn))) { |
264 } | 268 // idx-1 ok |
265 } | 269 fileIdx = fileIdx - 1; |
266 // if the file doesn't exists, try other file extensions | 270 } else if ((fileIdx+1 < dirFiles.length) |
267 if (f == null) { | 271 && (FileOps.basename(dirFiles[fileIdx + 1]) |
268 // try other file extensions | 272 .equals(baseFn))) { |
269 for (Iterator exts = FileOps.getImageExtensionIterator(); exts.hasNext();) { | 273 // idx+1 ok |
270 String s = (String) exts.next(); | 274 fileIdx = fileIdx + 1; |
271 int i = Arrays.binarySearch(dirFiles, baseFn + s); | 275 } else { |
272 if (i >= 0) { | 276 // basename doesn't match |
273 hints.put(FileOps.HINT_FILEEXT, s); | 277 continue; |
274 f = new File(dirs[j].getDir(), dirFiles[i]); | 278 } |
275 break; | 279 } |
276 } | 280 if (FileOps.classForFilename(dirFiles[fileIdx]) == FileOps.CLASS_IMAGE) { |
277 } | 281 logger.debug("adding file " + dirFiles[fileIdx] |
278 } | 282 + " to Fileset " + this.getName()); |
279 if (f != null) { | 283 add(new ImageFile(dirFiles[fileIdx], this, dirs[dirIdx])); |
280 add(new ImageFile(f.getName(), this, dirs[j])); | |
281 } | 284 } |
282 } | 285 } |
283 } | 286 } |
284 | 287 |
285 /** | 288 /** |