45d970a656322ef8e312f7cd6950a83123aedfe7
1 package be
.nikiroo
.fanfix
.supported
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.URISyntaxException
;
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
;
14 import org
.jsoup
.nodes
.Document
;
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
;
25 * Support class for local stories encoded in textual format, with a few rules:
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
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
41 class Text
extends BasicSupport
{
42 private File sourceFile
;
43 private InputStream in
;
45 protected File
getSourceFile() {
49 protected InputStream
getInput() {
53 } catch (IOException e
) {
54 Instance
.getInstance().getTraceHandler().error(new IOException("Cannot reset the Text stream", e
));
64 protected boolean isHtml() {
69 protected Document
loadDocument(URL source
) throws IOException
{
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
);
81 protected MetaData
getMeta() throws IOException
{
82 MetaData meta
= new MetaData();
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());
91 meta
.setLang(getLang()); // default is EN
92 meta
.setSubject(getSourceFile().getParentFile().getName());
93 meta
.setImageDocument(false);
94 meta
.setCover(getCover(getSourceFile()));
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 scan
.next(); // Title
104 scan
.next(); // Author (Date)
105 String chapter0
= scan
.next(); // empty or Chapter 0
106 while (chapter0
.isEmpty()) {
107 chapter0
= scan
.next();
110 String lang
= detectChapter(chapter0
, 0);
113 lang
= detectChapter(chapter0
, 1);
119 lang
= lang
.toLowerCase();
125 private String
getTitle() {
126 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
127 Scanner scan
= new Scanner(getInput(), "UTF-8");
128 scan
.useDelimiter("\\n");
132 private String
getAuthor() {
133 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
134 Scanner scan
= new Scanner(getInput(), "UTF-8");
135 scan
.useDelimiter("\\n");
137 String authorDate
= scan
.next();
139 String author
= authorDate
;
140 int pos
= authorDate
.indexOf('(');
142 author
= authorDate
.substring(0, pos
);
145 return bsHelper
.fixAuthor(author
);
148 private String
getDate() {
149 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
150 Scanner scan
= new Scanner(getInput(), "UTF-8");
151 scan
.useDelimiter("\\n");
153 String authorDate
= scan
.next();
156 int pos
= authorDate
.indexOf('(');
158 date
= authorDate
.substring(pos
+ 1).trim();
159 pos
= date
.lastIndexOf(')');
161 date
= date
.substring(0, pos
).trim();
169 protected String
getDesc() throws IOException
{
170 return getChapterContent(null, 0, null).trim();
173 protected Image
getCover(File sourceFile
) {
174 String path
= sourceFile
.getName();
176 for (String ext
: new String
[] { ".txt", ".text", ".story" }) {
177 if (path
.endsWith(ext
)) {
178 path
= path
.substring(0, path
.length() - ext
.length());
182 Image cover
= bsImages
.getImage(this, sourceFile
.getParentFile(), path
);
185 File tmp
= Instance
.getInstance().getTempFiles().createTempFile("test_cover_image");
186 ImageUtils
.getInstance().saveAsImage(cover
, tmp
, "png");
188 } catch (IOException e
) {
197 protected List
<Entry
<String
, URL
>> getChapters(Progress pg
)
199 List
<Entry
<String
, URL
>> chaps
= new ArrayList
<Entry
<String
, URL
>>();
200 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
201 Scanner scan
= new Scanner(getInput(), "UTF-8");
202 scan
.useDelimiter("\\n");
203 String line
= "first is not empty";
204 while (scan
.hasNext()) {
205 boolean prevLineEmpty
= line
.trim().isEmpty();
207 if (prevLineEmpty
&& detectChapter(line
, chaps
.size() + 1) != null) {
208 String chapName
= Integer
.toString(chaps
.size() + 1);
209 int pos
= line
.indexOf(':');
210 if (pos
>= 0 && pos
+ 1 < line
.length()) {
211 chapName
= line
.substring(pos
+ 1).trim();
214 chaps
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(//
216 getSourceFile().toURI().toURL()));
224 protected String
getChapterContent(URL source
, int number
, Progress pg
)
226 StringBuilder builder
= new StringBuilder();
227 @SuppressWarnings("resource") // cannot close, or we loose getInput()!
228 Scanner scan
= new Scanner(getInput(), "UTF-8");
229 scan
.useDelimiter("\\n");
231 scan
.next(); // title
233 scan
.next(); // author
235 scan
.next(); // date or empty
236 Boolean inChap
= null;
238 while (scan
.hasNext()) {
239 if (number
== 0 && !line
.trim().isEmpty()) {
240 // We found pre-chapter content, we are checking for
241 // Chapter 0 (fake chapter) --> keep the content
246 if ((inChap
== null || !inChap
) && detectChapter(line
, number
) != null) {
248 } else if (detectChapter(line
, number
+ 1) != null) {
250 } else if (inChap
!= null && inChap
) {
251 builder
.append(line
);
252 builder
.append("\n");
256 return builder
.toString();
260 protected void close() {
261 InputStream in
= getInput();
265 } catch (IOException e
) {
266 Instance
.getInstance().getTraceHandler()
267 .error(new IOException("Cannot close the text source file input", e
));
275 protected boolean supports(URL url
) {
276 return supports(url
, false);
280 * Check if we supports this {@link URL}, that is, if the info file can be
281 * found OR not found.
283 * It must also be a file, not another kind of URL.
286 * the {@link URL} to check
288 * TRUE to require the info file, FALSE to forbid the info file
290 * @return TRUE if it is supported
292 protected boolean supports(URL url
, boolean info
) {
293 if (!"file".equals(url
.getProtocol())) {
297 boolean infoPresent
= false;
300 file
= new File(url
.toURI());
301 file
= assureNoTxt(file
);
302 file
= new File(file
.getPath() + ".info");
303 } catch (URISyntaxException e
) {
304 Instance
.getInstance().getTraceHandler().error(e
);
308 infoPresent
= (file
!= null && file
.exists());
310 return infoPresent
== info
;
314 * Remove the ".txt" (or ".text") extension if it is present.
317 * the file to process
319 * @return the same file or a copy of it without the ".txt" extension if it
322 protected File
assureNoTxt(File file
) {
323 for (String ext
: new String
[] { ".txt", ".text" }) {
324 if (file
.getName().endsWith(ext
)) {
325 file
= new File(file
.getPath().substring(0,
326 file
.getPath().length() - ext
.length()));
334 * Check if the given line looks like the given starting chapter in a
335 * supported language, and return the language if it does (or NULL if not).
340 * the specific chapter number to check for
342 * @return the language or NULL
344 static private String
detectChapter(String line
, int number
) {
345 line
= line
.toUpperCase();
346 for (String lang
: Instance
.getInstance().getConfig().getList(Config
.CONF_CHAPTER
)) {
347 String chapter
= Instance
.getInstance().getConfig().getStringX(Config
.CONF_CHAPTER
, lang
);
348 if (chapter
!= null && !chapter
.isEmpty()) {
349 chapter
= chapter
.toUpperCase() + " ";
350 if (line
.startsWith(chapter
)) {
351 // We want "[CHAPTER] [number]: [name]", with ": [name]"
353 String test
= line
.substring(chapter
.length()).trim();
355 String possibleNum
= test
.trim();
356 if (possibleNum
.indexOf(':') > 0) {
357 possibleNum
= possibleNum
.substring(0,
358 possibleNum
.indexOf(':')).trim();
361 if (test
.startsWith(Integer
.toString(number
))) {
363 .substring(Integer
.toString(number
).length())
365 if (test
.isEmpty() || test
.startsWith(":")) {