--list was causing problems + move reader
[fanfix.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
similarity index 53%
rename from src/be/nikiroo/fanfix/reader/CliReader.java
rename to src/be/nikiroo/fanfix/reader/cli/CliReader.java
index 52a5ea4321f7ef6aa8a1607605b09e39f7708b77..e040a210cb3aaf5e4dbc7ef46721e86b38c5a86b 100644 (file)
@@ -1,18 +1,15 @@
-package be.nikiroo.fanfix.reader;
+package be.nikiroo.fanfix.reader.cli;
 
 import java.io.IOException;
-import java.net.URL;
 import java.util.List;
 
 import be.nikiroo.fanfix.Instance;
-import be.nikiroo.fanfix.Library;
 import be.nikiroo.fanfix.bundles.StringId;
 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.output.BasicOutput.OutputType;
-import be.nikiroo.fanfix.supported.BasicSupport;
+import be.nikiroo.fanfix.reader.FanfixReader;
 import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
 
 /**
@@ -22,56 +19,23 @@ import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
  * 
  * @author niki
  */
-public class CliReader {
-       private Story story;
-
+public class CliReader extends FanfixReader {
        /**
-        * Create a new {@link CliReader} for a {@link Story} in the {@link Library}
-        * .
+        * Start the {@link Story} Reading.
         * 
-        * @param luid
-        *            the {@link Story} ID
         * @throws IOException
-        *             in case of I/O error
+        *             in case of I/O error or if the {@link Story} was not
+        *             previously set
         */
-       public CliReader(String luid) throws IOException {
-               story = Instance.getLibrary().getStory(luid);
-               if (story == null) {
-                       throw new IOException("Cannot retrieve story from library: " + luid);
+       public void read() throws IOException {
+               if (getStory() == null) {
+                       throw new IOException("No story to read");
                }
-       }
-
-       /**
-        * Create a new {@link CliReader} for an external {@link Story}.
-        * 
-        * @param source
-        *            the {@link Story} {@link URL}
-        * @throws IOException
-        *             in case of I/O error
-        */
-       public CliReader(URL source) throws IOException {
-               BasicSupport support = BasicSupport.getSupport(source);
-               if (support == null) {
-                       throw new IOException("URL not supported: " + source.toString());
-               }
-
-               story = support.process(source);
-               if (story == null) {
-                       throw new IOException(
-                                       "Cannot retrieve story from external source: "
-                                                       + source.toString());
 
-               }
-       }
-
-       /**
-        * Read the information about the {@link Story}.
-        */
-       public void read() {
                String title = "";
                String author = "";
 
-               MetaData meta = story.getMeta();
+               MetaData meta = getStory().getMeta();
                if (meta != null) {
                        if (meta.getTitle() != null) {
                                title = meta.getTitle();
@@ -89,7 +53,7 @@ public class CliReader {
                System.out.println(author);
                System.out.println("");
 
-               for (Chapter chap : story) {
+               for (Chapter chap : getStory()) {
                        if (chap.getName() != null && !chap.getName().isEmpty()) {
                                System.out.println(Instance.getTrans().getString(
                                                StringId.CHAPTER_NAMED, chap.getNumber(),
@@ -108,10 +72,10 @@ public class CliReader {
         *            the chapter
         */
        public void read(int chapter) {
-               if (chapter > story.getChapters().size()) {
+               if (chapter > getStory().getChapters().size()) {
                        System.err.println("Chapter " + chapter + ": no such chapter");
                } else {
-                       Chapter chap = story.getChapters().get(chapter - 1);
+                       Chapter chap = getStory().getChapters().get(chapter - 1);
                        System.out.println("Chapter " + chap.getNumber() + ": "
                                        + chap.getName());
 
@@ -122,14 +86,8 @@ public class CliReader {
                }
        }
 
-       /**
-        * List all the stories available in the {@link Library} by
-        * {@link OutputType} (or all of them if the given type is NULL)
-        * 
-        * @param type
-        *            the {@link OutputType} or NULL for all stories
-        */
-       public static void list(SupportType type) {
+       @Override
+       public void start(SupportType type) {
                List<MetaData> stories;
                stories = Instance.getLibrary().getList(type);