1
2
3
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 package org.exolab.castor.xml.dtd.parser;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public final class InputCharStream implements CharStream {
65
66 public static final boolean staticFlag = false;
67 int bufsize;
68 int available;
69 int tokenBegin;
70 public int bufpos = -1;
71 private int bufline[];
72 private int bufcolumn[];
73
74 private int column = 0;
75 private int line = 1;
76
77 private boolean prevCharIsCR = false;
78 private boolean prevCharIsLF = false;
79
80 private java.io.Reader inputStream;
81
82 private char[] buffer;
83 private int maxNextCharInd = 0;
84 private int inBuf = 0;
85
86 private final void ExpandBuff(boolean wrapAround) {
87
88 char[] newbuffer = new char[bufsize + 2048];
89 int newbufline[] = new int[bufsize + 2048];
90 int newbufcolumn[] = new int[bufsize + 2048];
91
92 try {
93 if (wrapAround) {
94 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
95 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
96 buffer = newbuffer;
97
98 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
99 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
100 bufline = newbufline;
101
102 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
103 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
104 bufcolumn = newbufcolumn;
105
106 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
107
108 } else {
109
110 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
111 buffer = newbuffer;
112
113 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
114 bufline = newbufline;
115
116 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
117 bufcolumn = newbufcolumn;
118
119 maxNextCharInd = (bufpos -= tokenBegin);
120 }
121 } catch (Throwable t) {
122 throw new Error(t.getMessage());
123 }
124
125 bufsize += 2048;
126 available = bufsize;
127 tokenBegin = 0;
128 }
129
130 private final void FillBuff() throws java.io.IOException {
131 if (maxNextCharInd == available) {
132 if (available == bufsize) {
133 if (tokenBegin > 2048) {
134 bufpos = maxNextCharInd = 0;
135 available = tokenBegin;
136 } else if (tokenBegin < 0)
137 bufpos = maxNextCharInd = 0;
138 else
139 ExpandBuff(false);
140 } else if (available > tokenBegin)
141 available = bufsize;
142 else if ((tokenBegin - available) < 2048)
143 ExpandBuff(true);
144 else
145 available = tokenBegin;
146 }
147
148 int i;
149
150 try {
151 if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1) {
152 inputStream.close();
153 throw new java.io.IOException();
154 }
155 maxNextCharInd += i;
156 return;
157 } catch (java.io.IOException e) {
158 --bufpos;
159 backup(0);
160 if (tokenBegin == -1)
161 tokenBegin = bufpos;
162 throw e;
163 }
164 }
165
166 public final char BeginToken() throws java.io.IOException {
167
168 tokenBegin = -1;
169 char c = readChar();
170 tokenBegin = bufpos;
171 return c;
172 }
173
174 private final void UpdateLineColumn(char c) {
175
176 column++;
177 if (prevCharIsLF) {
178 prevCharIsLF = false;
179 line += (column = 1);
180 } else if (prevCharIsCR) {
181 prevCharIsCR = false;
182 if (c == '\n')
183 prevCharIsLF = true;
184 else
185 line += (column = 1);
186 }
187
188 switch (c) {
189 case '\r':
190 prevCharIsCR = true;
191 break;
192 case '\n':
193 prevCharIsLF = true;
194 break;
195 case '\t':
196 column--;
197 column += (8 - (column & 07));
198 break;
199 default:
200 break;
201 }
202
203 bufline[bufpos] = line;
204 bufcolumn[bufpos] = column;
205 }
206
207
208
209
210
211 public final char readChar() throws java.io.IOException {
212
213 if (inBuf > 0) {
214 --inBuf;
215
216
217
218
219
220 return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
221 }
222 if (++bufpos >= maxNextCharInd)
223 FillBuff();
224
225
226
227
228
229 char c = buffer[bufpos];
230
231 UpdateLineColumn(c);
232 return (c);
233 }
234
235
236
237
238
239 public final int getColumn() {
240 return bufcolumn[bufpos];
241 }
242
243
244
245
246
247 public final int getLine() {
248 return bufline[bufpos];
249 }
250
251 public final int getEndColumn() {
252 return bufcolumn[bufpos];
253 }
254
255 public final int getEndLine() {
256 return bufline[bufpos];
257 }
258
259 public final int getBeginColumn() {
260 return bufcolumn[tokenBegin];
261 }
262
263 public final int getBeginLine() {
264 return bufline[tokenBegin];
265 }
266
267 public final void backup(int amount) {
268 inBuf += amount;
269 if ((bufpos -= amount) < 0)
270 bufpos += bufsize;
271 }
272
273
274
275
276
277 public InputCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize) {
278 inputStream = dstream;
279 line = startline;
280 column = startcolumn - 1;
281 available = bufsize = buffersize;
282 buffer = new char[buffersize];
283 bufline = new int[buffersize];
284 bufcolumn = new int[buffersize];
285 }
286
287
288
289
290 public InputCharStream(java.io.Reader dstream, int startline, int startcolumn) {
291 this(dstream, startline, startcolumn, 4096);
292 }
293
294
295
296
297
298 public InputCharStream(java.io.Reader dstream) {
299 this(dstream, 1, 1);
300 }
301
302
303
304
305
306 public void ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize) {
307 inputStream = dstream;
308 line = startline;
309 column = startcolumn - 1;
310
311 if (buffer == null || buffersize != buffer.length) {
312 available = bufsize = buffersize;
313 buffer = new char[buffersize];
314 bufline = new int[buffersize];
315 bufcolumn = new int[buffersize];
316 }
317 prevCharIsLF = prevCharIsCR = false;
318 tokenBegin = inBuf = maxNextCharInd = 0;
319 bufpos = -1;
320 }
321
322
323
324
325
326 public void ReInit(java.io.Reader dstream, int startline, int startcolumn) {
327 ReInit(dstream, startline, startcolumn, 4096);
328 }
329
330
331
332
333
334 public void ReInit(java.io.Reader dstream) {
335 ReInit(dstream, 1, 1);
336 }
337
338 public final String GetImage() {
339 if (bufpos >= tokenBegin) {
340 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
341 }
342 return new String(buffer, tokenBegin, bufsize - tokenBegin) + new String(buffer, 0, bufpos + 1);
343 }
344
345 public final char[] GetSuffix(int len) {
346 char[] ret = new char[len];
347
348 if ((bufpos + 1) >= len)
349 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
350 else {
351 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, len - bufpos - 1);
352 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
353 }
354 return ret;
355 }
356
357 public void Done() {
358 buffer = null;
359 bufline = null;
360 bufcolumn = null;
361 }
362
363
364
365
366 public void adjustBeginLineColumn(int newLine, int newCol) {
367 int start = tokenBegin;
368 int len;
369
370 if (bufpos >= tokenBegin) {
371 len = bufpos - tokenBegin + inBuf + 1;
372 } else {
373 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
374 }
375
376 int i = 0, j = 0, k = 0;
377 int nextColDiff = 0, columnDiff = 0;
378
379 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
380 bufline[j] = newLine;
381 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
382 bufcolumn[j] = newCol + columnDiff;
383 columnDiff = nextColDiff;
384 i++;
385 }
386
387 if (i < len) {
388 bufline[j] = newLine++;
389 bufcolumn[j] = newCol + columnDiff;
390
391 while (i++ < len) {
392 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
393 bufline[j] = newLine++;
394 else
395 bufline[j] = newLine;
396 }
397 }
398
399 line = bufline[j];
400 column = bufcolumn[j];
401 }
402 }