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