private static Library lib;
private static boolean debug;
private static File coverDir;
+ private static File readerTmp;
static {
config = new ConfigBundle();
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(
}
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);
- 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(
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
*
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;
return 1;
}
- new CliReader().start(type);
+ BasicReader.getReader().start(type);
return 0;
}
*/
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 {
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-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")
# (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-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
*
* @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 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}.
}
/**
- * 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
}
/**
- * 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}
* 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;
+ }
}
-package be.nikiroo.fanfix.reader.cli;
+package be.nikiroo.fanfix.reader;
import java.io.IOException;
import java.util.List;
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;
/**
*
* @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");
}
}
- /**
- * 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");