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
.utils
.ui
.Progress
;
14 * The class that handles the different {@link Story} readers you can use.
16 * All the readers should be accessed via {@link BasicReader#getReader()}.
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 * the optional progress reporter
86 * in case of I/O error
88 public void setStory(String luid
, Progress pg
) throws IOException
{
89 story
= Instance
.getLibrary().getStory(luid
, pg
);
91 throw new IOException("Cannot retrieve story from library: " + luid
);
96 * Create a new {@link BasicReader} for an external {@link Story}.
99 * the {@link Story} {@link URL}
101 * the optional progress reporter
103 * @throws IOException
104 * in case of I/O error
106 public void setStory(URL source
, Progress pg
) throws IOException
{
107 BasicSupport support
= BasicSupport
.getSupport(source
);
108 if (support
== null) {
109 throw new IOException("URL not supported: " + source
.toString());
112 story
= support
.process(source
, pg
);
114 throw new IOException(
115 "Cannot retrieve story from external source: "
116 + source
.toString());
122 * Start the {@link Story} Reading.
124 * @throws IOException
125 * in case of I/O error or if the {@link Story} was not
128 public abstract void read() throws IOException
;
131 * Read the selected chapter (starting at 1).
136 public abstract void read(int chapter
);
139 * Start the reader in browse mode for the given type (or pass NULL for all
143 * the type of {@link Story} to take into account, or NULL for
146 public abstract void start(String type
);
149 * Return a new {@link BasicReader} ready for use if one is configured.
151 * Can return NULL if none are configured.
153 * @return a {@link BasicReader}, or NULL if none configured
155 public static BasicReader
getReader() {
157 if (defaultType
!= null) {
158 switch (defaultType
) {
160 return new LocalReader().setType(ReaderType
.LOCAL
);
162 return new CliReader().setType(ReaderType
.CLI
);
165 } catch (IOException e
) {
166 Instance
.syserr(new Exception("Cannot create a reader of type: "
174 * The default {@link ReaderType} used when calling
175 * {@link BasicReader#getReader()}.
177 * @return the default type
179 public static ReaderType
getDefaultReaderType() {
184 * The default {@link ReaderType} used when calling
185 * {@link BasicReader#getReader()}.
188 * the new default type
190 public static void setDefaultReaderType(ReaderType defaultType
) {
191 BasicReader
.defaultType
= defaultType
;