Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.supported; |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
4 | import java.io.File; | |
5 | import java.io.IOException; | |
6 | import java.io.InputStream; | |
7 | import java.net.MalformedURLException; | |
8 | import java.net.URL; | |
9 | import java.nio.charset.StandardCharsets; | |
10 | import java.util.ArrayList; | |
11 | import java.util.HashMap; | |
12 | import java.util.List; | |
13 | import java.util.Map; | |
14 | import java.util.Map.Entry; | |
15 | import java.util.Scanner; | |
16 | ||
17 | import be.nikiroo.fanfix.Instance; | |
18 | import be.nikiroo.fanfix.bundles.Config; | |
19 | import be.nikiroo.fanfix.bundles.StringId; | |
20 | import be.nikiroo.fanfix.data.Chapter; | |
21 | import be.nikiroo.fanfix.data.MetaData; | |
22 | import be.nikiroo.fanfix.data.Paragraph; | |
23 | import be.nikiroo.fanfix.data.Story; | |
24 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; | |
25 | import be.nikiroo.utils.StringUtils; | |
26 | ||
27 | /** | |
28 | * This class is the base class used by the other support classes. It can be | |
29 | * used outside of this package, and have static method that you can use to get | |
30 | * access to the correct support class. | |
31 | * <p> | |
32 | * It will be used with 'resources' (usually web pages or files). | |
33 | * | |
34 | * @author niki | |
35 | */ | |
36 | public abstract class BasicSupport { | |
37 | /** | |
38 | * The supported input types for which we can get a {@link BasicSupport} | |
39 | * object. | |
40 | * | |
41 | * @author niki | |
42 | */ | |
43 | public enum SupportType { | |
44 | /** EPUB files created with this program */ | |
45 | EPUB, | |
46 | /** Pure text file with some rules */ | |
47 | TEXT, | |
48 | /** TEXT but with associated .info file */ | |
49 | INFO_TEXT, | |
50 | /** My Little Pony fanfictions */ | |
51 | FIMFICTION, | |
52 | /** Fanfictions from a lot of different universes */ | |
53 | FANFICTION, | |
54 | /** Website with lots of Mangas */ | |
55 | MANGAFOX, | |
56 | /** Furry website with comics support */ | |
57 | E621, | |
58 | /** CBZ files */ | |
59 | CBZ; | |
60 | ||
61 | /** | |
62 | * A description of this support type (more information than the | |
63 | * {@link BasicSupport#getSourceName()}). | |
64 | * | |
65 | * @return the description | |
66 | */ | |
67 | public String getDesc() { | |
68 | String desc = Instance.getTrans().getStringX(StringId.INPUT_DESC, | |
69 | this.name()); | |
70 | ||
71 | if (desc == null) { | |
72 | desc = Instance.getTrans().getString(StringId.INPUT_DESC, this); | |
73 | } | |
74 | ||
75 | return desc; | |
76 | } | |
77 | ||
78 | /** | |
79 | * The name of this support type (a short version). | |
80 | * | |
81 | * @return the name | |
82 | */ | |
83 | public String getSourceName() { | |
84 | BasicSupport support = BasicSupport.getSupport(this); | |
85 | if (support != null) { | |
86 | return support.getSourceName(); | |
87 | } | |
88 | ||
89 | return null; | |
90 | } | |
91 | ||
92 | @Override | |
93 | public String toString() { | |
94 | return super.toString().toLowerCase(); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Call {@link SupportType#valueOf(String.toUpperCase())}. | |
99 | * | |
100 | * @param typeName | |
101 | * the possible type name | |
102 | * | |
103 | * @return NULL or the type | |
104 | */ | |
105 | public static SupportType valueOfUC(String typeName) { | |
106 | return SupportType.valueOf(typeName == null ? null : typeName | |
107 | .toUpperCase()); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Call {@link SupportType#valueOf(String.toUpperCase())} but return | |
112 | * NULL for NULL instead of raising exception. | |
113 | * | |
114 | * @param typeName | |
115 | * the possible type name | |
116 | * | |
117 | * @return NULL or the type | |
118 | */ | |
119 | public static SupportType valueOfNullOkUC(String typeName) { | |
120 | if (typeName == null) { | |
121 | return null; | |
122 | } | |
123 | ||
124 | return SupportType.valueOfUC(typeName); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Call {@link SupportType#valueOf(String.toUpperCase())} but return | |
129 | * NULL in case of error instead of raising an exception. | |
130 | * | |
131 | * @param typeName | |
132 | * the possible type name | |
133 | * | |
134 | * @return NULL or the type | |
135 | */ | |
136 | public static SupportType valueOfAllOkUC(String typeName) { | |
137 | try { | |
138 | return SupportType.valueOfUC(typeName); | |
139 | } catch (Exception e) { | |
140 | return null; | |
141 | } | |
142 | } | |
143 | } | |
144 | ||
145 | /** Only used by {@link BasicSupport#getInput()} just so it is always reset. */ | |
146 | private InputStream in; | |
147 | private SupportType type; | |
148 | private URL currentReferer; // with on 'r', as in 'HTTP'... | |
149 | ||
150 | // quote chars | |
151 | private char openQuote = Instance.getTrans().getChar( | |
152 | StringId.OPEN_SINGLE_QUOTE); | |
153 | private char closeQuote = Instance.getTrans().getChar( | |
154 | StringId.CLOSE_SINGLE_QUOTE); | |
155 | private char openDoubleQuote = Instance.getTrans().getChar( | |
156 | StringId.OPEN_DOUBLE_QUOTE); | |
157 | private char closeDoubleQuote = Instance.getTrans().getChar( | |
158 | StringId.CLOSE_DOUBLE_QUOTE); | |
159 | ||
160 | /** | |
161 | * The name of this support class. | |
162 | * | |
163 | * @return the name | |
164 | */ | |
165 | protected abstract String getSourceName(); | |
166 | ||
167 | /** | |
168 | * Check if the given resource is supported by this {@link BasicSupport}. | |
169 | * | |
170 | * @param url | |
171 | * the resource to check for | |
172 | * | |
173 | * @return TRUE if it is | |
174 | */ | |
175 | protected abstract boolean supports(URL url); | |
176 | ||
177 | /** | |
178 | * Return TRUE if the support will return HTML encoded content values for | |
179 | * the chapters content. | |
180 | * | |
181 | * @return TRUE for HTML | |
182 | */ | |
183 | protected abstract boolean isHtml(); | |
184 | ||
185 | /** | |
186 | * Return the story title. | |
187 | * | |
188 | * @param source | |
189 | * the source of the story | |
190 | * @param in | |
191 | * the input (the main resource) | |
192 | * | |
193 | * @return the title | |
194 | * | |
195 | * @throws IOException | |
196 | * in case of I/O error | |
197 | */ | |
198 | protected abstract String getTitle(URL source, InputStream in) | |
199 | throws IOException; | |
200 | ||
201 | /** | |
202 | * Return the story author. | |
203 | * | |
204 | * @param source | |
205 | * the source of the story | |
206 | * @param in | |
207 | * the input (the main resource) | |
208 | * | |
209 | * @return the author | |
210 | * | |
211 | * @throws IOException | |
212 | * in case of I/O error | |
213 | */ | |
214 | protected abstract String getAuthor(URL source, InputStream in) | |
215 | throws IOException; | |
216 | ||
217 | /** | |
218 | * Return the story publication date. | |
219 | * | |
220 | * @param source | |
221 | * the source of the story | |
222 | * @param in | |
223 | * the input (the main resource) | |
224 | * | |
225 | * @return the date | |
226 | * | |
227 | * @throws IOException | |
228 | * in case of I/O error | |
229 | */ | |
230 | protected abstract String getDate(URL source, InputStream in) | |
231 | throws IOException; | |
232 | ||
233 | /** | |
234 | * Return the subject of the story (for instance, if it is a fanfiction, | |
235 | * what is the original work; if it is a technical text, what is the | |
236 | * technical subject...). | |
237 | * | |
238 | * @param source | |
239 | * the source of the story | |
240 | * @param in | |
241 | * the input (the main resource) | |
242 | * | |
243 | * @return the subject | |
244 | * | |
245 | * @throws IOException | |
246 | * in case of I/O error | |
247 | */ | |
248 | protected abstract String getSubject(URL source, InputStream in) | |
249 | throws IOException; | |
250 | ||
251 | /** | |
252 | * Return the story description. | |
253 | * | |
254 | * @param source | |
255 | * the source of the story | |
256 | * @param in | |
257 | * the input (the main resource) | |
258 | * | |
259 | * @return the description | |
260 | * | |
261 | * @throws IOException | |
262 | * in case of I/O error | |
263 | */ | |
264 | protected abstract String getDesc(URL source, InputStream in) | |
265 | throws IOException; | |
266 | ||
267 | /** | |
268 | * Return the story cover resource if any, or NULL if none. | |
269 | * <p> | |
270 | * The default cover should not be checked for here. | |
271 | * | |
272 | * @param source | |
273 | * the source of the story | |
274 | * @param in | |
275 | * the input (the main resource) | |
276 | * | |
277 | * @return the cover or NULL | |
278 | * | |
279 | * @throws IOException | |
280 | * in case of I/O error | |
281 | */ | |
282 | protected abstract URL getCover(URL source, InputStream in) | |
283 | throws IOException; | |
284 | ||
285 | /** | |
286 | * Return the list of chapters (name and resource). | |
287 | * | |
288 | * @param source | |
289 | * the source of the story | |
290 | * @param in | |
291 | * the input (the main resource) | |
292 | * | |
293 | * @return the chapters | |
294 | * | |
295 | * @throws IOException | |
296 | * in case of I/O error | |
297 | */ | |
298 | protected abstract List<Entry<String, URL>> getChapters(URL source, | |
299 | InputStream in) throws IOException; | |
300 | ||
301 | /** | |
302 | * Return the content of the chapter (possibly HTML encoded, if | |
303 | * {@link BasicSupport#isHtml()} is TRUE). | |
304 | * | |
305 | * @param source | |
306 | * the source of the story | |
307 | * @param in | |
308 | * the input (the main resource) | |
309 | * @param number | |
310 | * the chapter number | |
311 | * | |
312 | * @return the content | |
313 | * | |
314 | * @throws IOException | |
315 | * in case of I/O error | |
316 | */ | |
317 | protected abstract String getChapterContent(URL source, InputStream in, | |
318 | int number) throws IOException; | |
319 | ||
320 | /** | |
321 | * Check if this {@link BasicSupport} is mainly catered to image files. | |
322 | * | |
323 | * @return TRUE if it is | |
324 | */ | |
325 | public boolean isImageDocument(URL source, InputStream in) | |
326 | throws IOException { | |
327 | return false; | |
328 | } | |
329 | ||
330 | /** | |
331 | * Return the list of cookies (values included) that must be used to | |
332 | * correctly fetch the resources. | |
333 | * <p> | |
334 | * You are expected to call the super method implementation if you override | |
335 | * it. | |
336 | * | |
337 | * @return the cookies | |
338 | */ | |
339 | public Map<String, String> getCookies() { | |
340 | return new HashMap<String, String>(); | |
341 | } | |
342 | ||
343 | /** | |
344 | * Process the given story resource into a partially filled {@link Story} | |
345 | * object containing the name and metadata, except for the description. | |
346 | * | |
347 | * @param url | |
348 | * the story resource | |
349 | * | |
350 | * @return the {@link Story} | |
351 | * | |
352 | * @throws IOException | |
353 | * in case of I/O error | |
354 | */ | |
355 | public Story processMeta(URL url) throws IOException { | |
356 | return processMeta(url, true, false); | |
357 | } | |
358 | ||
359 | /** | |
360 | * Process the given story resource into a partially filled {@link Story} | |
361 | * object containing the name and metadata. | |
362 | * | |
363 | * @param url | |
364 | * the story resource | |
365 | * | |
366 | * @param close | |
367 | * close "this" and "in" when done | |
368 | * | |
369 | * @return the {@link Story} | |
370 | * | |
371 | * @throws IOException | |
372 | * in case of I/O error | |
373 | */ | |
374 | protected Story processMeta(URL url, boolean close, boolean getDesc) | |
375 | throws IOException { | |
376 | in = Instance.getCache().open(url, this, false); | |
377 | if (in == null) { | |
378 | return null; | |
379 | } | |
380 | ||
381 | try { | |
382 | preprocess(getInput()); | |
383 | ||
384 | Story story = new Story(); | |
385 | story.setMeta(new MetaData()); | |
386 | story.getMeta().setTitle(ifUnhtml(getTitle(url, getInput()))); | |
387 | story.getMeta().setAuthor( | |
388 | fixAuthor(ifUnhtml(getAuthor(url, getInput())))); | |
389 | story.getMeta().setDate(ifUnhtml(getDate(url, getInput()))); | |
390 | story.getMeta().setTags(getTags(url, getInput())); | |
391 | story.getMeta().setSource(getSourceName()); | |
392 | story.getMeta().setPublisher( | |
393 | ifUnhtml(getPublisher(url, getInput()))); | |
394 | story.getMeta().setUuid(getUuid(url, getInput())); | |
395 | story.getMeta().setLuid(getLuid(url, getInput())); | |
396 | story.getMeta().setLang(getLang(url, getInput())); | |
397 | story.getMeta().setSubject(ifUnhtml(getSubject(url, getInput()))); | |
398 | story.getMeta().setImageDocument(isImageDocument(url, getInput())); | |
399 | ||
400 | if (getDesc) { | |
401 | String descChapterName = Instance.getTrans().getString( | |
402 | StringId.DESCRIPTION); | |
403 | story.getMeta().setResume( | |
404 | makeChapter(url, 0, descChapterName, | |
405 | getDesc(url, getInput()))); | |
406 | } | |
407 | ||
408 | return story; | |
409 | } finally { | |
410 | if (close) { | |
411 | try { | |
412 | close(); | |
413 | } catch (IOException e) { | |
414 | Instance.syserr(e); | |
415 | } | |
416 | ||
417 | if (in != null) { | |
418 | in.close(); | |
419 | } | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
424 | /** | |
425 | * Process the given story resource into a fully filled {@link Story} | |
426 | * object. | |
427 | * | |
428 | * @param url | |
429 | * the story resource | |
430 | * | |
431 | * @return the {@link Story} | |
432 | * | |
433 | * @throws IOException | |
434 | * in case of I/O error | |
435 | */ | |
436 | public Story process(URL url) throws IOException { | |
437 | setCurrentReferer(url); | |
438 | ||
439 | try { | |
440 | Story story = processMeta(url, false, true); | |
441 | if (story == null) { | |
442 | return null; | |
443 | } | |
444 | ||
445 | story.setChapters(new ArrayList<Chapter>()); | |
446 | ||
447 | URL cover = getCover(url, getInput()); | |
448 | if (cover == null) { | |
449 | String subject = story.getMeta() == null ? null : story | |
450 | .getMeta().getSubject(); | |
451 | if (subject != null && !subject.isEmpty() | |
452 | && Instance.getCoverDir() != null) { | |
453 | File fileCover = new File(Instance.getCoverDir(), subject); | |
454 | cover = getImage(fileCover.toURI().toURL(), subject); | |
455 | } | |
456 | } | |
457 | ||
458 | if (cover != null) { | |
459 | InputStream coverIn = null; | |
460 | try { | |
461 | coverIn = Instance.getCache().open(cover, this, true); | |
462 | story.getMeta().setCover(StringUtils.toImage(coverIn)); | |
463 | } catch (IOException e) { | |
464 | Instance.syserr(new IOException(Instance.getTrans() | |
465 | .getString(StringId.ERR_BS_NO_COVER, cover), e)); | |
466 | } finally { | |
467 | if (coverIn != null) | |
468 | coverIn.close(); | |
469 | } | |
470 | } | |
471 | ||
472 | List<Entry<String, URL>> chapters = getChapters(url, getInput()); | |
473 | int i = 1; | |
474 | if (chapters != null) { | |
475 | for (Entry<String, URL> chap : chapters) { | |
476 | setCurrentReferer(chap.getValue()); | |
477 | InputStream chapIn = Instance.getCache().open( | |
478 | chap.getValue(), this, true); | |
479 | try { | |
480 | story.getChapters().add( | |
481 | makeChapter(url, i, chap.getKey(), | |
482 | getChapterContent(url, chapIn, i))); | |
483 | } finally { | |
484 | chapIn.close(); | |
485 | } | |
486 | i++; | |
487 | } | |
488 | } | |
489 | ||
490 | return story; | |
491 | ||
492 | } finally { | |
493 | try { | |
494 | close(); | |
495 | } catch (IOException e) { | |
496 | Instance.syserr(e); | |
497 | } | |
498 | ||
499 | if (in != null) { | |
500 | in.close(); | |
501 | } | |
502 | ||
503 | currentReferer = null; | |
504 | } | |
505 | } | |
506 | ||
507 | /** | |
508 | * The support type.$ | |
509 | * | |
510 | * @return the type | |
511 | */ | |
512 | public SupportType getType() { | |
513 | return type; | |
514 | } | |
515 | ||
516 | /** | |
517 | * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e., | |
518 | * the current {@link URL} we work on. | |
519 | * | |
520 | * @return the referer | |
521 | */ | |
522 | public URL getCurrentReferer() { | |
523 | return currentReferer; | |
524 | } | |
525 | ||
526 | /** | |
527 | * The current referer {@link URL} (only one 'r', as in 'HTML'...), i.e., | |
528 | * the current {@link URL} we work on. | |
529 | * | |
530 | * @param currentReferer | |
531 | * the new referer | |
532 | */ | |
533 | protected void setCurrentReferer(URL currentReferer) { | |
534 | this.currentReferer = currentReferer; | |
535 | } | |
536 | ||
537 | /** | |
538 | * The support type. | |
539 | * | |
540 | * @param type | |
541 | * the new type | |
542 | * | |
543 | * @return this | |
544 | */ | |
545 | protected BasicSupport setType(SupportType type) { | |
546 | this.type = type; | |
547 | return this; | |
548 | } | |
549 | ||
550 | /** | |
551 | * Return the story publisher (by default, | |
552 | * {@link BasicSupport#getSourceName()}). | |
553 | * | |
554 | * @param source | |
555 | * the source of the story | |
556 | * @param in | |
557 | * the input (the main resource) | |
558 | * | |
559 | * @return the publisher | |
560 | * | |
561 | * @throws IOException | |
562 | * in case of I/O error | |
563 | */ | |
564 | protected String getPublisher(URL source, InputStream in) | |
565 | throws IOException { | |
566 | return getSourceName(); | |
567 | } | |
568 | ||
569 | /** | |
570 | * Return the story UUID, a unique value representing the story (it is often | |
571 | * an URL). | |
572 | * <p> | |
573 | * By default, this is the {@link URL} of the resource. | |
574 | * | |
575 | * @param source | |
576 | * the source of the story | |
577 | * @param in | |
578 | * the input (the main resource) | |
579 | * | |
580 | * @return the uuid | |
581 | * | |
582 | * @throws IOException | |
583 | * in case of I/O error | |
584 | */ | |
585 | protected String getUuid(URL source, InputStream in) throws IOException { | |
586 | return source.toString(); | |
587 | } | |
588 | ||
589 | /** | |
590 | * Return the story Library UID, a unique value representing the story (it | |
591 | * is often a number) in the local library. | |
592 | * <p> | |
593 | * By default, this is empty. | |
594 | * | |
595 | * @param source | |
596 | * the source of the story | |
597 | * @param in | |
598 | * the input (the main resource) | |
599 | * | |
600 | * @return the id | |
601 | * | |
602 | * @throws IOException | |
603 | * in case of I/O error | |
604 | */ | |
605 | protected String getLuid(URL source, InputStream in) throws IOException { | |
606 | return ""; | |
607 | } | |
608 | ||
609 | /** | |
610 | * Return the 2-letter language code of this story. | |
611 | * <p> | |
612 | * By default, this is 'EN'. | |
613 | * | |
614 | * @param source | |
615 | * the source of the story | |
616 | * @param in | |
617 | * the input (the main resource) | |
618 | * | |
619 | * @return the language | |
620 | * | |
621 | * @throws IOException | |
622 | * in case of I/O error | |
623 | */ | |
624 | protected String getLang(URL source, InputStream in) throws IOException { | |
625 | return "EN"; | |
626 | } | |
627 | ||
628 | /** | |
629 | * Return the list of tags for this story. | |
630 | * | |
631 | * @param source | |
632 | * the source of the story | |
633 | * @param in | |
634 | * the input (the main resource) | |
635 | * | |
636 | * @return the tags | |
637 | * | |
638 | * @throws IOException | |
639 | * in case of I/O error | |
640 | */ | |
641 | protected List<String> getTags(URL source, InputStream in) | |
642 | throws IOException { | |
643 | return new ArrayList<String>(); | |
644 | } | |
645 | ||
646 | /** | |
647 | * Return the first line from the given input which correspond to the given | |
648 | * selectors. | |
649 | * <p> | |
650 | * Do not reset the input, which will be pointing at the line just after the | |
651 | * result (input will be spent if no result is found). | |
652 | * | |
653 | * @param in | |
654 | * the input | |
655 | * @param needle | |
656 | * a string that must be found inside the target line | |
657 | * @param relativeLine | |
658 | * the line to return based upon the target line position (-1 = | |
659 | * the line before, 0 = the target line...) | |
660 | * | |
661 | * @return the line | |
662 | */ | |
663 | protected String getLine(InputStream in, String needle, int relativeLine) { | |
664 | return getLine(in, needle, relativeLine, true); | |
665 | } | |
666 | ||
667 | /** | |
668 | * Return a line from the given input which correspond to the given | |
669 | * selectors. | |
670 | * <p> | |
671 | * Do not reset the input, which will be pointing at the line just after the | |
672 | * result (input will be spent if no result is found) when first is TRUE, | |
673 | * and will always be spent if first is FALSE. | |
674 | * | |
675 | * @param in | |
676 | * the input | |
677 | * @param needle | |
678 | * a string that must be found inside the target line | |
679 | * @param relativeLine | |
680 | * the line to return based upon the target line position (-1 = | |
681 | * the line before, 0 = the target line...) | |
682 | * @param first | |
683 | * takes the first result (as opposed to the last one, which will | |
684 | * also always spend the input) | |
685 | * | |
686 | * @return the line | |
687 | */ | |
688 | protected String getLine(InputStream in, String needle, int relativeLine, | |
689 | boolean first) { | |
690 | String rep = null; | |
691 | ||
692 | List<String> lines = new ArrayList<String>(); | |
693 | @SuppressWarnings("resource") | |
694 | Scanner scan = new Scanner(in, "UTF-8"); | |
695 | int index = -1; | |
696 | scan.useDelimiter("\\n"); | |
697 | while (scan.hasNext()) { | |
698 | lines.add(scan.next()); | |
699 | ||
700 | if (index == -1 && lines.get(lines.size() - 1).contains(needle)) { | |
701 | index = lines.size() - 1; | |
702 | } | |
703 | ||
704 | if (index >= 0 && index + relativeLine < lines.size()) { | |
705 | rep = lines.get(index + relativeLine); | |
706 | if (first) { | |
707 | break; | |
708 | } | |
709 | } | |
710 | } | |
711 | ||
712 | return rep; | |
713 | } | |
714 | ||
715 | /** | |
716 | * Prepare the support if needed before processing. | |
717 | * | |
718 | * @throws IOException | |
719 | * on I/O error | |
720 | */ | |
721 | protected void preprocess(InputStream in) throws IOException { | |
722 | } | |
723 | ||
724 | /** | |
725 | * Now that we have processed the {@link Story}, close the resources if any. | |
726 | * | |
727 | * @throws IOException | |
728 | * on I/O error | |
729 | */ | |
730 | protected void close() throws IOException { | |
731 | } | |
732 | ||
733 | /** | |
734 | * Create a {@link Chapter} object from the given information, formatting | |
735 | * the content as it should be. | |
736 | * | |
737 | * @param number | |
738 | * the chapter number | |
739 | * @param name | |
740 | * the chapter name | |
741 | * @param content | |
742 | * the chapter content | |
743 | * | |
744 | * @return the {@link Chapter} | |
745 | * | |
746 | * @throws IOException | |
747 | * in case of I/O error | |
748 | */ | |
749 | protected Chapter makeChapter(URL source, int number, String name, | |
750 | String content) throws IOException { | |
751 | ||
752 | // Chapter name: process it correctly, then remove the possible | |
753 | // redundant "Chapter x: " in front of it | |
754 | String chapterName = processPara(name).getContent().trim(); | |
755 | for (String lang : Instance.getConfig().getString(Config.CHAPTER) | |
756 | .split(",")) { | |
757 | String chapterWord = Instance.getConfig().getStringX( | |
758 | Config.CHAPTER, lang); | |
759 | if (chapterName.startsWith(chapterWord)) { | |
760 | chapterName = chapterName.substring(chapterWord.length()) | |
761 | .trim(); | |
762 | break; | |
763 | } | |
764 | } | |
765 | ||
766 | if (chapterName.startsWith(Integer.toString(number))) { | |
767 | chapterName = chapterName.substring( | |
768 | Integer.toString(number).length()).trim(); | |
769 | } | |
770 | ||
771 | if (chapterName.startsWith(":")) { | |
772 | chapterName = chapterName.substring(1).trim(); | |
773 | } | |
774 | // | |
775 | ||
776 | Chapter chap = new Chapter(number, chapterName); | |
777 | ||
778 | if (content == null) { | |
779 | return chap; | |
780 | } | |
781 | ||
782 | if (isHtml()) { | |
783 | // Special <HR> processing: | |
784 | content = content.replaceAll("(<hr [^>]*>)|(<hr/>)|(<hr>)", | |
785 | "\n* * *\n"); | |
786 | } | |
787 | ||
788 | InputStream in = new ByteArrayInputStream( | |
789 | content.getBytes(StandardCharsets.UTF_8)); | |
790 | try { | |
791 | @SuppressWarnings("resource") | |
792 | Scanner scan = new Scanner(in, "UTF-8"); | |
793 | scan.useDelimiter("(\\n|</p>)"); // \n for test, </p> for html | |
794 | ||
795 | List<Paragraph> paras = new ArrayList<Paragraph>(); | |
796 | while (scan.hasNext()) { | |
797 | String line = scan.next().trim(); | |
798 | boolean image = false; | |
799 | if (line.startsWith("[") && line.endsWith("]")) { | |
800 | URL url = getImage(source, | |
801 | line.substring(1, line.length() - 1).trim()); | |
802 | if (url != null) { | |
803 | paras.add(new Paragraph(url)); | |
804 | image = true; | |
805 | } | |
806 | } | |
807 | ||
808 | if (!image) { | |
809 | paras.add(processPara(line)); | |
810 | } | |
811 | } | |
812 | ||
813 | // Check quotes for "bad" format | |
814 | List<Paragraph> newParas = new ArrayList<Paragraph>(); | |
815 | for (Paragraph para : paras) { | |
816 | newParas.addAll(requotify(para)); | |
817 | } | |
818 | paras = newParas; | |
819 | ||
820 | // Remove double blanks/brks | |
821 | boolean space = false; | |
822 | boolean brk = true; | |
823 | for (int i = 0; i < paras.size(); i++) { | |
824 | Paragraph para = paras.get(i); | |
825 | boolean thisSpace = para.getType() == ParagraphType.BLANK; | |
826 | boolean thisBrk = para.getType() == ParagraphType.BREAK; | |
827 | ||
828 | if (space && thisBrk) { | |
829 | paras.remove(i - 1); | |
830 | i--; | |
831 | } else if ((space || brk) && (thisSpace || thisBrk)) { | |
832 | paras.remove(i); | |
833 | i--; | |
834 | } | |
835 | ||
836 | space = thisSpace; | |
837 | brk = thisBrk; | |
838 | } | |
839 | ||
840 | // Remove blank/brk at start | |
841 | if (paras.size() > 0 | |
842 | && (paras.get(0).getType() == ParagraphType.BLANK || paras | |
843 | .get(0).getType() == ParagraphType.BREAK)) { | |
844 | paras.remove(0); | |
845 | } | |
846 | ||
847 | // Remove blank/brk at end | |
848 | int last = paras.size() - 1; | |
849 | if (paras.size() > 0 | |
850 | && (paras.get(last).getType() == ParagraphType.BLANK || paras | |
851 | .get(last).getType() == ParagraphType.BREAK)) { | |
852 | paras.remove(last); | |
853 | } | |
854 | ||
855 | chap.setParagraphs(paras); | |
856 | ||
857 | return chap; | |
858 | } finally { | |
859 | in.close(); | |
860 | } | |
861 | } | |
862 | ||
863 | /** | |
864 | * Return the list of supported image extensions. | |
865 | * | |
866 | * @return the extensions | |
867 | */ | |
868 | protected String[] getImageExt(boolean emptyAllowed) { | |
869 | if (emptyAllowed) { | |
870 | return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
871 | } else { | |
872 | return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" }; | |
873 | } | |
874 | } | |
875 | ||
876 | /** | |
877 | * Check if the given resource can be a local image or a remote image, then | |
878 | * refresh the cache with it if it is. | |
879 | * | |
880 | * @param source | |
881 | * the story source | |
882 | * @param line | |
883 | * the resource to check | |
884 | * | |
885 | * @return the image URL if found, or NULL | |
886 | * | |
887 | */ | |
888 | protected URL getImage(URL source, String line) { | |
889 | String path = new File(source.getFile()).getParent(); | |
890 | URL url = null; | |
891 | ||
892 | // try for files | |
893 | try { | |
894 | String urlBase = new File(new File(path), line.trim()).toURI() | |
895 | .toURL().toString(); | |
896 | for (String ext : getImageExt(true)) { | |
897 | if (new File(urlBase + ext).exists()) { | |
898 | url = new File(urlBase + ext).toURI().toURL(); | |
899 | } | |
900 | } | |
901 | } catch (Exception e) { | |
902 | // Nothing to do here | |
903 | } | |
904 | ||
905 | if (url == null) { | |
906 | // try for URLs | |
907 | try { | |
908 | for (String ext : getImageExt(true)) { | |
909 | if (Instance.getCache().check(new URL(line + ext))) { | |
910 | url = new URL(line + ext); | |
911 | } | |
912 | } | |
913 | ||
914 | // try out of cache | |
915 | if (url == null) { | |
916 | for (String ext : getImageExt(true)) { | |
917 | try { | |
918 | url = new URL(line + ext); | |
919 | Instance.getCache().refresh(url, this, true); | |
920 | break; | |
921 | } catch (IOException e) { | |
922 | // no image with this ext | |
923 | url = null; | |
924 | } | |
925 | } | |
926 | } | |
927 | } catch (MalformedURLException e) { | |
928 | // Not an url | |
929 | } | |
930 | } | |
931 | ||
932 | // refresh the cached file | |
933 | if (url != null) { | |
934 | try { | |
935 | Instance.getCache().refresh(url, this, true); | |
936 | } catch (IOException e) { | |
937 | // woops, broken image | |
938 | url = null; | |
939 | } | |
940 | } | |
941 | ||
942 | return url; | |
943 | } | |
944 | ||
945 | /** | |
946 | * Reset then return {@link BasicSupport#in}. | |
947 | * | |
948 | * @return {@link BasicSupport#in} | |
949 | * | |
950 | * @throws IOException | |
951 | * in case of I/O error | |
952 | */ | |
953 | protected InputStream getInput() throws IOException { | |
954 | in.reset(); | |
955 | return in; | |
956 | } | |
957 | ||
958 | /** | |
959 | * Fix the author name if it is prefixed with some "by" {@link String}. | |
960 | * | |
961 | * @param author | |
962 | * the author with a possible prefix | |
963 | * | |
964 | * @return the author without prefixes | |
965 | */ | |
966 | private String fixAuthor(String author) { | |
967 | if (author != null) { | |
968 | for (String suffix : new String[] { " ", ":" }) { | |
969 | for (String byString : Instance.getConfig() | |
970 | .getString(Config.BYS).split(",")) { | |
971 | byString += suffix; | |
972 | if (author.toUpperCase().startsWith(byString.toUpperCase())) { | |
973 | author = author.substring(byString.length()).trim(); | |
974 | } | |
975 | } | |
976 | } | |
977 | ||
978 | // Special case (without suffix): | |
979 | if (author.startsWith("©")) { | |
980 | author = author.substring(1); | |
981 | } | |
982 | } | |
983 | ||
984 | return author; | |
985 | } | |
986 | ||
987 | /** | |
988 | * Check quotes for bad format (i.e., quotes with normal paragraphs inside) | |
989 | * and requotify them (i.e., separate them into QUOTE paragraphs and other | |
990 | * paragraphs (quotes or not)). | |
991 | * | |
992 | * @param para | |
993 | * the paragraph to requotify (not necessaraly a quote) | |
994 | * | |
995 | * @return the correctly (or so we hope) quotified paragraphs | |
996 | */ | |
997 | private List<Paragraph> requotify(Paragraph para) { | |
998 | List<Paragraph> newParas = new ArrayList<Paragraph>(); | |
999 | ||
1000 | if (para.getType() == ParagraphType.QUOTE) { | |
1001 | String line = para.getContent(); | |
1002 | boolean singleQ = line.startsWith("" + openQuote); | |
1003 | boolean doubleQ = line.startsWith("" + openDoubleQuote); | |
1004 | ||
1005 | if (!singleQ && !doubleQ) { | |
1006 | line = openDoubleQuote + line + closeDoubleQuote; | |
1007 | newParas.add(new Paragraph(ParagraphType.QUOTE, line)); | |
1008 | } else { | |
1009 | char close = singleQ ? closeQuote : closeDoubleQuote; | |
1010 | int posClose = line.indexOf(close); | |
1011 | int posDot = line.indexOf("."); | |
1012 | while (posDot >= 0 && posDot < posClose) { | |
1013 | posDot = line.indexOf(".", posDot + 1); | |
1014 | } | |
1015 | ||
1016 | if (posDot >= 0) { | |
1017 | String rest = line.substring(posDot + 1).trim(); | |
1018 | line = line.substring(0, posDot + 1).trim(); | |
1019 | newParas.add(new Paragraph(ParagraphType.QUOTE, line)); | |
1020 | newParas.addAll(requotify(processPara(rest))); | |
1021 | } else { | |
1022 | newParas.add(para); | |
1023 | } | |
1024 | } | |
1025 | } else { | |
1026 | newParas.add(para); | |
1027 | } | |
1028 | ||
1029 | return newParas; | |
1030 | } | |
1031 | ||
1032 | /** | |
1033 | * Process a {@link Paragraph} from a raw line of text. | |
1034 | * <p> | |
1035 | * Will also fix quotes and HTML encoding if needed. | |
1036 | * | |
1037 | * @param line | |
1038 | * the raw line | |
1039 | * | |
1040 | * @return the processed {@link Paragraph} | |
1041 | */ | |
1042 | private Paragraph processPara(String line) { | |
1043 | line = ifUnhtml(line).trim(); | |
1044 | ||
1045 | boolean space = true; | |
1046 | boolean brk = true; | |
1047 | boolean quote = false; | |
1048 | boolean tentativeCloseQuote = false; | |
1049 | char prev = '\0'; | |
1050 | int dashCount = 0; | |
1051 | ||
1052 | StringBuilder builder = new StringBuilder(); | |
1053 | for (char car : line.toCharArray()) { | |
1054 | if (car != '-') { | |
1055 | if (dashCount > 0) { | |
1056 | // dash, ndash and mdash: - – — | |
1057 | // currently: always use mdash | |
1058 | builder.append(dashCount == 1 ? '-' : '—'); | |
1059 | } | |
1060 | dashCount = 0; | |
1061 | } | |
1062 | ||
1063 | if (tentativeCloseQuote) { | |
1064 | tentativeCloseQuote = false; | |
1065 | if ((car >= 'a' && car <= 'z') || (car >= 'A' && car <= 'Z') | |
1066 | || (car >= '0' && car <= '9')) { | |
1067 | builder.append("'"); | |
1068 | } else { | |
1069 | builder.append(closeQuote); | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | switch (car) { | |
1074 | case ' ': // note: unbreakable space | |
1075 | case ' ': | |
1076 | case '\t': | |
1077 | case '\n': // just in case | |
1078 | case '\r': // just in case | |
1079 | builder.append(' '); | |
1080 | break; | |
1081 | ||
1082 | case '\'': | |
1083 | if (space || (brk && quote)) { | |
1084 | quote = true; | |
1085 | builder.append(openQuote); | |
1086 | } else if (prev == ' ') { | |
1087 | builder.append(openQuote); | |
1088 | } else { | |
1089 | // it is a quote ("I'm off") or a 'quote' ("This | |
1090 | // 'good' restaurant"...) | |
1091 | tentativeCloseQuote = true; | |
1092 | } | |
1093 | break; | |
1094 | ||
1095 | case '"': | |
1096 | if (space || (brk && quote)) { | |
1097 | quote = true; | |
1098 | builder.append(openDoubleQuote); | |
1099 | } else if (prev == ' ') { | |
1100 | builder.append(openDoubleQuote); | |
1101 | } else { | |
1102 | builder.append(closeDoubleQuote); | |
1103 | } | |
1104 | break; | |
1105 | ||
1106 | case '-': | |
1107 | if (space) { | |
1108 | quote = true; | |
1109 | } else { | |
1110 | dashCount++; | |
1111 | } | |
1112 | space = false; | |
1113 | break; | |
1114 | ||
1115 | case '*': | |
1116 | case '~': | |
1117 | case '/': | |
1118 | case '\\': | |
1119 | case '<': | |
1120 | case '>': | |
1121 | case '=': | |
1122 | case '+': | |
1123 | case '_': | |
1124 | case '–': | |
1125 | case '—': | |
1126 | space = false; | |
1127 | builder.append(car); | |
1128 | break; | |
1129 | ||
1130 | case '‘': | |
1131 | case '`': | |
1132 | case '‹': | |
1133 | case '﹁': | |
1134 | case '〈': | |
1135 | case '「': | |
1136 | if (space || (brk && quote)) { | |
1137 | quote = true; | |
1138 | builder.append(openQuote); | |
1139 | } else { | |
1140 | builder.append(openQuote); | |
1141 | } | |
1142 | space = false; | |
1143 | brk = false; | |
1144 | break; | |
1145 | ||
1146 | case '’': | |
1147 | case '›': | |
1148 | case '﹂': | |
1149 | case '〉': | |
1150 | case '」': | |
1151 | space = false; | |
1152 | brk = false; | |
1153 | builder.append(closeQuote); | |
1154 | break; | |
1155 | ||
1156 | case '«': | |
1157 | case '“': | |
1158 | case '﹃': | |
1159 | case '《': | |
1160 | case '『': | |
1161 | if (space || (brk && quote)) { | |
1162 | quote = true; | |
1163 | builder.append(openDoubleQuote); | |
1164 | } else { | |
1165 | builder.append(openDoubleQuote); | |
1166 | } | |
1167 | space = false; | |
1168 | brk = false; | |
1169 | break; | |
1170 | ||
1171 | case '»': | |
1172 | case '”': | |
1173 | case '﹄': | |
1174 | case '》': | |
1175 | case '』': | |
1176 | space = false; | |
1177 | brk = false; | |
1178 | builder.append(closeDoubleQuote); | |
1179 | break; | |
1180 | ||
1181 | default: | |
1182 | space = false; | |
1183 | brk = false; | |
1184 | builder.append(car); | |
1185 | break; | |
1186 | } | |
1187 | ||
1188 | prev = car; | |
1189 | } | |
1190 | ||
1191 | if (tentativeCloseQuote) { | |
1192 | tentativeCloseQuote = false; | |
1193 | builder.append(closeQuote); | |
1194 | } | |
1195 | ||
1196 | line = builder.toString().trim(); | |
1197 | ||
1198 | ParagraphType type = ParagraphType.NORMAL; | |
1199 | if (space) { | |
1200 | type = ParagraphType.BLANK; | |
1201 | } else if (brk) { | |
1202 | type = ParagraphType.BREAK; | |
1203 | } else if (quote) { | |
1204 | type = ParagraphType.QUOTE; | |
1205 | } | |
1206 | ||
1207 | return new Paragraph(type, line); | |
1208 | } | |
1209 | ||
1210 | /** | |
1211 | * Remove the HTML from the inpit <b>if</b> {@link BasicSupport#isHtml()} is | |
1212 | * true. | |
1213 | * | |
1214 | * @param input | |
1215 | * the input | |
1216 | * | |
1217 | * @return the no html version if needed | |
1218 | */ | |
1219 | private String ifUnhtml(String input) { | |
1220 | if (isHtml() && input != null) { | |
1221 | return StringUtils.unhtml(input); | |
1222 | } | |
1223 | ||
1224 | return input; | |
1225 | } | |
1226 | ||
1227 | /** | |
1228 | * Return a {@link BasicSupport} implementation supporting the given | |
1229 | * resource if possible. | |
1230 | * | |
1231 | * @param url | |
1232 | * the story resource | |
1233 | * | |
1234 | * @return an implementation that supports it, or NULL | |
1235 | */ | |
1236 | public static BasicSupport getSupport(URL url) { | |
1237 | if (url == null) { | |
1238 | return null; | |
1239 | } | |
1240 | ||
1241 | // TEXT and INFO_TEXT always support files (not URLs though) | |
1242 | for (SupportType type : SupportType.values()) { | |
1243 | if (type != SupportType.TEXT && type != SupportType.INFO_TEXT) { | |
1244 | BasicSupport support = getSupport(type); | |
1245 | if (support != null && support.supports(url)) { | |
1246 | return support; | |
1247 | } | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | for (SupportType type : new SupportType[] { SupportType.TEXT, | |
1252 | SupportType.INFO_TEXT }) { | |
1253 | BasicSupport support = getSupport(type); | |
1254 | if (support != null && support.supports(url)) { | |
1255 | return support; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | return null; | |
1260 | } | |
1261 | ||
1262 | /** | |
1263 | * Return a {@link BasicSupport} implementation supporting the given type. | |
1264 | * | |
1265 | * @param type | |
1266 | * the type | |
1267 | * | |
1268 | * @return an implementation that supports it, or NULL | |
1269 | */ | |
1270 | public static BasicSupport getSupport(SupportType type) { | |
1271 | switch (type) { | |
1272 | case EPUB: | |
1273 | return new Epub().setType(type); | |
1274 | case INFO_TEXT: | |
1275 | return new InfoText().setType(type); | |
1276 | case FIMFICTION: | |
1277 | return new Fimfiction().setType(type); | |
1278 | case FANFICTION: | |
1279 | return new Fanfiction().setType(type); | |
1280 | case TEXT: | |
1281 | return new Text().setType(type); | |
1282 | case MANGAFOX: | |
1283 | return new MangaFox().setType(type); | |
1284 | case E621: | |
1285 | return new E621().setType(type); | |
1286 | case CBZ: | |
1287 | return new Cbz().setType(type); | |
1288 | } | |
1289 | ||
1290 | return null; | |
1291 | } | |
1292 | } |