1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.io
.IOException
;
6 import be
.nikiroo
.fanfix
.Instance
;
7 import be
.nikiroo
.fanfix
.Library
;
8 import be
.nikiroo
.fanfix
.bundles
.Config
;
9 import be
.nikiroo
.fanfix
.data
.Story
;
10 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
11 import be
.nikiroo
.fanfix
.supported
.BasicSupport
.SupportType
;
14 * Command line {@link Story} reader.
16 * Will output stories to the console.
20 public abstract class BasicReader
{
21 public enum ReaderType
{
22 /** Simple reader that outputs everything on the console */
24 /** Reader that starts local programs to handle the stories */
28 private static ReaderType defaultType
= ReaderType
.CLI
;
30 private ReaderType type
;
33 * Take the default reader type configuration from the config file.
36 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
37 if (typeString
!= null && !typeString
.isEmpty()) {
39 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
41 } catch (IllegalArgumentException e
) {
48 * The type of this reader.
52 public ReaderType
getType() {
57 * The type of this reader.
62 protected BasicReader
setType(ReaderType type
) {
68 * Return the current {@link Story}.
70 * @return the {@link Story}
72 public Story
getStory() {
77 * Create a new {@link BasicReader} for a {@link Story} in the
81 * the {@link Story} ID
83 * in case of I/O error
85 public void setStory(String luid
) throws IOException
{
86 story
= Instance
.getLibrary().getStory(luid
);
88 // if the LUID is wrong and < 3, pad it to 3 chars with "0" then
89 // retry (since LUIDs are %03d)
90 if (luid
!= null && luid
.length() < 3) {
91 while (luid
.length() < 3) {
96 throw new IOException("Cannot retrieve story from library: "
103 * Create a new {@link BasicReader} for an external {@link Story}.
106 * the {@link Story} {@link URL}
107 * @throws IOException
108 * in case of I/O error
110 public void setStory(URL source
) throws IOException
{
111 BasicSupport support
= BasicSupport
.getSupport(source
);
112 if (support
== null) {
113 throw new IOException("URL not supported: " + source
.toString());
116 story
= support
.process(source
);
118 throw new IOException(
119 "Cannot retrieve story from external source: "
120 + source
.toString());
126 * Start the {@link Story} Reading.
128 * @throws IOException
129 * in case of I/O error or if the {@link Story} was not
132 public abstract void read() throws IOException
;
135 * Read the selected chapter (starting at 1).
140 public abstract void read(int chapter
);
143 * Start the reader in browse mode for the given type (or pass NULL for all
147 * the type of {@link Story} to take into account, or NULL for
150 public abstract void start(SupportType type
);
153 * Return a new {@link BasicReader} ready for use if one is configured.
155 * Can return NULL if none are configured.
157 * @return a {@link BasicReader}, or NULL if none configured
159 public static BasicReader
getReader() {
160 if (defaultType
!= null) {
161 switch (defaultType
) {
163 //return new LocalReader().setType(ReaderType.LOCAL);
165 return new CliReader().setType(ReaderType
.CLI
);
173 * The default {@link ReaderType} used when calling
174 * {@link BasicReader#getReader()}.
176 * @return the default type
178 public static ReaderType
getDefaultReaderType() {
183 * The default {@link ReaderType} used when calling
184 * {@link BasicReader#getReader()}.
187 * the new default type
189 public static void setDefaultReaderType(ReaderType defaultType
) {
190 BasicReader
.defaultType
= defaultType
;