WIP: convert local "supports" to new BasicSupport
authorNiki Roo <niki@nikiroo.be>
Tue, 27 Mar 2018 11:27:23 +0000 (13:27 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 27 Mar 2018 11:27:23 +0000 (13:27 +0200)
src/be/nikiroo/fanfix/supported/BasicSupport.java
src/be/nikiroo/fanfix/supported/BasicSupportImages.java [new file with mode: 0644]
src/be/nikiroo/fanfix/supported/Cbz.java
src/be/nikiroo/fanfix/supported/Epub.java
src/be/nikiroo/fanfix/supported/InfoReader.java
src/be/nikiroo/fanfix/supported/InfoText.java
src/be/nikiroo/fanfix/supported/Text.java

index 4314b99ce3da1589eef307e7d76b851adeadc943..c35ed86b65b564d4e905c9c635ded5804bd038e4 100644 (file)
@@ -214,8 +214,8 @@ public abstract class BasicSupport {
        /**
         * Open an input link that will be used for the support.
         * <p>
-        * Can return NULL, in which case you are supposed to work without an
-        * {@link InputStream}.
+        * Can return NULL, in which case you are supposed to work without a source
+        * node.
         * 
         * @param source
         *            the source {@link URL}
@@ -237,7 +237,6 @@ public abstract class BasicSupport {
         * @throws IOException
         *             in case of I/O error
         */
-       @SuppressWarnings("unused")
        protected void login() throws IOException {
        }
 
@@ -247,7 +246,6 @@ public abstract class BasicSupport {
         * @throws IOException
         *             on I/O error
         */
-       @SuppressWarnings("unused")
        protected void preprocess() throws IOException {
        }
 
diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportImages.java b/src/be/nikiroo/fanfix/supported/BasicSupportImages.java
new file mode 100644 (file)
index 0000000..85b79c7
--- /dev/null
@@ -0,0 +1,161 @@
+package be.nikiroo.fanfix.supported;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import be.nikiroo.fanfix.Instance;
+import be.nikiroo.utils.Image;
+
+public class BasicSupportImages {
+       /**
+        * Check if the given resource can be a local image or a remote image, then
+        * refresh the cache with it if it is.
+        * 
+        * @param dir
+        *            the local directory to search, if any
+        * @param line
+        *            the resource to check
+        * 
+        * @return the image if found, or NULL
+        * 
+        */
+       static Image getImage(BasicSupport support, File dir, String line) {
+               URL url = getImageUrl(support, dir, line);
+               if (url != null) {
+                       if ("file".equals(url.getProtocol())) {
+                               if (new File(url.getPath()).isDirectory()) {
+                                       return null;
+                               }
+                       }
+                       InputStream in = null;
+                       try {
+                               in = Instance.getCache().open(url, support, true);
+                               return new Image(in);
+                       } catch (IOException e) {
+                       } finally {
+                               if (in != null) {
+                                       try {
+                                               in.close();
+                                       } catch (IOException e) {
+                                       }
+                               }
+                       }
+               }
+
+               return null;
+       }
+
+       /**
+        * Check if the given resource can be a local image or a remote image, then
+        * refresh the cache with it if it is.
+        * 
+        * @param dir
+        *            the local directory to search, if any
+        * @param line
+        *            the resource to check
+        * 
+        * @return the image URL if found, or NULL
+        * 
+        */
+       static URL getImageUrl(BasicSupport support, File dir, String line) {
+               URL url = null;
+
+               if (line != null) {
+                       // try for files
+                       if (dir != null && dir.exists() && !dir.isFile()) {
+                               try {
+
+                                       String relPath = null;
+                                       String absPath = null;
+                                       try {
+                                               relPath = new File(dir, line.trim()).getAbsolutePath();
+                                       } catch (Exception e) {
+                                               // Cannot be converted to path (one possibility to take
+                                               // into account: absolute path on Windows)
+                                       }
+                                       try {
+                                               absPath = new File(line.trim()).getAbsolutePath();
+                                       } catch (Exception e) {
+                                               // Cannot be converted to path (at all)
+                                       }
+
+                                       for (String ext : getImageExt(true)) {
+                                               File absFile = new File(absPath + ext);
+                                               File relFile = new File(relPath + ext);
+                                               if (absPath != null && absFile.exists()
+                                                               && absFile.isFile()) {
+                                                       url = absFile.toURI().toURL();
+                                               } else if (relPath != null && relFile.exists()
+                                                               && relFile.isFile()) {
+                                                       url = relFile.toURI().toURL();
+                                               }
+                                       }
+                               } catch (Exception e) {
+                                       // Should not happen since we control the correct arguments
+                               }
+                       }
+
+                       if (url == null) {
+                               // try for URLs
+                               try {
+                                       for (String ext : getImageExt(true)) {
+                                               if (Instance.getCache()
+                                                               .check(new URL(line + ext), true)) {
+                                                       url = new URL(line + ext);
+                                                       break;
+                                               }
+                                       }
+
+                                       // try out of cache
+                                       if (url == null) {
+                                               for (String ext : getImageExt(true)) {
+                                                       try {
+                                                               url = new URL(line + ext);
+                                                               Instance.getCache().refresh(url, support, true);
+                                                               break;
+                                                       } catch (IOException e) {
+                                                               // no image with this ext
+                                                               url = null;
+                                                       }
+                                               }
+                                       }
+                               } catch (MalformedURLException e) {
+                                       // Not an url
+                               }
+                       }
+
+                       // refresh the cached file
+                       if (url != null) {
+                               try {
+                                       Instance.getCache().refresh(url, support, true);
+                               } catch (IOException e) {
+                                       // woops, broken image
+                                       url = null;
+                               }
+                       }
+               }
+
+               return url;
+       }
+
+       /**
+        * Return the list of supported image extensions.
+        * 
+        * @param emptyAllowed
+        *            TRUE to allow an empty extension on first place, which can be
+        *            used when you may already have an extension in your input but
+        *            are not sure about it
+        * 
+        * @return the extensions
+        */
+       static String[] getImageExt(boolean emptyAllowed) {
+               if (emptyAllowed) {
+                       return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
+               }
+
+               return new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
+       }
+}
index f635a17867a57695f4db038cfb894cdd0ec45e49..ca0f48d2df6a0fc75d93b2bbf0fb705799420877 100644 (file)
@@ -13,6 +13,7 @@ import java.util.zip.ZipInputStream;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.Chapter;
+import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.utils.IOUtils;
@@ -57,7 +58,7 @@ class Cbz extends Epub {
        }
 
        @Override
-       public Story process(URL url, Progress pg) throws IOException {
+       public Story process(Progress pg) throws IOException {
                if (pg == null) {
                        pg = new Progress();
                } else {
@@ -66,7 +67,9 @@ class Cbz extends Epub {
 
                Progress pgMeta = new Progress();
                pg.addProgress(pgMeta, 10);
-               Story story = processMeta(url, false, true, pgMeta);
+               Story story = processMeta(true, pgMeta);
+               MetaData meta = story.getMeta();
+
                pgMeta.done(); // 10%
 
                File tmpDir = Instance.getTempFiles().createTempDir("info-text");
@@ -81,7 +84,7 @@ class Cbz extends Epub {
                                                && entry.getName().startsWith(getDataPrefix())) {
                                        String entryLName = entry.getName().toLowerCase();
                                        boolean imageEntry = false;
-                                       for (String ext : getImageExt(false)) {
+                                       for (String ext : BasicSupportImages.getImageExt(false)) {
                                                if (entryLName.endsWith(ext)) {
                                                        imageEntry = true;
                                                }
index 794998e0d75c24ed36288aec9dcb7c846ecd93fd..7ac3efc78703f7fb9e4a27fc2668c04c541965ad 100644 (file)
@@ -4,20 +4,20 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.ArrayList;
-import java.util.List;
-import java.util.Map.Entry;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
+import org.jsoup.nodes.Document;
+
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.MarkableFileInputStream;
-import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.StringUtils;
 
 /**
@@ -27,9 +27,10 @@ import be.nikiroo.utils.StringUtils;
  * @author niki
  */
 class Epub extends InfoText {
-       protected MetaData meta;
+       private File sourceFileOriginal;
+
+       private MetaData meta;
        private File tmpDir;
-       private File tmp;
        private String desc;
 
        private URL fakeSource;
@@ -40,166 +41,173 @@ class Epub extends InfoText {
                return "epub";
        }
 
-       @Override
-       protected boolean supports(URL url) {
-               if (url.getPath().toLowerCase().endsWith(".epub")) {
-                       return true;
-               }
-
-               return false;
+       public File getSourceFileOriginal() {
+               return sourceFileOriginal;
        }
 
        @Override
-       protected MetaData getMeta(URL source, InputStream in) throws IOException {
-               return meta;
-       }
-
-       @Override
-       protected String getDesc(URL source, InputStream in) throws IOException {
-               if (desc != null) {
-                       return desc;
-               }
-
-               if (fakeIn != null) {
-                       fakeIn.reset();
-                       return super.getDesc(fakeSource, fakeIn);
+       protected File getSourceFile() {
+               try {
+                       return new File(fakeSource.toURI());
+               } catch (URISyntaxException e) {
+                       Instance.getTraceHandler()
+                                       .error(new IOException(
+                                                       "Cannot get the source file from the info-text URL",
+                                                       e));
                }
 
                return null;
        }
 
        @Override
-       protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
-                       Progress pg) throws IOException {
-               if (fakeIn != null) {
-                       fakeIn.reset();
-                       return super.getChapters(fakeSource, fakeIn, pg);
-               }
-
-               return null;
+       protected InputStream getInput() {
+               return fakeIn;
        }
 
        @Override
-       protected String getChapterContent(URL source, InputStream in, int number,
-                       Progress pg) throws IOException {
-               if (fakeIn != null) {
-                       fakeIn.reset();
-                       return super.getChapterContent(fakeSource, fakeIn, number, pg);
-               }
+       protected boolean supports(URL url) {
+               return url.getPath().toLowerCase().endsWith(".epub");
+       }
 
-               return null;
+       @Override
+       protected MetaData getMeta() throws IOException {
+               return meta;
        }
 
        @Override
-       protected void preprocess(URL source, InputStream in) throws IOException {
-               // Note: do NOT close this stream, as it would also close "in"
-               ZipInputStream zipIn = new ZipInputStream(in);
-               tmpDir = Instance.getTempFiles().createTempDir("fanfic-reader-parser");
-               tmp = new File(tmpDir, "file.txt");
-               File tmpInfo = new File(tmpDir, "file.info");
-               fakeSource = tmp.toURI().toURL();
-               Image cover = null;
-
-               String url = source.toString();
-               String title = null;
-               String author = null;
-
-               for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
-                               .getNextEntry()) {
-                       if (!entry.isDirectory()
-                                       && entry.getName().startsWith(getDataPrefix())) {
-                               String entryLName = entry.getName().toLowerCase();
-
-                               boolean imageEntry = false;
-                               for (String ext : getImageExt(false)) {
-                                       if (entryLName.endsWith(ext)) {
-                                               imageEntry = true;
+       protected Document loadDocument(URL source) throws IOException {
+               super.loadDocument(source); // prepares super.getSourceFile() and
+                                                                       // super.getInput()
+
+               InputStream in = super.getInput();
+               ZipInputStream zipIn = null;
+               try {
+                       zipIn = new ZipInputStream(in);
+                       tmpDir = Instance.getTempFiles().createTempDir(
+                                       "fanfic-reader-parser");
+                       File tmp = new File(tmpDir, "file.txt");
+                       File tmpInfo = new File(tmpDir, "file.info");
+
+                       fakeSource = tmp.toURI().toURL();
+                       Image cover = null;
+
+                       String url;
+                       try {
+                               url = getSource().toURI().toURL().toString();
+                       } catch (URISyntaxException e1) {
+                               url = getSource().toString();
+                       }
+                       String title = null;
+                       String author = null;
+
+                       for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn
+                                       .getNextEntry()) {
+                               if (!entry.isDirectory()
+                                               && entry.getName().startsWith(getDataPrefix())) {
+                                       String entryLName = entry.getName().toLowerCase();
+
+                                       boolean imageEntry = false;
+                                       for (String ext : BasicSupportImages.getImageExt(false)) {
+                                               if (entryLName.endsWith(ext)) {
+                                                       imageEntry = true;
+                                               }
                                        }
-                               }
 
-                               if (entry.getName().equals(getDataPrefix() + "version")) {
-                                       // Nothing to do for now ("first"
-                                       // version is 3.0)
-                               } else if (entryLName.endsWith(".info")) {
-                                       // Info file
-                                       IOUtils.write(zipIn, tmpInfo);
-                               } else if (imageEntry) {
-                                       // Cover
-                                       if (getCover()) {
-                                               try {
-                                                       cover = new Image(zipIn);
-                                               } catch (Exception e) {
-                                                       Instance.getTraceHandler().error(e);
+                                       if (entry.getName().equals(getDataPrefix() + "version")) {
+                                               // Nothing to do for now ("first"
+                                               // version is 3.0)
+                                       } else if (entryLName.endsWith(".info")) {
+                                               // Info file
+                                               IOUtils.write(zipIn, tmpInfo);
+                                       } else if (imageEntry) {
+                                               // Cover
+                                               if (getCover()) {
+                                                       try {
+                                                               cover = new Image(zipIn);
+                                                       } catch (Exception e) {
+                                                               Instance.getTraceHandler().error(e);
+                                                       }
                                                }
-                                       }
-                               } else if (entry.getName().equals(getDataPrefix() + "URL")) {
-                                       String[] descArray = StringUtils
-                                                       .unhtml(IOUtils.readSmallStream(zipIn)).trim()
-                                                       .split("\n");
-                                       if (descArray.length > 0) {
-                                               url = descArray[0].trim();
-                                       }
-                               } else if (entry.getName().equals(getDataPrefix() + "SUMMARY")) {
-                                       String[] descArray = StringUtils
-                                                       .unhtml(IOUtils.readSmallStream(zipIn)).trim()
-                                                       .split("\n");
-                                       int skip = 0;
-                                       if (descArray.length > 1) {
-                                               title = descArray[0].trim();
-                                               skip = 1;
-                                               if (descArray.length > 2
-                                                               && descArray[1].startsWith("©")) {
-                                                       author = descArray[1].substring(1).trim();
-                                                       skip = 2;
+                                       } else if (entry.getName().equals(getDataPrefix() + "URL")) {
+                                               String[] descArray = StringUtils
+                                                               .unhtml(IOUtils.readSmallStream(zipIn)).trim()
+                                                               .split("\n");
+                                               if (descArray.length > 0) {
+                                                       url = descArray[0].trim();
+                                               }
+                                       } else if (entry.getName().equals(
+                                                       getDataPrefix() + "SUMMARY")) {
+                                               String[] descArray = StringUtils
+                                                               .unhtml(IOUtils.readSmallStream(zipIn)).trim()
+                                                               .split("\n");
+                                               int skip = 0;
+                                               if (descArray.length > 1) {
+                                                       title = descArray[0].trim();
+                                                       skip = 1;
+                                                       if (descArray.length > 2
+                                                                       && descArray[1].startsWith("©")) {
+                                                               author = descArray[1].substring(1).trim();
+                                                               skip = 2;
+                                                       }
+                                               }
+                                               this.desc = "";
+                                               for (int i = skip; i < descArray.length; i++) {
+                                                       this.desc += descArray[i].trim() + "\n";
                                                }
-                                       }
-                                       this.desc = "";
-                                       for (int i = skip; i < descArray.length; i++) {
-                                               this.desc += descArray[i].trim() + "\n";
-                                       }
 
-                                       this.desc = this.desc.trim();
-                               } else {
-                                       // Hopefully the data file
-                                       IOUtils.write(zipIn, tmp);
+                                               this.desc = this.desc.trim();
+                                       } else {
+                                               // Hopefully the data file
+                                               IOUtils.write(zipIn, tmp);
+                                       }
                                }
                        }
-               }
-
-               if (requireInfo() && (!tmp.exists() || !tmpInfo.exists())) {
-                       throw new IOException(
-                                       "file not supported (maybe not created with this program or corrupt)");
-               }
 
-               if (tmp.exists()) {
-                       this.fakeIn = new MarkableFileInputStream(new FileInputStream(tmp));
-               }
+                       if (requireInfo() && (!tmp.exists() || !tmpInfo.exists())) {
+                               throw new IOException(
+                                               "file not supported (maybe not created with this program or corrupt)");
+                       }
 
-               if (tmpInfo.exists()) {
-                       meta = InfoReader.readMeta(tmpInfo, true);
-                       if (cover != null) {
-                               meta.setCover(cover);
+                       if (tmp.exists()) {
+                               this.fakeIn = new MarkableFileInputStream(new FileInputStream(
+                                               tmp));
                        }
-                       tmpInfo.delete();
-               } else {
-                       if (title == null || title.isEmpty()) {
-                               title = new File(source.getPath()).getName();
-                               if (title.toLowerCase().endsWith(".cbz")) {
-                                       title = title.substring(0, title.length() - 4);
+
+                       if (tmpInfo.exists()) {
+                               meta = InfoReader.readMeta(tmpInfo, true);
+                               if (cover != null) {
+                                       meta.setCover(cover);
+                               }
+                               tmpInfo.delete();
+                       } else {
+                               if (title == null || title.isEmpty()) {
+                                       title = getSourceFileOriginal().getName();
+                                       if (title.toLowerCase().endsWith(".cbz")) {
+                                               title = title.substring(0, title.length() - 4);
+                                       }
+                                       title = URLDecoder.decode(title, "UTF-8").trim();
                                }
-                               title = URLDecoder.decode(title, "UTF-8").trim();
-                       }
 
-                       meta = new MetaData();
-                       meta.setLang("en");
-                       meta.setTags(new ArrayList<String>());
-                       meta.setSource(getSourceName());
-                       meta.setUuid(url);
-                       meta.setUrl(url);
-                       meta.setTitle(title);
-                       meta.setAuthor(author);
-                       meta.setImageDocument(isImagesDocumentByDefault());
+                               meta = new MetaData();
+                               meta.setLang("en");
+                               meta.setTags(new ArrayList<String>());
+                               meta.setSource(getSourceName());
+                               meta.setUuid(url);
+                               meta.setUrl(url);
+                               meta.setTitle(title);
+                               meta.setAuthor(author);
+                               meta.setImageDocument(isImagesDocumentByDefault());
+                       }
+               } finally {
+                       if (zipIn != null) {
+                               zipIn.close();
+                       }
+                       if (in != null) {
+                               in.close();
+                       }
                }
+
+               return null;
        }
 
        @Override
@@ -209,15 +217,6 @@ class Epub extends InfoText {
                }
 
                tmpDir = null;
-               tmp = null;
-
-               if (fakeIn != null) {
-                       try {
-                               fakeIn.close();
-                       } catch (Exception e) {
-                               Instance.getTraceHandler().error(e);
-                       }
-               }
 
                super.close();
        }
index 5203cc86107017c6fa3b3a236fe56f869def7c8c..8e1c385cfa8d115fd9fd6035be77eac10c580611 100644 (file)
@@ -8,6 +8,7 @@ import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Scanner;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
@@ -29,7 +30,6 @@ public class InfoReader {
                                return createMeta(infoFile.toURI().toURL(), in, withCover);
                        } finally {
                                in.close();
-                               in = null;
                        }
                }
 
@@ -138,7 +138,7 @@ public class InfoReader {
 
                if (in != null) {
                        in.reset();
-                       String value = BasicSupport_Deprecated.getLine(in, key, 0);
+                       String value = getLine(in, key, 0);
                        if (value != null && !value.isEmpty()) {
                                value = value.trim().substring(key.length() - 1).trim();
                                if (value.startsWith("'") && value.endsWith("'")
@@ -152,4 +152,81 @@ public class InfoReader {
 
                return null;
        }
+
+       /**
+        * Return the first line from the given input which correspond to the given
+        * selectors.
+        * 
+        * @param in
+        *            the input
+        * @param needle
+        *            a string that must be found inside the target line (also
+        *            supports "^" at start to say "only if it starts with" the
+        *            needle)
+        * @param relativeLine
+        *            the line to return based upon the target line position (-1 =
+        *            the line before, 0 = the target line...)
+        * 
+        * @return the line
+        */
+       static private String getLine(InputStream in, String needle,
+                       int relativeLine) {
+               return getLine(in, needle, relativeLine, true);
+       }
+
+       /**
+        * Return a line from the given input which correspond to the given
+        * selectors.
+        * 
+        * @param in
+        *            the input
+        * @param needle
+        *            a string that must be found inside the target line (also
+        *            supports "^" at start to say "only if it starts with" the
+        *            needle)
+        * @param relativeLine
+        *            the line to return based upon the target line position (-1 =
+        *            the line before, 0 = the target line...)
+        * @param first
+        *            takes the first result (as opposed to the last one, which will
+        *            also always spend the input)
+        * 
+        * @return the line
+        */
+       static private String getLine(InputStream in, String needle,
+                       int relativeLine, boolean first) {
+               String rep = null;
+
+               List<String> lines = new ArrayList<String>();
+               @SuppressWarnings("resource")
+               Scanner scan = new Scanner(in, "UTF-8");
+               int index = -1;
+               scan.useDelimiter("\\n");
+               while (scan.hasNext()) {
+                       lines.add(scan.next());
+
+                       if (index == -1) {
+                               if (needle.startsWith("^")) {
+                                       if (lines.get(lines.size() - 1).startsWith(
+                                                       needle.substring(1))) {
+                                               index = lines.size() - 1;
+                                       }
+
+                               } else {
+                                       if (lines.get(lines.size() - 1).contains(needle)) {
+                                               index = lines.size() - 1;
+                                       }
+                               }
+                       }
+
+                       if (index >= 0 && index + relativeLine < lines.size()) {
+                               rep = lines.get(index + relativeLine);
+                               if (first) {
+                                       break;
+                               }
+                       }
+               }
+
+               return rep;
+       }
 }
index 786e771a40c2f8c6cee016c7f0aeb14c2bd365f8..37f447aeb721a970fe14e6933a73dd396b1823d4 100644 (file)
@@ -2,8 +2,6 @@ package be.nikiroo.fanfix.supported;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.URISyntaxException;
 import java.net.URL;
 
 import be.nikiroo.fanfix.data.MetaData;
@@ -23,40 +21,36 @@ class InfoText extends Text {
                return "info-text";
        }
 
-       @Override
-       protected MetaData getMeta(URL source, InputStream in) throws IOException {
-               try {
-                       File sourceFile = new File(source.toURI());
-                       sourceFile = assureNoTxt(sourceFile);
-
-                       MetaData meta = InfoReader.readMeta(new File(sourceFile.getPath()
-                                       + ".info"), true);
+       protected File getInfoFile() {
+               return new File(assureNoTxt(getSourceFile()).getPath() + ".info");
+       }
 
-                       // Some old .info files don't have those now required fields...
-                       String test = meta.getTitle() == null ? "" : meta.getTitle();
-                       test += meta.getAuthor() == null ? "" : meta.getAuthor();
-                       test += meta.getDate() == null ? "" : meta.getDate();
-                       test += meta.getUrl() == null ? "" : meta.getUrl();
-                       if (test.isEmpty()) {
-                               MetaData superMeta = super.getMeta(source, reset(in));
-                               if (meta.getTitle() == null || meta.getTitle().isEmpty()) {
-                                       meta.setTitle(superMeta.getTitle());
-                               }
-                               if (meta.getAuthor() == null || meta.getAuthor().isEmpty()) {
-                                       meta.setAuthor(superMeta.getAuthor());
-                               }
-                               if (meta.getDate() == null || meta.getDate().isEmpty()) {
-                                       meta.setDate(superMeta.getDate());
-                               }
-                               if (meta.getUrl() == null || meta.getUrl().isEmpty()) {
-                                       meta.setUrl(superMeta.getUrl());
-                               }
+       @Override
+       protected MetaData getMeta() throws IOException {
+               MetaData meta = InfoReader.readMeta(getInfoFile(), true);
+
+               // Some old .info files don't have those now required fields...
+               String test = meta.getTitle() == null ? "" : meta.getTitle();
+               test += meta.getAuthor() == null ? "" : meta.getAuthor();
+               test += meta.getDate() == null ? "" : meta.getDate();
+               test += meta.getUrl() == null ? "" : meta.getUrl();
+               if (test.isEmpty()) {
+                       MetaData superMeta = super.getMeta();
+                       if (meta.getTitle() == null || meta.getTitle().isEmpty()) {
+                               meta.setTitle(superMeta.getTitle());
+                       }
+                       if (meta.getAuthor() == null || meta.getAuthor().isEmpty()) {
+                               meta.setAuthor(superMeta.getAuthor());
+                       }
+                       if (meta.getDate() == null || meta.getDate().isEmpty()) {
+                               meta.setDate(superMeta.getDate());
+                       }
+                       if (meta.getUrl() == null || meta.getUrl().isEmpty()) {
+                               meta.setUrl(superMeta.getUrl());
                        }
-
-                       return meta;
-               } catch (URISyntaxException e) {
-                       throw new IOException("Cannot parse URL to file: " + source, e);
                }
+
+               return meta;
        }
 
        @Override
index a6105947faab29f348f158edb32d008bc2301b5b..f6803cd09d6358880290cdf8e2c487caf5794e16 100644 (file)
@@ -1,19 +1,24 @@
 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;
 import java.net.URL;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Scanner;
 
+import org.jsoup.nodes.Document;
+
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.Config;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.utils.Image;
+import be.nikiroo.utils.MarkableFileInputStream;
 import be.nikiroo.utils.Progress;
 
 /**
@@ -33,7 +38,29 @@ import be.nikiroo.utils.Progress;
  * 
  * @author niki
  */
-class Text extends BasicSupport_Deprecated {
+class Text extends BasicSupport {
+       private File sourceFile;
+       private InputStream in;
+
+       protected File getSourceFile() {
+               return sourceFile;
+       }
+
+       protected InputStream getInput() {
+               if (in != null) {
+                       try {
+                               in.reset();
+                       } catch (IOException e) {
+                               Instance.getTraceHandler().error(
+                                               new IOException("Cannot reset the Text stream", e));
+                       }
+
+                       return in;
+               }
+
+               return null;
+       }
+
        @Override
        protected boolean isHtml() {
                return false;
@@ -45,41 +72,42 @@ class Text extends BasicSupport_Deprecated {
        }
 
        @Override
-       protected MetaData getMeta(URL source, InputStream in) throws IOException {
+       protected Document loadDocument(URL source) throws IOException {
+               try {
+                       sourceFile = new File(source.toURI());
+                       in = new MarkableFileInputStream(new FileInputStream(sourceFile));
+               } catch (URISyntaxException e) {
+                       throw new IOException("Cannot load the text document: " + source);
+               }
+
+               return null;
+       }
+
+       @Override
+       protected MetaData getMeta() throws IOException {
                MetaData meta = new MetaData();
 
-               meta.setTitle(getTitle(reset(in)));
-               meta.setAuthor(getAuthor(reset(in)));
-               meta.setDate(getDate(reset(in)));
+               meta.setTitle(getTitle());
+               meta.setAuthor(getAuthor());
+               meta.setDate(getDate());
                meta.setTags(new ArrayList<String>());
                meta.setSource(getSourceName());
-               meta.setUrl(source.toString());
+               meta.setUrl(getSourceFile().toURI().toURL().toString());
                meta.setPublisher("");
-               meta.setUuid(source.toString());
+               meta.setUuid(getSourceFile().toString());
                meta.setLuid("");
-               meta.setLang(getLang(reset(in))); // default is EN
-               meta.setSubject(getSubject(source));
+               meta.setLang(getLang()); // default is EN
+               meta.setSubject(getSourceFile().getParentFile().getName());
                meta.setType(getType().toString());
                meta.setImageDocument(false);
-               meta.setCover(getCover(source));
+               meta.setCover(getCover(getSourceFile()));
 
                return meta;
        }
 
-       private String getSubject(URL source) throws IOException {
-               try {
-                       File file = new File(source.toURI());
-                       return file.getParentFile().getName();
-               } catch (URISyntaxException e) {
-                       throw new IOException("Cannot parse the URL to File: "
-                                       + source.toString(), e);
-               }
-
-       }
-
-       private String getLang(InputStream in) {
+       private String getLang() {
                @SuppressWarnings("resource")
-               Scanner scan = new Scanner(in, "UTF-8");
+               Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                scan.next(); // Title
                scan.next(); // Author (Date)
@@ -103,16 +131,16 @@ class Text extends BasicSupport_Deprecated {
                return lang;
        }
 
-       private String getTitle(InputStream in) {
+       private String getTitle() {
                @SuppressWarnings("resource")
-               Scanner scan = new Scanner(in, "UTF-8");
+               Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                return scan.next();
        }
 
-       private String getAuthor(InputStream in) {
+       private String getAuthor() {
                @SuppressWarnings("resource")
-               Scanner scan = new Scanner(in, "UTF-8");
+               Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                scan.next();
                String authorDate = scan.next();
@@ -126,9 +154,9 @@ class Text extends BasicSupport_Deprecated {
                return BasicSupportHelper.fixAuthor(author);
        }
 
-       private String getDate(InputStream in) {
+       private String getDate() {
                @SuppressWarnings("resource")
-               Scanner scan = new Scanner(in, "UTF-8");
+               Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                scan.next();
                String authorDate = scan.next();
@@ -147,18 +175,12 @@ class Text extends BasicSupport_Deprecated {
        }
 
        @Override
-       protected String getDesc(URL source, InputStream in) throws IOException {
-               return getChapterContent(source, in, 0, null);
+       protected String getDesc() throws IOException {
+               return getChapterContent(null, 0, null);
        }
 
-       private Image getCover(URL source) {
-               String path;
-               try {
-                       path = new File(source.toURI()).getPath();
-               } catch (URISyntaxException e) {
-                       Instance.getTraceHandler().error(e);
-                       path = null;
-               }
+       private Image getCover(File sourceFile) {
+               String path = sourceFile.getName();
 
                for (String ext : new String[] { ".txt", ".text", ".story" }) {
                        if (path.endsWith(ext)) {
@@ -166,15 +188,16 @@ class Text extends BasicSupport_Deprecated {
                        }
                }
 
-               return getImage(this, source, path);
+               return BasicSupportImages.getImage(this, sourceFile.getParentFile(),
+                               path);
        }
 
        @Override
-       protected List<Entry<String, URL>> getChapters(URL source, InputStream in,
-                       Progress pg) throws IOException {
+       protected List<Entry<String, URL>> getChapters(Progress pg)
+                       throws IOException {
                List<Entry<String, URL>> chaps = new ArrayList<Entry<String, URL>>();
                @SuppressWarnings("resource")
-               Scanner scan = new Scanner(in, "UTF-8");
+               Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                boolean prevLineEmpty = false;
                while (scan.hasNext()) {
@@ -185,24 +208,10 @@ class Text extends BasicSupport_Deprecated {
                                if (pos >= 0 && pos + 1 < line.length()) {
                                        chapName = line.substring(pos + 1).trim();
                                }
-                               final URL value = source;
-                               final String key = chapName;
-                               chaps.add(new Entry<String, URL>() {
-                                       @Override
-                                       public URL setValue(URL value) {
-                                               return null;
-                                       }
 
-                                       @Override
-                                       public URL getValue() {
-                                               return value;
-                                       }
-
-                                       @Override
-                                       public String getKey() {
-                                               return key;
-                                       }
-                               });
+                               chaps.add(new AbstractMap.SimpleEntry<String, URL>(//
+                                               chapName, //
+                                               getSourceFile().toURI().toURL()));
                        }
 
                        prevLineEmpty = line.trim().isEmpty();
@@ -212,11 +221,11 @@ class Text extends BasicSupport_Deprecated {
        }
 
        @Override
-       protected String getChapterContent(URL source, InputStream in, int number,
-                       Progress pg) throws IOException {
+       protected String getChapterContent(URL source, int number, Progress pg)
+                       throws IOException {
                StringBuilder builder = new StringBuilder();
                @SuppressWarnings("resource")
-               Scanner scan = new Scanner(in, "UTF-8");
+               Scanner scan = new Scanner(getInput(), "UTF-8");
                scan.useDelimiter("\\n");
                boolean inChap = false;
                while (scan.hasNext()) {
@@ -234,6 +243,22 @@ class Text extends BasicSupport_Deprecated {
                return builder.toString();
        }
 
+       @Override
+       protected void close() {
+               InputStream in = getInput();
+               if (in != null) {
+                       try {
+                               in.close();
+                       } catch (IOException e) {
+                               Instance.getTraceHandler().error(
+                                               new IOException(
+                                                               "Cannot close the text source file input", e));
+                       }
+               }
+
+               super.close();
+       }
+
        @Override
        protected boolean supports(URL url) {
                return supports(url, false);
@@ -296,7 +321,7 @@ class Text extends BasicSupport_Deprecated {
         * 
         * @return the language or NULL
         */
-       private String detectChapter(String line, int number) {
+       static private String detectChapter(String line, int number) {
                line = line.toUpperCase();
                for (String lang : Instance.getConfig().getString(Config.CHAPTER)
                                .split(",")) {