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