001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
028: package fr.ign.cogit.geoxygene.util.conversion;
029:
030: /**
031: * An implementation of interface CharStream, where the stream is assumed to
032: * contain only ASCII characters (without unicode processing).
033: */
034:
035: public final class SimpleCharStream {
036: public static final boolean staticFlag = false;
037: int bufsize;
038: int available;
039: int tokenBegin;
040: public int bufpos = -1;
041: private int bufline[];
042: private int bufcolumn[];
043:
044: private int column = 0;
045: private int line = 1;
046:
047: private boolean prevCharIsCR = false;
048: private boolean prevCharIsLF = false;
049:
050: private java.io.Reader inputStream;
051:
052: private char[] buffer;
053: private int maxNextCharInd = 0;
054: private int inBuf = 0;
055:
056: private final void ExpandBuff(boolean wrapAround) {
057: char[] newbuffer = new char[bufsize + 2048];
058: int newbufline[] = new int[bufsize + 2048];
059: int newbufcolumn[] = new int[bufsize + 2048];
060:
061: try {
062: if (wrapAround) {
063: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
064: bufsize - tokenBegin);
065: System.arraycopy(buffer, 0, newbuffer, bufsize
066: - tokenBegin, bufpos);
067: buffer = newbuffer;
068:
069: System.arraycopy(bufline, tokenBegin, newbufline, 0,
070: bufsize - tokenBegin);
071: System.arraycopy(bufline, 0, newbufline, bufsize
072: - tokenBegin, bufpos);
073: bufline = newbufline;
074:
075: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
076: 0, bufsize - tokenBegin);
077: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
078: - tokenBegin, bufpos);
079: bufcolumn = newbufcolumn;
080:
081: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
082: } else {
083: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
084: bufsize - tokenBegin);
085: buffer = newbuffer;
086:
087: System.arraycopy(bufline, tokenBegin, newbufline, 0,
088: bufsize - tokenBegin);
089: bufline = newbufline;
090:
091: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
092: 0, bufsize - tokenBegin);
093: bufcolumn = newbufcolumn;
094:
095: maxNextCharInd = (bufpos -= tokenBegin);
096: }
097: } catch (Throwable t) {
098: throw new Error(t.getMessage());
099: }
100:
101: bufsize += 2048;
102: available = bufsize;
103: tokenBegin = 0;
104: }
105:
106: private final void FillBuff() throws java.io.IOException {
107: if (maxNextCharInd == available) {
108: if (available == bufsize) {
109: if (tokenBegin > 2048) {
110: bufpos = maxNextCharInd = 0;
111: available = tokenBegin;
112: } else if (tokenBegin < 0)
113: bufpos = maxNextCharInd = 0;
114: else
115: ExpandBuff(false);
116: } else if (available > tokenBegin)
117: available = bufsize;
118: else if ((tokenBegin - available) < 2048)
119: ExpandBuff(true);
120: else
121: available = tokenBegin;
122: }
123:
124: int i;
125: try {
126: if ((i = inputStream.read(buffer, maxNextCharInd, available
127: - maxNextCharInd)) == -1) {
128: inputStream.close();
129: throw new java.io.IOException();
130: } else
131: maxNextCharInd += i;
132: return;
133: } catch (java.io.IOException e) {
134: --bufpos;
135: backup(0);
136: if (tokenBegin == -1)
137: tokenBegin = bufpos;
138: throw e;
139: }
140: }
141:
142: public final char BeginToken() throws java.io.IOException {
143: tokenBegin = -1;
144: char c = readChar();
145: tokenBegin = bufpos;
146:
147: return c;
148: }
149:
150: private final void UpdateLineColumn(char c) {
151: column++;
152:
153: if (prevCharIsLF) {
154: prevCharIsLF = false;
155: line += (column = 1);
156: } else if (prevCharIsCR) {
157: prevCharIsCR = false;
158: if (c == '\n') {
159: prevCharIsLF = true;
160: } else
161: line += (column = 1);
162: }
163:
164: switch (c) {
165: case '\r':
166: prevCharIsCR = true;
167: break;
168: case '\n':
169: prevCharIsLF = true;
170: break;
171: case '\t':
172: column--;
173: column += (8 - (column & 07));
174: break;
175: default:
176: break;
177: }
178:
179: bufline[bufpos] = line;
180: bufcolumn[bufpos] = column;
181: }
182:
183: public final char readChar() throws java.io.IOException {
184: if (inBuf > 0) {
185: --inBuf;
186:
187: if (++bufpos == bufsize)
188: bufpos = 0;
189:
190: return buffer[bufpos];
191: }
192:
193: if (++bufpos >= maxNextCharInd)
194: FillBuff();
195:
196: char c = buffer[bufpos];
197:
198: UpdateLineColumn(c);
199: return (c);
200: }
201:
202: /**
203: * @deprecated
204: * @see #getEndColumn
205: */
206:
207: public final int getColumn() {
208: return bufcolumn[bufpos];
209: }
210:
211: /**
212: * @deprecated
213: * @see #getEndLine
214: */
215:
216: public final int getLine() {
217: return bufline[bufpos];
218: }
219:
220: public final int getEndColumn() {
221: return bufcolumn[bufpos];
222: }
223:
224: public final int getEndLine() {
225: return bufline[bufpos];
226: }
227:
228: public final int getBeginColumn() {
229: return bufcolumn[tokenBegin];
230: }
231:
232: public final int getBeginLine() {
233: return bufline[tokenBegin];
234: }
235:
236: public final void backup(int amount) {
237:
238: inBuf += amount;
239: if ((bufpos -= amount) < 0)
240: bufpos += bufsize;
241: }
242:
243: public SimpleCharStream(java.io.Reader dstream, int startline,
244: int startcolumn, int buffersize) {
245: inputStream = dstream;
246: line = startline;
247: column = startcolumn - 1;
248:
249: available = bufsize = buffersize;
250: buffer = new char[buffersize];
251: bufline = new int[buffersize];
252: bufcolumn = new int[buffersize];
253: }
254:
255: public SimpleCharStream(java.io.Reader dstream, int startline,
256: int startcolumn) {
257: this (dstream, startline, startcolumn, 4096);
258: }
259:
260: public SimpleCharStream(java.io.Reader dstream) {
261: this (dstream, 1, 1, 4096);
262: }
263:
264: public void ReInit(java.io.Reader dstream, int startline,
265: int startcolumn, int buffersize) {
266: inputStream = dstream;
267: line = startline;
268: column = startcolumn - 1;
269:
270: if (buffer == null || buffersize != buffer.length) {
271: available = bufsize = buffersize;
272: buffer = new char[buffersize];
273: bufline = new int[buffersize];
274: bufcolumn = new int[buffersize];
275: }
276: prevCharIsLF = prevCharIsCR = false;
277: tokenBegin = inBuf = maxNextCharInd = 0;
278: bufpos = -1;
279: }
280:
281: public void ReInit(java.io.Reader dstream, int startline,
282: int startcolumn) {
283: ReInit(dstream, startline, startcolumn, 4096);
284: }
285:
286: public void ReInit(java.io.Reader dstream) {
287: ReInit(dstream, 1, 1, 4096);
288: }
289:
290: public SimpleCharStream(java.io.InputStream dstream, int startline,
291: int startcolumn, int buffersize) {
292: this (new java.io.InputStreamReader(dstream), startline,
293: startcolumn, 4096);
294: }
295:
296: public SimpleCharStream(java.io.InputStream dstream, int startline,
297: int startcolumn) {
298: this (dstream, startline, startcolumn, 4096);
299: }
300:
301: public SimpleCharStream(java.io.InputStream dstream) {
302: this (dstream, 1, 1, 4096);
303: }
304:
305: public void ReInit(java.io.InputStream dstream, int startline,
306: int startcolumn, int buffersize) {
307: ReInit(new java.io.InputStreamReader(dstream), startline,
308: startcolumn, 4096);
309: }
310:
311: public void ReInit(java.io.InputStream dstream) {
312: ReInit(dstream, 1, 1, 4096);
313: }
314:
315: public void ReInit(java.io.InputStream dstream, int startline,
316: int startcolumn) {
317: ReInit(dstream, startline, startcolumn, 4096);
318: }
319:
320: public final String GetImage() {
321: if (bufpos >= tokenBegin)
322: return new String(buffer, tokenBegin, bufpos - tokenBegin
323: + 1);
324: else
325: return new String(buffer, tokenBegin, bufsize - tokenBegin)
326: + new String(buffer, 0, bufpos + 1);
327: }
328:
329: public final char[] GetSuffix(int len) {
330: char[] ret = new char[len];
331:
332: if ((bufpos + 1) >= len)
333: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
334: else {
335: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
336: 0, len - bufpos - 1);
337: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
338: bufpos + 1);
339: }
340:
341: return ret;
342: }
343:
344: public void Done() {
345: buffer = null;
346: bufline = null;
347: bufcolumn = null;
348: }
349:
350: /**
351: * Method to adjust line and column numbers for the start of a token.<BR>
352: */
353: public void adjustBeginLineColumn(int newLine, int newCol) {
354: int start = tokenBegin;
355: int len;
356:
357: if (bufpos >= tokenBegin) {
358: len = bufpos - tokenBegin + inBuf + 1;
359: } else {
360: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
361: }
362:
363: int i = 0, j = 0, k = 0;
364: int nextColDiff = 0, columnDiff = 0;
365:
366: while (i < len
367: && bufline[j = start % bufsize] == bufline[k = ++start
368: % bufsize]) {
369: bufline[j] = newLine;
370: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
371: bufcolumn[j] = newCol + columnDiff;
372: columnDiff = nextColDiff;
373: i++;
374: }
375:
376: if (i < len) {
377: bufline[j] = newLine++;
378: bufcolumn[j] = newCol + columnDiff;
379:
380: while (i++ < len) {
381: if (bufline[j = start % bufsize] != bufline[++start
382: % bufsize])
383: bufline[j] = newLine++;
384: else
385: bufline[j] = newLine;
386: }
387: }
388:
389: line = bufline[j];
390: column = bufcolumn[j];
391: }
392:
393: }
|