svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / labeling / parse / SimpleCharStream.java @ 36624
History | View | Annotate | Download (12.2 KB)
1 |
/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
|
---|---|
2 |
/* gvSIG. Sistema de Información Geográfica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
* For more information, contact:
|
21 |
*
|
22 |
* Generalitat Valenciana
|
23 |
* Conselleria d'Infraestructures i Transport
|
24 |
* Av. Blasco Ibáñez, 50
|
25 |
* 46010 VALENCIA
|
26 |
* SPAIN
|
27 |
*
|
28 |
* +34 963862235
|
29 |
* gvsig@gva.es
|
30 |
* www.gvsig.gva.es
|
31 |
*
|
32 |
* or
|
33 |
*
|
34 |
* IVER T.I. S.A
|
35 |
* Salamanca 50
|
36 |
* 46005 Valencia
|
37 |
* Spain
|
38 |
*
|
39 |
* +34 963163400
|
40 |
* dac@iver.es
|
41 |
*/
|
42 |
package org.gvsig.symbology.fmap.labeling.parse; |
43 |
|
44 |
/**
|
45 |
* An implementation of interface CharStream, where the stream is assumed to
|
46 |
* contain only ASCII characters (without unicode processing).
|
47 |
*/
|
48 |
|
49 |
public class SimpleCharStream |
50 |
{ |
51 |
public static final boolean staticFlag = false; |
52 |
int bufsize;
|
53 |
int available;
|
54 |
int tokenBegin;
|
55 |
public int bufpos = -1; |
56 |
protected int bufline[]; |
57 |
protected int bufcolumn[]; |
58 |
|
59 |
protected int column = 0; |
60 |
protected int line = 1; |
61 |
|
62 |
protected boolean prevCharIsCR = false; |
63 |
protected boolean prevCharIsLF = false; |
64 |
|
65 |
protected java.io.Reader inputStream;
|
66 |
|
67 |
protected char[] buffer; |
68 |
protected int maxNextCharInd = 0; |
69 |
protected int inBuf = 0; |
70 |
protected int tabSize = 8; |
71 |
|
72 |
protected void setTabSize(int i) { tabSize = i; } |
73 |
protected int getTabSize(int i) { return tabSize; } |
74 |
|
75 |
|
76 |
protected void ExpandBuff(boolean wrapAround) |
77 |
{ |
78 |
char[] newbuffer = new char[bufsize + 2048]; |
79 |
int newbufline[] = new int[bufsize + 2048]; |
80 |
int newbufcolumn[] = new int[bufsize + 2048]; |
81 |
|
82 |
try
|
83 |
{ |
84 |
if (wrapAround)
|
85 |
{ |
86 |
System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); |
87 |
System.arraycopy(buffer, 0, newbuffer, |
88 |
bufsize - tokenBegin, bufpos); |
89 |
buffer = newbuffer; |
90 |
|
91 |
System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); |
92 |
System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); |
93 |
bufline = newbufline; |
94 |
|
95 |
System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); |
96 |
System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); |
97 |
bufcolumn = newbufcolumn; |
98 |
|
99 |
maxNextCharInd = (bufpos += (bufsize - tokenBegin)); |
100 |
} |
101 |
else
|
102 |
{ |
103 |
System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); |
104 |
buffer = newbuffer; |
105 |
|
106 |
System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); |
107 |
bufline = newbufline; |
108 |
|
109 |
System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); |
110 |
bufcolumn = newbufcolumn; |
111 |
|
112 |
maxNextCharInd = (bufpos -= tokenBegin); |
113 |
} |
114 |
} |
115 |
catch (Throwable t) |
116 |
{ |
117 |
throw new Error(t.getMessage()); |
118 |
} |
119 |
|
120 |
|
121 |
bufsize += 2048;
|
122 |
available = bufsize; |
123 |
tokenBegin = 0;
|
124 |
} |
125 |
|
126 |
protected void FillBuff() throws java.io.IOException |
127 |
{ |
128 |
if (maxNextCharInd == available)
|
129 |
{ |
130 |
if (available == bufsize)
|
131 |
{ |
132 |
if (tokenBegin > 2048) |
133 |
{ |
134 |
bufpos = maxNextCharInd = 0;
|
135 |
available = tokenBegin; |
136 |
} |
137 |
else if (tokenBegin < 0) |
138 |
bufpos = maxNextCharInd = 0;
|
139 |
else
|
140 |
ExpandBuff(false);
|
141 |
} |
142 |
else if (available > tokenBegin) |
143 |
available = bufsize; |
144 |
else if ((tokenBegin - available) < 2048) |
145 |
ExpandBuff(true);
|
146 |
else
|
147 |
available = tokenBegin; |
148 |
} |
149 |
|
150 |
int i;
|
151 |
try {
|
152 |
if ((i = inputStream.read(buffer, maxNextCharInd,
|
153 |
available - maxNextCharInd)) == -1)
|
154 |
{ |
155 |
inputStream.close(); |
156 |
throw new java.io.IOException(); |
157 |
} |
158 |
else
|
159 |
maxNextCharInd += i; |
160 |
return;
|
161 |
} |
162 |
catch(java.io.IOException e) {
|
163 |
--bufpos; |
164 |
backup(0);
|
165 |
if (tokenBegin == -1) |
166 |
tokenBegin = bufpos; |
167 |
throw e;
|
168 |
} |
169 |
} |
170 |
|
171 |
public char BeginToken() throws java.io.IOException |
172 |
{ |
173 |
tokenBegin = -1;
|
174 |
char c = readChar();
|
175 |
tokenBegin = bufpos; |
176 |
|
177 |
return c;
|
178 |
} |
179 |
|
180 |
protected void UpdateLineColumn(char c) |
181 |
{ |
182 |
column++; |
183 |
|
184 |
if (prevCharIsLF)
|
185 |
{ |
186 |
prevCharIsLF = false;
|
187 |
line += (column = 1);
|
188 |
} |
189 |
else if (prevCharIsCR) |
190 |
{ |
191 |
prevCharIsCR = false;
|
192 |
if (c == '\n') |
193 |
{ |
194 |
prevCharIsLF = true;
|
195 |
} |
196 |
else
|
197 |
line += (column = 1);
|
198 |
} |
199 |
|
200 |
switch (c)
|
201 |
{ |
202 |
case '\r' : |
203 |
prevCharIsCR = true;
|
204 |
break;
|
205 |
case '\n' : |
206 |
prevCharIsLF = true;
|
207 |
break;
|
208 |
case '\t' : |
209 |
column--; |
210 |
column += (tabSize - (column % tabSize)); |
211 |
break;
|
212 |
default :
|
213 |
break;
|
214 |
} |
215 |
|
216 |
bufline[bufpos] = line; |
217 |
bufcolumn[bufpos] = column; |
218 |
} |
219 |
|
220 |
public char readChar() throws java.io.IOException |
221 |
{ |
222 |
if (inBuf > 0) |
223 |
{ |
224 |
--inBuf; |
225 |
|
226 |
if (++bufpos == bufsize)
|
227 |
bufpos = 0;
|
228 |
|
229 |
return buffer[bufpos];
|
230 |
} |
231 |
|
232 |
if (++bufpos >= maxNextCharInd)
|
233 |
FillBuff(); |
234 |
|
235 |
char c = buffer[bufpos];
|
236 |
|
237 |
UpdateLineColumn(c); |
238 |
return c;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* @deprecated
|
243 |
* @see #getEndColumn
|
244 |
*/
|
245 |
|
246 |
public int getColumn() { |
247 |
return bufcolumn[bufpos];
|
248 |
} |
249 |
|
250 |
/**
|
251 |
* @deprecated
|
252 |
* @see #getEndLine
|
253 |
*/
|
254 |
|
255 |
public int getLine() { |
256 |
return bufline[bufpos];
|
257 |
} |
258 |
|
259 |
public int getEndColumn() { |
260 |
return bufcolumn[bufpos];
|
261 |
} |
262 |
|
263 |
public int getEndLine() { |
264 |
return bufline[bufpos];
|
265 |
} |
266 |
|
267 |
public int getBeginColumn() { |
268 |
return bufcolumn[tokenBegin];
|
269 |
} |
270 |
|
271 |
public int getBeginLine() { |
272 |
return bufline[tokenBegin];
|
273 |
} |
274 |
|
275 |
public void backup(int amount) { |
276 |
|
277 |
inBuf += amount; |
278 |
if ((bufpos -= amount) < 0) |
279 |
bufpos += bufsize; |
280 |
} |
281 |
|
282 |
public SimpleCharStream(java.io.Reader dstream, int startline, |
283 |
int startcolumn, int buffersize) |
284 |
{ |
285 |
inputStream = dstream; |
286 |
line = startline; |
287 |
column = startcolumn - 1;
|
288 |
|
289 |
available = bufsize = buffersize; |
290 |
buffer = new char[buffersize]; |
291 |
bufline = new int[buffersize]; |
292 |
bufcolumn = new int[buffersize]; |
293 |
} |
294 |
|
295 |
public SimpleCharStream(java.io.Reader dstream, int startline, |
296 |
int startcolumn)
|
297 |
{ |
298 |
this(dstream, startline, startcolumn, 4096); |
299 |
} |
300 |
|
301 |
public SimpleCharStream(java.io.Reader dstream)
|
302 |
{ |
303 |
this(dstream, 1, 1, 4096); |
304 |
} |
305 |
public void ReInit(java.io.Reader dstream, int startline, |
306 |
int startcolumn, int buffersize) |
307 |
{ |
308 |
inputStream = dstream; |
309 |
line = startline; |
310 |
column = startcolumn - 1;
|
311 |
|
312 |
if (buffer == null || buffersize != buffer.length) |
313 |
{ |
314 |
available = bufsize = buffersize; |
315 |
buffer = new char[buffersize]; |
316 |
bufline = new int[buffersize]; |
317 |
bufcolumn = new int[buffersize]; |
318 |
} |
319 |
prevCharIsLF = prevCharIsCR = false;
|
320 |
tokenBegin = inBuf = maxNextCharInd = 0;
|
321 |
bufpos = -1;
|
322 |
} |
323 |
|
324 |
public void ReInit(java.io.Reader dstream, int startline, |
325 |
int startcolumn)
|
326 |
{ |
327 |
ReInit(dstream, startline, startcolumn, 4096);
|
328 |
} |
329 |
|
330 |
public void ReInit(java.io.Reader dstream) |
331 |
{ |
332 |
ReInit(dstream, 1, 1, 4096); |
333 |
} |
334 |
public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, |
335 |
int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException |
336 |
{ |
337 |
this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); |
338 |
} |
339 |
|
340 |
public SimpleCharStream(java.io.InputStream dstream, int startline, |
341 |
int startcolumn, int buffersize) |
342 |
{ |
343 |
this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); |
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 |
public SimpleCharStream(java.io.InputStream dstream, int startline, |
353 |
int startcolumn)
|
354 |
{ |
355 |
this(dstream, startline, startcolumn, 4096); |
356 |
} |
357 |
|
358 |
public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException |
359 |
{ |
360 |
this(dstream, encoding, 1, 1, 4096); |
361 |
} |
362 |
|
363 |
public SimpleCharStream(java.io.InputStream dstream)
|
364 |
{ |
365 |
this(dstream, 1, 1, 4096); |
366 |
} |
367 |
|
368 |
public void ReInit(java.io.InputStream dstream, String encoding, int startline, |
369 |
int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException |
370 |
{ |
371 |
ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); |
372 |
} |
373 |
|
374 |
public void ReInit(java.io.InputStream dstream, int startline, |
375 |
int startcolumn, int buffersize) |
376 |
{ |
377 |
ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
|
378 |
} |
379 |
|
380 |
public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException |
381 |
{ |
382 |
ReInit(dstream, encoding, 1, 1, 4096); |
383 |
} |
384 |
|
385 |
public void ReInit(java.io.InputStream dstream) |
386 |
{ |
387 |
ReInit(dstream, 1, 1, 4096); |
388 |
} |
389 |
public void ReInit(java.io.InputStream dstream, String encoding, int startline, |
390 |
int startcolumn) throws java.io.UnsupportedEncodingException |
391 |
{ |
392 |
ReInit(dstream, encoding, startline, startcolumn, 4096);
|
393 |
} |
394 |
public void ReInit(java.io.InputStream dstream, int startline, |
395 |
int startcolumn)
|
396 |
{ |
397 |
ReInit(dstream, startline, startcolumn, 4096);
|
398 |
} |
399 |
public String GetImage() |
400 |
{ |
401 |
if (bufpos >= tokenBegin)
|
402 |
return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); |
403 |
else
|
404 |
return new String(buffer, tokenBegin, bufsize - tokenBegin) + |
405 |
new String(buffer, 0, bufpos + 1); |
406 |
} |
407 |
|
408 |
public char[] GetSuffix(int len) |
409 |
{ |
410 |
char[] ret = new char[len]; |
411 |
|
412 |
if ((bufpos + 1) >= len) |
413 |
System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); |
414 |
else
|
415 |
{ |
416 |
System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, |
417 |
len - bufpos - 1);
|
418 |
System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); |
419 |
} |
420 |
|
421 |
return ret;
|
422 |
} |
423 |
|
424 |
public void Done() |
425 |
{ |
426 |
buffer = null;
|
427 |
bufline = null;
|
428 |
bufcolumn = null;
|
429 |
} |
430 |
|
431 |
/**
|
432 |
* Method to adjust line and column numbers for the start of a token.
|
433 |
*/
|
434 |
public void adjustBeginLineColumn(int newLine, int newCol) |
435 |
{ |
436 |
int start = tokenBegin;
|
437 |
int len;
|
438 |
|
439 |
if (bufpos >= tokenBegin)
|
440 |
{ |
441 |
len = bufpos - tokenBegin + inBuf + 1;
|
442 |
} |
443 |
else
|
444 |
{ |
445 |
len = bufsize - tokenBegin + bufpos + 1 + inBuf;
|
446 |
} |
447 |
|
448 |
int i = 0, j = 0, k = 0; |
449 |
int nextColDiff = 0, columnDiff = 0; |
450 |
|
451 |
while (i < len &&
|
452 |
bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) |
453 |
{ |
454 |
bufline[j] = newLine; |
455 |
nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; |
456 |
bufcolumn[j] = newCol + columnDiff; |
457 |
columnDiff = nextColDiff; |
458 |
i++; |
459 |
} |
460 |
|
461 |
if (i < len)
|
462 |
{ |
463 |
bufline[j] = newLine++; |
464 |
bufcolumn[j] = newCol + columnDiff; |
465 |
|
466 |
while (i++ < len)
|
467 |
{ |
468 |
if (bufline[j = start % bufsize] != bufline[++start % bufsize])
|
469 |
bufline[j] = newLine++; |
470 |
else
|
471 |
bufline[j] = newLine; |
472 |
} |
473 |
} |
474 |
|
475 |
line = bufline[j]; |
476 |
column = bufcolumn[j]; |
477 |
} |
478 |
|
479 |
} |