package be.nikiroo.fanfix.reader;
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.supported.BasicSupport.SupportType;
/**
* Command line {@link Story} reader.
*
* @author niki
*/
-public class CliReader {
- private Story story;
-
- /**
- * Create a new {@link CliReader} for a {@link Story} in the {@link Library}
- * .
- *
- * @param luid
- * the {@link Story} ID
- * @throws IOException
- * in case of I/O error
- */
- public CliReader(String luid) throws IOException {
- story = Instance.getLibrary().getStory(luid);
- if (story == null) {
- throw new IOException("Cannot retrieve story from library: " + luid);
- }
- }
-
- /**
- * 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());
+class CliReader extends BasicReader {
+ public void read() throws IOException {
+ MetaData meta = getMeta();
+ if (meta == null) {
+ throw new IOException("No story to read");
}
- }
- /**
- * Read the information about the {@link Story}.
- */
- public void read() {
String title = "";
String author = "";
- MetaData meta = story.getMeta();
- if (meta != null) {
- if (meta.getTitle() != null) {
- title = meta.getTitle();
- }
+ if (meta.getTitle() != null) {
+ title = meta.getTitle();
+ }
- if (meta.getAuthor() != null) {
- author = "©" + meta.getAuthor();
- if (meta.getDate() != null && !meta.getDate().isEmpty()) {
- author = author + " (" + meta.getDate() + ")";
- }
+ if (meta.getAuthor() != null) {
+ author = "©" + meta.getAuthor();
+ if (meta.getDate() != null && !meta.getDate().isEmpty()) {
+ author = author + " (" + meta.getDate() + ")";
}
}
System.out.println(author);
System.out.println("");
- for (Chapter chap : story) {
+ // TODO: progress?
+ for (Chapter chap : getStory(null)) {
if (chap.getName() != null && !chap.getName().isEmpty()) {
System.out.println(Instance.getTrans().getString(
StringId.CHAPTER_NAMED, chap.getNumber(),
}
}
- /**
- * Read the selected chapter (starting at 1).
- *
- * @param chapter
- * the chapter
- */
- public void read(int chapter) {
- if (chapter > story.getChapters().size()) {
+ public void read(int chapter) throws IOException {
+ MetaData meta = getMeta();
+
+ if (meta == null) {
+ throw new IOException("No story to read");
+ }
+
+ // TODO: progress?
+ if (chapter > getStory(null).getChapters().size()) {
System.err.println("Chapter " + chapter + ": no such chapter");
} else {
- Chapter chap = story.getChapters().get(chapter - 1);
+ Chapter chap = getStory(null).getChapters().get(chapter - 1);
System.out.println("Chapter " + chap.getNumber() + ": "
+ chap.getName());
}
}
- /**
- * 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) {
+ public void browse(String source) {
List<MetaData> stories;
- stories = Instance.getLibrary().getList(type);
+ stories = getLibrary().getListBySource(source);
for (MetaData story : stories) {
String author = "";