KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > ASCII_CharStream


1 /*
2  * Copyright 1999,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
17 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
18 package org.apache.taglibs.standard.lang.jpath.expression;
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 final class ASCII_CharStream
26 {
27   public static final boolean staticFlag = false;
28   int bufsize;
29   int available;
30   int tokenBegin;
31   public int bufpos = -1;
32   private int bufline[];
33   private int bufcolumn[];
34
35   private int column = 0;
36   private int line = 1;
37
38   private boolean prevCharIsCR = false;
39   private boolean prevCharIsLF = false;
40
41   private java.io.Reader JavaDoc inputStream;
42
43   private char[] buffer;
44   private int maxNextCharInd = 0;
45   private int inBuf = 0;
46
47   private final void ExpandBuff(boolean wrapAround)
48   {
49      char[] newbuffer = new char[bufsize + 2048];
50      int newbufline[] = new int[bufsize + 2048];
51      int newbufcolumn[] = new int[bufsize + 2048];
52
53      try
54      {
55         if (wrapAround)
56         {
57            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
58            System.arraycopy(buffer, 0, newbuffer,
59                                              bufsize - tokenBegin, bufpos);
60            buffer = newbuffer;
61
62            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
63            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
64            bufline = newbufline;
65
66            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
67            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
68            bufcolumn = newbufcolumn;
69
70            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
71         }
72         else
73         {
74            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
75            buffer = newbuffer;
76
77            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
78            bufline = newbufline;
79
80            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
81            bufcolumn = newbufcolumn;
82
83            maxNextCharInd = (bufpos -= tokenBegin);
84         }
85      }
86      catch (Throwable JavaDoc t)
87      {
88         throw new Error JavaDoc(t.getMessage());
89      }
90
91
92      bufsize += 2048;
93      available = bufsize;
94      tokenBegin = 0;
95   }
96
97   private final void FillBuff() throws java.io.IOException JavaDoc
98   {
99      if (maxNextCharInd == available)
100      {
101         if (available == bufsize)
102         {
103            if (tokenBegin > 2048)
104            {
105               bufpos = maxNextCharInd = 0;
106               available = tokenBegin;
107            }
108            else if (tokenBegin < 0)
109               bufpos = maxNextCharInd = 0;
110            else
111               ExpandBuff(false);
112         }
113         else if (available > tokenBegin)
114            available = bufsize;
115         else if ((tokenBegin - available) < 2048)
116            ExpandBuff(true);
117         else
118            available = tokenBegin;
119      }
120
121      int i;
122      try {
123         if ((i = inputStream.read(buffer, maxNextCharInd,
124                                     available - maxNextCharInd)) == -1)
125         {
126            inputStream.close();
127            throw new java.io.IOException JavaDoc();
128         }
129         else
130            maxNextCharInd += i;
131         return;
132      }
133      catch(java.io.IOException JavaDoc 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 JavaDoc
143   {
144      tokenBegin = -1;
145      char c = readChar();
146      tokenBegin = bufpos;
147
148      return c;
149   }
150
151   private final void UpdateLineColumn(char c)
152   {
153      column++;
154
155      if (prevCharIsLF)
156      {
157         prevCharIsLF = false;
158         line += (column = 1);
159      }
160      else if (prevCharIsCR)
161      {
162         prevCharIsCR = false;
163         if (c == '\n')
164         {
165            prevCharIsLF = true;
166         }
167         else
168            line += (column = 1);
169      }
170
171      switch (c)
172      {
173         case '\r' :
174            prevCharIsCR = true;
175            break;
176         case '\n' :
177            prevCharIsLF = true;
178            break;
179         case '\t' :
180            column--;
181            column += (8 - (column & 07));
182            break;
183         default :
184            break;
185      }
186
187      bufline[bufpos] = line;
188      bufcolumn[bufpos] = column;
189   }
190
191   public final char readChar() throws java.io.IOException JavaDoc
192   {
193      if (inBuf > 0)
194      {
195         --inBuf;
196         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
197      }
198
199      if (++bufpos >= maxNextCharInd)
200         FillBuff();
201
202      char c = (char)((char)0xff & buffer[bufpos]);
203
204      UpdateLineColumn(c);
205      return (c);
206   }
207
208   /**
209    * @deprecated
210    * @see #getEndColumn
211    */

212
213   public final int getColumn() {
214      return bufcolumn[bufpos];
215   }
216
217   /**
218    * @deprecated
219    * @see #getEndLine
220    */

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

348   public void adjustBeginLineColumn(int newLine, int newCol)
349   {
350      int start = tokenBegin;
351      int len;
352
353      if (bufpos >= tokenBegin)
354      {
355         len = bufpos - tokenBegin + inBuf + 1;
356      }
357      else
358      {
359         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
360      }
361
362      int i = 0, j = 0, k = 0;
363      int nextColDiff = 0, columnDiff = 0;
364
365      while (i < len &&
366             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
367      {
368         bufline[j] = newLine;
369         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
370         bufcolumn[j] = newCol + columnDiff;
371         columnDiff = nextColDiff;
372         i++;
373      }
374
375      if (i < len)
376      {
377         bufline[j] = newLine++;
378         bufcolumn[j] = newCol + columnDiff;
379
380         while (i++ < len)
381         {
382            if (bufline[j = start % bufsize] != bufline[++start % 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 }
394
Popular Tags