Initial commit (working)
[fanfix.git] / src / be / nikiroo / fanfix / reader / CliReader.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.reader;
2
3import java.io.IOException;
4import java.net.URL;
5import java.util.List;
6
7import be.nikiroo.fanfix.Instance;
8import be.nikiroo.fanfix.Library;
9import be.nikiroo.fanfix.bundles.StringId;
10import be.nikiroo.fanfix.data.Chapter;
11import be.nikiroo.fanfix.data.MetaData;
12import be.nikiroo.fanfix.data.Paragraph;
13import be.nikiroo.fanfix.data.Story;
14import be.nikiroo.fanfix.output.BasicOutput.OutputType;
15import be.nikiroo.fanfix.supported.BasicSupport;
16import be.nikiroo.fanfix.supported.BasicSupport.SupportType;
17
18/**
19 * Command line {@link Story} reader.
20 * <p>
21 * Will output stories to the console.
22 *
23 * @author niki
24 */
25public class CliReader {
26 private Story story;
27
28 /**
29 * Create a new {@link CliReader} for a {@link Story} in the {@link Library}
30 * .
31 *
32 * @param luid
33 * the {@link Story} ID
34 * @throws IOException
35 * in case of I/O error
36 */
37 public CliReader(String luid) throws IOException {
38 story = Instance.getLibrary().getStory(luid);
39 if (story == null) {
40 throw new IOException("Cannot retrieve story from library: " + luid);
41 }
42 }
43
44 /**
45 * Create a new {@link CliReader} for an external {@link Story}.
46 *
47 * @param source
48 * the {@link Story} {@link URL}
49 * @throws IOException
50 * in case of I/O error
51 */
52 public CliReader(URL source) throws IOException {
53 BasicSupport support = BasicSupport.getSupport(source);
54 if (support == null) {
55 throw new IOException("URL not supported: " + source.toString());
56 }
57
58 story = support.process(source);
59 if (story == null) {
60 throw new IOException(
61 "Cannot retrieve story from external source: "
62 + source.toString());
63
64 }
65 }
66
67 /**
68 * Read the information about the {@link Story}.
69 */
70 public void read() {
71 String title = "";
72 String author = "";
73
74 MetaData meta = story.getMeta();
75 if (meta != null) {
76 if (meta.getTitle() != null) {
77 title = meta.getTitle();
78 }
79
80 if (meta.getAuthor() != null) {
81 author = "©" + meta.getAuthor();
82 if (meta.getDate() != null && !meta.getDate().isEmpty()) {
83 author = author + " (" + meta.getDate() + ")";
84 }
85 }
86 }
87
88 System.out.println(title);
89 System.out.println(author);
90 System.out.println("");
91
92 for (Chapter chap : story) {
93 if (chap.getName() != null && !chap.getName().isEmpty()) {
94 System.out.println(Instance.getTrans().getString(
95 StringId.CHAPTER_NAMED, chap.getNumber(),
96 chap.getName()));
97 } else {
98 System.out.println(Instance.getTrans().getString(
99 StringId.CHAPTER_UNNAMED, chap.getNumber()));
100 }
101 }
102 }
103
104 /**
105 * Read the selected chapter (starting at 1).
106 *
107 * @param chapter
108 * the chapter
109 */
110 public void read(int chapter) {
111 if (chapter > story.getChapters().size()) {
112 System.err.println("Chapter " + chapter + ": no such chapter");
113 } else {
114 Chapter chap = story.getChapters().get(chapter - 1);
115 System.out.println("Chapter " + chap.getNumber() + ": "
116 + chap.getName());
117
118 for (Paragraph para : chap) {
119 System.out.println(para.getContent());
120 System.out.println("");
121 }
122 }
123 }
124
125 /**
126 * List all the stories available in the {@link Library} by
127 * {@link OutputType} (or all of them if the given type is NULL)
128 *
129 * @param type
130 * the {@link OutputType} or NULL for all stories
131 */
132 public static void list(SupportType type) {
133 List<MetaData> stories;
134 stories = Instance.getLibrary().getList(type);
135
136 for (MetaData story : stories) {
137 String author = "";
138 if (story.getAuthor() != null && !story.getAuthor().isEmpty()) {
139 author = " (" + story.getAuthor() + ")";
140 }
141
142 System.out.println(story.getLuid() + ": " + story.getTitle()
143 + author);
144 }
145 }
146}