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