1 |
|
package fi.jyu.mit.ohj2; |
2 |
|
import java.io.*; |
3 |
|
import java.util.*; |
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@author |
45 |
|
@version |
46 |
|
|
|
|
| 0% |
Uncovered Elements: 152 (152) |
Complexity: 44 |
Complexity Density: 0,48 |
|
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 |
|
|
62 |
|
@return |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
0
|
public int getLines() { return lines; }... |
65 |
|
|
66 |
|
|
67 |
|
@param |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
69 |
0
|
public void setLines(int lines) {... |
70 |
0
|
if ( lines > 0 ) |
71 |
0
|
this.lines = lines; |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
@param |
76 |
|
@return |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
0
|
private static boolean quit(String input) {... |
79 |
0
|
return firstIs(input, QUIT_CHAR); |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
@param |
85 |
|
@return |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
87 |
0
|
private static boolean firstIs(String s, char c) {... |
88 |
0
|
if ( s == null || s.length() == 0 ) |
89 |
0
|
return false; |
90 |
|
|
91 |
0
|
return (Character.toUpperCase(s.charAt(0)) == Character.toUpperCase(c)); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@return |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0,38 |
|
99 |
0
|
private boolean helpStop() {... |
100 |
0
|
String input = null; |
101 |
|
|
102 |
0
|
if (printedLineCounter != 0) { |
103 |
0
|
printedLineCounter = 0; |
104 |
0
|
System.out.print("Jatka ENTER > "); |
105 |
0
|
try { |
106 |
0
|
input = in.readLine(); |
107 |
|
} catch (IOException ioe) { |
108 |
0
|
System.out.println(ioe); |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
0
|
return quit(input); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
@param |
125 |
|
@return |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
127 |
0
|
@SuppressWarnings("unchecked")... |
128 |
|
public final Collection<String> addTopic(String topic) { |
129 |
0
|
String uptopic = topic.toUpperCase(); |
130 |
0
|
Collection<String> newtopic = topics.get(uptopic); |
131 |
0
|
if ( newtopic != null ) return newtopic; |
132 |
|
|
133 |
0
|
newtopic = new ArrayList(); |
134 |
0
|
topics.put(uptopic,newtopic); |
135 |
0
|
return newtopic; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@param |
142 |
|
@param |
143 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
144 |
0
|
@SuppressWarnings("unchecked")... |
145 |
|
public void addLine(String topic, String line) { |
146 |
0
|
Collection topicLines = addTopic(topic); |
147 |
0
|
topicLines.add(line); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
154 |
0
|
public Help() { ... |
155 |
|
|
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
@param |
166 |
|
@throws |
167 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 6 |
Complexity Density: 0,38 |
|
168 |
0
|
@SuppressWarnings("unchecked")... |
169 |
|
public final void readFile(String fileName) throws IOException { |
170 |
0
|
BufferedReader in = new BufferedReader(new FileReader(fileName)); |
171 |
0
|
Collection topic = null; |
172 |
0
|
String line; |
173 |
0
|
try { |
174 |
|
|
175 |
0
|
while ( (line = in.readLine()) != null ) { |
176 |
0
|
int comment = line.indexOf(COMMENT); |
177 |
|
|
178 |
0
|
if ( comment >= 0 ) line = line.substring(0, comment); |
179 |
|
|
180 |
0
|
if ( firstIs(line, TOPIC_START) ) { |
181 |
0
|
int topicEnd = line.indexOf(TOPIC_END); |
182 |
|
|
183 |
0
|
if (topicEnd >= 0) { |
184 |
0
|
String topicName = line.substring(1, topicEnd); |
185 |
|
|
186 |
0
|
topic = addTopic(topicName); |
187 |
|
} |
188 |
|
} else { |
189 |
0
|
if (topic != null) |
190 |
0
|
topic.add(line); |
191 |
|
} |
192 |
|
} |
193 |
|
} finally { |
194 |
0
|
in.close(); |
195 |
|
} |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@throws |
205 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
0
|
public Help(String fileName) throws IOException {... |
207 |
0
|
readFile(fileName); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@param |
214 |
|
@return |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
216 |
0
|
private boolean helpPrint(String text) {... |
217 |
0
|
if ( firstIs(text, HELP_PAUSE) ) return helpStop(); |
218 |
|
|
219 |
0
|
System.out.println(text); |
220 |
0
|
printedLineCounter++; |
221 |
|
|
222 |
0
|
if ( printedLineCounter >= lines ) return helpStop(); |
223 |
|
|
224 |
0
|
return false; |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
@param |
230 |
|
@return |
231 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0,57 |
|
232 |
0
|
public boolean printTopic(String topic) {... |
233 |
0
|
Collection<String> topicText = topics.get(topic.toUpperCase()); |
234 |
|
|
235 |
0
|
if ( topicText == null ) |
236 |
0
|
return helpPrint("Aihetta " + topic + " ei löydy"); |
237 |
|
|
238 |
0
|
for (Iterator<String> i = topicText.iterator(); i.hasNext(); ) |
239 |
0
|
if ( helpPrint(i.next()) ) |
240 |
0
|
return true; |
241 |
|
|
242 |
0
|
return false; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
@param |
248 |
|
@return |
249 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 5 |
Complexity Density: 0,62 |
|
250 |
0
|
public boolean printMatchingTopics(String topic) {... |
251 |
0
|
printedLineCounter = 0; |
252 |
|
|
253 |
0
|
if ( WildChars.containsWildChars(topic) ) { |
254 |
0
|
for ( Iterator<String> i = topics.keySet().iterator(); i.hasNext() ; ) { |
255 |
0
|
String s = i.next(); |
256 |
0
|
if ( WildChars.onkoSamat(s, topic) && printTopic(s) ) |
257 |
0
|
return true; |
258 |
|
} |
259 |
0
|
return false; |
260 |
|
} |
261 |
0
|
return printTopic(topic); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
@param |
268 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 6 |
Complexity Density: 0,43 |
|
269 |
0
|
public void browse(String topic) {... |
270 |
0
|
String newtopic = topic; |
271 |
0
|
while (true) { |
272 |
0
|
try { |
273 |
0
|
if (newtopic == null) newtopic = INDEX; |
274 |
|
|
275 |
0
|
if ( printMatchingTopics(newtopic) ) return; |
276 |
|
|
277 |
0
|
System.out.print("Valitse aihe (voi olla myös *) > "); |
278 |
0
|
newtopic = in.readLine(); |
279 |
0
|
if ( newtopic.length() == 0 ) break; |
280 |
0
|
if ( quit(newtopic) ) break; |
281 |
|
} |
282 |
|
catch (IOException ioe) { |
283 |
0
|
System.out.println(ioe); |
284 |
|
} |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
291 |
0
|
public void browse() { browse(null); }... |
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
@param |
296 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
297 |
0
|
public void helpTopic(String topic) {... |
298 |
0
|
if ( topic == null ) browse(); |
299 |
0
|
else printMatchingTopics(topic); |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
@param |
306 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0,2 |
|
307 |
0
|
public static void main(String[] args) {... |
308 |
0
|
try { |
309 |
0
|
Help h = new Help("kerho.txt"); |
310 |
0
|
h.helpPrint("Terve!"); |
311 |
0
|
h.helpTopic("Lisäys"); |
312 |
0
|
h.helpPrint("#"); |
313 |
0
|
h.browse(); |
314 |
0
|
h.addLine("Uusi otsikko","Eka rivi"); |
315 |
0
|
h.addLine("Uusi otsikko","Toka rivi"); |
316 |
0
|
h.printMatchingTopics("Uusi*"); |
317 |
|
} |
318 |
|
catch (IOException ioe) { |
319 |
0
|
System.err.println(ioe); |
320 |
|
} |
321 |
|
} |
322 |
|
} |