22afc92f8d3521bfc16ba05e38461ac2f4ef77a9
[nikiroo-utils.git] / supported / InfoReader.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Scanner;
11
12 import be.nikiroo.fanfix.Instance;
13 import be.nikiroo.fanfix.bundles.Config;
14 import be.nikiroo.fanfix.data.Chapter;
15 import be.nikiroo.fanfix.data.MetaData;
16 import be.nikiroo.utils.IOUtils;
17 import be.nikiroo.utils.Image;
18 import be.nikiroo.utils.streams.MarkableFileInputStream;
19
20 public class InfoReader {
21 static protected BasicSupportHelper bsHelper = new BasicSupportHelper();
22 static protected BasicSupportImages bsImages = new BasicSupportImages();
23 static protected BasicSupportPara bsPara = new BasicSupportPara(
24 new BasicSupportHelper(), new BasicSupportImages());
25
26 public static MetaData readMeta(File infoFile, boolean withCover)
27 throws IOException {
28 if (infoFile == null) {
29 throw new IOException("File is null");
30 }
31
32 MetaData meta = null;
33
34 if (infoFile.exists()) {
35 InputStream in = new MarkableFileInputStream(infoFile);
36 try {
37 meta = createMeta(infoFile.toURI().toURL(), in,
38 withCover);
39
40 // Some old .info files were using UUID for URL...
41 if (!hasIt(meta.getUrl()) && meta.getUuid() != null
42 && (meta.getUuid().startsWith("http://")
43 || meta.getUuid().startsWith("https://"))) {
44 meta.setUrl(meta.getUuid());
45 }
46
47 // Some old .info files don't have those now required fields...
48 // So we check if we can find the info in another way (many
49 // formats have a copy of the original text file)
50 if (!hasIt(meta.getTitle(), meta.getAuthor(), meta.getDate(),
51 meta.getUrl())) {
52 String base = infoFile.getPath();
53 if (base.endsWith(".info")) {
54 base = base.substring(0,
55 base.length() - ".info".length());
56 }
57 File textFile = new File(base);
58 if (!textFile.exists()) {
59 textFile = new File(base + ".txt");
60 }
61 if (!textFile.exists()) {
62 textFile = new File(base + ".text");
63 }
64
65 completeMeta(textFile, meta);
66 }
67
68
69 } finally {
70 in.close();
71 }
72 }
73
74 if (meta != null) {
75 try {
76 File summaryFile = new File(infoFile.getAbsolutePath()
77 .replaceFirst("\\.info$", ".summary"));
78 InputStream in = new MarkableFileInputStream(summaryFile);
79 try {
80 String content = IOUtils.readSmallStream(in);
81 Chapter desc = bsPara.makeChapter(null, null, 0,
82 "Description", content, false, null);
83 meta.setResume(desc);
84 } finally {
85 in.close();
86 }
87 } catch (IOException e) {
88 // ignore absent or bad summary
89 }
90
91 return meta;
92 }
93
94 throw new FileNotFoundException(
95 "File given as argument does not exists: "
96 + infoFile.getAbsolutePath());
97 }
98
99 /**
100 * Complete the given {@link MetaData} with the original text file if needed
101 * and possible.
102 *
103 * @param textFile
104 * the original text file
105 * @param meta
106 * the {@link MetaData} to complete if needed and possible
107 *
108 * @throws IOException
109 * in case of I/O errors
110 */
111 static public void completeMeta(File textFile,
112 MetaData meta) throws IOException {
113 // TODO: not nice, would be better to do it properly...
114 if (textFile != null && textFile.exists()) {
115 final URL source = textFile.toURI().toURL();
116 final MetaData[] superMetaA = new MetaData[1];
117 @SuppressWarnings("unused")
118 Text unused = new Text() {
119 private boolean loaded = loadDocument();
120
121 @Override
122 public SupportType getType() {
123 return SupportType.TEXT;
124 }
125
126 protected boolean loadDocument() throws IOException {
127 loadDocument(source);
128 superMetaA[0] = getMeta();
129 return true;
130 }
131
132 @Override
133 protected Image getCover(File sourceFile) {
134 return null;
135 }
136 };
137
138 MetaData superMeta = superMetaA[0];
139 if (!hasIt(meta.getTitle())) {
140 meta.setTitle(superMeta.getTitle());
141 }
142 if (!hasIt(meta.getAuthor())) {
143 meta.setAuthor(superMeta.getAuthor());
144 }
145 if (!hasIt(meta.getDate())) {
146 meta.setDate(superMeta.getDate());
147 }
148 if (!hasIt(meta.getUrl())) {
149 meta.setUrl(superMeta.getUrl());
150 }
151 }
152 }
153
154 /**
155 * Check if we have non-empty values for all the given {@link String}s.
156 *
157 * @param values
158 * the values to check
159 *
160 * @return TRUE if none of them was NULL or empty
161 */
162 static private boolean hasIt(String... values) {
163 for (String value : values) {
164 if (value == null || value.trim().isEmpty()) {
165 return false;
166 }
167 }
168
169 return true;
170 }
171
172 private static MetaData createMeta(URL sourceInfoFile, InputStream in,
173 boolean withCover) throws IOException {
174 MetaData meta = new MetaData();
175
176 meta.setTitle(getInfoTag(in, "TITLE"));
177 meta.setAuthor(getInfoTag(in, "AUTHOR"));
178 meta.setDate(bsHelper.formatDate(getInfoTag(in, "DATE")));
179 meta.setTags(getInfoTagList(in, "TAGS", ","));
180 meta.setSource(getInfoTag(in, "SOURCE"));
181 meta.setUrl(getInfoTag(in, "URL"));
182 meta.setPublisher(getInfoTag(in, "PUBLISHER"));
183 meta.setUuid(getInfoTag(in, "UUID"));
184 meta.setLuid(getInfoTag(in, "LUID"));
185 meta.setLang(getInfoTag(in, "LANG"));
186 meta.setSubject(getInfoTag(in, "SUBJECT"));
187 meta.setType(getInfoTag(in, "TYPE"));
188 meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false));
189 if (withCover) {
190 String infoTag = getInfoTag(in, "COVER");
191 if (infoTag != null && !infoTag.trim().isEmpty()) {
192 meta.setCover(bsHelper.getImage(null, sourceInfoFile, infoTag));
193 }
194 if (meta.getCover() == null) {
195 // Second chance: try to check for a cover next to the info file
196 meta.setCover(getCoverByName(sourceInfoFile));
197 }
198 }
199 try {
200 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
201 } catch (NumberFormatException e) {
202 meta.setWords(0);
203 }
204 meta.setCreationDate(
205 bsHelper.formatDate(getInfoTag(in, "CREATION_DATE")));
206 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
207
208 if (withCover && meta.getCover() == null) {
209 meta.setCover(bsHelper.getDefaultCover(meta.getSubject()));
210 }
211
212 return meta;
213 }
214
215 /**
216 * Return the cover image if it is next to the source file.
217 *
218 * @param sourceInfoFile
219 * the source file
220 *
221 * @return the cover if present, NULL if not
222 */
223 public static Image getCoverByName(URL sourceInfoFile) {
224 Image cover = null;
225
226 File basefile = new File(sourceInfoFile.getFile());
227
228 String ext = "." + Instance.getInstance().getConfig()
229 .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
230
231 // Without removing ext
232 cover = bsHelper.getImage(null, sourceInfoFile,
233 basefile.getAbsolutePath() + ext);
234
235 // Try without ext
236 String name = basefile.getName();
237 int pos = name.lastIndexOf(".");
238 if (cover == null && pos > 0) {
239 name = name.substring(0, pos);
240 basefile = new File(basefile.getParent(), name);
241
242 cover = bsHelper.getImage(null, sourceInfoFile,
243 basefile.getAbsolutePath() + ext);
244 }
245
246 return cover;
247 }
248
249 private static boolean getInfoTagBoolean(InputStream in, String key,
250 boolean def) throws IOException {
251 Boolean value = getInfoTagBoolean(in, key);
252 return value == null ? def : value;
253 }
254
255 private static Boolean getInfoTagBoolean(InputStream in, String key)
256 throws IOException {
257 String value = getInfoTag(in, key);
258 if (value != null && !value.trim().isEmpty()) {
259 value = value.toLowerCase().trim();
260 return value.equals("1") || value.equals("on")
261 || value.equals("true") || value.equals("yes");
262 }
263
264 return null;
265 }
266
267 private static List<String> getInfoTagList(InputStream in, String key,
268 String separator) throws IOException {
269 List<String> list = new ArrayList<String>();
270 String tt = getInfoTag(in, key);
271 if (tt != null) {
272 for (String tag : tt.split(separator)) {
273 list.add(tag.trim());
274 }
275 }
276
277 return list;
278 }
279
280 /**
281 * Return the value of the given tag in the <tt>.info</tt> file if present.
282 *
283 * @param key
284 * the tag key
285 *
286 * @return the value or NULL
287 *
288 * @throws IOException
289 * in case of I/O error
290 */
291 private static String getInfoTag(InputStream in, String key)
292 throws IOException {
293 key = "^" + key + "=";
294
295 if (in != null) {
296 in.reset();
297 String value = getLine(in, key, 0);
298 if (value != null && !value.isEmpty()) {
299 value = value.trim().substring(key.length() - 1).trim();
300 if (value.length() > 1 && //
301 (value.startsWith("'") && value.endsWith("'")
302 || value.startsWith("\"")
303 && value.endsWith("\""))) {
304 value = value.substring(1, value.length() - 1).trim();
305 }
306
307 // Some old files ended up with TITLE="'xxxxx'"
308 if ("^TITLE=".equals(key)) {
309 if (value.startsWith("'") && value.endsWith("'")
310 && value.length() > 1) {
311 value = value.substring(1, value.length() - 1).trim();
312 }
313 }
314
315 return value;
316 }
317 }
318
319 return null;
320 }
321
322 /**
323 * Return the first line from the given input which correspond to the given
324 * selectors.
325 *
326 * @param in
327 * the input
328 * @param needle
329 * a string that must be found inside the target line (also
330 * supports "^" at start to say "only if it starts with" the
331 * needle)
332 * @param relativeLine
333 * the line to return based upon the target line position (-1 =
334 * the line before, 0 = the target line...)
335 *
336 * @return the line
337 */
338 static private String getLine(InputStream in, String needle,
339 int relativeLine) {
340 return getLine(in, needle, relativeLine, true);
341 }
342
343 /**
344 * Return a line from the given input which correspond to the given
345 * selectors.
346 *
347 * @param in
348 * the input
349 * @param needle
350 * a string that must be found inside the target line (also
351 * supports "^" at start to say "only if it starts with" the
352 * needle)
353 * @param relativeLine
354 * the line to return based upon the target line position (-1 =
355 * the line before, 0 = the target line...)
356 * @param first
357 * takes the first result (as opposed to the last one, which will
358 * also always spend the input)
359 *
360 * @return the line
361 */
362 static private String getLine(InputStream in, String needle,
363 int relativeLine, boolean first) {
364 String rep = null;
365
366 List<String> lines = new ArrayList<String>();
367 @SuppressWarnings("resource")
368 Scanner scan = new Scanner(in, "UTF-8");
369 int index = -1;
370 scan.useDelimiter("\\n");
371 while (scan.hasNext()) {
372 lines.add(scan.next());
373
374 if (index == -1) {
375 if (needle.startsWith("^")) {
376 if (lines.get(lines.size() - 1)
377 .startsWith(needle.substring(1))) {
378 index = lines.size() - 1;
379 }
380
381 } else {
382 if (lines.get(lines.size() - 1).contains(needle)) {
383 index = lines.size() - 1;
384 }
385 }
386 }
387
388 if (index >= 0 && index + relativeLine < lines.size()) {
389 rep = lines.get(index + relativeLine);
390 if (first) {
391 break;
392 }
393 }
394 }
395
396 return rep;
397 }
398 }