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")); | |
68686a37 NR |
63 | |
64 | if (meta.getCover() == null) { | |
65 | meta.setCover(BasicSupport.getDefaultCover(meta.getSubject())); | |
66 | } | |
67 | ||
68 | return meta; | |
69 | } | |
70 | ||
71 | private static boolean getInfoTagBoolean(InputStream in, String key, | |
72 | boolean def) throws IOException { | |
73 | Boolean value = getInfoTagBoolean(in, key); | |
74 | return value == null ? def : value; | |
75 | } | |
76 | ||
77 | private static Boolean getInfoTagBoolean(InputStream in, String key) | |
78 | throws IOException { | |
79 | String value = getInfoTag(in, key); | |
80 | if (value != null && !value.trim().isEmpty()) { | |
81 | value = value.toLowerCase().trim(); | |
82 | return value.equals("1") || value.equals("on") | |
83 | || value.equals("true") || value.equals("yes"); | |
84 | } | |
85 | ||
86 | return null; | |
87 | } | |
88 | ||
89 | private static List<String> getInfoTagList(InputStream in, String key, | |
90 | String separator) throws IOException { | |
91 | List<String> list = new ArrayList<String>(); | |
92 | String tt = getInfoTag(in, key); | |
93 | if (tt != null) { | |
94 | for (String tag : tt.split(separator)) { | |
95 | list.add(tag.trim()); | |
96 | } | |
97 | } | |
98 | ||
99 | return list; | |
100 | } | |
101 | ||
102 | /** | |
103 | * Return the value of the given tag in the <tt>.info</tt> file if present. | |
104 | * | |
105 | * @param key | |
106 | * the tag key | |
107 | * | |
108 | * @return the value or NULL | |
109 | * | |
110 | * @throws IOException | |
111 | * in case of I/O error | |
112 | */ | |
113 | private static String getInfoTag(InputStream in, String key) | |
114 | throws IOException { | |
115 | key = "^" + key + "="; | |
116 | ||
117 | if (in != null) { | |
118 | in.reset(); | |
119 | String value = BasicSupport.getLine(in, key, 0); | |
120 | if (value != null && !value.isEmpty()) { | |
121 | value = value.trim().substring(key.length() - 1).trim(); | |
122 | if (value.startsWith("'") && value.endsWith("'") | |
123 | || value.startsWith("\"") && value.endsWith("\"")) { | |
124 | value = value.substring(1, value.length() - 1).trim(); | |
125 | } | |
126 | ||
127 | return value; | |
128 | } | |
129 | } | |
130 | ||
131 | return null; | |
132 | } | |
133 | } |