Commit | Line | Data |
---|---|---|
89cb07a6 NR |
1 | package be.nikiroo.fanfix.reader; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.net.URL; | |
5 | ||
6 | import be.nikiroo.fanfix.Instance; | |
7 | import be.nikiroo.fanfix.Library; | |
d0114000 | 8 | import be.nikiroo.fanfix.bundles.Config; |
89cb07a6 NR |
9 | import be.nikiroo.fanfix.data.Story; |
10 | import be.nikiroo.fanfix.supported.BasicSupport; | |
89cb07a6 NR |
11 | |
12 | /** | |
dd56a893 | 13 | * The class that handles the different {@link Story} readers you can use. |
89cb07a6 | 14 | * <p> |
dd56a893 | 15 | * All the readers should be accessed via {@link BasicReader#getReader()}. |
89cb07a6 NR |
16 | * |
17 | * @author niki | |
18 | */ | |
3727aae2 NR |
19 | public abstract class BasicReader { |
20 | public enum ReaderType { | |
21 | /** Simple reader that outputs everything on the console */ | |
22 | CLI, | |
23 | /** Reader that starts local programs to handle the stories */ | |
24 | LOCAL | |
25 | } | |
26 | ||
d0114000 | 27 | private static ReaderType defaultType = ReaderType.CLI; |
89cb07a6 | 28 | private Story story; |
3727aae2 NR |
29 | private ReaderType type; |
30 | ||
d0114000 NR |
31 | /** |
32 | * Take the default reader type configuration from the config file. | |
33 | */ | |
3727aae2 | 34 | static { |
d0114000 NR |
35 | String typeString = Instance.getConfig().getString(Config.READER_TYPE); |
36 | if (typeString != null && !typeString.isEmpty()) { | |
37 | try { | |
38 | ReaderType type = ReaderType.valueOf(typeString.toUpperCase()); | |
39 | defaultType = type; | |
40 | } catch (IllegalArgumentException e) { | |
41 | // Do nothing | |
42 | } | |
43 | } | |
3727aae2 NR |
44 | } |
45 | ||
46 | /** | |
47 | * The type of this reader. | |
48 | * | |
49 | * @return the type | |
50 | */ | |
51 | public ReaderType getType() { | |
52 | return type; | |
53 | } | |
54 | ||
55 | /** | |
56 | * The type of this reader. | |
57 | * | |
58 | * @param type | |
59 | * the new type | |
60 | */ | |
61 | protected BasicReader setType(ReaderType type) { | |
62 | this.type = type; | |
63 | return this; | |
64 | } | |
89cb07a6 NR |
65 | |
66 | /** | |
67 | * Return the current {@link Story}. | |
68 | * | |
69 | * @return the {@link Story} | |
70 | */ | |
71 | public Story getStory() { | |
72 | return story; | |
73 | } | |
74 | ||
75 | /** | |
3727aae2 | 76 | * Create a new {@link BasicReader} for a {@link Story} in the |
89cb07a6 NR |
77 | * {@link Library} . |
78 | * | |
79 | * @param luid | |
80 | * the {@link Story} ID | |
81 | * @throws IOException | |
82 | * in case of I/O error | |
83 | */ | |
84 | public void setStory(String luid) throws IOException { | |
85 | story = Instance.getLibrary().getStory(luid); | |
86 | if (story == null) { | |
d0114000 NR |
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) { | |
91 | luid = "0" + luid; | |
92 | } | |
93 | setStory(luid); | |
94 | } else { | |
95 | throw new IOException("Cannot retrieve story from library: " | |
96 | + luid); | |
97 | } | |
89cb07a6 NR |
98 | } |
99 | } | |
100 | ||
101 | /** | |
3727aae2 | 102 | * Create a new {@link BasicReader} for an external {@link Story}. |
89cb07a6 NR |
103 | * |
104 | * @param source | |
105 | * the {@link Story} {@link URL} | |
106 | * @throws IOException | |
107 | * in case of I/O error | |
108 | */ | |
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()); | |
113 | } | |
114 | ||
115 | story = support.process(source); | |
116 | if (story == null) { | |
117 | throw new IOException( | |
118 | "Cannot retrieve story from external source: " | |
119 | + source.toString()); | |
120 | ||
121 | } | |
122 | } | |
123 | ||
124 | /** | |
125 | * Start the {@link Story} Reading. | |
126 | * | |
127 | * @throws IOException | |
128 | * in case of I/O error or if the {@link Story} was not | |
129 | * previously set | |
130 | */ | |
131 | public abstract void read() throws IOException; | |
132 | ||
133 | /** | |
134 | * Read the selected chapter (starting at 1). | |
135 | * | |
136 | * @param chapter | |
137 | * the chapter | |
138 | */ | |
139 | public abstract void read(int chapter); | |
140 | ||
141 | /** | |
142 | * Start the reader in browse mode for the given type (or pass NULL for all | |
143 | * types). | |
144 | * | |
145 | * @param type | |
146 | * the type of {@link Story} to take into account, or NULL for | |
147 | * all | |
148 | */ | |
333f0e7b | 149 | public abstract void start(String type); |
3727aae2 NR |
150 | |
151 | /** | |
d0114000 NR |
152 | * Return a new {@link BasicReader} ready for use if one is configured. |
153 | * <p> | |
154 | * Can return NULL if none are configured. | |
3727aae2 | 155 | * |
d0114000 | 156 | * @return a {@link BasicReader}, or NULL if none configured |
3727aae2 NR |
157 | */ |
158 | public static BasicReader getReader() { | |
333f0e7b NR |
159 | try { |
160 | if (defaultType != null) { | |
161 | switch (defaultType) { | |
162 | case LOCAL: | |
163 | return new LocalReader().setType(ReaderType.LOCAL); | |
164 | case CLI: | |
165 | return new CliReader().setType(ReaderType.CLI); | |
166 | } | |
d0114000 | 167 | } |
333f0e7b NR |
168 | } catch (IOException e) { |
169 | Instance.syserr(new Exception("Cannot create a reader of type: " | |
170 | + defaultType, e)); | |
3727aae2 NR |
171 | } |
172 | ||
173 | return null; | |
174 | } | |
175 | ||
176 | /** | |
177 | * The default {@link ReaderType} used when calling | |
178 | * {@link BasicReader#getReader()}. | |
179 | * | |
180 | * @return the default type | |
181 | */ | |
182 | public static ReaderType getDefaultReaderType() { | |
183 | return defaultType; | |
184 | } | |
185 | ||
186 | /** | |
187 | * The default {@link ReaderType} used when calling | |
188 | * {@link BasicReader#getReader()}. | |
189 | * | |
190 | * @param defaultType | |
191 | * the new default type | |
192 | */ | |
193 | public static void setDefaultReaderType(ReaderType defaultType) { | |
194 | BasicReader.defaultType = defaultType; | |
195 | } | |
89cb07a6 | 196 | } |