1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.qedeq.kernel.utility; |
19 |
| |
20 |
| import java.io.File; |
21 |
| import java.io.IOException; |
22 |
| import java.io.InputStream; |
23 |
| import java.net.URL; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public final class TextInput extends InputStream { |
36 |
| |
37 |
| |
38 |
| public static final int EOF = -1; |
39 |
| |
40 |
| |
41 |
| |
42 |
| public static final char CR = '\012'; |
43 |
| |
44 |
| |
45 |
| private static final String MARKER = "#####"; |
46 |
| |
47 |
| |
48 |
| private final StringBuffer source; |
49 |
| |
50 |
| |
51 |
| private int lineNumber = 0; |
52 |
| |
53 |
| |
54 |
| private int column = 0; |
55 |
| |
56 |
| |
57 |
| private int position = 0; |
58 |
| |
59 |
| |
60 |
| private final URL address; |
61 |
| |
62 |
| |
63 |
| private final URL localAddress; |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
3161
| public TextInput(final File file) throws IOException {
|
74 |
3161
| this(file, file.getAbsoluteFile().toURI().toURL());
|
75 |
| } |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
3161
| public TextInput(final File file, final URL address) throws IOException {
|
87 |
3161
| if (file == null || address == null) {
|
88 |
0
| throw new NullPointerException(
|
89 |
| "no null pointer as argument accepted"); |
90 |
| } |
91 |
3161
| this.source = new StringBuffer();
|
92 |
3161
| IoUtility.loadFile(file, source);
|
93 |
3161
| this.address = address;
|
94 |
3161
| this.localAddress = file.getAbsoluteFile().toURI().toURL();
|
95 |
| } |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
83
| public TextInput(final StringBuffer source) {
|
105 |
83
| if (source == null) {
|
106 |
1
| throw new NullPointerException(
|
107 |
| "no null pointer as argument accepted"); |
108 |
| } |
109 |
82
| this.source = source;
|
110 |
82
| this.address = null;
|
111 |
82
| this.localAddress = null;
|
112 |
| } |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
37
| public TextInput(final String source) {
|
121 |
37
| if (source == null) {
|
122 |
1
| throw new NullPointerException(
|
123 |
| "no null pointer as argument accepted"); |
124 |
| } |
125 |
36
| this.source = new StringBuffer(source);
|
126 |
36
| this.address = null;
|
127 |
36
| this.localAddress = null;
|
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
0
| public TextInput(final URL url) throws IOException {
|
138 |
0
| if (url == null) {
|
139 |
0
| throw new NullPointerException(
|
140 |
| "no null pointer as argument accepted"); |
141 |
| } |
142 |
0
| this.source = new StringBuffer();
|
143 |
0
| IoUtility.loadFile(url, source);
|
144 |
0
| this.address = url;
|
145 |
0
| this.localAddress = url;
|
146 |
| } |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
274863642
| public final int read() {
|
158 |
274863642
| if (position >= source.length()) {
|
159 |
32
| return EOF;
|
160 |
| } |
161 |
274863610
| if (getChar() == CR) {
|
162 |
7262485
| lineNumber++;
|
163 |
7262485
| column = 0;
|
164 |
| } else { |
165 |
267601125
| column++;
|
166 |
| } |
167 |
274863610
| return source.charAt(position++);
|
168 |
| } |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
72165
| public final int readInverse() {
|
179 |
72165
| if (position <= 0) {
|
180 |
1
| return -1;
|
181 |
| } |
182 |
72164
| final char c = source.charAt(--position);
|
183 |
72164
| if (c == CR) {
|
184 |
7
| lineNumber--;
|
185 |
7
| int pos = source.lastIndexOf("" + CR, position - 1);
|
186 |
7
| if (pos < 0) {
|
187 |
2
| column = position;
|
188 |
| } else { |
189 |
5
| column = position - 1 - pos;
|
190 |
| } |
191 |
| } else { |
192 |
72157
| column--;
|
193 |
72157
| if (column < 0) {
|
194 |
0
| throw new IllegalStateException("column less then 0");
|
195 |
| } |
196 |
| } |
197 |
72164
| return c;
|
198 |
| } |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
2
| public final String readString(final int number) {
|
208 |
2
| final StringBuffer result = new StringBuffer(number);
|
209 |
2
| for (int i = 0; i < number; i++) {
|
210 |
10
| final int c = read();
|
211 |
10
| if (c != -1) {
|
212 |
10
| result.append((char) c);
|
213 |
| } else { |
214 |
0
| break;
|
215 |
| } |
216 |
| } |
217 |
2
| return result.toString();
|
218 |
| } |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
275401200
| public final int getChar() {
|
229 |
275401200
| if (position >= source.length()) {
|
230 |
374
| return -1;
|
231 |
| } |
232 |
275400826
| return source.charAt(position);
|
233 |
| } |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
173
| public final int getChar(final int skip) {
|
246 |
173
| if (position + skip < 0 || position + skip >= source.length()) {
|
247 |
20
| return -1;
|
248 |
| } |
249 |
153
| return source.charAt(position + skip);
|
250 |
| } |
251 |
| |
252 |
| |
253 |
| |
254 |
| |
255 |
| |
256 |
| |
257 |
| |
258 |
1953
| public final void skipWhiteSpace() {
|
259 |
1953
| while (!isEmpty() && Character.isWhitespace((char) getChar())) {
|
260 |
453
| read();
|
261 |
| } |
262 |
| } |
263 |
| |
264 |
| |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
3
| public final void skipWhiteSpaceInverse() {
|
270 |
3
| while (getPosition() > 0 && Character.isWhitespace((char) getChar(-1))) {
|
271 |
10
| readInverse();
|
272 |
| } |
273 |
| } |
274 |
| |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
3163
| public final void skipBackToBeginOfXmlTag() {
|
282 |
3163
| if ('<' == getChar()) {
|
283 |
1
| return;
|
284 |
| } |
285 |
3162
| boolean quoted = false;
|
286 |
3162
| do {
|
287 |
72152
| if (-1 == readInverse()) {
|
288 |
0
| throw new IllegalArgumentException("begin of xml tag not found");
|
289 |
| } |
290 |
72152
| if ('\"' == getChar()) {
|
291 |
5328
| quoted = !quoted;
|
292 |
| } |
293 |
72152
| } while (quoted || '<' != getChar());
|
294 |
| } |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
0
| public final void skipForwardToEndOfXmlTag() {
|
303 |
0
| if ('>' == getChar()) {
|
304 |
0
| return;
|
305 |
| } |
306 |
0
| boolean quoted = false;
|
307 |
0
| while ('>' != getChar()) {
|
308 |
0
| if ('\"' == getChar()) {
|
309 |
0
| quoted = !quoted;
|
310 |
| } |
311 |
0
| if (!quoted) {
|
312 |
0
| if (-1 == read()) {
|
313 |
0
| throw new IllegalArgumentException("end of xml tag not found");
|
314 |
| } |
315 |
| } |
316 |
| } |
317 |
0
| read();
|
318 |
| } |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
704
| public final String readNextXmlName() {
|
329 |
704
| skipWhiteSpace();
|
330 |
704
| if (isEmpty() || '=' == getChar() || '>' == getChar()) {
|
331 |
0
| throw new IllegalArgumentException(
|
332 |
| "begin of attribute expected"); |
333 |
| } |
334 |
704
| StringBuffer buffer = new StringBuffer();
|
335 |
704
| while (!isEmpty() && '=' != getChar() && '>' != getChar()
|
336 |
| && !Character.isWhitespace((char) getChar())) { |
337 |
4174
| buffer.append((char) read());
|
338 |
| } |
339 |
704
| return buffer.toString();
|
340 |
| } |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
| |
349 |
| |
350 |
409
| public final String readNextAttributeValue() {
|
351 |
409
| skipWhiteSpace();
|
352 |
409
| if (isEmpty() || '=' != getChar()) {
|
353 |
0
| throw new IllegalArgumentException(
|
354 |
| "\"=\" expected"); |
355 |
| } |
356 |
409
| read();
|
357 |
409
| skipWhiteSpace();
|
358 |
409
| if (isEmpty() || '>' == getChar()) {
|
359 |
0
| throw new IllegalArgumentException(
|
360 |
| "attribute value expected"); |
361 |
| } |
362 |
409
| StringBuffer buffer = new StringBuffer();
|
363 |
409
| if ('\"' == getChar()) {
|
364 |
409
| read();
|
365 |
409
| while (!isEmpty() && '\"' != getChar()) {
|
366 |
5782
| buffer.append((char) read());
|
367 |
| } |
368 |
409
| if ('\"' != getChar()) {
|
369 |
0
| throw new IllegalArgumentException("\" expected");
|
370 |
| } |
371 |
409
| read();
|
372 |
| } else { |
373 |
0
| while (!isEmpty() && '>' != getChar()
|
374 |
| && !Character.isWhitespace((char) getChar())) { |
375 |
0
| buffer.append((char) read());
|
376 |
| } |
377 |
| } |
378 |
409
| return buffer.toString();
|
379 |
| } |
380 |
| |
381 |
| |
382 |
| |
383 |
| |
384 |
| |
385 |
| |
386 |
34797
| public final boolean isEmpty() {
|
387 |
34797
| return position >= source.length();
|
388 |
| } |
389 |
| |
390 |
| |
391 |
| |
392 |
| |
393 |
| |
394 |
| |
395 |
| |
396 |
8
| public final boolean isEmpty(final int skip) {
|
397 |
8
| return position + skip >= source.length();
|
398 |
| } |
399 |
| |
400 |
| |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
6
| public final String readLetterDigitString() {
|
410 |
6
| skipWhiteSpace();
|
411 |
6
| if (isEmpty() || !Character.isLetterOrDigit((char) getChar())) {
|
412 |
3
| read();
|
413 |
3
| throw new IllegalArgumentException(
|
414 |
| "letter or digit expected"); |
415 |
| } |
416 |
3
| StringBuffer buffer = new StringBuffer();
|
417 |
3
| while (!isEmpty() && Character.isLetterOrDigit((char) getChar())) {
|
418 |
19
| buffer.append((char) read());
|
419 |
| } |
420 |
3
| return buffer.toString();
|
421 |
| } |
422 |
| |
423 |
| |
424 |
| |
425 |
| |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
| |
431 |
| |
432 |
| |
433 |
| |
434 |
| |
435 |
5
| public final String readCounter() {
|
436 |
5
| skipWhiteSpace();
|
437 |
5
| if (isEmpty()) {
|
438 |
0
| throw new IllegalArgumentException("integer expected");
|
439 |
| } |
440 |
5
| StringBuffer buffer = new StringBuffer();
|
441 |
5
| if (getChar() == '-') {
|
442 |
0
| buffer.append(read());
|
443 |
| } |
444 |
5
| final int begin = getPosition();
|
445 |
5
| if (!Character.isDigit((char) getChar())) {
|
446 |
2
| throw new IllegalArgumentException("digit expected");
|
447 |
| } |
448 |
3
| while (!isEmpty() && Character.isDigit((char) getChar())) {
|
449 |
13
| buffer.append((char) read());
|
450 |
| } |
451 |
3
| if (buffer.length() >= 2 && ('0' == buffer.charAt(0)
|
452 |
| || '-' == buffer.charAt(0) && '0' == buffer.charAt(1))) { |
453 |
1
| setPosition(begin);
|
454 |
1
| throw new IllegalArgumentException("no leading zeros allowed");
|
455 |
| } |
456 |
2
| return buffer.toString();
|
457 |
| } |
458 |
| |
459 |
| |
460 |
| |
461 |
| |
462 |
| |
463 |
| |
464 |
| |
465 |
| |
466 |
| |
467 |
| |
468 |
| |
469 |
5
| public final String readQuoted() {
|
470 |
5
| skipWhiteSpace();
|
471 |
5
| if (isEmpty() || read() != '\"') {
|
472 |
1
| throw new IllegalArgumentException(
|
473 |
| "\" expected"); |
474 |
| } |
475 |
4
| StringBuffer unquoted = new StringBuffer();
|
476 |
4
| char c;
|
477 |
4
| do {
|
478 |
14
| if (isEmpty()) {
|
479 |
0
| throw new IllegalArgumentException(
|
480 |
| "ending \" expected"); |
481 |
| } |
482 |
14
| c = (char) read();
|
483 |
14
| if (c != '\"') {
|
484 |
9
| unquoted.append(c);
|
485 |
| } else { |
486 |
5
| if (isEmpty() || getChar() != '\"') {
|
487 |
4
| break;
|
488 |
| } |
489 |
1
| unquoted.append((char) read());
|
490 |
| } |
491 |
| } while (true); |
492 |
4
| return unquoted.toString();
|
493 |
| } |
494 |
| |
495 |
| |
496 |
| |
497 |
| |
498 |
| |
499 |
| |
500 |
272591946
| public final int getRow() {
|
501 |
272591946
| return lineNumber + 1;
|
502 |
| } |
503 |
| |
504 |
| |
505 |
| |
506 |
| |
507 |
| |
508 |
| |
509 |
185002
| public final int getColumn() {
|
510 |
185002
| return column + 1;
|
511 |
| } |
512 |
| |
513 |
| |
514 |
| |
515 |
| |
516 |
| |
517 |
| |
518 |
2
| public final String getLine() {
|
519 |
2
| int min = position - 1;
|
520 |
2
| while (min >= 0 && source.charAt(min) != CR) {
|
521 |
0
| min--;
|
522 |
| } |
523 |
2
| int max = position;
|
524 |
2
| while (max < source.length()
|
525 |
| && source.charAt(max) != CR) { |
526 |
120
| max++;
|
527 |
| } |
528 |
2
| if (min + 1 >= max) {
|
529 |
0
| return "";
|
530 |
| } |
531 |
2
| return source.substring(min + 1, max);
|
532 |
| } |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
25961
| public final int getPosition() {
|
540 |
25961
| return position;
|
541 |
| } |
542 |
| |
543 |
| |
544 |
| |
545 |
| |
546 |
| |
547 |
| |
548 |
| |
549 |
5
| public final int getMaximumPosition() {
|
550 |
5
| return source.length();
|
551 |
| } |
552 |
| |
553 |
| |
554 |
| |
555 |
| |
556 |
| |
557 |
| |
558 |
7395
| public final void setPosition(final int position) {
|
559 |
7395
| if (position >= source.length()) {
|
560 |
445
| this.position = source.length();
|
561 |
6950
| } else if (this.position != position) {
|
562 |
4718
| this.position = 0;
|
563 |
4718
| this.lineNumber = 0;
|
564 |
4718
| this.column = 0;
|
565 |
4718
| for (int i = 0; i < position; i++) {
|
566 |
2103816
| read();
|
567 |
| } |
568 |
| } |
569 |
| } |
570 |
| |
571 |
| |
572 |
| |
573 |
| |
574 |
| |
575 |
| |
576 |
6031
| public final void setRow(final int row) {
|
577 |
6031
| int r = row;
|
578 |
| |
579 |
6031
| if (r <= 0) {
|
580 |
0
| r = 1;
|
581 |
| } |
582 |
| |
583 |
6031
| if (getRow() == r) {
|
584 |
443
| return;
|
585 |
| } |
586 |
| |
587 |
5588
| if (getPosition() >= source.length() && getRow() >= r) {
|
588 |
0
| return;
|
589 |
| } |
590 |
5588
| if (getRow() > r) {
|
591 |
| |
592 |
0
| this.position = 0;
|
593 |
0
| this.lineNumber = 0;
|
594 |
0
| this.column = 0;
|
595 |
| } |
596 |
5588
| for (int i = 0; getRow() < r; i++) {
|
597 |
272568008
| if (EOF == read()) {
|
598 |
0
| return;
|
599 |
| } |
600 |
| } |
601 |
| } |
602 |
| |
603 |
| |
604 |
| |
605 |
| |
606 |
| |
607 |
| |
608 |
| |
609 |
| |
610 |
6027
| public final void setColumn(final int column) {
|
611 |
6027
| int c = column;
|
612 |
| |
613 |
6027
| if (c <= 0) {
|
614 |
0
| c = 1;
|
615 |
| } |
616 |
| |
617 |
6027
| if (getColumn() == c) {
|
618 |
0
| return;
|
619 |
| } |
620 |
6027
| if (getColumn() > c) {
|
621 |
1
| do {
|
622 |
17
| this.position--;
|
623 |
17
| this.column--;
|
624 |
17
| } while (getColumn() > c);
|
625 |
1
| return;
|
626 |
| } |
627 |
6026
| while (getChar() != CR && getChar() != EOF && getColumn() < c) {
|
628 |
164744
| read();
|
629 |
| } |
630 |
| } |
631 |
| |
632 |
| |
633 |
| |
634 |
| |
635 |
| |
636 |
| |
637 |
0
| public final URL getAddress() {
|
638 |
0
| return this.address;
|
639 |
| } |
640 |
| |
641 |
| |
642 |
| |
643 |
| |
644 |
| |
645 |
| |
646 |
9775
| public final URL getLocalAddress() {
|
647 |
9775
| return this.localAddress;
|
648 |
| } |
649 |
| |
650 |
| |
651 |
| |
652 |
| |
653 |
| |
654 |
| |
655 |
0
| public final String showLinePosition() {
|
656 |
0
| final String line = getLine();
|
657 |
0
| final StringBuffer buffer = new StringBuffer();
|
658 |
0
| final int col = getColumn() - 1;
|
659 |
0
| if (col > 0) {
|
660 |
0
| if (col < line.length()) {
|
661 |
0
| buffer.append(line.substring(0, col));
|
662 |
| } else { |
663 |
0
| buffer.append(line);
|
664 |
| } |
665 |
| } |
666 |
0
| buffer.append(MARKER);
|
667 |
0
| if (col < line.length()) {
|
668 |
0
| buffer.append(line.substring(col));
|
669 |
| } |
670 |
0
| return buffer.toString();
|
671 |
| } |
672 |
| |
673 |
| } |