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
.getTraceHandler().error(
55 new IOException("Cannot reset the Text stream", e
));
65 protected boolean isHtml() {
70 protected Document
loadDocument(URL source
) throws IOException
{
72 sourceFile
= new File(source
.toURI());
73 in
= new MarkableFileInputStream(sourceFile
);
74 } catch (URISyntaxException e
) {
75 throw new IOException("Cannot load the text document: " + source
);
82 protected MetaData
getMeta() throws IOException
{
83 MetaData meta
= new MetaData();
85 meta
.setTitle(getTitle());
86 meta
.setAuthor(getAuthor());
87 meta
.setDate(getDate());
88 meta
.setTags(new ArrayList
<String
>());
89 meta
.setSource(getType().getSourceName());
90 meta
.setUrl(getSourceFile().toURI().toURL().toString());
91 meta
.setPublisher("");
92 meta
.setUuid(getSourceFile().toString());
94 meta
.setLang(getLang()); // default is EN
95 meta
.setSubject(getSourceFile().getParentFile().getName());
96 meta
.setType(getType().toString());
97 meta
.setImageDocument(false);
98 meta
.setCover(getCover(getSourceFile()));
103 private String
getLang() {
104 @SuppressWarnings("resource")
105 Scanner scan
= new Scanner(getInput(), "UTF-8");
106 scan
.useDelimiter("\\n");
107 scan
.next(); // Title
108 scan
.next(); // Author (Date)
109 String chapter0
= scan
.next(); // empty or Chapter 0
110 while (chapter0
.isEmpty()) {
111 chapter0
= scan
.next();
114 String lang
= detectChapter(chapter0
, 0);
117 lang
= detectChapter(chapter0
, 1);
123 lang
= lang
.toLowerCase();
129 private String
getTitle() {
130 @SuppressWarnings("resource")
131 Scanner scan
= new Scanner(getInput(), "UTF-8");
132 scan
.useDelimiter("\\n");
136 private String
getAuthor() {
137 @SuppressWarnings("resource")
138 Scanner scan
= new Scanner(getInput(), "UTF-8");
139 scan
.useDelimiter("\\n");
141 String authorDate
= scan
.next();
143 String author
= authorDate
;
144 int pos
= authorDate
.indexOf('(');
146 author
= authorDate
.substring(0, pos
);
149 return bsHelper
.fixAuthor(author
);
152 private String
getDate() {
153 @SuppressWarnings("resource")
154 Scanner scan
= new Scanner(getInput(), "UTF-8");
155 scan
.useDelimiter("\\n");
157 String authorDate
= scan
.next();
160 int pos
= authorDate
.indexOf('(');
162 date
= authorDate
.substring(pos
+ 1).trim();
163 pos
= date
.lastIndexOf(')');
165 date
= date
.substring(0, pos
).trim();
173 protected String
getDesc() throws IOException
{
174 return getChapterContent(null, 0, null);
177 private Image
getCover(File sourceFile
) {
178 String path
= sourceFile
.getName();
180 for (String ext
: new String
[] { ".txt", ".text", ".story" }) {
181 if (path
.endsWith(ext
)) {
182 path
= path
.substring(0, path
.length() - ext
.length());
186 Image cover
= bsImages
.getImage(this,
187 sourceFile
.getParentFile(), path
);
190 File tmp
= Instance
.getTempFiles().createTempFile(
192 ImageUtils
.getInstance().saveAsImage(cover
, tmp
, "png");
194 } catch (IOException e
) {
203 protected List
<Entry
<String
, URL
>> getChapters(Progress pg
)
205 List
<Entry
<String
, URL
>> chaps
= new ArrayList
<Entry
<String
, URL
>>();
206 @SuppressWarnings("resource")
207 Scanner scan
= new Scanner(getInput(), "UTF-8");
208 scan
.useDelimiter("\\n");
209 boolean prevLineEmpty
= false;
210 while (scan
.hasNext()) {
211 String line
= scan
.next();
212 if (prevLineEmpty
&& detectChapter(line
, chaps
.size() + 1) != null) {
213 String chapName
= Integer
.toString(chaps
.size() + 1);
214 int pos
= line
.indexOf(':');
215 if (pos
>= 0 && pos
+ 1 < line
.length()) {
216 chapName
= line
.substring(pos
+ 1).trim();
219 chaps
.add(new AbstractMap
.SimpleEntry
<String
, URL
>(//
221 getSourceFile().toURI().toURL()));
224 prevLineEmpty
= line
.trim().isEmpty();
231 protected String
getChapterContent(URL source
, int number
, Progress pg
)
233 StringBuilder builder
= new StringBuilder();
234 @SuppressWarnings("resource")
235 Scanner scan
= new Scanner(getInput(), "UTF-8");
236 scan
.useDelimiter("\\n");
237 boolean inChap
= false;
238 while (scan
.hasNext()) {
239 String line
= scan
.next();
240 if (detectChapter(line
, number
) != null) {
242 } else if (inChap
&& detectChapter(line
, number
+ 1) != null) {
245 builder
.append(line
);
246 builder
.append("\n");
250 return builder
.toString();
254 protected void close() {
255 InputStream in
= getInput();
259 } catch (IOException e
) {
260 Instance
.getTraceHandler().error(
262 "Cannot close the text source file input", e
));
270 protected boolean supports(URL url
) {
271 return supports(url
, false);
275 * Check if we supports this {@link URL}, that is, if the info file can be
276 * found OR not found.
279 * the {@link URL} to check
281 * TRUE to require the info file, FALSE to forbid the info file
283 * @return TRUE if it is supported
285 protected boolean supports(URL url
, boolean info
) {
286 boolean infoPresent
= false;
287 if ("file".equals(url
.getProtocol())) {
290 file
= new File(url
.toURI());
291 file
= assureNoTxt(file
);
292 file
= new File(file
.getPath() + ".info");
293 } catch (URISyntaxException e
) {
294 Instance
.getTraceHandler().error(e
);
298 infoPresent
= (file
!= null && file
.exists());
301 return infoPresent
== info
;
305 * Remove the ".txt" extension if it is present.
308 * the file to process
310 * @return the same file or a copy of it without the ".txt" extension if it
313 protected File
assureNoTxt(File file
) {
314 if (file
.getName().endsWith(".txt")) {
315 file
= new File(file
.getPath().substring(0,
316 file
.getPath().length() - 4));
323 * Check if the given line looks like the given starting chapter in a
324 * supported language, and return the language if it does (or NULL if not).
329 * @return the language or NULL
331 static private String
detectChapter(String line
, int number
) {
332 line
= line
.toUpperCase();
333 for (String lang
: Instance
.getConfig().getList(Config
.CONF_CHAPTER
)) {
334 String chapter
= Instance
.getConfig().getStringX(Config
.CONF_CHAPTER
,
336 if (chapter
!= null && !chapter
.isEmpty()) {
337 chapter
= chapter
.toUpperCase() + " ";
338 if (line
.startsWith(chapter
)) {
339 // We want "[CHAPTER] [number]: [name]", with ": [name]"
341 String test
= line
.substring(chapter
.length()).trim();
342 if (test
.startsWith(Integer
.toString(number
))) {
344 .substring(Integer
.toString(number
).length())
346 if (test
.isEmpty() || test
.startsWith(":")) {