1 package be
.nikiroo
.fanfix
.reader
;
4 import java
.io
.IOException
;
5 import java
.net
.MalformedURLException
;
8 import be
.nikiroo
.fanfix
.Instance
;
9 import be
.nikiroo
.fanfix
.Library
;
10 import be
.nikiroo
.fanfix
.bundles
.Config
;
11 import be
.nikiroo
.fanfix
.data
.Story
;
12 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
13 import be
.nikiroo
.utils
.Progress
;
16 * The class that handles the different {@link Story} readers you can use.
18 * All the readers should be accessed via {@link BasicReader#getReader()}.
22 public abstract class BasicReader
{
23 public enum ReaderType
{
24 /** Simple reader that outputs everything on the console */
26 /** Reader that starts local programs to handle the stories */
30 private static ReaderType defaultType
= ReaderType
.CLI
;
32 private ReaderType type
;
35 * Take the default reader type configuration from the config file.
38 String typeString
= Instance
.getConfig().getString(Config
.READER_TYPE
);
39 if (typeString
!= null && !typeString
.isEmpty()) {
41 ReaderType type
= ReaderType
.valueOf(typeString
.toUpperCase());
43 } catch (IllegalArgumentException e
) {
50 * The type of this reader.
54 public ReaderType
getType() {
59 * The type of this reader.
64 protected BasicReader
setType(ReaderType type
) {
70 * Return the current {@link Story}.
72 * @return the {@link Story}
74 public Story
getStory() {
79 * Create a new {@link BasicReader} for a {@link Story} in the
83 * the {@link Story} ID
85 * the optional progress reporter
88 * in case of I/O error
90 public void setStory(String luid
, Progress pg
) throws IOException
{
91 story
= Instance
.getLibrary().getStory(luid
, pg
);
93 throw new IOException("Cannot retrieve story from library: " + luid
);
98 * Create a new {@link BasicReader} for an external {@link Story}.
101 * the {@link Story} {@link URL}
103 * the optional progress reporter
105 * @throws IOException
106 * in case of I/O error
108 public void setStory(URL source
, Progress pg
) throws IOException
{
109 BasicSupport support
= BasicSupport
.getSupport(source
);
110 if (support
== null) {
111 throw new IOException("URL not supported: " + source
.toString());
114 story
= support
.process(source
, pg
);
116 throw new IOException(
117 "Cannot retrieve story from external source: "
118 + source
.toString());
124 * Start the {@link Story} Reading.
126 * @throws IOException
127 * in case of I/O error or if the {@link Story} was not
130 public abstract void read() throws IOException
;
133 * Read the selected chapter (starting at 1).
138 * @throws IOException
139 * in case of I/O error or if the {@link Story} was not
142 public abstract void read(int chapter
) throws IOException
;
145 * Start the reader in browse mode for the given type (or pass NULL for all
149 * the type of {@link Story} to take into account, or NULL for
152 public abstract void start(String type
);
155 * Return a new {@link BasicReader} ready for use if one is configured.
157 * Can return NULL if none are configured.
159 * @return a {@link BasicReader}, or NULL if none configured
161 public static BasicReader
getReader() {
163 if (defaultType
!= null) {
164 switch (defaultType
) {
166 return new LocalReader().setType(ReaderType
.LOCAL
);
168 return new CliReader().setType(ReaderType
.CLI
);
171 } catch (IOException e
) {
172 Instance
.syserr(new Exception("Cannot create a reader of type: "
180 * The default {@link ReaderType} used when calling
181 * {@link BasicReader#getReader()}.
183 * @return the default type
185 public static ReaderType
getDefaultReaderType() {
190 * The default {@link ReaderType} used when calling
191 * {@link BasicReader#getReader()}.
194 * the new default type
196 public static void setDefaultReaderType(ReaderType defaultType
) {
197 BasicReader
.defaultType
= defaultType
;
201 * Return an {@link URL} from this {@link String}, be it a file path or an
202 * actual {@link URL}.
204 * @param sourceString
207 * @return the corresponding {@link URL}
209 * @throws MalformedURLException
210 * if this is neither a file nor a conventional {@link URL}
212 public static URL
getUrl(String sourceString
) throws MalformedURLException
{
213 if (sourceString
== null || sourceString
.isEmpty()) {
214 throw new MalformedURLException("Empty url");
219 source
= new URL(sourceString
);
220 } catch (MalformedURLException e
) {
221 File sourceFile
= new File(sourceString
);
222 source
= sourceFile
.toURI().toURL();