Add more warnings source to 1.6) and fix warnings
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / InfoReader.java
CommitLineData
68686a37
NR
1package be.nikiroo.fanfix.supported;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
333f0e7b 8import java.net.URL;
68686a37
NR
9import java.util.ArrayList;
10import java.util.List;
11
2d2a3222
NR
12import be.nikiroo.fanfix.Instance;
13import be.nikiroo.fanfix.bundles.Config;
68686a37
NR
14import be.nikiroo.fanfix.data.MetaData;
15import be.nikiroo.utils.MarkableFileInputStream;
16
17// not complete: no "description" tag
18public class InfoReader {
57f02339
NR
19 public static MetaData readMeta(File infoFile, boolean withCover)
20 throws IOException {
68686a37
NR
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 {
57f02339 29 return createMeta(infoFile.toURI().toURL(), in, withCover);
68686a37
NR
30 } finally {
31 in.close();
32 in = null;
33 }
68686a37 34 }
211f7ddb
NR
35
36 throw new FileNotFoundException(
37 "File given as argument does not exists: "
38 + infoFile.getAbsolutePath());
68686a37
NR
39 }
40
57f02339
NR
41 private static MetaData createMeta(URL sourceInfoFile, InputStream in,
42 boolean withCover) throws IOException {
68686a37
NR
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"));
2206ef66 50 meta.setUrl(getInfoTag(in, "URL"));
68686a37
NR
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));
57f02339
NR
58 if (withCover) {
59 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
60 getInfoTag(in, "COVER")));
2d2a3222
NR
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().getString(
68 Config.IMAGE_FORMAT_COVER);
69 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
70 info + ext));
71 }
72 }
57f02339 73 }
793f1071
NR
74 try {
75 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
76 } catch (NumberFormatException e) {
77 meta.setWords(0);
78 }
79 meta.setCreationDate(getInfoTag(in, "CREATION_DATE"));
a9eb3f46 80 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
68686a37 81
57f02339 82 if (withCover && meta.getCover() == null) {
68686a37
NR
83 meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
84 }
85
86 return meta;
87 }
88
89 private static boolean getInfoTagBoolean(InputStream in, String key,
90 boolean def) throws IOException {
91 Boolean value = getInfoTagBoolean(in, key);
92 return value == null ? def : value;
93 }
94
95 private static Boolean getInfoTagBoolean(InputStream in, String key)
96 throws IOException {
97 String value = getInfoTag(in, key);
98 if (value != null && !value.trim().isEmpty()) {
99 value = value.toLowerCase().trim();
100 return value.equals("1") || value.equals("on")
101 || value.equals("true") || value.equals("yes");
102 }
103
104 return null;
105 }
106
107 private static List<String> getInfoTagList(InputStream in, String key,
108 String separator) throws IOException {
109 List<String> list = new ArrayList<String>();
110 String tt = getInfoTag(in, key);
111 if (tt != null) {
112 for (String tag : tt.split(separator)) {
113 list.add(tag.trim());
114 }
115 }
116
117 return list;
118 }
119
120 /**
121 * Return the value of the given tag in the <tt>.info</tt> file if present.
122 *
123 * @param key
124 * the tag key
125 *
126 * @return the value or NULL
127 *
128 * @throws IOException
129 * in case of I/O error
130 */
131 private static String getInfoTag(InputStream in, String key)
132 throws IOException {
133 key = "^" + key + "=";
134
135 if (in != null) {
136 in.reset();
137 String value = BasicSupport.getLine(in, key, 0);
138 if (value != null && !value.isEmpty()) {
139 value = value.trim().substring(key.length() - 1).trim();
140 if (value.startsWith("'") && value.endsWith("'")
141 || value.startsWith("\"") && value.endsWith("\"")) {
142 value = value.substring(1, value.length() - 1).trim();
143 }
144
145 return value;
146 }
147 }
148
149 return null;
150 }
151}