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 package org.jdtaus.core.container.ri.client.versioning;
26
27
28
29
30
31
32 public class SimpleCharStream
33 {
34
35 public static final boolean staticFlag = false;
36 int bufsize;
37 int available;
38 int tokenBegin;
39
40 public int bufpos = -1;
41 protected int bufline[];
42 protected int bufcolumn[];
43
44 protected int column = 0;
45 protected int line = 1;
46
47 protected boolean prevCharIsCR = false;
48 protected boolean prevCharIsLF = false;
49
50 protected java.io.Reader inputStream;
51
52 protected char[] buffer;
53 protected int maxNextCharInd = 0;
54 protected int inBuf = 0;
55 protected int tabSize = 8;
56
57 protected void setTabSize(int i) { tabSize = i; }
58 protected int getTabSize(int i) { return tabSize; }
59
60
61 protected void ExpandBuff(boolean wrapAround)
62 {
63 char[] newbuffer = new char[bufsize + 2048];
64 int newbufline[] = new int[bufsize + 2048];
65 int newbufcolumn[] = new int[bufsize + 2048];
66
67 try
68 {
69 if (wrapAround)
70 {
71 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
72 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
73 buffer = newbuffer;
74
75 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
76 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
77 bufline = newbufline;
78
79 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
80 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
81 bufcolumn = newbufcolumn;
82
83 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
84 }
85 else
86 {
87 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
88 buffer = newbuffer;
89
90 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
91 bufline = newbufline;
92
93 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
94 bufcolumn = newbufcolumn;
95
96 maxNextCharInd = (bufpos -= tokenBegin);
97 }
98 }
99 catch (Throwable t)
100 {
101 throw new Error(t.getMessage());
102 }
103
104
105 bufsize += 2048;
106 available = bufsize;
107 tokenBegin = 0;
108 }
109
110 protected void FillBuff() throws java.io.IOException
111 {
112 if (maxNextCharInd == available)
113 {
114 if (available == bufsize)
115 {
116 if (tokenBegin > 2048)
117 {
118 bufpos = maxNextCharInd = 0;
119 available = tokenBegin;
120 }
121 else if (tokenBegin < 0)
122 bufpos = maxNextCharInd = 0;
123 else
124 ExpandBuff(false);
125 }
126 else if (available > tokenBegin)
127 available = bufsize;
128 else if ((tokenBegin - available) < 2048)
129 ExpandBuff(true);
130 else
131 available = tokenBegin;
132 }
133
134 int i;
135 try {
136 if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
137 {
138 inputStream.close();
139 throw new java.io.IOException();
140 }
141 else
142 maxNextCharInd += i;
143 return;
144 }
145 catch(java.io.IOException e) {
146 --bufpos;
147 backup(0);
148 if (tokenBegin == -1)
149 tokenBegin = bufpos;
150 throw e;
151 }
152 }
153
154
155 public char BeginToken() throws java.io.IOException
156 {
157 tokenBegin = -1;
158 char c = readChar();
159 tokenBegin = bufpos;
160
161 return c;
162 }
163
164 protected void UpdateLineColumn(char c)
165 {
166 column++;
167
168 if (prevCharIsLF)
169 {
170 prevCharIsLF = false;
171 line += (column = 1);
172 }
173 else if (prevCharIsCR)
174 {
175 prevCharIsCR = false;
176 if (c == '\n')
177 {
178 prevCharIsLF = true;
179 }
180 else
181 line += (column = 1);
182 }
183
184 switch (c)
185 {
186 case '\r' :
187 prevCharIsCR = true;
188 break;
189 case '\n' :
190 prevCharIsLF = true;
191 break;
192 case '\t' :
193 column--;
194 column += (tabSize - (column % tabSize));
195 break;
196 default :
197 break;
198 }
199
200 bufline[bufpos] = line;
201 bufcolumn[bufpos] = column;
202 }
203
204
205 public char readChar() throws java.io.IOException
206 {
207 if (inBuf > 0)
208 {
209 --inBuf;
210
211 if (++bufpos == bufsize)
212 bufpos = 0;
213
214 return buffer[bufpos];
215 }
216
217 if (++bufpos >= maxNextCharInd)
218 FillBuff();
219
220 char c = buffer[bufpos];
221
222 UpdateLineColumn(c);
223 return c;
224 }
225
226
227
228
229
230
231 public int getColumn() {
232 return bufcolumn[bufpos];
233 }
234
235
236
237
238
239
240 public int getLine() {
241 return bufline[bufpos];
242 }
243
244
245 public int getEndColumn() {
246 return bufcolumn[bufpos];
247 }
248
249
250 public int getEndLine() {
251 return bufline[bufpos];
252 }
253
254
255 public int getBeginColumn() {
256 return bufcolumn[tokenBegin];
257 }
258
259
260 public int getBeginLine() {
261 return bufline[tokenBegin];
262 }
263
264
265 public void backup(int amount) {
266
267 inBuf += amount;
268 if ((bufpos -= amount) < 0)
269 bufpos += bufsize;
270 }
271
272
273 public SimpleCharStream(java.io.Reader dstream, int startline,
274 int startcolumn, int buffersize)
275 {
276 inputStream = dstream;
277 line = startline;
278 column = startcolumn - 1;
279
280 available = bufsize = buffersize;
281 buffer = new char[buffersize];
282 bufline = new int[buffersize];
283 bufcolumn = new int[buffersize];
284 }
285
286
287 public SimpleCharStream(java.io.Reader dstream, int startline,
288 int startcolumn)
289 {
290 this(dstream, startline, startcolumn, 4096);
291 }
292
293
294 public SimpleCharStream(java.io.Reader dstream)
295 {
296 this(dstream, 1, 1, 4096);
297 }
298
299
300 public void ReInit(java.io.Reader dstream, int startline,
301 int startcolumn, int buffersize)
302 {
303 inputStream = dstream;
304 line = startline;
305 column = startcolumn - 1;
306
307 if (buffer == null || buffersize != buffer.length)
308 {
309 available = bufsize = buffersize;
310 buffer = new char[buffersize];
311 bufline = new int[buffersize];
312 bufcolumn = new int[buffersize];
313 }
314 prevCharIsLF = prevCharIsCR = false;
315 tokenBegin = inBuf = maxNextCharInd = 0;
316 bufpos = -1;
317 }
318
319
320 public void ReInit(java.io.Reader dstream, int startline,
321 int startcolumn)
322 {
323 ReInit(dstream, startline, startcolumn, 4096);
324 }
325
326
327 public void ReInit(java.io.Reader dstream)
328 {
329 ReInit(dstream, 1, 1, 4096);
330 }
331
332 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
333 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
334 {
335 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
336 }
337
338
339 public SimpleCharStream(java.io.InputStream dstream, int startline,
340 int startcolumn, int buffersize)
341 {
342 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
343 }
344
345
346 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
347 int startcolumn) throws java.io.UnsupportedEncodingException
348 {
349 this(dstream, encoding, startline, startcolumn, 4096);
350 }
351
352
353 public SimpleCharStream(java.io.InputStream dstream, int startline,
354 int startcolumn)
355 {
356 this(dstream, startline, startcolumn, 4096);
357 }
358
359
360 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
361 {
362 this(dstream, encoding, 1, 1, 4096);
363 }
364
365
366 public SimpleCharStream(java.io.InputStream dstream)
367 {
368 this(dstream, 1, 1, 4096);
369 }
370
371
372 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
373 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
374 {
375 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
376 }
377
378
379 public void ReInit(java.io.InputStream dstream, int startline,
380 int startcolumn, int buffersize)
381 {
382 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
383 }
384
385
386 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
387 {
388 ReInit(dstream, encoding, 1, 1, 4096);
389 }
390
391
392 public void ReInit(java.io.InputStream dstream)
393 {
394 ReInit(dstream, 1, 1, 4096);
395 }
396
397 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
398 int startcolumn) throws java.io.UnsupportedEncodingException
399 {
400 ReInit(dstream, encoding, startline, startcolumn, 4096);
401 }
402
403 public void ReInit(java.io.InputStream dstream, int startline,
404 int startcolumn)
405 {
406 ReInit(dstream, startline, startcolumn, 4096);
407 }
408
409 public String GetImage()
410 {
411 if (bufpos >= tokenBegin)
412 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
413 else
414 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
415 new String(buffer, 0, bufpos + 1);
416 }
417
418
419 public char[] GetSuffix(int len)
420 {
421 char[] ret = new char[len];
422
423 if ((bufpos + 1) >= len)
424 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
425 else
426 {
427 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
428 len - bufpos - 1);
429 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
430 }
431
432 return ret;
433 }
434
435
436 public void Done()
437 {
438 buffer = null;
439 bufline = null;
440 bufcolumn = null;
441 }
442
443
444
445
446 public void adjustBeginLineColumn(int newLine, int newCol)
447 {
448 int start = tokenBegin;
449 int len;
450
451 if (bufpos >= tokenBegin)
452 {
453 len = bufpos - tokenBegin + inBuf + 1;
454 }
455 else
456 {
457 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
458 }
459
460 int i = 0, j = 0, k = 0;
461 int nextColDiff = 0, columnDiff = 0;
462
463 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
464 {
465 bufline[j] = newLine;
466 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
467 bufcolumn[j] = newCol + columnDiff;
468 columnDiff = nextColDiff;
469 i++;
470 }
471
472 if (i < len)
473 {
474 bufline[j] = newLine++;
475 bufcolumn[j] = newCol + columnDiff;
476
477 while (i++ < len)
478 {
479 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
480 bufline[j] = newLine++;
481 else
482 bufline[j] = newLine;
483 }
484 }
485
486 line = bufline[j];
487 column = bufcolumn[j];
488 }
489
490 }
491