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 consoleIn = new BufferedReader(new InputStreamReader(System.in));
60
61 private PrintStream out = System.out;
62 private boolean stdout = true;
63
64
67 public int getLines() { return lines; }
68
69
72 public void setLines(int lines) {
73 if ( lines > 0 )
74 this.lines = lines;
75 }
76
77
81 private static boolean quit(String input) {
82 return firstIs(input, QUIT_CHAR);
83 }
84
85
90 private static boolean firstIs(String s, char c) {
91 if ( s == null || s.length() == 0 )
92 return false;
93
94 return (Character.toUpperCase(s.charAt(0)) == Character.toUpperCase(c));
95 }
96
97
102 private boolean helpStop() {
103 if ( !stdout ) return false;
104 String input = null;
105
106 if (printedLineCounter != 0) {
107 printedLineCounter = 0;
108 System.out.print("Jatka ENTER > ");
109 try {
110 input = consoleIn.readLine();
111 } catch (IOException ioe) {
112 System.out.println(ioe);
113 }
114 }
115
116 return quit(input);
117 }
118
119
131 public final Collection<String> addTopic(String topic) {
132 String uptopic = topic.toUpperCase(); Collection<String> newtopic = topics.get(uptopic);
134 if ( newtopic != null ) return newtopic;
135
136 newtopic = new ArrayList<String>();
137 topics.put(uptopic,newtopic);
138 return newtopic;
139 }
140
141
147 public void addLine(String topic, String line) {
148 Collection<String> topicLines = addTopic(topic);
149 topicLines.add(line);
150 }
151
152
156 public Help() {
157 }
159
160
161
165 public void setOut(PrintStream newout) {
166 out = newout;
167 stdout = false;
168 }
169
170
171
172
182 public final void readFile(String fileName) throws IOException {
183 @SuppressWarnings("resource")
184 BufferedReader in = new BufferedReader(new FileReader(fileName));
185 Collection<String> topic = null;
186 String line;
187 try {
188
189 while ( (line = in.readLine()) != null ) {
190 int comment = line.indexOf(COMMENT);
191
192 if ( comment >= 0 ) line = line.substring(0, comment);
193
194 if ( firstIs(line, TOPIC_START) ) {
195 int topicEnd = line.indexOf(TOPIC_END);
196
197 if (topicEnd >= 0) {
198 String topicName = line.substring(1, topicEnd);
199
200 topic = addTopic(topicName);
201 }
202 } else {
203 if (topic != null)
204 topic.add(line);
205 }
206 }
207 } finally {
208 in.close();
209 }
210 }
211
212
220 public Help(String fileName) throws IOException {
221 readFile(fileName);
222 }
223
224
230 private boolean helpPrint(String text) {
231 if ( firstIs(text, HELP_PAUSE) ) return helpStop();
232
233 out.println(text);
234 printedLineCounter++;
235
236 if ( printedLineCounter >= lines ) return helpStop();
237
238 return false;
239 }
240
241
246 public boolean printTopic(String topic) {
247 Collection<String> topicText = topics.get(topic.toUpperCase());
249 if ( topicText == null )
250 return helpPrint("Aihetta " + topic + " ei löydy");
251
252 for (Iterator<String> i = topicText.iterator(); i.hasNext(); )
253 if ( helpPrint(i.next()) )
254 return true;
255
256 return false;
257 }
258
259
264 public boolean printMatchingTopics(String topic) {
265 printedLineCounter = 0;
266
267 if ( WildChars.containsWildChars(topic) ) {
268 for ( Iterator<String> i = topics.keySet().iterator(); i.hasNext() ; ) {
269 String s = i.next();
270 if ( WildChars.onkoSamat(s, topic) && printTopic(s) )
271 return true;
272 }
273 return false;
274 }
275 return printTopic(topic);
276 }
277
278
283 public void browse(String topic) {
284 String newtopic = topic;
285 while (true) {
286 try {
287 if (newtopic == null) newtopic = INDEX;
288
289 if ( printMatchingTopics(newtopic) || !stdout ) return;
290
291 System.out.print("Valitse aihe (voi olla myös *) > ");
292 newtopic = consoleIn.readLine();
293 if ( newtopic.length() == 0 ) break;
294 if ( quit(newtopic) ) break;
295 }
296 catch (IOException ioe) {
297 System.out.println(ioe);
298 }
299 }
300 }
301
302
305 public void browse() { browse(null); }
306
307
311 public void helpTopic(String topic) {
312 if ( topic == null ) browse();
313 else printMatchingTopics(topic);
314 }
315
316
317
321 public static void main(String[] args) {
322 try {
323 Help h = new Help(); h.helpPrint("Terve!");
325 h.helpTopic("Lisäys");
326 h.helpPrint("#");
327 h.browse();
328 h.addLine("Uusi otsikko","Eka rivi");
329 h.addLine("Uusi otsikko","Toka rivi");
330 h.printMatchingTopics("Uusi*");
331 }
332 catch (Exception ioe) {
333 System.err.println(ioe);
334 }
335 }
336 }
337