Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / supported / Text.java
index cba23bf8c2f1815d2eddba3ff611ed1951b9e4b6..c54b6a5d44ce0a328d63451420c6a9c00a82bd3c 100644 (file)
@@ -1,7 +1,6 @@
 package be.nikiroo.fanfix.supported;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URISyntaxException;
@@ -16,11 +15,13 @@ import org.jsoup.nodes.Document;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
+import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
+import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.ImageUtils;
-import be.nikiroo.utils.MarkableFileInputStream;
 import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.streams.MarkableFileInputStream;
 
 /**
  * Support class for local stories encoded in textual format, with a few rules:
@@ -52,8 +53,7 @@ class Text extends BasicSupport {
                        try {
                                in.reset();
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(
-                                               new IOException("Cannot reset the Text stream", e));
+                               Instance.getInstance().getTraceHandler().error(new IOException("Cannot reset the Text stream", e));
                        }
 
                        return in;
@@ -67,16 +67,11 @@ class Text extends BasicSupport {
                return false;
        }
 
-       @Override
-       public String getSourceName() {
-               return "text";
-       }
-
        @Override
        protected Document loadDocument(URL source) throws IOException {
                try {
                        sourceFile = new File(source.toURI());
-                       in = new MarkableFileInputStream(new FileInputStream(sourceFile));
+                       in = new MarkableFileInputStream(sourceFile);
                } catch (URISyntaxException e) {
                        throw new IOException("Cannot load the text document: " + source);
                }
@@ -92,7 +87,7 @@ class Text extends BasicSupport {
                meta.setAuthor(getAuthor());
                meta.setDate(getDate());
                meta.setTags(new ArrayList<String>());
-               meta.setSource(getSourceName());
+               meta.setSource(getType().getSourceName());
                meta.setUrl(getSourceFile().toURI().toURL().toString());
                meta.setPublisher("");
                meta.setUuid(getSourceFile().toString());
@@ -152,7 +147,7 @@ class Text extends BasicSupport {
                        author = authorDate.substring(0, pos);
                }
 
-               return BasicSupportHelper.fixAuthor(author);
+               return bsHelper.fixAuthor(author);
        }
 
        private String getDate() {
@@ -177,7 +172,20 @@ class Text extends BasicSupport {
 
        @Override
        protected String getDesc() throws IOException {
-               return getChapterContent(null, 0, null);
+               String content = getChapterContent(null, 0, null).trim();
+               if (!content.isEmpty()) {
+                       Chapter desc = bsPara.makeChapter(this, null, 0, "Description",
+                                       content, isHtml(), null);
+                       StringBuilder builder = new StringBuilder();
+                       for (Paragraph para : desc) {
+                               if (builder.length() > 0) {
+                                       builder.append("\n");
+                               }
+                               builder.append(para.getContent());
+                       }
+               }
+
+               return content;
        }
 
        private Image getCover(File sourceFile) {
@@ -189,12 +197,10 @@ class Text extends BasicSupport {
                        }
                }
 
-               Image cover = BasicSupportImages.getImage(this,
-                               sourceFile.getParentFile(), path);
+               Image cover = bsImages.getImage(this, sourceFile.getParentFile(), path);
                if (cover != null) {
                        try {
-                               File tmp = Instance.getTempFiles().createTempFile(
-                                               "test_cover_image");
+                               File tmp = Instance.getInstance().getTempFiles().createTempFile("test_cover_image");
                                ImageUtils.getInstance().saveAsImage(cover, tmp, "png");
                                tmp.delete();
                        } catch (IOException e) {
@@ -243,9 +249,9 @@ class Text extends BasicSupport {
                boolean inChap = false;
                while (scan.hasNext()) {
                        String line = scan.next();
-                       if (detectChapter(line, number) != null) {
+                       if (!inChap && detectChapter(line, number) != null) {
                                inChap = true;
-                       } else if (inChap && detectChapter(line, number + 1) != null) {
+                       } else if (detectChapter(line, number + 1) != null) {
                                break;
                        } else if (inChap) {
                                builder.append(line);
@@ -263,9 +269,8 @@ class Text extends BasicSupport {
                        try {
                                in.close();
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(
-                                               new IOException(
-                                                               "Cannot close the text source file input", e));
+                               Instance.getInstance().getTraceHandler()
+                                               .error(new IOException("Cannot close the text source file input", e));
                        }
                }
 
@@ -280,6 +285,8 @@ class Text extends BasicSupport {
        /**
         * Check if we supports this {@link URL}, that is, if the info file can be
         * found OR not found.
+        * <p>
+        * It must also be a file, not another kind of URL.
         * 
         * @param url
         *            the {@link URL} to check
@@ -289,21 +296,23 @@ class Text extends BasicSupport {
         * @return TRUE if it is supported
         */
        protected boolean supports(URL url, boolean info) {
-               boolean infoPresent = false;
-               if ("file".equals(url.getProtocol())) {
-                       File file;
-                       try {
-                               file = new File(url.toURI());
-                               file = assureNoTxt(file);
-                               file = new File(file.getPath() + ".info");
-                       } catch (URISyntaxException e) {
-                               Instance.getTraceHandler().error(e);
-                               file = null;
-                       }
+               if (!"file".equals(url.getProtocol())) {
+                       return false;
+               }
 
-                       infoPresent = (file != null && file.exists());
+               boolean infoPresent = false;
+               File file;
+               try {
+                       file = new File(url.toURI());
+                       file = assureNoTxt(file);
+                       file = new File(file.getPath() + ".info");
+               } catch (URISyntaxException e) {
+                       Instance.getInstance().getTraceHandler().error(e);
+                       file = null;
                }
 
+               infoPresent = (file != null && file.exists());
+
                return infoPresent == info;
        }
 
@@ -331,21 +340,28 @@ class Text extends BasicSupport {
         * 
         * @param line
         *            the line to check
+        * @param number
+        *            the specific chapter number to check for
         * 
         * @return the language or NULL
         */
        static private String detectChapter(String line, int number) {
                line = line.toUpperCase();
-               for (String lang : Instance.getConfig().getString(Config.CHAPTER)
-                               .split(",")) {
-                       String chapter = Instance.getConfig().getStringX(Config.CHAPTER,
-                                       lang);
+               for (String lang : Instance.getInstance().getConfig().getList(Config.CONF_CHAPTER)) {
+                       String chapter = Instance.getInstance().getConfig().getStringX(Config.CONF_CHAPTER, lang);
                        if (chapter != null && !chapter.isEmpty()) {
                                chapter = chapter.toUpperCase() + " ";
                                if (line.startsWith(chapter)) {
                                        // We want "[CHAPTER] [number]: [name]", with ": [name]"
                                        // optional
                                        String test = line.substring(chapter.length()).trim();
+
+                                       String possibleNum = test.trim();
+                                       if (possibleNum.indexOf(':') > 0) {
+                                               possibleNum = possibleNum.substring(0,
+                                                               possibleNum.indexOf(':')).trim();
+                                       }
+
                                        if (test.startsWith(Integer.toString(number))) {
                                                test = test
                                                                .substring(Integer.toString(number).length())