Code cleanup: Libraries/Readers
[fanfix.git] / src / be / nikiroo / fanfix / reader / CliReader.java
CommitLineData
3727aae2 1package be.nikiroo.fanfix.reader;
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;
08fe2e33
NR
12
13/**
14 * Command line {@link Story} reader.
15 * <p>
16 * Will output stories to the console.
17 *
18 * @author niki
19 */
3727aae2 20class CliReader extends BasicReader {
89cb07a6
NR
21 public void read() throws IOException {
22 if (getStory() == null) {
23 throw new IOException("No story to read");
08fe2e33 24 }
08fe2e33 25
08fe2e33
NR
26 String title = "";
27 String author = "";
28
89cb07a6 29 MetaData meta = getStory().getMeta();
08fe2e33
NR
30 if (meta != null) {
31 if (meta.getTitle() != null) {
32 title = meta.getTitle();
33 }
34
35 if (meta.getAuthor() != null) {
36 author = "©" + meta.getAuthor();
37 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
38 author = author + " (" + meta.getDate() + ")";
39 }
40 }
41 }
42
43 System.out.println(title);
44 System.out.println(author);
45 System.out.println("");
46
89cb07a6 47 for (Chapter chap : getStory()) {
08fe2e33
NR
48 if (chap.getName() != null && !chap.getName().isEmpty()) {
49 System.out.println(Instance.getTrans().getString(
50 StringId.CHAPTER_NAMED, chap.getNumber(),
51 chap.getName()));
52 } else {
53 System.out.println(Instance.getTrans().getString(
54 StringId.CHAPTER_UNNAMED, chap.getNumber()));
55 }
56 }
57 }
58
edd46289
NR
59 public void read(int chapter) throws IOException {
60 if (getStory() == null) {
61 throw new IOException("No story to read");
62 }
63
89cb07a6 64 if (chapter > getStory().getChapters().size()) {
08fe2e33
NR
65 System.err.println("Chapter " + chapter + ": no such chapter");
66 } else {
89cb07a6 67 Chapter chap = getStory().getChapters().get(chapter - 1);
08fe2e33
NR
68 System.out.println("Chapter " + chap.getNumber() + ": "
69 + chap.getName());
70
71 for (Paragraph para : chap) {
72 System.out.println(para.getContent());
73 System.out.println("");
74 }
75 }
76 }
77
b0e88ebd 78 public void browse(String source) {
08fe2e33 79 List<MetaData> stories;
b0e88ebd 80 stories = getLibrary().getListBySource(source);
08fe2e33
NR
81
82 for (MetaData story : stories) {
83 String author = "";
84 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
85 author = " (" + story.getAuthor() + ")";
86 }
87
88 System.out.println(story.getLuid() + ": " + story.getTitle()
89 + author);
90 }
91 }
92}