Prepare for LocalReader
authorNiki Roo <niki@nikiroo.be>
Sat, 11 Feb 2017 23:21:24 +0000 (00:21 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 11 Feb 2017 23:21:24 +0000 (00:21 +0100)
This will allow HTML or TEXT conversion first if configured

src/be/nikiroo/fanfix/Instance.java
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/bundles/Config.java
src/be/nikiroo/fanfix/bundles/config.properties
src/be/nikiroo/fanfix/reader/BasicReader.java [moved from src/be/nikiroo/fanfix/reader/FanfixReader.java with 57% similarity]
src/be/nikiroo/fanfix/reader/CliReader.java [moved from src/be/nikiroo/fanfix/reader/cli/CliReader.java with 84% similarity]

index 5c198ee73d9580f50773bfd3fcffe92c9751a3ab..94e854aa19536a77e405e3b3223a1958ffdedf82 100644 (file)
@@ -20,6 +20,7 @@ public class Instance {
        private static Library lib;
        private static boolean debug;
        private static File coverDir;
        private static Library lib;
        private static boolean debug;
        private static File coverDir;
+       private static File readerTmp;
 
        static {
                config = new ConfigBundle();
 
        static {
                config = new ConfigBundle();
@@ -29,6 +30,23 @@ public class Instance {
                lib = new Library(getFile(Config.LIBRARY_DIR));
                debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
                coverDir = getFile(Config.DEFAULT_COVERS_DIR);
                lib = new Library(getFile(Config.LIBRARY_DIR));
                debug = Instance.getConfig().getBoolean(Config.DEBUG_ERR, false);
                coverDir = getFile(Config.DEFAULT_COVERS_DIR);
+               File tmp = getFile(Config.CACHE_DIR);
+               readerTmp = getFile(Config.CACHE_DIR_LOCAL_READER);
+
+               if (tmp == null || readerTmp == null) {
+                       String tmpDir = System.getProperty("java.io.tmpdir");
+                       if (tmpDir != null) {
+                               if (tmp == null) {
+                                       tmp = new File(tmpDir, "fanfic-tmp");
+                               }
+                               if (readerTmp == null) {
+                                       tmp = new File(tmpDir, "fanfic-reader");
+                               }
+                       } else {
+                               syserr(new IOException(
+                                               "The system does not have a default temporary directory"));
+                       }
+               }
 
                if (coverDir != null && !coverDir.exists()) {
                        syserr(new IOException(
 
                if (coverDir != null && !coverDir.exists()) {
                        syserr(new IOException(
@@ -71,22 +89,12 @@ public class Instance {
                }
 
                try {
                }
 
                try {
-                       File tmp = getFile(Config.CACHE_DIR);
+
                        String ua = config.getString(Config.USER_AGENT);
                        int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
                        int hoursLarge = config
                                        .getInteger(Config.CACHE_MAX_TIME_STABLE, -1);
 
                        String ua = config.getString(Config.USER_AGENT);
                        int hours = config.getInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
                        int hoursLarge = config
                                        .getInteger(Config.CACHE_MAX_TIME_STABLE, -1);
 
-                       if (tmp == null) {
-                               String tmpDir = System.getProperty("java.io.tmpdir");
-                               if (tmpDir != null) {
-                                       tmp = new File(tmpDir, "fanfic-tmp");
-                               } else {
-                                       syserr(new IOException(
-                                                       "The system does not have a default temporary directory"));
-                               }
-                       }
-
                        cache = new Cache(tmp, ua, hours, hoursLarge);
                } catch (IOException e) {
                        syserr(new IOException(
                        cache = new Cache(tmp, ua, hours, hoursLarge);
                } catch (IOException e) {
                        syserr(new IOException(
@@ -139,6 +147,15 @@ public class Instance {
                return coverDir;
        }
 
                return coverDir;
        }
 
+       /**
+        * Return the directory where to store temporary files for the local reader.
+        * 
+        * @return the directory
+        */
+       public static File getReaderDir() {
+               return readerTmp;
+       }
+
        /**
         * Report an error to the user
         * 
        /**
         * Report an error to the user
         * 
index 3ecc612eb2f111f14cd39a5c7e853ed447fcd32c..30febd336d2d94f9d6e2c9257dd11726888213b8 100644 (file)
@@ -10,8 +10,7 @@ import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.fanfix.output.BasicOutput;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
-import be.nikiroo.fanfix.reader.FanfixReader;
-import be.nikiroo.fanfix.reader.cli.CliReader;
+import be.nikiroo.fanfix.reader.BasicReader;
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
 import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
@@ -192,7 +191,7 @@ public class Main {
                        return 1;
                }
 
                        return 1;
                }
 
-               new CliReader().start(type);
+               BasicReader.getReader().start(type);
 
                return 0;
        }
 
                return 0;
        }
@@ -214,7 +213,7 @@ public class Main {
         */
        private static int read(String story, String chap, boolean library) {
                try {
         */
        private static int read(String story, String chap, boolean library) {
                try {
-                       FanfixReader reader = new CliReader();
+                       BasicReader reader = BasicReader.getReader();
                        if (library) {
                                reader.setStory(story);
                        } else {
                        if (library) {
                                reader.setStory(story);
                        } else {
index 969eb271ad2c5a82687c643f60f49a7f86915035..7a663e403e626fe7a2568ef22b50cb0b72b582b4 100644 (file)
@@ -10,8 +10,12 @@ import be.nikiroo.utils.resources.Meta;
 public enum Config {
        @Meta(what = "language", where = "", format = "language (example: en-GB) or nothing for default system language", info = "Force the language (can be overwritten again with the env variable $LANG)")
        LANG, //
 public enum Config {
        @Meta(what = "language", where = "", format = "language (example: en-GB) or nothing for default system language", info = "Force the language (can be overwritten again with the env variable $LANG)")
        LANG, //
+       @Meta(what = "reader type", where = "", format = "CLI or LOCAL", info = "Select the reader to use to read stories (CLI = simple output to console, LOCAL = use local system file handler)")
+       READER_TYPE, //
        @Meta(what = "directory", where = "", format = "absolute path, $HOME variable supported, / is always accepted as dir separator", info = "The directory where to store temporary files, defaults to a directory 'fanfic-tmp' in the system default temporary directory")
        CACHE_DIR, //
        @Meta(what = "directory", where = "", format = "absolute path, $HOME variable supported, / is always accepted as dir separator", info = "The directory where to store temporary files, defaults to a directory 'fanfic-tmp' in the system default temporary directory")
        CACHE_DIR, //
+       @Meta(what = "directory", where = "", format = "absolute path, $HOME variable supported, / is always accepted as dir separator", info = "The directory where to store temporary files, defaults to a directory 'fanfic-reader' in the system default temporary directory")
+       CACHE_DIR_LOCAL_READER, //
        @Meta(what = "delay in hours", where = "", format = "integer | 0: no cache | -1: infinite time cache which is default", info = "The delay after which a cached resource that is thought to change ~often is considered too old and triggers a refresh")
        CACHE_MAX_TIME_CHANGING, //
        @Meta(what = "delay in hours", where = "", format = "integer | 0: no cache | -1: infinite time cache which is default", info = "The delay after which a cached resource that is thought to change rarely is considered too old and triggers a refresh")
        @Meta(what = "delay in hours", where = "", format = "integer | 0: no cache | -1: infinite time cache which is default", info = "The delay after which a cached resource that is thought to change ~often is considered too old and triggers a refresh")
        CACHE_MAX_TIME_CHANGING, //
        @Meta(what = "delay in hours", where = "", format = "integer | 0: no cache | -1: infinite time cache which is default", info = "The delay after which a cached resource that is thought to change rarely is considered too old and triggers a refresh")
index 8a1d6c1ef66a61fec2eea0c058a6323a64615ac0..79f2ac9c67701a5dc5c28b795eb9ac3d68d80903 100644 (file)
@@ -5,9 +5,15 @@
 # (WHAT: language, FORMAT: language (example: en-GB) or nothing for default system language)
 # Force the language (can be overwritten again with the env variable $LANG)
 LANG = 
 # (WHAT: language, FORMAT: language (example: en-GB) or nothing for default system language)
 # Force the language (can be overwritten again with the env variable $LANG)
 LANG = 
+# (WHAT: reader type, FORMAT: CLI or LOCAL)
+# Select the reader to use to read stories (CLI = simple output to console, LOCAL = use local system file handler)
+READER_TYPE = 
 # (WHAT: directory, FORMAT: absolute path, $HOME variable supported, / is always accepted as dir separator)
 # The directory where to store temporary files, defaults to a directory 'fanfic-tmp' in the system default temporary directory
 CACHE_DIR = 
 # (WHAT: directory, FORMAT: absolute path, $HOME variable supported, / is always accepted as dir separator)
 # The directory where to store temporary files, defaults to a directory 'fanfic-tmp' in the system default temporary directory
 CACHE_DIR = 
+# (WHAT: directory, FORMAT: absolute path, $HOME variable supported, / is always accepted as dir separator)
+# The directory where to store temporary files, defaults to a directory 'fanfic-reader' in the system default temporary directory
+CACHE_DIR_LOCAL_READER = 
 # (WHAT: delay in hours, FORMAT: integer | 0: no cache | -1: infinite time cache which is default)
 # The delay after which a cached resource that is thought to change ~often is considered too old and triggers a refresh
 CACHE_MAX_TIME_CHANGING = 24
 # (WHAT: delay in hours, FORMAT: integer | 0: no cache | -1: infinite time cache which is default)
 # The delay after which a cached resource that is thought to change ~often is considered too old and triggers a refresh
 CACHE_MAX_TIME_CHANGING = 24
similarity index 57%
rename from src/be/nikiroo/fanfix/reader/FanfixReader.java
rename to src/be/nikiroo/fanfix/reader/BasicReader.java
index 009271e594cc51bcd6f47d357386465faa739621..eeb00af1c6ff2f5439af6c4bdfbe9bd8669142e4 100644 (file)
@@ -16,8 +16,41 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
  * 
  * @author niki
  */
  * 
  * @author niki
  */
-public abstract class FanfixReader {
+public abstract class BasicReader {
+       public enum ReaderType {
+               /** Simple reader that outputs everything on the console */
+               CLI,
+               /** Reader that starts local programs to handle the stories */
+               LOCAL
+       }
+
+       private static ReaderType defaultType;
        private Story story;
        private Story story;
+       private ReaderType type;
+
+       static {
+               // TODO: default type from config
+       }
+
+       /**
+        * The type of this reader.
+        * 
+        * @return the type
+        */
+       public ReaderType getType() {
+               return type;
+       }
+
+       /**
+        * The type of this reader.
+        * 
+        * @param type
+        *            the new type
+        */
+       protected BasicReader setType(ReaderType type) {
+               this.type = type;
+               return this;
+       }
 
        /**
         * Return the current {@link Story}.
 
        /**
         * Return the current {@link Story}.
@@ -29,7 +62,7 @@ public abstract class FanfixReader {
        }
 
        /**
        }
 
        /**
-        * Create a new {@link FanfixReader} for a {@link Story} in the
+        * Create a new {@link BasicReader} for a {@link Story} in the
         * {@link Library} .
         * 
         * @param luid
         * {@link Library} .
         * 
         * @param luid
@@ -45,7 +78,7 @@ public abstract class FanfixReader {
        }
 
        /**
        }
 
        /**
-        * Create a new {@link FanfixReader} for an external {@link Story}.
+        * Create a new {@link BasicReader} for an external {@link Story}.
         * 
         * @param source
         *            the {@link Story} {@link URL}
         * 
         * @param source
         *            the {@link Story} {@link URL}
@@ -93,4 +126,41 @@ public abstract class FanfixReader {
         *            all
         */
        public abstract void start(SupportType type);
         *            all
         */
        public abstract void start(SupportType type);
+
+       /**
+        * Return a new {@link BasicReader} ready for use.
+        * 
+        * @return a {@link BasicReader}
+        */
+       public static BasicReader getReader() {
+               switch (defaultType) {
+               // case LOCAL:
+               // return new LocalReader().setType(ReaderType.LOCAL);
+               case CLI:
+                       return new CliReader().setType(ReaderType.CLI);
+               }
+
+               return null;
+       }
+
+       /**
+        * The default {@link ReaderType} used when calling
+        * {@link BasicReader#getReader()}.
+        * 
+        * @return the default type
+        */
+       public static ReaderType getDefaultReaderType() {
+               return defaultType;
+       }
+
+       /**
+        * The default {@link ReaderType} used when calling
+        * {@link BasicReader#getReader()}.
+        * 
+        * @param defaultType
+        *            the new default type
+        */
+       public static void setDefaultReaderType(ReaderType defaultType) {
+               BasicReader.defaultType = defaultType;
+       }
 }
 }
similarity index 84%
rename from src/be/nikiroo/fanfix/reader/cli/CliReader.java
rename to src/be/nikiroo/fanfix/reader/CliReader.java
index e040a210cb3aaf5e4dbc7ef46721e86b38c5a86b..b3dd3edc79300b28c2ea779c91c887bbbbd9d704 100644 (file)
@@ -1,4 +1,4 @@
-package be.nikiroo.fanfix.reader.cli;
+package be.nikiroo.fanfix.reader;
 
 import java.io.IOException;
 import java.util.List;
 
 import java.io.IOException;
 import java.util.List;
@@ -9,7 +9,6 @@ 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.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Paragraph;
 import be.nikiroo.fanfix.data.Story;
-import be.nikiroo.fanfix.reader.FanfixReader;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
 /**
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
 /**
@@ -19,14 +18,8 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
  * 
  * @author niki
  */
  * 
  * @author niki
  */
-public class CliReader extends FanfixReader {
-       /**
-        * Start the {@link Story} Reading.
-        * 
-        * @throws IOException
-        *             in case of I/O error or if the {@link Story} was not
-        *             previously set
-        */
+class CliReader extends BasicReader {
+       @Override
        public void read() throws IOException {
                if (getStory() == null) {
                        throw new IOException("No story to read");
        public void read() throws IOException {
                if (getStory() == null) {
                        throw new IOException("No story to read");
@@ -65,12 +58,7 @@ public class CliReader extends FanfixReader {
                }
        }
 
                }
        }
 
-       /**
-        * Read the selected chapter (starting at 1).
-        * 
-        * @param chapter
-        *            the chapter
-        */
+       @Override
        public void read(int chapter) {
                if (chapter > getStory().getChapters().size()) {
                        System.err.println("Chapter " + chapter + ": no such chapter");
        public void read(int chapter) {
                if (chapter > getStory().getChapters().size()) {
                        System.err.println("Chapter " + chapter + ": no such chapter");