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; | |
8 | import be.nikiroo.fanfix.data.Story; | |
9 | import be.nikiroo.fanfix.supported.BasicSupport; | |
10 | import be.nikiroo.fanfix.supported.BasicSupport.SupportType; | |
11 | ||
12 | /** | |
13 | * Command line {@link Story} reader. | |
14 | * <p> | |
15 | * Will output stories to the console. | |
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 | ||
27 | private static ReaderType defaultType; | |
89cb07a6 | 28 | private Story story; |
3727aae2 NR |
29 | private ReaderType type; |
30 | ||
31 | static { | |
32 | // TODO: default type from config | |
33 | } | |
34 | ||
35 | /** | |
36 | * The type of this reader. | |
37 | * | |
38 | * @return the type | |
39 | */ | |
40 | public ReaderType getType() { | |
41 | return type; | |
42 | } | |
43 | ||
44 | /** | |
45 | * The type of this reader. | |
46 | * | |
47 | * @param type | |
48 | * the new type | |
49 | */ | |
50 | protected BasicReader setType(ReaderType type) { | |
51 | this.type = type; | |
52 | return this; | |
53 | } | |
89cb07a6 NR |
54 | |
55 | /** | |
56 | * Return the current {@link Story}. | |
57 | * | |
58 | * @return the {@link Story} | |
59 | */ | |
60 | public Story getStory() { | |
61 | return story; | |
62 | } | |
63 | ||
64 | /** | |
3727aae2 | 65 | * Create a new {@link BasicReader} for a {@link Story} in the |
89cb07a6 NR |
66 | * {@link Library} . |
67 | * | |
68 | * @param luid | |
69 | * the {@link Story} ID | |
70 | * @throws IOException | |
71 | * in case of I/O error | |
72 | */ | |
73 | public void setStory(String luid) throws IOException { | |
74 | story = Instance.getLibrary().getStory(luid); | |
75 | if (story == null) { | |
76 | throw new IOException("Cannot retrieve story from library: " + luid); | |
77 | } | |
78 | } | |
79 | ||
80 | /** | |
3727aae2 | 81 | * Create a new {@link BasicReader} for an external {@link Story}. |
89cb07a6 NR |
82 | * |
83 | * @param source | |
84 | * the {@link Story} {@link URL} | |
85 | * @throws IOException | |
86 | * in case of I/O error | |
87 | */ | |
88 | public void setStory(URL source) throws IOException { | |
89 | BasicSupport support = BasicSupport.getSupport(source); | |
90 | if (support == null) { | |
91 | throw new IOException("URL not supported: " + source.toString()); | |
92 | } | |
93 | ||
94 | story = support.process(source); | |
95 | if (story == null) { | |
96 | throw new IOException( | |
97 | "Cannot retrieve story from external source: " | |
98 | + source.toString()); | |
99 | ||
100 | } | |
101 | } | |
102 | ||
103 | /** | |
104 | * Start the {@link Story} Reading. | |
105 | * | |
106 | * @throws IOException | |
107 | * in case of I/O error or if the {@link Story} was not | |
108 | * previously set | |
109 | */ | |
110 | public abstract void read() throws IOException; | |
111 | ||
112 | /** | |
113 | * Read the selected chapter (starting at 1). | |
114 | * | |
115 | * @param chapter | |
116 | * the chapter | |
117 | */ | |
118 | public abstract void read(int chapter); | |
119 | ||
120 | /** | |
121 | * Start the reader in browse mode for the given type (or pass NULL for all | |
122 | * types). | |
123 | * | |
124 | * @param type | |
125 | * the type of {@link Story} to take into account, or NULL for | |
126 | * all | |
127 | */ | |
128 | public abstract void start(SupportType type); | |
3727aae2 NR |
129 | |
130 | /** | |
131 | * Return a new {@link BasicReader} ready for use. | |
132 | * | |
133 | * @return a {@link BasicReader} | |
134 | */ | |
135 | public static BasicReader getReader() { | |
136 | switch (defaultType) { | |
137 | // case LOCAL: | |
138 | // return new LocalReader().setType(ReaderType.LOCAL); | |
139 | case CLI: | |
140 | return new CliReader().setType(ReaderType.CLI); | |
141 | } | |
142 | ||
143 | return null; | |
144 | } | |
145 | ||
146 | /** | |
147 | * The default {@link ReaderType} used when calling | |
148 | * {@link BasicReader#getReader()}. | |
149 | * | |
150 | * @return the default type | |
151 | */ | |
152 | public static ReaderType getDefaultReaderType() { | |
153 | return defaultType; | |
154 | } | |
155 | ||
156 | /** | |
157 | * The default {@link ReaderType} used when calling | |
158 | * {@link BasicReader#getReader()}. | |
159 | * | |
160 | * @param defaultType | |
161 | * the new default type | |
162 | */ | |
163 | public static void setDefaultReaderType(ReaderType defaultType) { | |
164 | BasicReader.defaultType = defaultType; | |
165 | } | |
89cb07a6 | 166 | } |