52a5ea4321f7ef6aa8a1607605b09e39f7708b77
1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.io
.IOException
;
7 import be
.nikiroo
.fanfix
.Instance
;
8 import be
.nikiroo
.fanfix
.Library
;
9 import be
.nikiroo
.fanfix
.bundles
.StringId
;
10 import be
.nikiroo
.fanfix
.data
.Chapter
;
11 import be
.nikiroo
.fanfix
.data
.MetaData
;
12 import be
.nikiroo
.fanfix
.data
.Paragraph
;
13 import be
.nikiroo
.fanfix
.data
.Story
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
15 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
16 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
19 * Command line {@link Story} reader.
21 * Will output stories to the console.
25 public class CliReader
{
29 * Create a new {@link CliReader} for a {@link Story} in the {@link Library}
33 * the {@link Story} ID
35 * in case of I/O error
37 public CliReader(String luid
) throws IOException
{
38 story
= Instance
.getLibrary().getStory(luid
);
40 throw new IOException("Cannot retrieve story from library: " + luid
);
45 * Create a new {@link CliReader} for an external {@link Story}.
48 * the {@link Story} {@link URL}
50 * in case of I/O error
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());
58 story
= support
.process(source
);
60 throw new IOException(
61 "Cannot retrieve story from external source: "
68 * Read the information about the {@link Story}.
74 MetaData meta
= story
.getMeta();
76 if (meta
.getTitle() != null) {
77 title
= meta
.getTitle();
80 if (meta
.getAuthor() != null) {
81 author
= "©" + meta
.getAuthor();
82 if (meta
.getDate() != null && !meta
.getDate().isEmpty()) {
83 author
= author
+ " (" + meta
.getDate() + ")";
88 System
.out
.println(title
);
89 System
.out
.println(author
);
90 System
.out
.println("");
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(),
98 System
.out
.println(Instance
.getTrans().getString(
99 StringId
.CHAPTER_UNNAMED
, chap
.getNumber()));
105 * Read the selected chapter (starting at 1).
110 public void read(int chapter
) {
111 if (chapter
> story
.getChapters().size()) {
112 System
.err
.println("Chapter " + chapter
+ ": no such chapter");
114 Chapter chap
= story
.getChapters().get(chapter
- 1);
115 System
.out
.println("Chapter " + chap
.getNumber() + ": "
118 for (Paragraph para
: chap
) {
119 System
.out
.println(para
.getContent());
120 System
.out
.println("");
126 * List all the stories available in the {@link Library} by
127 * {@link OutputType} (or all of them if the given type is NULL)
130 * the {@link OutputType} or NULL for all stories
132 public static void list(SupportType type
) {
133 List
<MetaData
> stories
;
134 stories
= Instance
.getLibrary().getList(type
);
136 for (MetaData story
: stories
) {
138 if (story
.getAuthor() != null && !story
.getAuthor().isEmpty()) {
139 author
= " (" + story
.getAuthor() + ")";
142 System
.out
.println(story
.getLuid() + ": " + story
.getTitle()