--list was causing problems + move reader
[fanfix.git] / src / be / nikiroo / fanfix / reader / cli / CliReader.java
CommitLineData
89cb07a6 1package be.nikiroo.fanfix.reader.cli;
08fe2e33
NR
2
3import java.io.IOException;
08fe2e33
NR
4import java.util.List;
5
6import be.nikiroo.fanfix.Instance;
08fe2e33
NR
7import be.nikiroo.fanfix.bundles.StringId;
8import be.nikiroo.fanfix.data.Chapter;
9import be.nikiroo.fanfix.data.MetaData;
10import be.nikiroo.fanfix.data.Paragraph;
11import be.nikiroo.fanfix.data.Story;
89cb07a6 12import be.nikiroo.fanfix.reader.FanfixReader;
08fe2e33
NR
13import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
14
15/**
16 * Command line {@link Story} reader.
17 * <p>
18 * Will output stories to the console.
19 *
20 * @author niki
21 */
89cb07a6 22public class CliReader extends FanfixReader {
08fe2e33 23 /**
89cb07a6 24 * Start the {@link Story} Reading.
08fe2e33 25 *
08fe2e33 26 * @throws IOException
89cb07a6
NR
27 * in case of I/O error or if the {@link Story} was not
28 * previously set
08fe2e33 29 */
89cb07a6
NR
30 public void read() throws IOException {
31 if (getStory() == null) {
32 throw new IOException("No story to read");
08fe2e33 33 }
08fe2e33 34
08fe2e33
NR
35 String title = "";
36 String author = "";
37
89cb07a6 38 MetaData meta = getStory().getMeta();
08fe2e33
NR
39 if (meta != null) {
40 if (meta.getTitle() != null) {
41 title = meta.getTitle();
42 }
43
44 if (meta.getAuthor() != null) {
45 author = "©" + meta.getAuthor();
46 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
47 author = author + " (" + meta.getDate() + ")";
48 }
49 }
50 }
51
52 System.out.println(title);
53 System.out.println(author);
54 System.out.println("");
55
89cb07a6 56 for (Chapter chap : getStory()) {
08fe2e33
NR
57 if (chap.getName() != null && !chap.getName().isEmpty()) {
58 System.out.println(Instance.getTrans().getString(
59 StringId.CHAPTER_NAMED, chap.getNumber(),
60 chap.getName()));
61 } else {
62 System.out.println(Instance.getTrans().getString(
63 StringId.CHAPTER_UNNAMED, chap.getNumber()));
64 }
65 }
66 }
67
68 /**
69 * Read the selected chapter (starting at 1).
70 *
71 * @param chapter
72 * the chapter
73 */
74 public void read(int chapter) {
89cb07a6 75 if (chapter > getStory().getChapters().size()) {
08fe2e33
NR
76 System.err.println("Chapter " + chapter + ": no such chapter");
77 } else {
89cb07a6 78 Chapter chap = getStory().getChapters().get(chapter - 1);
08fe2e33
NR
79 System.out.println("Chapter " + chap.getNumber() + ": "
80 + chap.getName());
81
82 for (Paragraph para : chap) {
83 System.out.println(para.getContent());
84 System.out.println("");
85 }
86 }
87 }
88
89cb07a6
NR
89 @Override
90 public void start(SupportType type) {
08fe2e33
NR
91 List<MetaData> stories;
92 stories = Instance.getLibrary().getList(type);
93
94 for (MetaData story : stories) {
95 String author = "";
96 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
97 author = " (" + story.getAuthor() + ")";
98 }
99
100 System.out.println(story.getLuid() + ": " + story.getTitle()
101 + author);
102 }
103 }
104}