KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > steadystate > css > parser > ASCII_CharStream


1 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
2 package com.steadystate.css.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 ASCII_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 inputStream;
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 = inputStream.read(buffer, maxNextCharInd,
108                                     available - maxNextCharInd)) == -1)
109         {
110            inputStream.close();
111            throw new java.io.IOException JavaDoc();
112         }
113         maxNextCharInd += i;
114         return;
115      }
116      catch(java.io.IOException JavaDoc e) {
117         --bufpos;
118         backup(0);
119         if (tokenBegin == -1)
120            tokenBegin = bufpos;
121         throw e;
122      }
123   }
124
125   public final char BeginToken() throws java.io.IOException JavaDoc
126   {
127      tokenBegin = -1;
128      char c = readChar();
129      tokenBegin = bufpos;
130
131      return c;
132   }
133
134   private final void UpdateLineColumn(char c)
135   {
136      column++;
137
138      if (prevCharIsLF)
139      {
140         prevCharIsLF = false;
141         line += (column = 1);
142      }
143      else if (prevCharIsCR)
144      {
145         prevCharIsCR = false;
146         if (c == '\n')
147         {
148            prevCharIsLF = true;
149         }
150         else
151            line += (column = 1);
152      }
153
154      switch (c)
155      {
156         case '\r' :
157            prevCharIsCR = true;
158            break;
159         case '\n' :
160            prevCharIsLF = true;
161            break;
162         case '\t' :
163            column--;
164            column += (8 - (column & 07));
165            break;
166         default :
167            break;
168      }
169
170      bufline[bufpos] = line;
171      bufcolumn[bufpos] = column;
172   }
173
174   public final char readChar() throws java.io.IOException JavaDoc
175   {
176      if (inBuf > 0)
177      {
178         --inBuf;
179         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
180      }
181
182      if (++bufpos >= maxNextCharInd)
183         FillBuff();
184
185      char c = (char)((char)0xff & buffer[bufpos]);
186
187      UpdateLineColumn(c);
188      return (c);
189   }
190
191   /**
192    * @deprecated
193    * @see #getEndColumn
194    */

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

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

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