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