KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > ij > UCode_CharStream


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.ij.UCode_CharStream
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 /* Generated By:JavaCC: Do not edit this line. UCode_CharStream.java Version 0.7pre6 */
23 package org.apache.derby.impl.tools.ij;
24
25 /**
26  * An implementation of interface CharStream, where the stream is assumed to
27  * contain only Unicode characters.
28  */

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

231
232   public final int getColumn() {
233      return bufcolumn[bufpos];
234   }
235
236   /**
237    * @deprecated
238    * @see #getEndLine
239    */

240
241   public final int getLine() {
242      return bufline[bufpos];
243   }
244
245   public final int getEndColumn() {
246      return bufcolumn[bufpos];
247   }
248
249   public final int getEndLine() {
250      return bufline[bufpos];
251   }
252
253   public final int getBeginColumn() {
254      return bufcolumn[tokenBegin];
255   }
256
257   public final int getBeginLine() {
258      return bufline[tokenBegin];
259   }
260
261   public final void backup(int amount) {
262
263     inBuf += amount;
264     if ((bufpos -= amount) < 0)
265        bufpos += bufsize;
266   }
267
268   public UCode_CharStream(java.io.Reader JavaDoc dstream,
269                  int startline, int startcolumn, int buffersize)
270   {
271     inputStream = dstream;
272     line = startline;
273     column = startcolumn - 1;
274
275     available = bufsize = buffersize;
276     buffer = new char[buffersize];
277     nextCharBuf = new char[buffersize];
278     bufline = new int[buffersize];
279     bufcolumn = new int[buffersize];
280   }
281         
282
283   public UCode_CharStream(java.io.Reader JavaDoc dstream,
284                                         int startline, int startcolumn)
285   {
286      this(dstream, startline, startcolumn, 4096);
287   }
288
289   public void ReInit(java.io.Reader JavaDoc dstream,
290                  int startline, int startcolumn, int buffersize)
291   {
292     inputStream = dstream;
293     line = startline;
294     column = startcolumn - 1;
295
296     if (buffer == null || buffersize != buffer.length)
297     {
298       available = bufsize = buffersize;
299       buffer = new char[buffersize];
300       nextCharBuf = new char[buffersize];
301       bufline = new int[buffersize];
302       bufcolumn = new int[buffersize];
303     }
304     tokenBegin = inBuf = maxNextCharInd = 0;
305     nextCharInd = bufpos = -1;
306   }
307
308   public void ReInit(java.io.Reader JavaDoc dstream,
309                                         int startline, int startcolumn)
310   {
311      ReInit(dstream, startline, startcolumn, 4096);
312   }
313   public UCode_CharStream(java.io.InputStream JavaDoc dstream, int startline,
314   int startcolumn, int buffersize)
315   {
316      this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
317   }
318
319   public UCode_CharStream(java.io.InputStream JavaDoc dstream, int startline,
320                                                            int startcolumn)
321   {
322      this(dstream, startline, startcolumn, 4096);
323   }
324
325   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
326   int startcolumn, int buffersize)
327   {
328      ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, 4096);
329   }
330   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
331                                                            int startcolumn)
332   {
333      ReInit(dstream, startline, startcolumn, 4096);
334   }
335
336   public final String JavaDoc GetImage()
337   {
338      if (bufpos >= tokenBegin)
339         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
340      else
341         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
342                               new String JavaDoc(buffer, 0, bufpos + 1);
343   }
344
345   public final char[] GetSuffix(int len)
346   {
347      char[] ret = new char[len];
348
349      if ((bufpos + 1) >= len)
350         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
351      else
352      {
353         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
354                                                           len - bufpos - 1);
355         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
356      }
357
358      return ret;
359   }
360
361   public void Done()
362   {
363      nextCharBuf = null;
364      buffer = null;
365      bufline = null;
366      bufcolumn = null;
367   }
368
369   /**
370    * Method to adjust line and column numbers for the start of a token.<BR>
371    */

372   public void adjustBeginLineColumn(int newLine, int newCol)
373   {
374      int start = tokenBegin;
375      int len;
376
377      if (bufpos >= tokenBegin)
378      {
379         len = bufpos - tokenBegin + inBuf + 1;
380      }
381      else
382      {
383         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
384      }
385
386      int i = 0, j = 0, k = 0;
387      int nextColDiff = 0, columnDiff = 0;
388
389      while (i < len &&
390             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
391      {
392         bufline[j] = newLine;
393         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
394         bufcolumn[j] = newCol + columnDiff;
395         columnDiff = nextColDiff;
396         i++;
397      }
398
399      if (i < len)
400      {
401         bufline[j] = newLine++;
402         bufcolumn[j] = newCol + columnDiff;
403
404         while (i++ < len)
405         {
406            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
407               bufline[j] = newLine++;
408            else
409               bufline[j] = newLine;
410         }
411      }
412
413      line = bufline[j];
414      column = bufcolumn[j];
415   }
416
417 }
418
Popular Tags