Remove or move java.awt dependencies
[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 String infoTag = getInfoTag(in, "COVER");
60 if (infoTag != null && !infoTag.trim().isEmpty()) {
61 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
62 infoTag));
63 }
64 // Second chance: try to check for a cover next to the info file
65 if (meta.getCover() == null) {
66 String info = sourceInfoFile.getFile().toString();
67 if (info.endsWith(".info")) {
68 info = info.substring(0, info.length() - ".info".length());
69 String ext = "."
70 + Instance.getConfig()
71 .getString(Config.IMAGE_FORMAT_COVER)
72 .toLowerCase();
73 meta.setCover(BasicSupport.getImage(null, sourceInfoFile,
74 info + ext));
75 }
76 }
77 }
78 try {
79 meta.setWords(Long.parseLong(getInfoTag(in, "WORDCOUNT")));
80 } catch (NumberFormatException e) {
81 meta.setWords(0);
82 }
83 meta.setCreationDate(getInfoTag(in, "CREATION_DATE"));
84 meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
85
86 if (withCover && meta.getCover() == null) {
87 meta.setCover(BasicSupport.getDefaultCover(meta.getSubject()));
88 }
89
90 return meta;
91 }
92
93 private static boolean getInfoTagBoolean(InputStream in, String key,
94 boolean def) throws IOException {
95 Boolean value = getInfoTagBoolean(in, key);
96 return value == null ? def : value;
97 }
98
99 private static Boolean getInfoTagBoolean(InputStream in, String key)
100 throws IOException {
101 String value = getInfoTag(in, key);
102 if (value != null && !value.trim().isEmpty()) {
103 value = value.toLowerCase().trim();
104 return value.equals("1") || value.equals("on")
105 || value.equals("true") || value.equals("yes");
106 }
107
108 return null;
109 }
110
111 private static List<String> getInfoTagList(InputStream in, String key,
112 String separator) throws IOException {
113 List<String> list = new ArrayList<String>();
114 String tt = getInfoTag(in, key);
115 if (tt != null) {
116 for (String tag : tt.split(separator)) {
117 list.add(tag.trim());
118 }
119 }
120
121 return list;
122 }
123
124 /**
125 * Return the value of the given tag in the <tt>.info</tt> file if present.
126 *
127 * @param key
128 * the tag key
129 *
130 * @return the value or NULL
131 *
132 * @throws IOException
133 * in case of I/O error
134 */
135 private static String getInfoTag(InputStream in, String key)
136 throws IOException {
137 key = "^" + key + "=";
138
139 if (in != null) {
140 in.reset();
141 String value = BasicSupport.getLine(in, key, 0);
142 if (value != null && !value.isEmpty()) {
143 value = value.trim().substring(key.length() - 1).trim();
144 if (value.startsWith("'") && value.endsWith("'")
145 || value.startsWith("\"") && value.endsWith("\"")) {
146 value = value.substring(1, value.length() - 1).trim();
147 }
148
149 return value;
150 }
151 }
152
153 return null;
154 }
155 }