Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.FileInputStream; | |
5 | import java.io.IOException; | |
6 | import java.io.InputStream; | |
7 | import java.net.MalformedURLException; | |
8 | import java.net.URISyntaxException; | |
9 | import java.net.URL; | |
10 | import java.util.List; | |
11 | ||
12 | import be.nikiroo.fanfix.Instance; | |
13 | ||
14 | /** | |
15 | * Support class for <tt>.info</tt> text files ({@link Text} files with a | |
16 | * <tt>.info</tt> metadata file next to them). | |
17 | * <p> | |
18 | * The <tt>.info</tt> file is supposed to be written by this program, or | |
19 | * compatible. | |
20 | * | |
21 | * @author niki | |
22 | */ | |
23 | class InfoText extends Text { | |
24 | @Override | |
25 | public String getSourceName() { | |
26 | return "info-text"; | |
27 | } | |
28 | ||
29 | @Override | |
30 | protected String getTitle(URL source, InputStream in) throws IOException { | |
31 | String tag = getInfoTag(source, "TITLE"); | |
32 | if (tag != null) { | |
33 | return tag; | |
34 | } | |
35 | ||
36 | return super.getTitle(source, in); | |
37 | } | |
38 | ||
39 | @Override | |
40 | protected String getAuthor(URL source, InputStream in) throws IOException { | |
41 | String tag = getInfoTag(source, "AUTHOR"); | |
42 | if (tag != null) { | |
43 | return tag; | |
44 | } | |
45 | ||
46 | return super.getAuthor(source, in); | |
47 | } | |
48 | ||
49 | @Override | |
50 | protected String getDate(URL source, InputStream in) throws IOException { | |
51 | String tag = getInfoTag(source, "DATE"); | |
52 | if (tag != null) { | |
53 | return tag; | |
54 | } | |
55 | ||
56 | return super.getDate(source, in); | |
57 | } | |
58 | ||
59 | @Override | |
60 | protected String getSubject(URL source, InputStream in) throws IOException { | |
61 | String tag = getInfoTag(source, "SUBJECT"); | |
62 | if (tag != null) { | |
63 | return tag; | |
64 | } | |
65 | ||
66 | return super.getSubject(source, in); | |
67 | } | |
68 | ||
69 | @Override | |
70 | protected String getLang(URL source, InputStream in) throws IOException { | |
71 | String tag = getInfoTag(source, "LANG"); | |
72 | if (tag != null) { | |
73 | return tag; | |
74 | } | |
75 | ||
76 | return super.getLang(source, in); | |
77 | } | |
78 | ||
79 | @Override | |
80 | protected String getPublisher(URL source, InputStream in) | |
81 | throws IOException { | |
82 | String tag = getInfoTag(source, "PUBLISHER"); | |
83 | if (tag != null) { | |
84 | return tag; | |
85 | } | |
86 | ||
87 | return super.getPublisher(source, in); | |
88 | } | |
89 | ||
90 | @Override | |
91 | protected String getUuid(URL source, InputStream in) throws IOException { | |
92 | String tag = getInfoTag(source, "UUID"); | |
93 | if (tag != null) { | |
94 | return tag; | |
95 | } | |
96 | ||
97 | return super.getUuid(source, in); | |
98 | } | |
99 | ||
100 | @Override | |
101 | protected String getLuid(URL source, InputStream in) throws IOException { | |
102 | String tag = getInfoTag(source, "LUID"); | |
103 | if (tag != null) { | |
104 | return tag; | |
105 | } | |
106 | ||
107 | return super.getLuid(source, in); | |
108 | } | |
109 | ||
110 | @Override | |
111 | protected List<String> getTags(URL source, InputStream in) | |
112 | throws IOException { | |
113 | List<String> tags = super.getTags(source, in); | |
114 | ||
115 | String tt = getInfoTag(source, "TAGS"); | |
116 | if (tt != null) { | |
117 | for (String tag : tt.split(",")) { | |
118 | tags.add(tag.trim()); | |
119 | } | |
120 | } | |
121 | ||
122 | return tags; | |
123 | } | |
124 | ||
125 | @Override | |
126 | public boolean isImageDocument(URL source, InputStream in) | |
127 | throws IOException { | |
128 | String tag = getInfoTag(source, "IMAGES_DOCUMENT"); | |
129 | if (tag != null) { | |
130 | return tag.trim().toLowerCase().equals("true"); | |
131 | } | |
132 | ||
133 | return super.isImageDocument(source, in); | |
134 | } | |
135 | ||
136 | @Override | |
137 | protected URL getCover(URL source, InputStream in) { | |
138 | File file; | |
139 | try { | |
140 | file = new File(source.toURI()); | |
141 | file = new File(file.getPath() + ".info"); | |
142 | } catch (URISyntaxException e) { | |
143 | Instance.syserr(e); | |
144 | file = null; | |
145 | } | |
146 | ||
147 | String path = null; | |
148 | if (file != null && file.exists()) { | |
149 | try { | |
150 | InputStream infoIn = new FileInputStream(file); | |
151 | try { | |
152 | String key = "COVER="; | |
153 | String tt = getLine(infoIn, key, 0); | |
154 | if (tt != null && !tt.isEmpty()) { | |
155 | tt = tt.substring(key.length()).trim(); | |
156 | if (tt.startsWith("'") && tt.endsWith("'")) { | |
157 | tt = tt.substring(1, tt.length() - 1).trim(); | |
158 | } | |
159 | ||
160 | URL cover = getImage(source, tt); | |
161 | if (cover != null) { | |
162 | path = cover.getFile(); | |
163 | } | |
164 | } | |
165 | } finally { | |
166 | infoIn.close(); | |
167 | } | |
168 | } catch (MalformedURLException e) { | |
169 | Instance.syserr(e); | |
170 | } catch (IOException e) { | |
171 | Instance.syserr(e); | |
172 | } | |
173 | } | |
174 | ||
175 | if (path != null) { | |
176 | try { | |
177 | return new File(path).toURI().toURL(); | |
178 | } catch (MalformedURLException e) { | |
179 | Instance.syserr(e); | |
180 | } | |
181 | } | |
182 | ||
183 | return null; | |
184 | } | |
185 | ||
186 | @Override | |
187 | protected boolean supports(URL url) { | |
188 | if ("file".equals(url.getProtocol())) { | |
189 | File file; | |
190 | try { | |
191 | file = new File(url.toURI()); | |
192 | file = new File(file.getPath() + ".info"); | |
193 | } catch (URISyntaxException e) { | |
194 | Instance.syserr(e); | |
195 | file = null; | |
196 | } | |
197 | ||
198 | return file != null && file.exists(); | |
199 | } | |
200 | ||
201 | return false; | |
202 | } | |
203 | ||
204 | /** | |
205 | * Return the value of the given tag in the <tt>.info</tt> file if present. | |
206 | * | |
207 | * @param source | |
208 | * the source story {@link URL} | |
209 | * @param key | |
210 | * the tag key | |
211 | * | |
212 | * @return the value or NULL | |
213 | * | |
214 | * @throws IOException | |
215 | * in case of I/O error | |
216 | */ | |
217 | private String getInfoTag(URL source, String key) throws IOException { | |
d0114000 | 218 | key = "^" + key + "="; |
08fe2e33 NR |
219 | |
220 | File file; | |
221 | try { | |
222 | file = new File(source.toURI()); | |
223 | file = new File(file.getPath() + ".info"); | |
224 | } catch (URISyntaxException e) { | |
225 | throw new IOException(e); | |
226 | } | |
227 | ||
228 | if (file.exists()) { | |
229 | InputStream infoIn = new FileInputStream(file); | |
230 | try { | |
231 | String value = getLine(infoIn, key, 0); | |
232 | if (value != null && !value.isEmpty()) { | |
7de079f1 | 233 | value = value.trim().substring(key.length() - 1).trim(); |
08fe2e33 NR |
234 | if (value.startsWith("'") && value.endsWith("'") |
235 | || value.startsWith("\"") && value.endsWith("\"")) { | |
236 | value = value.substring(1, value.length() - 1).trim(); | |
237 | } | |
7de079f1 | 238 | |
08fe2e33 NR |
239 | return value; |
240 | } | |
241 | } finally { | |
242 | infoIn.close(); | |
243 | } | |
244 | } | |
245 | ||
246 | return null; | |
247 | } | |
248 | } |