Mercurial > hg > digilib-old
comparison common/src/main/java/digilib/io/ImageFileSet.java @ 903:7779b37d1d05
refactored into maven modules per servlet type.
can build servlet-api 2.3 and 3.0 via profile now!
author | robcast |
---|---|
date | Tue, 26 Apr 2011 20:24:31 +0200 |
parents | servlet/src/main/java/digilib/io/ImageFileSet.java@ba1eb2d821a2 |
children | 4f84a635a820 |
comparison
equal
deleted
inserted
replaced
902:89ba3ffcf552 | 903:7779b37d1d05 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package digilib.io; | |
5 | |
6 import java.io.File; | |
7 import java.util.ArrayList; | |
8 import java.util.Arrays; | |
9 import java.util.Map; | |
10 | |
11 import org.apache.log4j.Logger; | |
12 | |
13 import digilib.io.FileOps.FileClass; | |
14 import digilib.meta.MetadataMap; | |
15 import digilib.meta.XMLMetaLoader; | |
16 | |
17 /** | |
18 * @author casties | |
19 * | |
20 */ | |
21 public class ImageFileSet extends ImageSet implements DocuDirent { | |
22 | |
23 /** this is an image file */ | |
24 protected static FileClass fileClass = FileClass.IMAGE; | |
25 /** the (main) file */ | |
26 protected File file = null; | |
27 /** the file name */ | |
28 protected String name = null; | |
29 /** HashMap with metadata */ | |
30 protected MetadataMap fileMeta = null; | |
31 /** Is the Metadata valid */ | |
32 protected boolean metaChecked = false; | |
33 /** the parent directory */ | |
34 protected Directory parentDir = null; | |
35 | |
36 /** | |
37 * Constructor with a File and Directories. | |
38 * | |
39 * @param file | |
40 * @param scaleDirs | |
41 */ | |
42 public ImageFileSet(File file, Directory[] scaleDirs) { | |
43 int nb = scaleDirs.length; | |
44 list = new ArrayList<ImageInput>(nb); | |
45 // first dir is our parent | |
46 parentDir = scaleDirs[0]; | |
47 this.file = file; | |
48 this.name = file.getName(); | |
49 fill(scaleDirs, file); | |
50 } | |
51 | |
52 /* (non-Javadoc) | |
53 * @see digilib.io.DocuDirent#getName() | |
54 */ | |
55 public String getName() { | |
56 return this.name; | |
57 } | |
58 | |
59 /* (non-Javadoc) | |
60 * @see digilib.io.DocuDirent#getParent() | |
61 */ | |
62 public Directory getParent() { | |
63 return this.parentDir; | |
64 } | |
65 | |
66 /* (non-Javadoc) | |
67 * @see digilib.io.DocuDirent#setParent(digilib.io.Directory) | |
68 */ | |
69 public void setParent(Directory parent) { | |
70 this.parentDir = parent; | |
71 } | |
72 | |
73 /* (non-Javadoc) | |
74 * @see digilib.io.DocuDirent#getFileMeta() | |
75 */ | |
76 public MetadataMap getFileMeta() { | |
77 return this.fileMeta; | |
78 } | |
79 | |
80 /* (non-Javadoc) | |
81 * @see digilib.io.DocuDirent#setFileMeta(digilib.io.MetadataMap) | |
82 */ | |
83 public void setFileMeta(MetadataMap fileMeta) { | |
84 this.fileMeta = fileMeta; | |
85 } | |
86 | |
87 /* (non-Javadoc) | |
88 * @see digilib.io.DocuDirent#isMetaChecked() | |
89 */ | |
90 public boolean isMetaChecked() { | |
91 return this.metaChecked; | |
92 } | |
93 | |
94 /* (non-Javadoc) | |
95 * @see digilib.io.DocuDirent#compareTo(java.lang.Object) | |
96 */ | |
97 public int compareTo(Object arg0) { | |
98 if (arg0 instanceof DocuDirent) { | |
99 return name.compareTo(((DocuDirent) arg0).getName()); | |
100 } else { | |
101 return getName().compareTo((String) arg0); | |
102 } | |
103 } | |
104 | |
105 /* (non-Javadoc) | |
106 * @see digilib.io.DocuDirent#getFile() | |
107 */ | |
108 public File getFile() { | |
109 return file; | |
110 } | |
111 | |
112 /** | |
113 * Adds an ImageFile to this Fileset. | |
114 * | |
115 * The files should be added in the order of higher to lower resolutions. | |
116 * The first file is considered the hires "original". | |
117 * | |
118 * | |
119 * @param f | |
120 * file to add | |
121 * @return true (always) | |
122 */ | |
123 public boolean add(ImageInput f) { | |
124 f.setParent(this); | |
125 return list.add(f); | |
126 } | |
127 | |
128 /** | |
129 * Fill the ImageSet with files from different base directories. | |
130 * | |
131 * | |
132 * @param dirs | |
133 * list of base directories | |
134 * @param fl | |
135 * file (from first base dir) | |
136 * @param hints | |
137 * | |
138 */ | |
139 void fill(Directory[] dirs, File fl) { | |
140 String fn = fl.getName(); | |
141 String baseFn = FileOps.basename(fn); | |
142 // add the first ImageFile to the ImageSet | |
143 add(new ImageFile(fl, this, parentDir)); | |
144 // iterate the remaining base directories | |
145 for (int i = 1; i < dirs.length; ++i) { | |
146 Directory dir = dirs[i]; | |
147 if (dir == null) { | |
148 continue; | |
149 } | |
150 // read the directory | |
151 if (dir.getFilenames() == null) { | |
152 dir.readDir(); | |
153 } | |
154 String[] dirFiles = dir.getFilenames(); | |
155 // try the same filename as the original | |
156 int fileIdx = Arrays.binarySearch(dirFiles, fn); | |
157 if (fileIdx < 0) { | |
158 // try closest matches without extension | |
159 fileIdx = -fileIdx - 1; | |
160 // try idx | |
161 if ((fileIdx < dirFiles.length) | |
162 && (FileOps.basename(dirFiles[fileIdx]).equals(baseFn))) { | |
163 // idx ok | |
164 } else if ((fileIdx > 0) | |
165 && (FileOps.basename(dirFiles[fileIdx - 1]) | |
166 .equals(baseFn))) { | |
167 // idx-1 ok | |
168 fileIdx = fileIdx - 1; | |
169 } else if ((fileIdx+1 < dirFiles.length) | |
170 && (FileOps.basename(dirFiles[fileIdx + 1]) | |
171 .equals(baseFn))) { | |
172 // idx+1 ok | |
173 fileIdx = fileIdx + 1; | |
174 } else { | |
175 // basename doesn't match | |
176 continue; | |
177 } | |
178 } | |
179 if (FileOps.classForFilename(dirFiles[fileIdx]) == fileClass) { | |
180 /* logger.debug("adding file " + dirFiles[fileIdx] | |
181 + " to Fileset " + this.getName()); */ | |
182 add(new ImageFile(dirFiles[fileIdx], this, dir)); | |
183 } | |
184 } | |
185 } | |
186 | |
187 /** | |
188 * Checks metadata and sets resolution in resX and resY. | |
189 * | |
190 */ | |
191 public void checkMeta() { | |
192 if (metaChecked) { | |
193 return; | |
194 } | |
195 if (fileMeta == null) { | |
196 // try to read metadata file | |
197 readMeta(); | |
198 if (fileMeta == null) { | |
199 // try directory metadata | |
200 ((DocuDirectory) parentDir).checkMeta(); | |
201 if (((DocuDirectory) parentDir).getDirMeta() != null) { | |
202 fileMeta = ((DocuDirectory) parentDir).getDirMeta(); | |
203 } else { | |
204 // try parent directory metadata | |
205 DocuDirectory gp = (DocuDirectory) parentDir.getParent(); | |
206 if (gp != null) { | |
207 gp.checkMeta(); | |
208 if (gp.getDirMeta() != null) { | |
209 fileMeta = gp.getDirMeta(); | |
210 } | |
211 } | |
212 } | |
213 } | |
214 } | |
215 if (fileMeta == null) { | |
216 // no metadata available | |
217 metaChecked = true; | |
218 return; | |
219 } | |
220 metaChecked = true; | |
221 float dpi = 0; | |
222 float dpix = 0; | |
223 float dpiy = 0; | |
224 float sizex = 0; | |
225 float sizey = 0; | |
226 float pixx = 0; | |
227 float pixy = 0; | |
228 // DPI is valid for X and Y | |
229 if (fileMeta.containsKey("original-dpi")) { | |
230 try { | |
231 dpi = Float.parseFloat((String) fileMeta.get("original-dpi")); | |
232 } catch (NumberFormatException e) { | |
233 } | |
234 if (dpi != 0) { | |
235 resX = dpi; | |
236 resY = dpi; | |
237 return; | |
238 } | |
239 } | |
240 // DPI-X and DPI-Y | |
241 if (fileMeta.containsKey("original-dpi-x") | |
242 && fileMeta.containsKey("original-dpi-y")) { | |
243 try { | |
244 dpix = Float.parseFloat((String) fileMeta | |
245 .get("original-dpi-x")); | |
246 dpiy = Float.parseFloat((String) fileMeta | |
247 .get("original-dpi-y")); | |
248 } catch (NumberFormatException e) { | |
249 } | |
250 if ((dpix != 0) && (dpiy != 0)) { | |
251 resX = dpix; | |
252 resY = dpiy; | |
253 return; | |
254 } | |
255 } | |
256 // SIZE-X and SIZE-Y and PIXEL-X and PIXEL-Y | |
257 if (fileMeta.containsKey("original-size-x") | |
258 && fileMeta.containsKey("original-size-y") | |
259 && fileMeta.containsKey("original-pixel-x") | |
260 && fileMeta.containsKey("original-pixel-y")) { | |
261 try { | |
262 sizex = Float.parseFloat((String) fileMeta | |
263 .get("original-size-x")); | |
264 sizey = Float.parseFloat((String) fileMeta | |
265 .get("original-size-y")); | |
266 pixx = Float.parseFloat((String) fileMeta | |
267 .get("original-pixel-x")); | |
268 pixy = Float.parseFloat((String) fileMeta | |
269 .get("original-pixel-y")); | |
270 } catch (NumberFormatException e) { | |
271 } | |
272 if ((sizex != 0) && (sizey != 0) && (pixx != 0) && (pixy != 0)) { | |
273 resX = pixx / (sizex * 100 / 2.54f); | |
274 resY = pixy / (sizey * 100 / 2.54f); | |
275 return; | |
276 } | |
277 } | |
278 } | |
279 | |
280 /* (non-Javadoc) | |
281 * @see digilib.io.DocuDirent#readMeta() | |
282 */ | |
283 public void readMeta() { | |
284 if ((fileMeta != null) || (file == null)) { | |
285 // there is already metadata or there is no file | |
286 return; | |
287 } | |
288 // metadata is in the file {filename}.meta | |
289 String fn = file.getAbsolutePath(); | |
290 File mf = new File(fn + ".meta"); | |
291 if (mf.canRead()) { | |
292 XMLMetaLoader ml = new XMLMetaLoader(); | |
293 try { | |
294 // read meta file | |
295 Map<String, MetadataMap> meta = ml.loadURL(mf.getAbsolutePath()); | |
296 if (meta == null) { | |
297 return; | |
298 } | |
299 fileMeta = meta.get(name); | |
300 } catch (Exception e) { | |
301 Logger.getLogger(this.getClass()).warn("error reading file .meta", e); | |
302 } | |
303 } | |
304 } | |
305 | |
306 | |
307 } |