001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
021: package org.openharmonise.commons.xml.parser;
022:
023: /**
024: * An implementation of interface CharStream, where the stream is assumed to
025: * contain only ASCII characters (without unicode processing).
026: */
027:
028: public final class SimpleCharStream {
029: public static final boolean staticFlag = false;
030: int bufsize;
031: int available;
032: int tokenBegin;
033: public int bufpos = -1;
034: private int bufline[];
035: private int bufcolumn[];
036:
037: private int column = 0;
038: private int line = 1;
039:
040: private boolean prevCharIsCR = false;
041: private boolean prevCharIsLF = false;
042:
043: private java.io.Reader inputStream;
044:
045: private char[] buffer;
046: private int maxNextCharInd = 0;
047: private int inBuf = 0;
048:
049: private final void ExpandBuff(boolean wrapAround) {
050: char[] newbuffer = new char[bufsize + 2048];
051: int newbufline[] = new int[bufsize + 2048];
052: int newbufcolumn[] = new int[bufsize + 2048];
053:
054: try {
055: if (wrapAround) {
056: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
057: bufsize - tokenBegin);
058: System.arraycopy(buffer, 0, newbuffer, bufsize
059: - tokenBegin, bufpos);
060: buffer = newbuffer;
061:
062: System.arraycopy(bufline, tokenBegin, newbufline, 0,
063: bufsize - tokenBegin);
064: System.arraycopy(bufline, 0, newbufline, bufsize
065: - tokenBegin, bufpos);
066: bufline = newbufline;
067:
068: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
069: 0, bufsize - tokenBegin);
070: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
071: - tokenBegin, bufpos);
072: bufcolumn = newbufcolumn;
073:
074: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
075: } else {
076: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
077: bufsize - tokenBegin);
078: buffer = newbuffer;
079:
080: System.arraycopy(bufline, tokenBegin, newbufline, 0,
081: bufsize - tokenBegin);
082: bufline = newbufline;
083:
084: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
085: 0, bufsize - tokenBegin);
086: bufcolumn = newbufcolumn;
087:
088: maxNextCharInd = (bufpos -= tokenBegin);
089: }
090: } catch (Throwable t) {
091: throw new Error(t.getMessage());
092: }
093:
094: bufsize += 2048;
095: available = bufsize;
096: tokenBegin = 0;
097: }
098:
099: private final void FillBuff() throws java.io.IOException {
100: if (maxNextCharInd == available) {
101: if (available == bufsize) {
102: if (tokenBegin > 2048) {
103: bufpos = maxNextCharInd = 0;
104: available = tokenBegin;
105: } else if (tokenBegin < 0)
106: bufpos = maxNextCharInd = 0;
107: else
108: ExpandBuff(false);
109: } else if (available > tokenBegin)
110: available = bufsize;
111: else if ((tokenBegin - available) < 2048)
112: ExpandBuff(true);
113: else
114: available = tokenBegin;
115: }
116:
117: int i;
118: try {
119: if ((i = inputStream.read(buffer, maxNextCharInd, available
120: - maxNextCharInd)) == -1) {
121: inputStream.close();
122: throw new java.io.IOException();
123: } else
124: maxNextCharInd += i;
125: return;
126: } catch (java.io.IOException e) {
127: --bufpos;
128: backup(0);
129: if (tokenBegin == -1)
130: tokenBegin = bufpos;
131: throw e;
132: }
133: }
134:
135: public final char BeginToken() throws java.io.IOException {
136: tokenBegin = -1;
137: char c = readChar();
138: tokenBegin = bufpos;
139:
140: return c;
141: }
142:
143: private final void UpdateLineColumn(char c) {
144: column++;
145:
146: if (prevCharIsLF) {
147: prevCharIsLF = false;
148: line += (column = 1);
149: } else if (prevCharIsCR) {
150: prevCharIsCR = false;
151: if (c == '\n') {
152: prevCharIsLF = true;
153: } else
154: line += (column = 1);
155: }
156:
157: switch (c) {
158: case '\r':
159: prevCharIsCR = true;
160: break;
161: case '\n':
162: prevCharIsLF = true;
163: break;
164: case '\t':
165: column--;
166: column += (8 - (column & 07));
167: break;
168: default:
169: break;
170: }
171:
172: bufline[bufpos] = line;
173: bufcolumn[bufpos] = column;
174: }
175:
176: public final char readChar() throws java.io.IOException {
177: if (inBuf > 0) {
178: --inBuf;
179:
180: if (++bufpos == bufsize)
181: bufpos = 0;
182:
183: return buffer[bufpos];
184: }
185:
186: if (++bufpos >= maxNextCharInd)
187: FillBuff();
188:
189: char c = buffer[bufpos];
190:
191: UpdateLineColumn(c);
192: return (c);
193: }
194:
195: /**
196: * @deprecated
197: * @see #getEndColumn
198: */
199:
200: public final int getColumn() {
201: return bufcolumn[bufpos];
202: }
203:
204: /**
205: * @deprecated
206: * @see #getEndLine
207: */
208:
209: public final int getLine() {
210: return bufline[bufpos];
211: }
212:
213: public final int getEndColumn() {
214: return bufcolumn[bufpos];
215: }
216:
217: public final int getEndLine() {
218: return bufline[bufpos];
219: }
220:
221: public final int getBeginColumn() {
222: return bufcolumn[tokenBegin];
223: }
224:
225: public final int getBeginLine() {
226: return bufline[tokenBegin];
227: }
228:
229: public final void backup(int amount) {
230:
231: inBuf += amount;
232: if ((bufpos -= amount) < 0)
233: bufpos += bufsize;
234: }
235:
236: public SimpleCharStream(java.io.Reader dstream, int startline,
237: int startcolumn, int buffersize) {
238: inputStream = dstream;
239: line = startline;
240: column = startcolumn - 1;
241:
242: available = bufsize = buffersize;
243: buffer = new char[buffersize];
244: bufline = new int[buffersize];
245: bufcolumn = new int[buffersize];
246: }
247:
248: public SimpleCharStream(java.io.Reader dstream, int startline,
249: int startcolumn) {
250: this (dstream, startline, startcolumn, 4096);
251: }
252:
253: public SimpleCharStream(java.io.Reader dstream) {
254: this (dstream, 1, 1, 4096);
255: }
256:
257: public void ReInit(java.io.Reader dstream, int startline,
258: int startcolumn, int buffersize) {
259: inputStream = dstream;
260: line = startline;
261: column = startcolumn - 1;
262:
263: if (buffer == null || buffersize != buffer.length) {
264: available = bufsize = buffersize;
265: buffer = new char[buffersize];
266: bufline = new int[buffersize];
267: bufcolumn = new int[buffersize];
268: }
269: prevCharIsLF = prevCharIsCR = false;
270: tokenBegin = inBuf = maxNextCharInd = 0;
271: bufpos = -1;
272: }
273:
274: public void ReInit(java.io.Reader dstream, int startline,
275: int startcolumn) {
276: ReInit(dstream, startline, startcolumn, 4096);
277: }
278:
279: public void ReInit(java.io.Reader dstream) {
280: ReInit(dstream, 1, 1, 4096);
281: }
282:
283: public SimpleCharStream(java.io.InputStream dstream, int startline,
284: int startcolumn, int buffersize) {
285: this (new java.io.InputStreamReader(dstream), startline,
286: startcolumn, 4096);
287: }
288:
289: public SimpleCharStream(java.io.InputStream dstream, int startline,
290: int startcolumn) {
291: this (dstream, startline, startcolumn, 4096);
292: }
293:
294: public SimpleCharStream(java.io.InputStream dstream) {
295: this (dstream, 1, 1, 4096);
296: }
297:
298: public void ReInit(java.io.InputStream dstream, int startline,
299: int startcolumn, int buffersize) {
300: ReInit(new java.io.InputStreamReader(dstream), startline,
301: startcolumn, 4096);
302: }
303:
304: public void ReInit(java.io.InputStream dstream) {
305: ReInit(dstream, 1, 1, 4096);
306: }
307:
308: public void ReInit(java.io.InputStream dstream, int startline,
309: int startcolumn) {
310: ReInit(dstream, startline, startcolumn, 4096);
311: }
312:
313: public final String GetImage() {
314: if (bufpos >= tokenBegin)
315: return new String(buffer, tokenBegin, bufpos - tokenBegin
316: + 1);
317: else
318: return new String(buffer, tokenBegin, bufsize - tokenBegin)
319: + new String(buffer, 0, bufpos + 1);
320: }
321:
322: public final char[] GetSuffix(int len) {
323: char[] ret = new char[len];
324:
325: if ((bufpos + 1) >= len)
326: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
327: else {
328: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
329: 0, len - bufpos - 1);
330: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
331: bufpos + 1);
332: }
333:
334: return ret;
335: }
336:
337: public void Done() {
338: buffer = null;
339: bufline = null;
340: bufcolumn = null;
341: }
342:
343: /**
344: * Method to adjust line and column numbers for the start of a token.<BR>
345: */
346: public void adjustBeginLineColumn(int newLine, int newCol) {
347: int start = tokenBegin;
348: int len;
349:
350: if (bufpos >= tokenBegin) {
351: len = bufpos - tokenBegin + inBuf + 1;
352: } else {
353: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
354: }
355:
356: int i = 0, j = 0, k = 0;
357: int nextColDiff = 0, columnDiff = 0;
358:
359: while (i < len
360: && bufline[j = start % bufsize] == bufline[k = ++start
361: % bufsize]) {
362: bufline[j] = newLine;
363: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
364: bufcolumn[j] = newCol + columnDiff;
365: columnDiff = nextColDiff;
366: i++;
367: }
368:
369: if (i < len) {
370: bufline[j] = newLine++;
371: bufcolumn[j] = newCol + columnDiff;
372:
373: while (i++ < len) {
374: if (bufline[j = start % bufsize] != bufline[++start
375: % bufsize])
376: bufline[j] = newLine++;
377: else
378: bufline[j] = newLine;
379: }
380: }
381:
382: line = bufline[j];
383: column = bufcolumn[j];
384: }
385:
386: }
|