KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > w3c > flute > parser > Generic_CharStream


1 /* Generated By:JavaCC: Do not edit this line. Generic_CharStream.java Version 0.7pre6 */
2 package org.w3c.flute.parser;
3
4 /**
5  * An implementation of interface CharStream, where the stream is assumed to
6  * contain only ASCII characters (without unicode processing).
7  */

8
9 public final class Generic_CharStream implements CharStream
10 {
11   public static final boolean staticFlag = false;
12   int bufsize;
13   int available;
14   int tokenBegin;
15   public int bufpos = -1;
16   private int bufline[];
17   private int bufcolumn[];
18
19   private int column = 0;
20   private int line = 1;
21
22   private boolean prevCharIsCR = false;
23   private boolean prevCharIsLF = false;
24
25   private java.io.Reader JavaDoc reader;
26
27   private char[] buffer;
28   private int maxNextCharInd = 0;
29   private int inBuf = 0;
30
31   private final void ExpandBuff(boolean wrapAround)
32   {
33      char[] newbuffer = new char[bufsize + 2048];
34      int newbufline[] = new int[bufsize + 2048];
35      int newbufcolumn[] = new int[bufsize + 2048];
36
37      try
38      {
39         if (wrapAround)
40         {
41            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
42            System.arraycopy(buffer, 0, newbuffer,
43                                              bufsize - tokenBegin, bufpos);
44            buffer = newbuffer;
45
46            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
47            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
48            bufline = newbufline;
49
50            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
51            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
52            bufcolumn = newbufcolumn;
53
54            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
55         }
56         else
57         {
58            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
59            buffer = newbuffer;
60
61            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
62            bufline = newbufline;
63
64            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
65            bufcolumn = newbufcolumn;
66
67            maxNextCharInd = (bufpos -= tokenBegin);
68         }
69      }
70      catch (Throwable JavaDoc t)
71      {
72         throw new Error JavaDoc(t.getMessage());
73      }
74
75
76      bufsize += 2048;
77      available = bufsize;
78      tokenBegin = 0;
79   }
80
81   private final void FillBuff() throws java.io.IOException JavaDoc
82   {
83      if (maxNextCharInd == available)
84      {
85         if (available == bufsize)
86         {
87            if (tokenBegin > 2048)
88            {
89               bufpos = maxNextCharInd = 0;
90               available = tokenBegin;
91            }
92            else if (tokenBegin < 0)
93               bufpos = maxNextCharInd = 0;
94            else
95               ExpandBuff(false);
96         }
97         else if (available > tokenBegin)
98            available = bufsize;
99         else if ((tokenBegin - available) < 2048)
100            ExpandBuff(true);
101         else
102            available = tokenBegin;
103      }
104
105      int i;
106      try {
107         if ((i = reader.read(buffer, maxNextCharInd,
108                                     available - maxNextCharInd)) == -1)
109         {
110            reader.close();
111            throw new java.io.IOException JavaDoc();
112         }
113         else
114            maxNextCharInd += i;
115         return;
116      }
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 final char BeginToken() throws java.io.IOException JavaDoc
127   {
128      tokenBegin = -1;
129      char c = readChar();
130      tokenBegin = bufpos;
131
132      return c;
133   }
134
135   private final void UpdateLineColumn(char c)
136   {
137      column++;
138
139      if (prevCharIsLF)
140      {
141         prevCharIsLF = false;
142         line += (column = 1);
143      }
144      else if (prevCharIsCR)
145      {
146         prevCharIsCR = false;
147         if (c == '\n')
148         {
149            prevCharIsLF = true;
150         }
151         else
152            line += (column = 1);
153      }
154
155      switch (c)
156      {
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 final char readChar() throws java.io.IOException JavaDoc
176   {
177      if (inBuf > 0)
178      {
179         --inBuf;
180         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
181      }
182
183      if (++bufpos >= maxNextCharInd)
184         FillBuff();
185
186      char c = (char)((char)0xff & buffer[bufpos]);
187
188      UpdateLineColumn(c);
189      return (c);
190   }
191
192   /**
193    * @deprecated
194    * @see #getEndColumn
195    */

196
197   public final int getColumn() {
198      return bufcolumn[bufpos];
199   }
200
201   /**
202    * @deprecated
203    * @see #getEndLine
204    */

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

310   public void adjustBeginLineColumn(int newLine, int newCol)
311   {
312      int start = tokenBegin;
313      int len;
314
315      if (bufpos >= tokenBegin)
316      {
317         len = bufpos - tokenBegin + inBuf + 1;
318      }
319      else
320      {
321         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
322      }
323
324      int i = 0, j = 0, k = 0;
325      int nextColDiff = 0, columnDiff = 0;
326
327      while (i < len &&
328             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
329      {
330         bufline[j] = newLine;
331         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
332         bufcolumn[j] = newCol + columnDiff;
333         columnDiff = nextColDiff;
334         i++;
335      }
336
337      if (i < len)
338      {
339         bufline[j] = newLine++;
340         bufcolumn[j] = newCol + columnDiff;
341
342         while (i++ < len)
343         {
344            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
345               bufline[j] = newLine++;
346            else
347               bufline[j] = newLine;
348         }
349      }
350
351      line = bufline[j];
352      column = bufcolumn[j];
353   }
354
355 }
356
Popular Tags