comparison servlet/src/digilib/io/DocuDirectory.java @ 472:f8ca069517a2

Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
author hertzhaft
date Fri, 07 Apr 2006 20:39:07 +0200
parents d10e4ce2a153
children fb51443ca0ee
comparison
equal deleted inserted replaced
471:d623c6fee3c9 472:f8ca069517a2
28 import java.util.Collections; 28 import java.util.Collections;
29 import java.util.Iterator; 29 import java.util.Iterator;
30 import java.util.List; 30 import java.util.List;
31 import java.util.Map; 31 import java.util.Map;
32 32
33 import org.apache.log4j.Logger;
34
33 import org.xml.sax.SAXException; 35 import org.xml.sax.SAXException;
34 36
35 /** 37 /**
36 * @author casties 38 * @author casties
37 */ 39 */
173 return false; 175 return false;
174 } 176 }
175 // first file extension to try for scaled directories 177 // first file extension to try for scaled directories
176 String scalext = null; 178 String scalext = null;
177 // read all filenames 179 // read all filenames
178 //logger.debug("reading directory " + dir.getPath()); 180 logger.debug("reading directory " + dir.getPath());
179 /* 181 /*
180 * using ReadableFileFilter is safer (we won't get directories with file 182 * using ReadableFileFilter is safer (we won't get directories with file
181 * extensions) but slower. 183 * extensions) but slower.
182 */ 184 */
183 File[] allFiles = null; 185 File[] allFiles = null;
202 // fill array with the remaining directories 204 // fill array with the remaining directories
203 for (int j = 1; j < nb; j++) { 205 for (int j = 1; j < nb; j++) {
204 File d = new File(baseDirNames[j], dirName); 206 File d = new File(baseDirNames[j], dirName);
205 if (d.isDirectory()) { 207 if (d.isDirectory()) {
206 dirs[j] = new Directory(d); 208 dirs[j] = new Directory(d);
207 //logger.debug(" reading scaled directory " + d.getPath()); 209 logger.debug(" reading scaled directory " + d.getPath());
208 dirs[j].readDir(); 210 dirs[j].readDir();
209 //logger.debug(" done"); 211 //logger.debug(" done");
210 } 212 }
211 } 213 }
212 214
222 int numFiles = fileList.length; 224 int numFiles = fileList.length;
223 if (numFiles > 0) { 225 if (numFiles > 0) {
224 // create new list 226 // create new list
225 list[fileClass] = new ArrayList(numFiles); 227 list[fileClass] = new ArrayList(numFiles);
226 // sort the file names alphabetically and iterate the list 228 // sort the file names alphabetically and iterate the list
227 Arrays.sort(fileList); 229 // Arrays.sort(fileList); // not needed <hertzhaft>
228 Map hints = FileOps.newHints(FileOps.HINT_BASEDIRS, dirs); 230 Map hints = FileOps.newHints(FileOps.HINT_BASEDIRS, dirs);
229 hints.put(FileOps.HINT_FILEEXT, scalext); 231 hints.put(FileOps.HINT_FILEEXT, scalext);
230 for (int i = 0; i < numFiles; i++) { 232 for (int i = 0; i < numFiles; i++) {
231 DocuDirent f = FileOps.fileForClass(fileClass, fileList[i], 233 DocuDirent f = FileOps.fileForClass(fileClass, fileList[i],
232 hints); 234 hints);
233 // add the file to our list 235 // add the file to our list
236 // logger.debug(f.getName());
237
234 list[fileClass].add(f); 238 list[fileClass].add(f);
235 f.setParent(this); 239 f.setParent(this);
236 } 240 }
241 // we need to sort the ArrayList, not the Array, for binarysearch to work
242 Collections.sort(list[fileClass]);
237 } 243 }
238 } 244 }
239 // clear the scaled directories 245 // clear the scaled directories
240 for (int j = 1; j < nb; j++) { 246 for (int j = 1; j < nb; j++) {
241 if (dirs[j] != null) { 247 if (dirs[j] != null) {
416 List fileList = list[fc]; 422 List fileList = list[fc];
417 // empty directory? 423 // empty directory?
418 if (fileList == null) { 424 if (fileList == null) {
419 return -1; 425 return -1;
420 } 426 }
427
421 // search for exact match 428 // search for exact match
429 // OBS: fileList needs to be sorted first (see )! <hertzhaft>
422 int idx = Collections.binarySearch(fileList, fn); 430 int idx = Collections.binarySearch(fileList, fn);
423 if (idx >= 0) { 431 if (idx >= 0) {
424 return idx; 432 return idx;
425 } else { 433 } else {
434 logger.debug(fn + " not found by binarysearch");
426 // try closest matches without extension 435 // try closest matches without extension
427 idx = -idx - 1; 436 idx = -idx - 1;
428 if ((idx < fileList.size()) 437 if ((idx < fileList.size())
429 && isBaseInList(fileList, idx, fn)) { 438 && isBaseInList(fileList, idx, fn)) {
430 // idx matches 439 // idx matches