|
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 |
26689
| public TextInput(final File file) throws IOException {
|
|
74 |
26689
| this(file, file.getAbsoluteFile().toURI().toURL());
|
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
26689
| public TextInput(final File file, final URL address) throws IOException {
|
|
87 |
26689
| if (file == null || address == null) {
|
|
88 |
0
| throw new NullPointerException(
|
|
89 |
| "no null pointer as argument accepted"); |
|
90 |
| } |
|
91 |
26689
| this.source = new StringBuffer();
|
|
92 |
26689
| IoUtility.loadFile(file, source);
|
|
93 |
26689
| this.address = address;
|
|
94 |
26689
| 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 |
| public final int read() { |
|
158 |
| if (position >= source.length()) { |
|
159 |
32
| return EOF;
|
|
160 |
| } |
|
161 |
| if (getChar() == CR) { |
|
162 |
74379346
| lineNumber++;
|
|
163 |
74379346
| column = 0;
|
|
164 |
| } else { |
|
165 |
| column++; |
|
166 |
| } |
|
167 |
| return source.charAt(position++); |
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
469558
| public final int readInverse() {
|
|
179 |
469558
| if (position <= 0) {
|
|
180 |
1
| return -1;
|
|
181 |
| } |
|
182 |
469557
| final char c = source.charAt(--position);
|
|
183 |
469557
| if (c == CR) {
|
|
184 |
35
| lineNumber--;
|
|
185 |
35
| int pos = source.lastIndexOf("" + CR, position - 1);
|
|
186 |
35
| if (pos < 0) {
|
|
187 |
2
| column = position;
|
|
188 |
| } else { |
|
189 |
33
| column = position - 1 - pos;
|
|
190 |
| } |
|
191 |
| } else { |
|
192 |
469522
| column--;
|
|
193 |
469522
| if (column < 0) {
|
|
194 |
0
| throw new IllegalStateException("column less then 0");
|
|
195 |
| } |
|
196 |
| } |
|
197 |
469557
| 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 |
| public final int getChar() { |
|
229 |
| if (position >= source.length()) { |
|
230 |
386
| return -1;
|
|
231 |
| } |
|
232 |
| 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 |
16838
| public final void skipWhiteSpace() {
|
|
259 |
16838
| while (!isEmpty() && Character.isWhitespace((char) getChar())) {
|
|
260 |
3460
| 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 |
26691
| public final void skipBackToBeginOfXmlTag() {
|
|
282 |
26691
| if ('<' == getChar()) {
|
|
283 |
1
| return;
|
|
284 |
| } |
|
285 |
26690
| boolean quoted = false;
|
|
286 |
26690
| do {
|
|
287 |
469545
| if (-1 == readInverse()) {
|
|
288 |
0
| throw new IllegalArgumentException("begin of xml tag not found");
|
|
289 |
| } |
|
290 |
469545
| if ('\"' == getChar()) {
|
|
291 |
41882
| quoted = !quoted;
|
|
292 |
| } |
|
293 |
469545
| } 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 |
6568
| public final String readNextXmlName() {
|
|
329 |
6568
| skipWhiteSpace();
|
|
330 |
6568
| if (isEmpty() || '=' == getChar() || '>' == getChar()) {
|
|
331 |
0
| throw new IllegalArgumentException(
|
|
332 |
| "begin of attribute expected"); |
|
333 |
| } |
|
334 |
6568
| StringBuffer buffer = new StringBuffer();
|
|
335 |
6568
| while (!isEmpty() && '=' != getChar() && '>' != getChar()
|
|
336 |
| && !Character.isWhitespace((char) getChar())) { |
|
337 |
26933
| buffer.append((char) read());
|
|
338 |
| } |
|
339 |
6568
| return buffer.toString();
|
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
3416
| public final String readNextAttributeValue() {
|
|
351 |
3416
| skipWhiteSpace();
|
|
352 |
3416
| if (isEmpty() || '=' != getChar()) {
|
|
353 |
0
| throw new IllegalArgumentException(
|
|
354 |
| "\"=\" expected"); |
|
355 |
| } |
|
356 |
3416
| read();
|
|
357 |
3416
| skipWhiteSpace();
|
|
358 |
3416
| if (isEmpty() || '>' == getChar()) {
|
|
359 |
0
| throw new IllegalArgumentException(
|
|
360 |
| "attribute value expected"); |
|
361 |
| } |
|
362 |
3416
| StringBuffer buffer = new StringBuffer();
|
|
363 |
3416
| if ('\"' == getChar()) {
|
|
364 |
3416
| read();
|
|
365 |
3416
| while (!isEmpty() && '\"' != getChar()) {
|
|
366 |
22522
| buffer.append((char) read());
|
|
367 |
| } |
|
368 |
3416
| if ('\"' != getChar()) {
|
|
369 |
0
| throw new IllegalArgumentException("\" expected");
|
|
370 |
| } |
|
371 |
3416
| read();
|
|
372 |
| } else { |
|
373 |
0
| while (!isEmpty() && '>' != getChar()
|
|
374 |
| && !Character.isWhitespace((char) getChar())) { |
|
375 |
0
| buffer.append((char) read());
|
|
376 |
| } |
|
377 |
| } |
|
378 |
3416
| return buffer.toString();
|
|
379 |
| } |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
112937
| public final boolean isEmpty() {
|
|
387 |
112937
| 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
| |