Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
68686a37 | 3 | import java.awt.image.BufferedImage; |
08fe2e33 NR |
4 | import java.io.File; |
5 | import java.io.IOException; | |
6 | import java.io.InputStream; | |
7 | import java.net.URISyntaxException; | |
8 | import java.net.URL; | |
9 | import java.util.ArrayList; | |
10 | import java.util.List; | |
11 | import java.util.Map.Entry; | |
12 | import java.util.Scanner; | |
13 | ||
14 | import be.nikiroo.fanfix.Instance; | |
15 | import be.nikiroo.fanfix.bundles.Config; | |
68686a37 | 16 | import be.nikiroo.fanfix.data.MetaData; |
08fe2e33 NR |
17 | |
18 | /** | |
19 | * Support class for local stories encoded in textual format, with a few rules: | |
20 | * <ul> | |
21 | * <li>The title must be on the first line</li> | |
22 | * <li>The author (preceded by nothing, "by " or "©") must be on the second | |
23 | * line, possibly with the publication date in parenthesis (i.e., " | |
24 | * <tt>By Unknown (3rd October 1998)</tt>")</li> | |
25 | * <li>Chapters must be declared with "<tt>Chapter x</tt>" or " | |
26 | * <tt>Chapter x: NAME OF THE CHAPTER</tt>", where "<tt>x</tt>" is the chapter | |
27 | * number</li> | |
28 | * <li>A description of the story must be given as chapter number 0</li> | |
29 | * <li>A cover may be present, with the same filename but a PNG, JPEG or JPG | |
30 | * extension</li< | |
31 | * </ul> | |
32 | * | |
33 | * @author niki | |
34 | */ | |
35 | class Text extends BasicSupport { | |
36 | @Override | |
37 | protected boolean isHtml() { | |
38 | return false; | |
39 | } | |
40 | ||
41 | @Override | |
42 | public String getSourceName() { | |
43 | return "text"; | |
44 | } | |
45 | ||
46 | @Override | |
68686a37 NR |
47 | protected MetaData getMeta(URL source, InputStream in) throws IOException { |
48 | MetaData meta = new MetaData(); | |
49 | ||
50 | meta.setTitle(getTitle(reset(in))); | |
51 | meta.setAuthor(getAuthor(reset(in))); | |
52 | meta.setDate(getDate(reset(in))); | |
53 | meta.setTags(new ArrayList<String>()); | |
54 | meta.setSource(getSourceName()); | |
2206ef66 NR |
55 | meta.setUrl(source.toString()); |
56 | meta.setPublisher(""); | |
68686a37 NR |
57 | meta.setUuid(source.toString()); |
58 | meta.setLuid(""); | |
59 | meta.setLang(getLang(source, reset(in))); // default is EN | |
60 | meta.setSubject(getSubject(source)); | |
61 | meta.setType(getType().toString()); | |
62 | meta.setImageDocument(false); | |
63 | meta.setCover(getCover(source)); | |
64 | ||
65 | return meta; | |
08fe2e33 NR |
66 | } |
67 | ||
68686a37 | 68 | private String getSubject(URL source) throws IOException { |
08fe2e33 NR |
69 | try { |
70 | File file = new File(source.toURI()); | |
71 | return file.getParentFile().getName(); | |
72 | } catch (URISyntaxException e) { | |
73 | throw new IOException("Cannot parse the URL to File: " | |
74 | + source.toString(), e); | |
75 | } | |
76 | ||
77 | } | |
78 | ||
68686a37 | 79 | private String getLang(URL source, InputStream in) throws IOException { |
08fe2e33 NR |
80 | @SuppressWarnings("resource") |
81 | Scanner scan = new Scanner(in, "UTF-8"); | |
82 | scan.useDelimiter("\\n"); | |
83 | scan.next(); // Title | |
84 | scan.next(); // Author (Date) | |
85 | String chapter0 = scan.next(); // empty or Chapter 0 | |
86 | while (chapter0.isEmpty()) { | |
87 | chapter0 = scan.next(); | |
88 | } | |
89 | ||
90 | String lang = detectChapter(chapter0); | |
91 | if (lang == null) { | |
68686a37 | 92 | lang = "EN"; |
08fe2e33 NR |
93 | } else { |
94 | lang = lang.toUpperCase(); | |
95 | } | |
96 | ||
97 | return lang; | |
98 | } | |
99 | ||
68686a37 | 100 | private String getTitle(InputStream in) throws IOException { |
08fe2e33 NR |
101 | @SuppressWarnings("resource") |
102 | Scanner scan = new Scanner(in, "UTF-8"); | |
103 | scan.useDelimiter("\\n"); | |
104 | return scan.next(); | |
105 | } | |
106 | ||
68686a37 | 107 | private String getAuthor(InputStream in) throws IOException { |
08fe2e33 NR |
108 | @SuppressWarnings("resource") |
109 | Scanner scan = new Scanner(in, "UTF-8"); | |
110 | scan.useDelimiter("\\n"); | |
111 | scan.next(); | |
112 | String authorDate = scan.next(); | |
113 | ||
114 | String author = authorDate; | |
115 | int pos = authorDate.indexOf('('); | |
116 | if (pos >= 0) { | |
117 | author = authorDate.substring(0, pos); | |
118 | } | |
119 | ||
68686a37 | 120 | return fixAuthor(author); |
08fe2e33 NR |
121 | } |
122 | ||
68686a37 | 123 | private String getDate(InputStream in) throws IOException { |
08fe2e33 NR |
124 | @SuppressWarnings("resource") |
125 | Scanner scan = new Scanner(in, "UTF-8"); | |
126 | scan.useDelimiter("\\n"); | |
127 | scan.next(); | |
128 | String authorDate = scan.next(); | |
129 | ||
130 | String date = ""; | |
131 | int pos = authorDate.indexOf('('); | |
132 | if (pos >= 0) { | |
133 | date = authorDate.substring(pos + 1).trim(); | |
134 | pos = date.lastIndexOf(')'); | |
135 | if (pos >= 0) { | |
136 | date = date.substring(0, pos).trim(); | |
137 | } | |
138 | } | |
139 | ||
140 | return date; | |
141 | } | |
142 | ||
143 | @Override | |
68686a37 | 144 | protected String getDesc(URL source, InputStream in) throws IOException { |
08fe2e33 NR |
145 | return getChapterContent(source, in, 0); |
146 | } | |
147 | ||
68686a37 | 148 | private BufferedImage getCover(URL source) throws IOException { |
08fe2e33 NR |
149 | String path; |
150 | try { | |
151 | path = new File(source.toURI()).getPath(); | |
152 | } catch (URISyntaxException e) { | |
153 | Instance.syserr(e); | |
154 | path = null; | |
155 | } | |
156 | ||
157 | for (String ext : new String[] { ".txt", ".text", ".story" }) { | |
158 | if (path.endsWith(ext)) { | |
159 | path = path.substring(0, path.length() - ext.length()); | |
160 | } | |
161 | } | |
162 | ||
333f0e7b | 163 | return getImage(this, source, path); |
08fe2e33 NR |
164 | } |
165 | ||
166 | @Override | |
68686a37 NR |
167 | protected List<Entry<String, URL>> getChapters(URL source, InputStream in) |
168 | throws IOException { | |
08fe2e33 NR |
169 | List<Entry<String, URL>> chaps = new ArrayList<Entry<String, URL>>(); |
170 | @SuppressWarnings("resource") | |
171 | Scanner scan = new Scanner(in, "UTF-8"); | |
172 | scan.useDelimiter("\\n"); | |
173 | boolean descSkipped = false; | |
174 | boolean prevLineEmpty = false; | |
175 | while (scan.hasNext()) { | |
176 | String line = scan.next(); | |
177 | if (prevLineEmpty && detectChapter(line) != null) { | |
178 | if (descSkipped) { | |
179 | String chapName = Integer.toString(chaps.size()); | |
180 | int pos = line.indexOf(':'); | |
181 | if (pos >= 0 && pos + 1 < line.length()) { | |
182 | chapName = line.substring(pos + 1).trim(); | |
183 | } | |
184 | final URL value = source; | |
185 | final String key = chapName; | |
186 | chaps.add(new Entry<String, URL>() { | |
187 | public URL setValue(URL value) { | |
188 | return null; | |
189 | } | |
190 | ||
191 | public URL getValue() { | |
192 | return value; | |
193 | } | |
194 | ||
195 | public String getKey() { | |
196 | return key; | |
197 | } | |
198 | }); | |
199 | } else { | |
200 | descSkipped = true; | |
201 | } | |
202 | } | |
203 | ||
204 | prevLineEmpty = line.trim().isEmpty(); | |
205 | } | |
206 | ||
207 | return chaps; | |
208 | } | |
209 | ||
210 | @Override | |
68686a37 NR |
211 | protected String getChapterContent(URL source, InputStream in, int number) |
212 | throws IOException { | |
08fe2e33 NR |
213 | StringBuilder builder = new StringBuilder(); |
214 | @SuppressWarnings("resource") | |
215 | Scanner scan = new Scanner(in, "UTF-8"); | |
216 | scan.useDelimiter("\\n"); | |
217 | boolean inChap = false; | |
08fe2e33 NR |
218 | while (scan.hasNext()) { |
219 | String line = scan.next(); | |
68686a37 NR |
220 | if (detectChapter(line, number) != null) { |
221 | inChap = true; | |
222 | } else if (inChap && detectChapter(line) != null) { | |
223 | break; | |
224 | } else if (inChap) { | |
225 | builder.append(line); | |
226 | builder.append("\n"); | |
08fe2e33 | 227 | } |
08fe2e33 NR |
228 | } |
229 | ||
230 | return builder.toString(); | |
231 | } | |
232 | ||
233 | @Override | |
234 | protected boolean supports(URL url) { | |
235 | if ("file".equals(url.getProtocol())) { | |
236 | File file; | |
237 | try { | |
238 | file = new File(url.toURI()); | |
239 | file = new File(file.getPath() + ".info"); | |
240 | } catch (URISyntaxException e) { | |
241 | Instance.syserr(e); | |
242 | file = null; | |
243 | } | |
244 | ||
245 | return file == null || !file.exists(); | |
246 | } | |
247 | ||
248 | return false; | |
249 | } | |
250 | ||
251 | /** | |
252 | * Check if the given line looks like a starting chapter in a supported | |
253 | * language, and return the language if it does (or NULL if not). | |
254 | * | |
255 | * @param line | |
256 | * the line to check | |
257 | * | |
258 | * @return the language or NULL | |
259 | */ | |
260 | private String detectChapter(String line) { | |
261 | return detectChapter(line, null); | |
262 | } | |
263 | ||
264 | /** | |
265 | * Check if the given line looks like the given starting chapter in a | |
266 | * supported language, and return the language if it does (or NULL if not). | |
267 | * | |
268 | * @param line | |
269 | * the line to check | |
270 | * | |
271 | * @return the language or NULL | |
272 | */ | |
273 | private String detectChapter(String line, Integer number) { | |
274 | line = line.toUpperCase(); | |
275 | for (String lang : Instance.getConfig().getString(Config.CHAPTER) | |
276 | .split(",")) { | |
277 | String chapter = Instance.getConfig().getStringX(Config.CHAPTER, | |
278 | lang); | |
279 | if (chapter != null && !chapter.isEmpty()) { | |
280 | chapter = chapter.toUpperCase() + " "; | |
281 | if (line.startsWith(chapter)) { | |
282 | if (number != null) { | |
283 | // We want "[CHAPTER] [number]: [name]", with ": [name]" | |
284 | // optional | |
285 | String test = line.substring(chapter.length()).trim(); | |
286 | if (test.startsWith(Integer.toString(number))) { | |
287 | test = test.substring( | |
288 | Integer.toString(number).length()).trim(); | |
289 | if (test.isEmpty() || test.startsWith(":")) { | |
290 | return lang; | |
291 | } | |
292 | } | |
293 | } else { | |
294 | return lang; | |
295 | } | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | return null; | |
301 | } | |
302 | } |