try+1
[fanfix.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 import java.util.Scanner;
12
13 import be.nikiroo.fanfix.Instance;
14 import be.nikiroo.fanfix.bundles.Config;
15 import be.nikiroo.fanfix.data.MetaData;
16 import be.nikiroo.utils.Image;
17 import be.nikiroo.utils.MarkableFileInputStream;
18
19 // not complete: no "description" tag
20 public class InfoReader {
21 public static MetaData readMeta(File infoFile, boolean withCover)
22 throws IOException {
23 if (infoFile == null) {
24 throw new IOException("File is null");
25 }
26
27 if (infoFile.exists()) {
28 InputStream in = new MarkableFileInputStream(new FileInputStream(
29 infoFile));
30 try {
31 return createMeta(infoFile.toURI().toURL(), in, withCover);
32 } finally {
33 in.close();
34 }
35 }
36
37 throw new FileNotFoundException(
38 "File given as argument does not exists: "
39 + infoFile.getAbsolutePath());
40 }
41
42 private static MetaData createMeta(URL sourceInfoFile, InputStream in,
43 boolean withCover) throws IOException {
44 MetaData meta = new MetaData();
45
46 meta.setTitle(getInfoTag(in, "TITLE"));
47 meta.setAuthor(getInfoTag(in, "AUTHOR"));
48 meta.setDate(getInfoTag(in, "DATE"));
49 meta.setTags(getInfoTagList(in, "TAGS", ","));
50 meta.setSource(getInfoTag(in, "SOURCE"));
51 meta.setUrl(getInfoTag(in, "URL"));
52 meta.setPublisher(getInfoTag(in, "PUBLISHER"));
53 meta.setUuid(getInfoTag(in, "UUID"));
54 meta.setLuid(getInfoTag(in, "LUID"));
55 meta.setLang(getInfoTag(in, "LANG"));
56 meta.setSubject(getInfoTag(in, "SUBJECT"));
57 meta.setType(getInfoTag(in, "TYPE"));
58 meta.setImageDocument(getInfoTagBoolean(in, "IMAGES_DOCUMENT", false));
59 if (withCover) {
60 String infoTag = getInfoTag(in, "COVER");
61 if (infoTag != null && !infoTag.trim().isEmpty()) {
62 meta.setCover(BasicSupportHelper.getImage(null, sourceInfoFile,
63 infoTag));
64 }
65 if (meta.getCover() == null) {
66 // Second chance: try to check for a cover next to the info file
67 meta.setCover(getCoverByName(sourceInfoFile));
68 }
69 }
70 try {
71 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
72 } catch (NumberFormatException e) {
73 meta.setWords(0);
74 }
75 meta.setCreationDate(getInfoTag(in, "CREATION_DATE"));
76 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
77
78 if (withCover && meta.getCover() == null) {
79 meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
80 }
81
82 return meta;
83 }
84
85 /**
86 * Return the cover image if it is next to the source file.
87 *
88 * @param sourceInfoFile
89 * the source file
90 *
91 * @return the cover if present, NULL if not
92 */
93 public static Image getCoverByName(URL sourceInfoFile) {
94 Image cover = null;
95
96 File basefile = new File(sourceInfoFile.getFile());
97
98 String ext = "."
99 + Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER)
100 .toLowerCase();
101
102 // Without removing ext
103 cover = BasicSupportHelper.getImage(null, sourceInfoFile,
104 basefile.getAbsolutePath() + ext);
105
106 // Try without ext
107 String name = basefile.getName();
108 int pos = name.lastIndexOf(".");
109 if (cover == null && pos > 0) {
110 name = name.substring(0, pos);
111 basefile = new File(basefile.getParent(), basefile.getName());
112
113 System.out.println(">>> " + basefile);
114 cover = BasicSupportHelper.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 }