eabbe7e4408ddf758c623fd65c835e1a3643a95a
[fanfix.git] / src / be / nikiroo / fanfix / reader / BasicReader.java
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;
8 import be.nikiroo.fanfix.bundles.Config;
9 import be.nikiroo.fanfix.data.Story;
10 import be.nikiroo.fanfix.supported.BasicSupport;
11
12 /**
13 * The class that handles the different {@link Story} readers you can use.
14 * <p>
15 * All the readers should be accessed via {@link BasicReader#getReader()}.
16 *
17 * @author niki
18 */
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
27 private static ReaderType defaultType = ReaderType.CLI;
28 private Story story;
29 private ReaderType type;
30
31 /**
32 * Take the default reader type configuration from the config file.
33 */
34 static {
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 }
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 }
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 /**
76 * Create a new {@link BasicReader} for a {@link Story} in the
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) {
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 }
98 }
99 }
100
101 /**
102 * Create a new {@link BasicReader} for an external {@link Story}.
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 */
149 public abstract void start(String type);
150
151 /**
152 * Return a new {@link BasicReader} ready for use if one is configured.
153 * <p>
154 * Can return NULL if none are configured.
155 *
156 * @return a {@link BasicReader}, or NULL if none configured
157 */
158 public static BasicReader getReader() {
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 }
167 }
168 } catch (IOException e) {
169 Instance.syserr(new Exception("Cannot create a reader of type: "
170 + defaultType, e));
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 }
196 }