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
;
13 * The class that handles the different {@link Story} readers you can use.
15 * All the readers should be accessed via {@link BasicReader#getReader()}.
19 public abstract class BasicReader
{
20 public enum ReaderType
{
21 /** Simple reader that outputs everything on the console */
23 /** Reader that starts local programs to handle the stories */
27 private static ReaderType defaultType
= ReaderType
.CLI
;
29 private ReaderType type
;
32 * Take the default reader type configuration from the config file.
35 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
36 if (typeString
!= null && !typeString
.isEmpty()) {
38 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
40 } catch (IllegalArgumentException e
) {
47 * The type of this reader.
51 public ReaderType
getType() {
56 * The type of this reader.
61 protected BasicReader
setType(ReaderType type
) {
67 * Return the current {@link Story}.
69 * @return the {@link Story}
71 public Story
getStory() {
76 * Create a new {@link BasicReader} for a {@link Story} in the
80 * the {@link Story} ID
82 * in case of I/O error
84 public void setStory(String luid
) throws IOException
{
85 story
= Instance
.getLibrary().getStory(luid
);
87 // if the LUID is wrong and < 3, pad it to 3 chars with "0" then
88 // retry (since LUIDs are %03d)
89 if (luid
!= null && luid
.length() < 3) {
90 while (luid
.length() < 3) {
95 throw new IOException("Cannot retrieve story from library: "
102 * Create a new {@link BasicReader} for an external {@link Story}.
105 * the {@link Story} {@link URL}
106 * @throws IOException
107 * in case of I/O error
109 public void setStory(URL source
) throws IOException
{
110 BasicSupport support
= BasicSupport
.getSupport(source
);
111 if (support
== null) {
112 throw new IOException("URL not supported: " + source
.toString());
115 story
= support
.process(source
);
117 throw new IOException(
118 "Cannot retrieve story from external source: "
119 + source
.toString());
125 * Start the {@link Story} Reading.
127 * @throws IOException
128 * in case of I/O error or if the {@link Story} was not
131 public abstract void read() throws IOException
;
134 * Read the selected chapter (starting at 1).
139 public abstract void read(int chapter
);
142 * Start the reader in browse mode for the given type (or pass NULL for all
146 * the type of {@link Story} to take into account, or NULL for
149 public abstract void start(String type
);
152 * Return a new {@link BasicReader} ready for use if one is configured.
154 * Can return NULL if none are configured.
156 * @return a {@link BasicReader}, or NULL if none configured
158 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
);
168 } catch (IOException e
) {
169 Instance
.syserr(new Exception("Cannot create a reader of type: "
177 * The default {@link ReaderType} used when calling
178 * {@link BasicReader#getReader()}.
180 * @return the default type
182 public static ReaderType
getDefaultReaderType() {
187 * The default {@link ReaderType} used when calling
188 * {@link BasicReader#getReader()}.
191 * the new default type
193 public static void setDefaultReaderType(ReaderType defaultType
) {
194 BasicReader
.defaultType
= defaultType
;