Merge branch 'master' into subtree
[nikiroo-utils.git] / supported / BasicSupport_Deprecated.java
index 1faac03eba405657f6861f53660f2150357a4387..9aac6d5c61b713a4641fc251796b981e48d3a2ec 100644 (file)
@@ -42,14 +42,10 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
        private InputStream in;
 
        // quote chars
-       private char openQuote = Instance.getTrans().getCharacter(
-                       StringId.OPEN_SINGLE_QUOTE);
-       private char closeQuote = Instance.getTrans().getCharacter(
-                       StringId.CLOSE_SINGLE_QUOTE);
-       private char openDoubleQuote = Instance.getTrans().getCharacter(
-                       StringId.OPEN_DOUBLE_QUOTE);
-       private char closeDoubleQuote = Instance.getTrans().getCharacter(
-                       StringId.CLOSE_DOUBLE_QUOTE);
+       private char openQuote = Instance.getInstance().getTrans().getCharacter(StringId.OPEN_SINGLE_QUOTE);
+       private char closeQuote = Instance.getInstance().getTrans().getCharacter(StringId.CLOSE_SINGLE_QUOTE);
+       private char openDoubleQuote = Instance.getInstance().getTrans().getCharacter(StringId.OPEN_DOUBLE_QUOTE);
+       private char closeDoubleQuote = Instance.getInstance().getTrans().getCharacter(StringId.CLOSE_DOUBLE_QUOTE);
 
        // New methods not used in Deprecated mode
        @Override
@@ -207,12 +203,19 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                        pg.setProgress(30);
 
                        Story story = new Story();
+                       
                        MetaData meta = getMeta(url, getInput());
+                       meta.setType(getType().toString());
+                       meta.setSource(getType().getSourceName());
+                       meta.setPublisher(getType().getSourceName());
+                       
                        if (meta.getCreationDate() == null
-                                       || meta.getCreationDate().isEmpty()) {
-                               meta.setCreationDate(StringUtils.fromTime(new Date().getTime()));
+                                       || meta.getCreationDate().trim().isEmpty()) {
+                               meta.setCreationDate(bsHelper.formatDate(
+                                               StringUtils.fromTime(new Date().getTime())));
                        }
                        story.setMeta(meta);
+                       pg.put("meta", meta);
 
                        pg.setProgress(50);
 
@@ -223,11 +226,8 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                        pg.setProgress(60);
 
                        if (getDesc) {
-                               String descChapterName = Instance.getTrans().getString(
-                                               StringId.DESCRIPTION);
-                               story.getMeta().setResume(
-                                               makeChapter(url, 0, descChapterName,
-                                                               getDesc(url, getInput()), null));
+                               String descChapterName = Instance.getInstance().getTrans().getString(StringId.DESCRIPTION);
+                               story.getMeta().setResume(makeChapter(url, 0, descChapterName, getDesc(url, getInput()), null));
                        }
 
                        pg.setProgress(100);
@@ -270,12 +270,11 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                        Progress pgMeta = new Progress();
                        pg.addProgress(pgMeta, 10);
                        Story story = processMeta(url, false, true, pgMeta);
+                       pg.put("meta", story.getMeta());
                        if (!pgMeta.isDone()) {
                                pgMeta.setProgress(pgMeta.getMax()); // 10%
                        }
 
-                       pg.setName("Retrieving " + story.getMeta().getTitle());
-
                        setCurrentReferer(url);
 
                        Progress pgGetChapters = new Progress();
@@ -299,8 +298,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                                        InputStream chapIn = null;
                                        if (chap.getValue() != null) {
                                                setCurrentReferer(chap.getValue());
-                                               chapIn = Instance.getCache().open(chap.getValue(),
-                                                               this, false);
+                                               chapIn = Instance.getInstance().getCache().open(chap.getValue(), this, false);
                                        }
                                        pgChaps.setProgress(i * 100);
                                        try {
@@ -324,7 +322,6 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
 
                                                words += cc.getWords();
                                                story.getChapters().add(cc);
-                                               story.getMeta().setWords(words);
                                        } finally {
                                                if (chapIn != null) {
                                                        chapIn.close();
@@ -333,14 +330,31 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
 
                                        i++;
                                }
+                               
+                               story.getMeta().setWords(words);
 
                                pgChaps.setName("Extracting chapters");
                        } else {
                                pg.setProgress(80);
                        }
 
-                       return story;
+                       // Check for "no chapters" stories
+                       if (story.getChapters().isEmpty()
+                                       && story.getMeta().getResume() != null
+                                       && !story.getMeta().getResume().getParagraphs().isEmpty()) {
+                               Chapter resume = story.getMeta().getResume();
+                               resume.setName("");
+                               resume.setNumber(1);
+                               story.getChapters().add(resume);
+                               story.getMeta().setWords(resume.getWords());
+
+                               String descChapterName = Instance.getInstance().getTrans()
+                                               .getString(StringId.DESCRIPTION);
+                               resume = new Chapter(0, descChapterName);
+                               story.getMeta().setResume(resume);
+                       }
 
+                       return story;
                } finally {
                        close();
 
@@ -380,7 +394,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param pg
         *            the optional progress reporter
         * 
-        * @return the {@link Chapter}
+        * @return the {@link Chapter}, never NULL
         * 
         * @throws IOException
         *             in case of I/O error
@@ -391,9 +405,8 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                // redundant "Chapter x: " in front of it, or "-" (as in
                // "Chapter 5: - Fun!" after the ": " was automatically added)
                String chapterName = processPara(name).getContent().trim();
-               for (String lang : Instance.getConfig().getList(Config.CONF_CHAPTER)) {
-                       String chapterWord = Instance.getConfig().getStringX(
-                                       Config.CONF_CHAPTER, lang);
+               for (String lang : Instance.getInstance().getConfig().getList(Config.CONF_CHAPTER)) {
+                       String chapterWord = Instance.getInstance().getConfig().getStringX(Config.CONF_CHAPTER, lang);
                        if (chapterName.startsWith(chapterWord)) {
                                chapterName = chapterName.substring(chapterWord.length())
                                                .trim();
@@ -437,7 +450,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param pg
         *            the optional progress reporter
         * 
-        * @return the {@link Paragraph}s
+        * @return the {@link Paragraph}s (can be empty, but never NULL)
         * 
         * @throws IOException
         *             in case of I/O error
@@ -455,7 +468,6 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                }
 
                List<Paragraph> paras = new ArrayList<Paragraph>();
-
                if (content != null && !content.trim().isEmpty()) {
                        if (isHtml()) {
                                String[] tab = content.split("(<p>|</p>|<br>|<br/>)");
@@ -520,7 +532,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param line
         *            the textual content of the paragraph
         * 
-        * @return the {@link Paragraph}
+        * @return the {@link Paragraph}, never NULL
         */
        private Paragraph makeParagraph(URL source, String line) {
                Image image = null;
@@ -591,10 +603,9 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @return the cover if any, or NULL
         */
        static Image getDefaultCover(String subject) {
-               if (subject != null && !subject.isEmpty()
-                               && Instance.getCoverDir() != null) {
+               if (subject != null && !subject.isEmpty() && Instance.getInstance().getCoverDir() != null) {
                        try {
-                               File fileCover = new File(Instance.getCoverDir(), subject);
+                               File fileCover = new File(Instance.getInstance().getCoverDir(), subject);
                                return getImage(null, fileCover.toURI().toURL(), subject);
                        } catch (MalformedURLException e) {
                        }
@@ -644,8 +655,14 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                        }
                        InputStream in = null;
                        try {
-                               in = Instance.getCache().open(url, getSupport(url), true);
-                               return new Image(in);
+                               in = Instance.getInstance().getCache().open(url, getSupport(url), true);
+                               Image img = new Image(in);
+                               if (img.getSize() == 0) {
+                                       img.close();
+                                       throw new IOException(
+                                                       "Empty image not accepted");
+                               }
+                               return img;
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
@@ -716,8 +733,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                                // try for URLs
                                try {
                                        for (String ext : getImageExt(true)) {
-                                               if (Instance.getCache()
-                                                               .check(new URL(line + ext), true)) {
+                                               if (Instance.getInstance().getCache().check(new URL(line + ext), true)) {
                                                        url = new URL(line + ext);
                                                        break;
                                                }
@@ -728,7 +744,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                                                for (String ext : getImageExt(true)) {
                                                        try {
                                                                url = new URL(line + ext);
-                                                               Instance.getCache().refresh(url, support, true);
+                                                               Instance.getInstance().getCache().refresh(url, support, true);
                                                                break;
                                                        } catch (IOException e) {
                                                                // no image with this ext
@@ -744,7 +760,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                        // refresh the cached file
                        if (url != null) {
                                try {
-                                       Instance.getCache().refresh(url, support, true);
+                                       Instance.getInstance().getCache().refresh(url, support, true);
                                } catch (IOException e) {
                                        // woops, broken image
                                        url = null;
@@ -770,7 +786,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         *             in case of I/O error
         */
        protected InputStream openInput(URL source) throws IOException {
-               return Instance.getCache().open(source, this, false);
+               return Instance.getInstance().getCache().open(source, this, false);
        }
 
        /**
@@ -872,7 +888,7 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
         * @param line
         *            the raw line
         * 
-        * @return the processed {@link Paragraph}
+        * @return the processed {@link Paragraph}, never NULL
         */
        protected Paragraph processPara(String line) {
                line = ifUnhtml(line).trim();