220350e6dc904495de7de918e4843cf3d15dda01
[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.MetaData;
15 import be.nikiroo.utils.Image;
16 import be.nikiroo.utils.streams.MarkableFileInputStream;
17
18 // not complete: no "description" tag
19 public class InfoReader {
20 static protected BasicSupportHelper bsHelper = new BasicSupportHelper();
21 // static protected BasicSupportImages bsImages = new BasicSupportImages();
22 // static protected BasicSupportPara bsPara = new BasicSupportPara(new BasicSupportHelper(), new BasicSupportImages());
23
24 public static MetaData readMeta(File infoFile, boolean withCover)
25 throws IOException {
26 if (infoFile == null) {
27 throw new IOException("File is null");
28 }
29
30 if (infoFile.exists()) {
31 InputStream in = new MarkableFileInputStream(infoFile);
32 try {
33 return createMeta(infoFile.toURI().toURL(), in, withCover);
34 } finally {
35 in.close();
36 }
37 }
38
39 throw new FileNotFoundException(
40 "File given as argument does not exists: "
41 + infoFile.getAbsolutePath());
42 }
43
44 private static MetaData createMeta(URL sourceInfoFile, InputStream in,
45 boolean withCover) throws IOException {
46 MetaData meta = new MetaData();
47
48 meta.setTitle(getInfoTag(in, "TITLE"));
49 meta.setAuthor(getInfoTag(in, "AUTHOR"));
50 meta.setDate(getInfoTag(in, "DATE"));
51 meta.setTags(getInfoTagList(in, "TAGS", ","));
52 meta.setSource(getInfoTag(in, "SOURCE"));
53 meta.setUrl(getInfoTag(in, "URL"));
54 meta.setPublisher(getInfoTag(in, "PUBLISHER"));
55 meta.setUuid(getInfoTag(in, "UUID"));
56 meta.setLuid(getInfoTag(in, "LUID"));
57 meta.setLang(getInfoTag(in, "LANG"));
58 meta.setSubject(getInfoTag(in, "SUBJECT"));
59 meta.setType(getInfoTag(in, "TYPE"));
60 meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false));
61 if (withCover) {
62 String infoTag = getInfoTag(in, "COVER");
63 if (infoTag != null && !infoTag.trim().isEmpty()) {
64 meta.setCover(bsHelper.getImage(null, sourceInfoFile,
65 infoTag));
66 }
67 if (meta.getCover() == null) {
68 // Second chance: try to check for a cover next to the info file
69 meta.setCover(getCoverByName(sourceInfoFile));
70 }
71 }
72 try {
73 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
74 } catch (NumberFormatException e) {
75 meta.setWords(0);
76 }
77 meta.setCreationDate(getInfoTag(in, "CREATION_DATE"));
78 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
79
80 if (withCover && meta.getCover() == null) {
81 meta.setCover(bsHelper.getDefaultCover(meta.getSubject()));
82 }
83
84 return meta;
85 }
86
87 /**
88 * Return the cover image if it is next to the source file.
89 *
90 * @param sourceInfoFile
91 * the source file
92 *
93 * @return the cover if present, NULL if not
94 */
95 public static Image getCoverByName(URL sourceInfoFile) {
96 Image cover = null;
97
98 File basefile = new File(sourceInfoFile.getFile());
99
100 String ext = "."
101 + Instance.getInstance().getConfig().getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase();
102
103 // Without removing ext
104 cover = bsHelper.getImage(null, sourceInfoFile,
105 basefile.getAbsolutePath() + ext);
106
107 // Try without ext
108 String name = basefile.getName();
109 int pos = name.lastIndexOf(".");
110 if (cover == null && pos > 0) {
111 name = name.substring(0, pos);
112 basefile = new File(basefile.getParent(), name);
113
114 cover = bsHelper.getImage(null, sourceInfoFile,
115 basefile.getAbsolutePath() + ext);
116 }
117
118 return cover;
119 }
120
121 private static boolean getInfoTagBoolean(InputStream in, String key,
122 boolean def) throws IOException {
123 Boolean value = getInfoTagBoolean(in, key);
124 return value == null ? def : value;
125 }
126
127 private static Boolean getInfoTagBoolean(InputStream in, String key)
128 throws IOException {
129 String value = getInfoTag(in, key);
130 if (value != null && !value.trim().isEmpty()) {
131 value = value.toLowerCase().trim();
132 return value.equals("1") || value.equals("on")
133 || value.equals("true") || value.equals("yes");
134 }
135
136 return null;
137 }
138
139 private static List<String> getInfoTagList(InputStream in, String key,
140 String separator) throws IOException {
141 List<String> list = new ArrayList<String>();
142 String tt = getInfoTag(in, key);
143 if (tt != null) {
144 for (String tag : tt.split(separator)) {
145 list.add(tag.trim());
146 }
147 }
148
149 return list;
150 }
151
152 /**
153 * Return the value of the given tag in the <tt>.info</tt> file if present.
154 *
155 * @param key
156 * the tag key
157 *
158 * @return the value or NULL
159 *
160 * @throws IOException
161 * in case of I/O error
162 */
163 private static String getInfoTag(InputStream in, String key)
164 throws IOException {
165 key = "^" + key + "=";
166
167 if (in != null) {
168 in.reset();
169 String value = getLine(in, key, 0);
170 if (value != null && !value.isEmpty()) {
171 value = value.trim().substring(key.length() - 1).trim();
172 if (value.startsWith("'") && value.endsWith("'")
173 || value.startsWith("\"") && value.endsWith("\"")) {
174 value = value.substring(1, value.length() - 1).trim();
175 }
176
177 return value;
178 }
179 }
180
181 return null;
182 }
183
184 /**
185 * Return the first line from the given input which correspond to the given
186 * selectors.
187 *
188 * @param in
189 * the input
190 * @param needle
191 * a string that must be found inside the target line (also
192 * supports "^" at start to say "only if it starts with" the
193 * needle)
194 * @param relativeLine
195 * the line to return based upon the target line position (-1 =
196 * the line before, 0 = the target line...)
197 *
198 * @return the line
199 */
200 static private String getLine(InputStream in, String needle,
201 int relativeLine) {
202 return getLine(in, needle, relativeLine, true);
203 }
204
205 /**
206 * Return a line from the given input which correspond to the given
207 * selectors.
208 *
209 * @param in
210 * the input
211 * @param needle
212 * a string that must be found inside the target line (also
213 * supports "^" at start to say "only if it starts with" the
214 * needle)
215 * @param relativeLine
216 * the line to return based upon the target line position (-1 =
217 * the line before, 0 = the target line...)
218 * @param first
219 * takes the first result (as opposed to the last one, which will
220 * also always spend the input)
221 *
222 * @return the line
223 */
224 static private String getLine(InputStream in, String needle,
225 int relativeLine, boolean first) {
226 String rep = null;
227
228 List<String> lines = new ArrayList<String>();
229 @SuppressWarnings("resource")
230 Scanner scan = new Scanner(in, "UTF-8");
231 int index = -1;
232 scan.useDelimiter("\\n");
233 while (scan.hasNext()) {
234 lines.add(scan.next());
235
236 if (index == -1) {
237 if (needle.startsWith("^")) {
238 if (lines.get(lines.size() - 1).startsWith(
239 needle.substring(1))) {
240 index = lines.size() - 1;
241 }
242
243 } else {
244 if (lines.get(lines.size() - 1).contains(needle)) {
245 index = lines.size() - 1;
246 }
247 }
248 }
249
250 if (index >= 0 && index + relativeLine < lines.size()) {
251 rep = lines.get(index + relativeLine);
252 if (first) {
253 break;
254 }
255 }
256 }
257
258 return rep;
259 }
260 }