1 package fi.jyu.mit.ohj2;
2 import java.io.*;
3 import java.util.*;
4
5
47 public class Help {
48 private static final char COMMENT = ';';
49 private static final char TOPIC_START = '[';
50 private static final char TOPIC_END = ']';
51 private static final char HELP_PAUSE = '#';
52 private static final char QUIT_CHAR = 'q';
53 private static final int DEFAULT_LINES = 23;
54 private static final String INDEX = "SISÄLLYS";
55
56 private final Map<String,Collection<String>> topics = new HashMap<String,Collection<String>>();
57 private int lines = DEFAULT_LINES;
58 private int printedLineCounter = 0;
59 private final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
60
61
64 public int getLines() { return lines; }
65
66
69 public void setLines(int lines) {
70 if ( lines > 0 )
71 this.lines = lines;
72 }
73
74
78 private static boolean quit(String input) {
79 return firstIs(input, QUIT_CHAR);
80 }
81
82
87 private static boolean firstIs(String s, char c) {
88 if ( s == null || s.length() == 0 )
89 return false;
90
91 return (Character.toUpperCase(s.charAt(0)) == Character.toUpperCase(c));
92 }
93
94
99 private boolean helpStop() {
100 String input = null;
101
102 if (printedLineCounter != 0) {
103 printedLineCounter = 0;
104 System.out.print("Jatka ENTER > ");
105 try {
106 input = in.readLine();
107 } catch (IOException ioe) {
108 System.out.println(ioe);
109 }
110 }
111
112 return quit(input);
113 }
114
115
127 @SuppressWarnings("unchecked")
128 public final Collection<String> addTopic(String topic) {
129 String uptopic = topic.toUpperCase(); Collection<String> newtopic = topics.get(uptopic);
131 if ( newtopic != null ) return newtopic;
132
133 newtopic = new ArrayList();
134 topics.put(uptopic,newtopic);
135 return newtopic;
136 }
137
138
144 @SuppressWarnings("unchecked")
145 public void addLine(String topic, String line) {
146 Collection topicLines = addTopic(topic);
147 topicLines.add(line);
148 }
149
150
154 public Help() {
155 }
157
158
168 @SuppressWarnings("unchecked")
169 public final void readFile(String fileName) throws IOException {
170 BufferedReader in = new BufferedReader(new FileReader(fileName));
171 Collection topic = null;
172 String line;
173 try {
174
175 while ( (line = in.readLine()) != null ) {
176 int comment = line.indexOf(COMMENT);
177
178 if ( comment >= 0 ) line = line.substring(0, comment);
179
180 if ( firstIs(line, TOPIC_START) ) {
181 int topicEnd = line.indexOf(TOPIC_END);
182
183 if (topicEnd >= 0) {
184 String topicName = line.substring(1, topicEnd);
185
186 topic = addTopic(topicName);
187 }
188 } else {
189 if (topic != null)
190 topic.add(line);
191 }
192 }
193 } finally {
194 in.close();
195 }
196 }
197
198
206 public Help(String fileName) throws IOException {
207 readFile(fileName);
208 }
209
210
216 private boolean helpPrint(String text) {
217 if ( firstIs(text, HELP_PAUSE) ) return helpStop();
218
219 System.out.println(text);
220 printedLineCounter++;
221
222 if ( printedLineCounter >= lines ) return helpStop();
223
224 return false;
225 }
226
227
232 public boolean printTopic(String topic) {
233 Collection<String> topicText = topics.get(topic.toUpperCase());
235 if ( topicText == null )
236 return helpPrint("Aihetta " + topic + " ei löydy");
237
238 for (Iterator<String> i = topicText.iterator(); i.hasNext(); )
239 if ( helpPrint(i.next()) )
240 return true;
241
242 return false;
243 }
244
245
250 public boolean printMatchingTopics(String topic) {
251 printedLineCounter = 0;
252
253 if ( WildChars.containsWildChars(topic) ) {
254 for ( Iterator<String> i = topics.keySet().iterator(); i.hasNext() ; ) {
255 String s = i.next();
256 if ( WildChars.onkoSamat(s, topic) && printTopic(s) )
257 return true;
258 }
259 return false;
260 }
261 return printTopic(topic);
262 }
263
264
269 public void browse(String topic) {
270 String newtopic = topic;
271 while (true) {
272 try {
273 if (newtopic == null) newtopic = INDEX;
274
275 if ( printMatchingTopics(newtopic) ) return;
276
277 System.out.print("Valitse aihe (voi olla myös *) > ");
278 newtopic = in.readLine();
279 if ( newtopic.length() == 0 ) break;
280 if ( quit(newtopic) ) break;
281 }
282 catch (IOException ioe) {
283 System.out.println(ioe);
284 }
285 }
286 }
287
288
291 public void browse() { browse(null); }
292
293
297 public void helpTopic(String topic) {
298 if ( topic == null ) browse();
299 else printMatchingTopics(topic);
300 }
301
302
303
307 public static void main(String[] args) {
308 try {
309 Help h = new Help("kerho.txt");
310 h.helpPrint("Terve!");
311 h.helpTopic("Lisäys");
312 h.helpPrint("#");
313 h.browse();
314 h.addLine("Uusi otsikko","Eka rivi");
315 h.addLine("Uusi otsikko","Toka rivi");
316 h.printMatchingTopics("Uusi*");
317 }
318 catch (IOException ioe) {
319 System.err.println(ioe);
320 }
321 }
322 }
323