|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
AbstractOutput | Line # 27 | 104 | 55 | 75.8% |
0.7582418
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(55) | |||
Result | |||
0.510989
|
org.qedeq.base.io.StringOutputTest.testComplete
![]() |
1 PASS | |
0.50549453
|
org.qedeq.base.io.AbstractOutputTest.testComplete
![]() |
1 PASS | |
0.5
|
org.qedeq.base.io.TextOutputTest.testComplete
![]() |
1 PASS | |
0.48351648
|
org.qedeq.base.io.AbstractOutputTest.testPrintChar02
![]() |
1 PASS | |
0.47252747
|
org.qedeq.base.io.AbstractOutputTest.testPrintString01
![]() |
1 PASS | |
0.46703297
|
org.qedeq.base.io.AbstractOutputTest.testPrintChar01
![]() |
1 PASS | |
0.46703297
|
org.qedeq.base.io.AbstractOutputTest.testPrintString04
![]() |
1 PASS | |
0.46703297
|
org.qedeq.base.io.AbstractOutputTest.testPrintString02
![]() |
1 PASS | |
0.44505495
|
org.qedeq.base.io.AbstractOutputTest.testPrintString03
![]() |
1 PASS | |
0.43406594
|
org.qedeq.base.io.TextOutputTest.testErrorStream
![]() |
1 PASS | |
0.4010989
|
org.qedeq.base.io.TextOutputTest.testPushPop
![]() |
1 PASS | |
0.4010989
|
org.qedeq.base.io.StringOutputTest.testPushPop
![]() |
1 PASS | |
0.3956044
|
org.qedeq.base.io.StringOutputTest.testGetPosition
![]() |
1 PASS | |
0.3956044
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken20
![]() |
1 PASS | |
0.3956044
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken19
![]() |
1 PASS | |
0.3956044
|
org.qedeq.base.io.AbstractOutputTest.testPushPop
![]() |
1 PASS | |
0.3846154
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken01
![]() |
1 PASS | |
0.3846154
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken22
![]() |
1 PASS | |
0.3846154
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken21
![]() |
1 PASS | |
0.3846154
|
org.qedeq.base.io.AbstractOutputTest.testAdd18
![]() |
1 PASS | |
0.37362638
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken18
![]() |
1 PASS | |
0.37362638
|
org.qedeq.base.io.AbstractOutputTest.testAdd06
![]() |
1 PASS | |
0.36813188
|
org.qedeq.base.io.AbstractOutputTest.testAdd03
![]() |
1 PASS | |
0.36813188
|
org.qedeq.base.io.AbstractOutputTest.testAdd11
![]() |
1 PASS | |
0.36813188
|
org.qedeq.base.io.AbstractOutputTest.testAdd05
![]() |
1 PASS | |
0.36813188
|
org.qedeq.base.io.AbstractOutputTest.testAdd08
![]() |
1 PASS | |
0.36813188
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken16
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAdd16
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken14
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.TextOutputTest.testGetPosition
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken15
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken12
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAdd12
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken08
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken09
![]() |
1 PASS | |
0.36263737
|
org.qedeq.base.io.AbstractOutputTest.testAdd17
![]() |
1 PASS | |
0.35714287
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken11
![]() |
1 PASS | |
0.35714287
|
org.qedeq.base.io.AbstractOutputTest.testAdd01
![]() |
1 PASS | |
0.35714287
|
org.qedeq.base.io.AbstractOutputTest.testAdd02
![]() |
1 PASS | |
0.35714287
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken06
![]() |
1 PASS | |
0.35164836
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken02
![]() |
1 PASS | |
0.35164836
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken13
![]() |
1 PASS | |
0.35164836
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken05
![]() |
1 PASS | |
0.35164836
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken04
![]() |
1 PASS | |
0.34615386
|
org.qedeq.base.io.AbstractOutputTest.testAdd14
![]() |
1 PASS | |
0.34615386
|
org.qedeq.base.io.AbstractOutputTest.testAdd15
![]() |
1 PASS | |
0.34065935
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken03
![]() |
1 PASS | |
0.32967034
|
org.qedeq.base.io.AbstractOutputTest.testAdd10
![]() |
1 PASS | |
0.32417583
|
org.qedeq.base.io.AbstractOutputTest.testAdd13
![]() |
1 PASS | |
0.32417583
|
org.qedeq.base.io.AbstractOutputTest.testAdd09
![]() |
1 PASS | |
0.32417583
|
org.qedeq.base.io.AbstractOutputTest.testAdd07
![]() |
1 PASS | |
0.31868133
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken17
![]() |
1 PASS | |
0.31868133
|
org.qedeq.base.io.AbstractOutputTest.testAdd04
![]() |
1 PASS | |
0.30769232
|
org.qedeq.base.io.AbstractOutputTest.testAddWsToken07
![]() |
1 PASS | |
0.0054945056
|
org.qedeq.base.io.TextOutputTest.testConstructorAndGetName
![]() |
1 PASS | |
1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org | |
2 | * | |
3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. | |
4 | * | |
5 | * "Hilbert II" is free software; you can redistribute | |
6 | * it and/or modify it under the terms of the GNU General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | */ | |
15 | ||
16 | package org.qedeq.base.io; | |
17 | ||
18 | import org.qedeq.base.utility.Splitter; | |
19 | import org.qedeq.base.utility.StringUtility; | |
20 | ||
21 | ||
22 | /** | |
23 | * Wraps a text output stream. | |
24 | * | |
25 | * @author Michael Meyling | |
26 | */ | |
27 | public abstract class AbstractOutput { | |
28 | ||
29 | /** Tab level. */ | |
30 | private StringBuffer spaces = new StringBuffer(); | |
31 | ||
32 | /** Break at this column if greater zero. */ | |
33 | private int breakAt; | |
34 | ||
35 | /** Tab level of current line. This is equal to spaces before any character is | |
36 | * written. After writing to the current line this value is fixed and doesn't change even | |
37 | * if the tab level is changed. | |
38 | */ | |
39 | private String spacesForCurrentLine = ""; | |
40 | ||
41 | /** Current column. */ | |
42 | private int col; | |
43 | ||
44 | /** Token buffer. */ | |
45 | private StringBuffer tokenBuffer = new StringBuffer(); | |
46 | ||
47 | /** Whitespace buffer. */ | |
48 | private StringBuffer wsBuffer = new StringBuffer(); | |
49 | ||
50 | /** | |
51 | * Constructor. | |
52 | */ | |
53 | 57 |
![]() |
54 | } | |
55 | ||
56 | /** | |
57 | * Add whitespace to output. | |
58 | * | |
59 | * @param ws Add this whitespace. | |
60 | */ | |
61 | 70 |
![]() |
62 | 70 | final String[] lines = StringUtility.split(ws, "\n"); |
63 | 150 | for (int i = 0; i < lines.length; i++) { |
64 | 80 | if (i > 0) { |
65 | 10 | println(); |
66 | } | |
67 | 80 | addWsWithoutCR(lines[i]); |
68 | } | |
69 | } | |
70 | ||
71 | /** | |
72 | * Add whitespace to output. Must not contain CRs. | |
73 | * | |
74 | * @param ws Add this whitespace. | |
75 | */ | |
76 | 543 |
![]() |
77 | 543 | if (tokenBuffer.length() > 0) { |
78 | 271 | if (fits(wsBuffer.length() + tokenBuffer.length())) { |
79 | 236 | if (col == 0) { |
80 | 142 | appendSpaces(); |
81 | } | |
82 | 236 | append(wsBuffer.toString()); |
83 | 236 | col += wsBuffer.length(); |
84 | 236 | append(tokenBuffer.toString()); |
85 | 236 | col += tokenBuffer.length(); |
86 | } else { | |
87 | // forget non fitting part of white space | |
88 | 35 | if (col != 0) { |
89 | 27 | appendFittingPart(wsBuffer.toString()); |
90 | 27 | append("\n"); |
91 | } | |
92 | 35 | col = 0; |
93 | 35 | appendSpaces(); |
94 | 35 | append(tokenBuffer.toString()); |
95 | 35 | col += tokenBuffer.length(); |
96 | } | |
97 | 271 | wsBuffer.setLength(0); |
98 | 271 | tokenBuffer.setLength(0); |
99 | } | |
100 | 543 | wsBuffer.append(ws); |
101 | } | |
102 | ||
103 | /** | |
104 | * Append token to output. | |
105 | * | |
106 | * @param part Add this part. | |
107 | */ | |
108 | 282 |
![]() |
109 | // remember tabular spaces when we start writing | |
110 | 282 | if (col == 0 && part.length() > 0) { |
111 | 157 | setTabLevel(); |
112 | } | |
113 | 282 | tokenBuffer.append(part); |
114 | } | |
115 | ||
116 | /** | |
117 | * Flush output. | |
118 | */ | |
119 | 367 |
![]() |
120 | 367 | addWsWithoutCR(""); |
121 | 367 | appendFittingPart(wsBuffer.toString()); |
122 | 367 | wsBuffer.setLength(0); |
123 | } | |
124 | ||
125 | /** | |
126 | * Append a part of given text so that the current maximum column is not exceeded. | |
127 | * | |
128 | * @param txt Write this text. | |
129 | * @return Length of written characters. | |
130 | */ | |
131 | 394 |
![]() |
132 | 394 | final int columnsLeft = columnsLeft(); |
133 | 394 | if (columnsLeft > 0) { |
134 | 76 | final String part = StringUtility.substring(txt, 0, columnsLeft); |
135 | 76 | append(part); |
136 | 76 | col += part.length(); |
137 | 76 | return part.length(); |
138 | 318 | } else if (columnsLeft < 0) { |
139 | 281 | append(txt); |
140 | 281 | col += txt.length(); |
141 | 281 | return txt.length(); |
142 | } | |
143 | 37 | return 0; |
144 | } | |
145 | ||
146 | /** | |
147 | * Print character to output. | |
148 | * | |
149 | * @param c Append this. | |
150 | */ | |
151 | 8 |
![]() |
152 | 8 | print("" + c); |
153 | } | |
154 | ||
155 | /** | |
156 | * Print text and split at white space if text doesn't fit within maximum column size. | |
157 | * Also flushes output. | |
158 | * | |
159 | * @param text Append this. | |
160 | */ | |
161 | 110 |
![]() |
162 | 110 | flush(); |
163 | 110 | if (text == null) { |
164 | 0 | addToken("null"); |
165 | } else { | |
166 | 110 | final String[] lines = StringUtility.split(text, "\n"); |
167 | 224 | for (int i = 0; i < lines.length; i++) { |
168 | 114 | final Splitter split = new Splitter(lines[i]); |
169 | 396 | while (split.hasNext()) { |
170 | 282 | final String token = split.nextToken(); |
171 | 282 | final boolean isWhitespace = token.trim().length() == 0; |
172 | 282 | if (isWhitespace) { |
173 | 96 | addWsWithoutCR(token); |
174 | } else { | |
175 | 186 | addToken(token); |
176 | } | |
177 | } | |
178 | 114 | if (i + 1 < lines.length) { |
179 | 4 | println(); |
180 | } | |
181 | } | |
182 | } | |
183 | 110 | flush(); |
184 | } | |
185 | ||
186 | /** | |
187 | * Append text directly to output device. | |
188 | * | |
189 | * @param text Append this text. | |
190 | */ | |
191 | public abstract void append(final String text); | |
192 | ||
193 | /** | |
194 | * Get writing position. | |
195 | * | |
196 | * @return Writing position. | |
197 | */ | |
198 | public abstract long getPosition(); | |
199 | ||
200 | /** | |
201 | * Print spaces and text to output. | |
202 | * | |
203 | * @param text Append this. | |
204 | */ | |
205 | 0 |
![]() |
206 | 0 | flush(); |
207 | 0 | if (text == null) { |
208 | 0 | return; |
209 | } | |
210 | 0 | if (col == 0) { |
211 | 0 | if (text.length() > 0) { |
212 | // remember tabular spaces when we start writing | |
213 | 0 | setTabLevel(); |
214 | 0 | appendSpaces(); |
215 | } | |
216 | 0 | } else if (!fits(text)) { |
217 | 0 | println(); |
218 | 0 | appendSpaces(); |
219 | } | |
220 | 0 | append(text); |
221 | 0 | col += text.length(); |
222 | } | |
223 | ||
224 | /** | |
225 | * Does the text fit to current line? | |
226 | * | |
227 | * @param text Check if this text could be appended without line break. | |
228 | * @return Does it fit? | |
229 | */ | |
230 | 0 |
![]() |
231 | 0 | if (text == null) { |
232 | 0 | return true; |
233 | } | |
234 | 0 | return fits(text.length()); |
235 | } | |
236 | ||
237 | /** | |
238 | * Does a text with given length fit to current line? | |
239 | * TODO 20110104 m31: should't we use spacesForCurrentLine also? | |
240 | * | |
241 | * @param length Check if a text of this length could be appended without line break. | |
242 | * @return Does it fit? | |
243 | */ | |
244 | 271 |
![]() |
245 | 271 | return breakAt <= 0 || col + length <= breakAt; |
246 | } | |
247 | ||
248 | /** | |
249 | * How many columns have we left? | |
250 | * | |
251 | * @return Columns left. Returns -1 if there is no limit. | |
252 | */ | |
253 | 394 |
![]() |
254 | 394 | if (breakAt <= 0) { |
255 | 281 | return -1; |
256 | } else { | |
257 | 113 | return Math.max(0, breakAt - col); |
258 | } | |
259 | } | |
260 | ||
261 | /** | |
262 | * Print object to output. | |
263 | * | |
264 | * @param object Append text representation of this. | |
265 | */ | |
266 | 3 |
![]() |
267 | 3 | print(String.valueOf(object)); |
268 | } | |
269 | ||
270 | /** | |
271 | * Print spaces text and new line to output. | |
272 | * | |
273 | * @param token Append this. | |
274 | */ | |
275 | 79 |
![]() |
276 | 79 | print(token); |
277 | 79 | println(); |
278 | } | |
279 | ||
280 | /** | |
281 | * Print object and new line to output. | |
282 | * | |
283 | * @param object Append text representation of this. | |
284 | */ | |
285 | 3 |
![]() |
286 | 3 | println(String.valueOf(object)); |
287 | } | |
288 | ||
289 | /** | |
290 | * Print new line to output. | |
291 | */ | |
292 | 100 |
![]() |
293 | 100 | flush(); |
294 | 100 | if (col == 0 && spaces.toString().trim().length() > 0) { |
295 | 0 | setTabLevel(); |
296 | 0 | appendSpaces(); |
297 | } | |
298 | 100 | append("\n"); |
299 | 100 | col = 0; |
300 | } | |
301 | ||
302 | /** | |
303 | * Skip until given column. To do this we append spaces. | |
304 | * | |
305 | * @param column Skip to this column. | |
306 | */ | |
307 | 0 |
![]() |
308 | 0 | for (int i = col; i < column; i++) { |
309 | 0 | printWithoutSplit(" "); |
310 | } | |
311 | } | |
312 | ||
313 | /** | |
314 | * Reset tab level to zero. | |
315 | */ | |
316 | 3 |
![]() |
317 | // flush(); | |
318 | 3 | spaces.setLength(0); |
319 | } | |
320 | ||
321 | /** | |
322 | * Decrement tab level. | |
323 | */ | |
324 | 33 |
![]() |
325 | 33 | if (spaces.length() > 0) { |
326 | 27 | spaces.setLength(spaces.length() - 2); |
327 | } | |
328 | } | |
329 | ||
330 | /** | |
331 | * Decrement tab level. | |
332 | * | |
333 | * @param characters Number of characters to reduce from tab level. | |
334 | */ | |
335 | 4 |
![]() |
336 | 4 | if (spaces.length() > 0) { |
337 | 4 | spaces.setLength(Math.max(spaces.length() - characters, 0)); |
338 | } | |
339 | } | |
340 | ||
341 | /** | |
342 | * Return current tab string. | |
343 | * | |
344 | * @return Current tab string. | |
345 | */ | |
346 | 0 |
![]() |
347 | 0 | return spaces.toString(); |
348 | } | |
349 | ||
350 | /** | |
351 | * Set current tab string. | |
352 | * | |
353 | * @param level Tab string. | |
354 | */ | |
355 | 2 |
![]() |
356 | 2 | spaces.setLength(0); |
357 | 2 | spaces.append(level); |
358 | } | |
359 | ||
360 | /** | |
361 | * Increment tab level (this are two spaces). | |
362 | */ | |
363 | 56 |
![]() |
364 | 56 | spaces.append(" "); |
365 | } | |
366 | ||
367 | /** | |
368 | * Increment tab level with following symbols. | |
369 | * | |
370 | * @param symbols Symbols to tab width. Length should be exactly 2 characters! | |
371 | */ | |
372 | 9 |
![]() |
373 | 9 | spaces.append(symbols); |
374 | } | |
375 | ||
376 | /** | |
377 | * Set current tab level to current level. Might change unwritten lines. | |
378 | */ | |
379 | 158 |
![]() |
380 | 158 | spacesForCurrentLine = spaces.toString(); |
381 | } | |
382 | ||
383 | /** | |
384 | * Set number of maximum columns. If possible we break before we reach this column number. | |
385 | * If less or equal to zero no line breaking is done automatically. | |
386 | * | |
387 | * @param columns Maximum column size. | |
388 | */ | |
389 | 45 |
![]() |
390 | 45 | if (columns < 0) { |
391 | 0 | breakAt = 0; |
392 | } else { | |
393 | 45 | breakAt = columns; |
394 | } | |
395 | } | |
396 | ||
397 | /** | |
398 | * Return number of maximum columns. If equal to zero no line breaking is done automatically. | |
399 | * | |
400 | * @return Maximum column size. | |
401 | */ | |
402 | 0 |
![]() |
403 | 0 | return breakAt; |
404 | } | |
405 | ||
406 | /** | |
407 | * Append tabulation and increase current column. | |
408 | */ | |
409 | 177 |
![]() |
410 | 177 | append(spacesForCurrentLine.toString()); |
411 | 177 | col += spacesForCurrentLine.length(); |
412 | } | |
413 | ||
414 | } |
|