Update nikiroo-utils, bugfixes:
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / InfoReader.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.URL;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import be.nikiroo.fanfix.Instance;
13 import be.nikiroo.fanfix.bundles.Config;
14 import be.nikiroo.fanfix.data.MetaData;
15 import be.nikiroo.utils.MarkableFileInputStream;
16
17 // not complete: no "description" tag
18 public class InfoReader {
19 public static MetaData readMeta(File infoFile, boolean withCover)
20 throws IOException {
21 if (infoFile == null) {
22 throw new IOException("File is null");
23 }
24
25 if (infoFile.exists()) {
26 InputStream in = new MarkableFileInputStream(new FileInputStream(
27 infoFile));
28 try {
29 return createMeta(infoFile.toURI().toURL(), in, withCover);
30 } finally {
31 in.close();
32 in = null;
33 }
34 }
35
36 throw new FileNotFoundException(
37 "File given as argument does not exists: "
38 + infoFile.getAbsolutePath());
39 }
40
41 private static MetaData createMeta(URL sourceInfoFile, InputStream in,
42 boolean withCover) throws IOException {
43 MetaData meta = new MetaData();
44
45 meta.setTitle(getInfoTag(in, "TITLE"));
46 meta.setAuthor(getInfoTag(in, "AUTHOR"));
47 meta.setDate(getInfoTag(in, "DATE"));
48 meta.setTags(getInfoTagList(in, "TAGS", ","));
49 meta.setSource(getInfoTag(in, "SOURCE"));
50 meta.setUrl(getInfoTag(in, "URL"));
51 meta.setPublisher(getInfoTag(in, "PUBLISHER"));
52 meta.setUuid(getInfoTag(in, "UUID"));
53 meta.setLuid(getInfoTag(in, "LUID"));
54 meta.setLang(getInfoTag(in, "LANG"));
55 meta.setSubject(getInfoTag(in, "SUBJECT"));
56 meta.setType(getInfoTag(in, "TYPE"));
57 meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false));
58 if (withCover) {
59 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
60 getInfoTag(in, "COVER")));
61 // Second chance: try to check for a cover next to the info file
62 if (meta.getCover() == null) {
63 String info = sourceInfoFile.getFile().toString();
64 if (info.endsWith(".info")) {
65 info = info.substring(0, info.length() - ".info".length());
66 String ext = "."
67 + Instance.getConfig()
68 .getString(Config.IMAGE_FORMAT_COVER)
69 .toLowerCase();
70 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
71 info + ext));
72 }
73 }
74 }
75 try {
76 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
77 } catch (NumberFormatException e) {
78 meta.setWords(0);
79 }
80 meta.setCreationDate(getInfoTag(in, "CREATION_DATE"));
81 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
82
83 if (withCover && meta.getCover() == null) {
84 meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
85 }
86
87 return meta;
88 }
89
90 private static boolean getInfoTagBoolean(InputStream in, String key,
91 boolean def) throws IOException {
92 Boolean value = getInfoTagBoolean(in, key);
93 return value == null ? def : value;
94 }
95
96 private static Boolean getInfoTagBoolean(InputStream in, String key)
97 throws IOException {
98 String value = getInfoTag(in, key);
99 if (value != null && !value.trim().isEmpty()) {
100 value = value.toLowerCase().trim();
101 return value.equals("1") || value.equals("on")
102 || value.equals("true") || value.equals("yes");
103 }
104
105 return null;
106 }
107
108 private static List<String> getInfoTagList(InputStream in, String key,
109 String separator) throws IOException {
110 List<String> list = new ArrayList<String>();
111 String tt = getInfoTag(in, key);
112 if (tt != null) {
113 for (String tag : tt.split(separator)) {
114 list.add(tag.trim());
115 }
116 }
117
118 return list;
119 }
120
121 /**
122 * Return the value of the given tag in the <tt>.info</tt> file if present.
123 *
124 * @param key
125 * the tag key
126 *
127 * @return the value or NULL
128 *
129 * @throws IOException
130 * in case of I/O error
131 */
132 private static String getInfoTag(InputStream in, String key)
133 throws IOException {
134 key = "^" + key + "=";
135
136 if (in != null) {
137 in.reset();
138 String value = BasicSupport.getLine(in, key, 0);
139 if (value != null && !value.isEmpty()) {
140 value = value.trim().substring(key.length() - 1).trim();
141 if (value.startsWith("'") && value.endsWith("'")
142 || value.startsWith("\"") && value.endsWith("\"")) {
143 value = value.substring(1, value.length() - 1).trim();
144 }
145
146 return value;
147 }
148 }
149
150 return null;
151 }
152 }