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