KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > parser > SimpleCharStream


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.el.parser;
19
20 /**
21  * An implementation of interface CharStream, where the stream is assumed to
22  * contain only ASCII characters (without unicode processing).
23  */

24
25 public class SimpleCharStream {
26
27     public static final boolean staticFlag = false;
28     int bufsize;
29     int available;
30     int tokenBegin;
31     public int bufpos = -1;
32     protected int bufline[];
33     protected int bufcolumn[];
34
35     protected int column = 0;
36     protected int line = 1;
37
38     protected boolean prevCharIsCR = false;
39     protected boolean prevCharIsLF = false;
40
41     protected java.io.Reader JavaDoc inputStream;
42
43     protected char[] buffer;
44     protected int maxNextCharInd = 0;
45     protected int inBuf = 0;
46
47     protected void ExpandBuff(boolean wrapAround) {
48         char[] newbuffer = new char[bufsize + 2048];
49         int newbufline[] = new int[bufsize + 2048];
50         int newbufcolumn[] = new int[bufsize + 2048];
51
52         try {
53             if(wrapAround) {
54                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
55                 System.arraycopy(buffer, 0, newbuffer,
56                     bufsize - tokenBegin, bufpos);
57                 buffer = newbuffer;
58
59                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
60                 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
61                 bufline = newbufline;
62
63                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
64                 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
65                 bufcolumn = newbufcolumn;
66
67                 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
68             } else {
69                 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
70                 buffer = newbuffer;
71
72                 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
73                 bufline = newbufline;
74
75                 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
76                 bufcolumn = newbufcolumn;
77
78                 maxNextCharInd = (bufpos -= tokenBegin);
79             }
80         } catch(Throwable JavaDoc t) {
81             throw new Error JavaDoc(t.getMessage());
82         }
83
84
85         bufsize += 2048;
86         available = bufsize;
87         tokenBegin = 0;
88     }
89
90     protected void FillBuff() throws java.io.IOException JavaDoc {
91         if(maxNextCharInd == available) {
92             if(available == bufsize) {
93                 if(tokenBegin > 2048) {
94                     bufpos = maxNextCharInd = 0;
95                     available = tokenBegin;
96                 } else if(tokenBegin < 0)
97                     bufpos = maxNextCharInd = 0;
98                 else
99                     ExpandBuff(false);
100             } else if(available > tokenBegin)
101                 available = bufsize;
102             else if((tokenBegin - available) < 2048)
103                 ExpandBuff(true);
104             else
105                 available = tokenBegin;
106         }
107
108         int i;
109         try {
110             if((i = inputStream.read(buffer, maxNextCharInd,
111                 available - maxNextCharInd)) == -1) {
112                 inputStream.close();
113                 throw new java.io.IOException JavaDoc();
114             } else
115                 maxNextCharInd += i;
116             return;
117         } catch(java.io.IOException JavaDoc e) {
118             --bufpos;
119             backup(0);
120             if(tokenBegin == -1)
121                 tokenBegin = bufpos;
122             throw e;
123         }
124     }
125
126     public char BeginToken() throws java.io.IOException JavaDoc {
127         tokenBegin = -1;
128         char c = readChar();
129         tokenBegin = bufpos;
130
131         return c;
132     }
133
134     protected void UpdateLineColumn(char c) {
135         column++;
136
137         if(prevCharIsLF) {
138             prevCharIsLF = false;
139             line += (column = 1);
140         } else if(prevCharIsCR) {
141             prevCharIsCR = false;
142             if(c == '\n') {
143                 prevCharIsLF = true;
144             } else
145                 line += (column = 1);
146         }
147
148         switch(c) {
149             case '\r':
150                 prevCharIsCR = true;
151                 break;
152             case '\n':
153                 prevCharIsLF = true;
154                 break;
155             case '\t':
156                 column--;
157                 column += (8 - (column & 07));
158                 break;
159             default :
160                 break;
161         }
162
163         bufline[bufpos] = line;
164         bufcolumn[bufpos] = column;
165     }
166
167     public char readChar() throws java.io.IOException JavaDoc {
168         if(inBuf > 0) {
169             --inBuf;
170
171             if(++bufpos == bufsize)
172                 bufpos = 0;
173
174             return buffer[bufpos];
175         }
176
177         if(++bufpos >= maxNextCharInd)
178             FillBuff();
179
180         char c = buffer[bufpos];
181
182         UpdateLineColumn(c);
183         return (c);
184     }
185
186     /**
187      * @see #getEndColumn
188      * @deprecated
189      */

190
191     public int getColumn() {
192         return bufcolumn[bufpos];
193     }
194
195     /**
196      * @see #getEndLine
197      * @deprecated
198      */

199
200     public int getLine() {
201         return bufline[bufpos];
202     }
203
204     public int getEndColumn() {
205         return bufcolumn[bufpos];
206     }
207
208     public int getEndLine() {
209         return bufline[bufpos];
210     }
211
212     public int getBeginColumn() {
213         return bufcolumn[tokenBegin];
214     }
215
216     public int getBeginLine() {
217         return bufline[tokenBegin];
218     }
219
220     public void backup(int amount) {
221
222         inBuf += amount;
223         if((bufpos -= amount) < 0)
224             bufpos += bufsize;
225     }
226
227     public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline,
228                             int startcolumn, int buffersize) {
229         inputStream = dstream;
230         line = startline;
231         column = startcolumn - 1;
232
233         available = bufsize = buffersize;
234         buffer = new char[buffersize];
235         bufline = new int[buffersize];
236         bufcolumn = new int[buffersize];
237     }
238
239     public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline,
240                             int startcolumn) {
241         this(dstream, startline, startcolumn, 4096);
242     }
243
244     public SimpleCharStream(java.io.Reader JavaDoc dstream) {
245         this(dstream, 1, 1, 4096);
246     }
247
248     public void ReInit(java.io.Reader JavaDoc dstream, int startline,
249                        int startcolumn, int buffersize) {
250         inputStream = dstream;
251         line = startline;
252         column = startcolumn - 1;
253
254         if(buffer == null || buffersize != buffer.length) {
255             available = bufsize = buffersize;
256             buffer = new char[buffersize];
257             bufline = new int[buffersize];
258             bufcolumn = new int[buffersize];
259         }
260         prevCharIsLF = prevCharIsCR = false;
261         tokenBegin = inBuf = maxNextCharInd = 0;
262         bufpos = -1;
263     }
264
265     public void ReInit(java.io.Reader JavaDoc dstream, int startline,
266                        int startcolumn) {
267         ReInit(dstream, startline, startcolumn, 4096);
268     }
269
270     public void ReInit(java.io.Reader JavaDoc dstream) {
271         ReInit(dstream, 1, 1, 4096);
272     }
273
274     public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
275                             int startcolumn, int buffersize) {
276         this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
277     }
278
279     public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
280                             int startcolumn) {
281         this(dstream, startline, startcolumn, 4096);
282     }
283
284     public SimpleCharStream(java.io.InputStream JavaDoc dstream) {
285         this(dstream, 1, 1, 4096);
286     }
287
288     public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
289                        int startcolumn, int buffersize) {
290         ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
291     }
292
293     public void ReInit(java.io.InputStream JavaDoc dstream) {
294         ReInit(dstream, 1, 1, 4096);
295     }
296
297     public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
298                        int startcolumn) {
299         ReInit(dstream, startline, startcolumn, 4096);
300     }
301
302     public String JavaDoc GetImage() {
303         if(bufpos >= tokenBegin)
304             return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
305         else
306             return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
307                 new String JavaDoc(buffer, 0, bufpos + 1);
308     }
309
310     public char[] GetSuffix(int len) {
311         char[] ret = new char[len];
312
313         if((bufpos + 1) >= len)
314             System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
315         else {
316             System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
317                 len - bufpos - 1);
318             System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
319         }
320
321         return ret;
322     }
323
324     public void Done() {
325         buffer = null;
326         bufline = null;
327         bufcolumn = null;
328     }
329
330     /**
331      * Method to adjust line and column numbers for the start of a token.<BR>
332      */

333     public void adjustBeginLineColumn(int newLine, int newCol) {
334         int start = tokenBegin;
335         int len;
336
337         if(bufpos >= tokenBegin) {
338             len = bufpos - tokenBegin + inBuf + 1;
339         } else {
340             len = bufsize - tokenBegin + bufpos + 1 + inBuf;
341         }
342
343         int i = 0, j = 0, k = 0;
344         int nextColDiff = 0, columnDiff = 0;
345
346         while(i < len &&
347             bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) {
348             bufline[j] = newLine;
349             nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
350             bufcolumn[j] = newCol + columnDiff;
351             columnDiff = nextColDiff;
352             i++;
353         }
354
355         if(i < len) {
356             bufline[j] = newLine++;
357             bufcolumn[j] = newCol + columnDiff;
358
359             while(i++ < len) {
360                 if(bufline[j = start % bufsize] != bufline[++start % bufsize])
361                     bufline[j] = newLine++;
362                 else
363                     bufline[j] = newLine;
364             }
365         }
366
367         line = bufline[j];
368         column = bufcolumn[j];
369     }
370
371 }
372
Popular Tags