update from master
[fanfix.git] / supported / Text.java
1 package be.nikiroo.fanfix.supported;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URISyntaxException;
7 import java.net.URL;
8 import java.util.AbstractMap;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Map.Entry;
12 import java.util.Scanner;
13
14 import org.jsoup.nodes.Document;
15
16 import be.nikiroo.fanfix.Instance;
17 import be.nikiroo.fanfix.bundles.Config;
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.utils.Image;
20 import be.nikiroo.utils.ImageUtils;
21 import be.nikiroo.utils.Progress;
22 import be.nikiroo.utils.streams.MarkableFileInputStream;
23
24 /**
25 * Support class for local stories encoded in textual format, with a few rules:
26 * <ul>
27 * <li>The title must be on the first line</li>
28 * <li>The author (preceded by nothing, "by " or "©") must be on the second
29 * line, possibly with the publication date in parenthesis (i.e., "
30 * <tt>By Unknown (3rd October 1998)</tt>")</li>
31 * <li>Chapters must be declared with "<tt>Chapter x</tt>" or "
32 * <tt>Chapter x: NAME OF THE CHAPTER</tt>", where "<tt>x</tt>" is the chapter
33 * number</li>
34 * <li>A description of the story must be given as chapter number 0</li>
35 * <li>A cover may be present, with the same filename but a PNG, JPEG or JPG
36 * extension</li>
37 * </ul>
38 *
39 * @author niki
40 */
41 class Text extends BasicSupport {
42 private File sourceFile;
43 private InputStream in;
44
45 protected File getSourceFile() {
46 return sourceFile;
47 }
48
49 protected InputStream getInput() {
50 if (in != null) {
51 try {
52 in.reset();
53 } catch (IOException e) {
54 Instance.getInstance().getTraceHandler().error(new IOException("Cannot reset the Text stream", e));
55 }
56
57 return in;
58 }
59
60 return null;
61 }
62
63 @Override
64 protected boolean isHtml() {
65 return false;
66 }
67
68 @Override
69 protected Document loadDocument(URL source) throws IOException {
70 try {
71 sourceFile = new File(source.toURI());
72 in = new MarkableFileInputStream(sourceFile);
73 } catch (URISyntaxException e) {
74 throw new IOException("Cannot load the text document: " + source);
75 }
76
77 return null;
78 }
79
80 @Override
81 protected MetaData getMeta() throws IOException {
82 MetaData meta = new MetaData();
83
84 meta.setTitle(getTitle());
85 meta.setAuthor(getAuthor());
86 meta.setDate(bsHelper.formatDate(getDate()));
87 meta.setTags(new ArrayList<String>());
88 meta.setUrl(getSourceFile().toURI().toURL().toString());
89 meta.setUuid(getSourceFile().toString());
90 meta.setLuid("");
91 meta.setLang(getLang()); // default is EN
92 meta.setSubject(getSourceFile().getParentFile().getName());
93 meta.setImageDocument(false);
94 meta.setCover(getCover(getSourceFile()));
95
96 return meta;
97 }
98
99 private String getLang() {
100 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
101 Scanner scan = new Scanner(getInput(), "UTF-8");
102 scan.useDelimiter("\\n");
103 if (scan.hasNext())
104 scan.next(); // Title
105 if (scan.hasNext())
106 scan.next(); // Author (Date)
107 String chapter0 = "";
108 while (scan.hasNext() && chapter0.isEmpty()) {
109 chapter0 = scan.next();
110 }
111
112 String lang = detectChapter(chapter0, 0);
113 if (lang == null) {
114 // No description??
115 lang = detectChapter(chapter0, 1);
116 }
117
118 if (lang == null) {
119 lang = "en";
120 } else {
121 lang = lang.toLowerCase();
122 }
123
124 return lang;
125 }
126
127 private String getTitle() {
128 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
129 Scanner scan = new Scanner(getInput(), "UTF-8");
130 scan.useDelimiter("\\n");
131 if (scan.hasNext())
132 return scan.next();
133 return "";
134 }
135
136 private String getAuthor() {
137 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
138 Scanner scan = new Scanner(getInput(), "UTF-8");
139 scan.useDelimiter("\\n");
140 if (scan.hasNext())
141 scan.next();
142 String authorDate = "";
143 if (scan.hasNext())
144 authorDate = scan.next();
145
146 String author = authorDate;
147 int pos = authorDate.indexOf('(');
148 if (pos >= 0) {
149 author = authorDate.substring(0, pos);
150 }
151
152 return bsHelper.fixAuthor(author);
153 }
154
155 private String getDate() {
156 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
157 Scanner scan = new Scanner(getInput(), "UTF-8");
158 scan.useDelimiter("\\n");
159 if (scan.hasNext())
160 scan.next();
161 String authorDate = "";
162 if (scan.hasNext())
163 authorDate = scan.next();
164
165 String date = "";
166 int pos = authorDate.indexOf('(');
167 if (pos >= 0) {
168 date = authorDate.substring(pos + 1).trim();
169 pos = date.lastIndexOf(')');
170 if (pos >= 0) {
171 date = date.substring(0, pos).trim();
172 }
173 }
174
175 return date;
176 }
177
178 @Override
179 protected String getDesc() throws IOException {
180 return getChapterContent(null, 0, null).trim();
181 }
182
183 protected Image getCover(File sourceFile) {
184 String path = sourceFile.getName();
185
186 for (String ext : new String[] { ".txt", ".text", ".story" }) {
187 if (path.endsWith(ext)) {
188 path = path.substring(0, path.length() - ext.length());
189 }
190 }
191
192 Image cover = bsImages.getImage(this, sourceFile.getParentFile(), path);
193 if (cover != null) {
194 try {
195 File tmp = Instance.getInstance().getTempFiles().createTempFile("test_cover_image");
196 ImageUtils.getInstance().saveAsImage(cover, tmp, "png");
197 tmp.delete();
198 } catch (IOException e) {
199 cover = null;
200 }
201 }
202
203 return cover;
204 }
205
206 @Override
207 protected List<Entry<String, URL>> getChapters(Progress pg)
208 throws IOException {
209 List<Entry<String, URL>> chaps = new ArrayList<Entry<String, URL>>();
210 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
211 Scanner scan = new Scanner(getInput(), "UTF-8");
212 scan.useDelimiter("\\n");
213 String line = "first is not empty";
214 while (scan.hasNext()) {
215 boolean prevLineEmpty = line.trim().isEmpty();
216 line = scan.next();
217 if (prevLineEmpty && detectChapter(line, chaps.size() + 1) != null) {
218 String chapName = Integer.toString(chaps.size() + 1);
219 int pos = line.indexOf(':');
220 if (pos >= 0 && pos + 1 < line.length()) {
221 chapName = line.substring(pos + 1).trim();
222 }
223
224 chaps.add(new AbstractMap.SimpleEntry<String, URL>(//
225 chapName, //
226 getSourceFile().toURI().toURL()));
227 }
228 }
229
230 return chaps;
231 }
232
233 @Override
234 protected String getChapterContent(URL source, int number, Progress pg)
235 throws IOException {
236 StringBuilder builder = new StringBuilder();
237 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
238 Scanner scan = new Scanner(getInput(), "UTF-8");
239 scan.useDelimiter("\\n");
240 if (scan.hasNext())
241 scan.next(); // title
242 if (scan.hasNext())
243 scan.next(); // author
244 if (scan.hasNext())
245 scan.next(); // date or empty
246 Boolean inChap = null;
247 String line = "";
248 while (scan.hasNext()) {
249 if (number == 0 && !line.trim().isEmpty()) {
250 // We found pre-chapter content, we are checking for
251 // Chapter 0 (fake chapter) --> keep the content
252 if (inChap == null)
253 inChap = true;
254 }
255 line = scan.next();
256 if ((inChap == null || !inChap) && detectChapter(line, number) != null) {
257 inChap = true;
258 } else if (detectChapter(line, number + 1) != null) {
259 break;
260 } else if (inChap != null && inChap) {
261 builder.append(line);
262 builder.append("\n");
263 }
264 }
265
266 return builder.toString();
267 }
268
269 @Override
270 protected void close() {
271 InputStream in = getInput();
272 if (in != null) {
273 try {
274 in.close();
275 } catch (IOException e) {
276 Instance.getInstance().getTraceHandler()
277 .error(new IOException("Cannot close the text source file input", e));
278 }
279 }
280
281 super.close();
282 }
283
284 @Override
285 protected boolean supports(URL url) {
286 return supports(url, false);
287 }
288
289 /**
290 * Check if we supports this {@link URL}, that is, if the info file can be
291 * found OR not found.
292 * <p>
293 * It must also be a file, not another kind of URL.
294 *
295 * @param url
296 * the {@link URL} to check
297 * @param info
298 * TRUE to require the info file, FALSE to forbid the info file
299 *
300 * @return TRUE if it is supported
301 */
302 protected boolean supports(URL url, boolean info) {
303 if (!"file".equals(url.getProtocol())) {
304 return false;
305 }
306
307 boolean infoPresent = false;
308 File file;
309 try {
310 file = new File(url.toURI());
311 file = assureNoTxt(file);
312 file = new File(file.getPath() + ".info");
313 } catch (URISyntaxException e) {
314 Instance.getInstance().getTraceHandler().error(e);
315 file = null;
316 }
317
318 infoPresent = (file != null && file.exists());
319
320 return infoPresent == info;
321 }
322
323 /**
324 * Remove the ".txt" (or ".text") extension if it is present.
325 *
326 * @param file
327 * the file to process
328 *
329 * @return the same file or a copy of it without the ".txt" extension if it
330 * was present
331 */
332 protected File assureNoTxt(File file) {
333 for (String ext : new String[] { ".txt", ".text" }) {
334 if (file.getName().endsWith(ext)) {
335 file = new File(file.getPath().substring(0,
336 file.getPath().length() - ext.length()));
337 }
338 }
339
340 return file;
341 }
342
343 /**
344 * Check if the given line looks like the given starting chapter in a
345 * supported language, and return the language if it does (or NULL if not).
346 *
347 * @param line
348 * the line to check
349 * @param number
350 * the specific chapter number to check for
351 *
352 * @return the language or NULL
353 */
354 static private String detectChapter(String line, int number) {
355 line = line.toUpperCase();
356 for (String lang : Instance.getInstance().getConfig().getList(Config.CONF_CHAPTER)) {
357 String chapter = Instance.getInstance().getConfig().getStringX(Config.CONF_CHAPTER, lang);
358 if (chapter != null && !chapter.isEmpty()) {
359 chapter = chapter.toUpperCase() + " ";
360 if (line.startsWith(chapter)) {
361 // We want "[CHAPTER] [number]: [name]", with ": [name]"
362 // optional
363 String test = line.substring(chapter.length()).trim();
364
365 String possibleNum = test.trim();
366 if (possibleNum.indexOf(':') > 0) {
367 possibleNum = possibleNum.substring(0,
368 possibleNum.indexOf(':')).trim();
369 }
370
371 if (test.startsWith(Integer.toString(number))) {
372 test = test
373 .substring(Integer.toString(number).length())
374 .trim();
375 if (test.isEmpty() || test.startsWith(":")) {
376 return lang;
377 }
378 }
379 }
380 }
381 }
382
383 return null;
384 }
385 }