001/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
002package org.apache.commons.jxpath.ri.parser;
003
004/**
005 * An implementation of interface CharStream, where the stream is assumed to
006 * contain only ASCII characters (without unicode processing).
007 */
008
009public class SimpleCharStream
010{
011  public static final boolean staticFlag = false;
012  int bufsize;
013  int available;
014  int tokenBegin;
015  public int bufpos = -1;
016  protected int bufline[];
017  protected int bufcolumn[];
018
019  protected int column = 0;
020  protected int line = 1;
021
022  protected boolean prevCharIsCR = false;
023  protected boolean prevCharIsLF = false;
024
025  protected java.io.Reader inputStream;
026
027  protected char[] buffer;
028  protected int maxNextCharInd = 0;
029  protected int inBuf = 0;
030
031  protected void ExpandBuff(boolean wrapAround)
032  {
033     char[] newbuffer = new char[bufsize + 2048];
034     int newbufline[] = new int[bufsize + 2048];
035     int newbufcolumn[] = new int[bufsize + 2048];
036
037     try
038     {
039        if (wrapAround)
040        {
041           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
042           System.arraycopy(buffer, 0, newbuffer,
043                                             bufsize - tokenBegin, bufpos);
044           buffer = newbuffer;
045
046           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
047           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
048           bufline = newbufline;
049
050           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
051           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
052           bufcolumn = newbufcolumn;
053
054           maxNextCharInd = (bufpos += (bufsize - tokenBegin));
055        }
056        else
057        {
058           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
059           buffer = newbuffer;
060
061           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
062           bufline = newbufline;
063
064           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
065           bufcolumn = newbufcolumn;
066
067           maxNextCharInd = (bufpos -= tokenBegin);
068        }
069     }
070     catch (Throwable t)
071     {
072        throw new Error(t.getMessage());
073     }
074
075
076     bufsize += 2048;
077     available = bufsize;
078     tokenBegin = 0;
079  }
080
081  protected void FillBuff() throws java.io.IOException
082  {
083     if (maxNextCharInd == available)
084     {
085        if (available == bufsize)
086        {
087           if (tokenBegin > 2048)
088           {
089              bufpos = maxNextCharInd = 0;
090              available = tokenBegin;
091           }
092           else if (tokenBegin < 0)
093              bufpos = maxNextCharInd = 0;
094           else
095              ExpandBuff(false);
096        }
097        else if (available > tokenBegin)
098           available = bufsize;
099        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();
112        }
113        else
114           maxNextCharInd += i;
115        return;
116     }
117     catch(java.io.IOException 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
127  {
128     tokenBegin = -1;
129     char c = readChar();
130     tokenBegin = bufpos;
131
132     return c;
133  }
134
135  protected 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 char readChar() throws java.io.IOException
176  {
177     if (inBuf > 0)
178     {
179        --inBuf;
180
181        if (++bufpos == bufsize)
182           bufpos = 0;
183
184        return buffer[bufpos];
185     }
186
187     if (++bufpos >= maxNextCharInd)
188        FillBuff();
189
190     char c = buffer[bufpos];
191
192     UpdateLineColumn(c);
193     return (c);
194  }
195
196  /**
197   * @deprecated
198   * @see #getEndColumn
199   */
200
201  public int getColumn() {
202     return bufcolumn[bufpos];
203  }
204
205  /**
206   * @deprecated
207   * @see #getEndLine
208   */
209
210  public int getLine() {
211     return bufline[bufpos];
212  }
213
214  public int getEndColumn() {
215     return bufcolumn[bufpos];
216  }
217
218  public int getEndLine() {
219     return bufline[bufpos];
220  }
221
222  public int getBeginColumn() {
223     return bufcolumn[tokenBegin];
224  }
225
226  public int getBeginLine() {
227     return bufline[tokenBegin];
228  }
229
230  public void backup(int amount) {
231
232    inBuf += amount;
233    if ((bufpos -= amount) < 0)
234       bufpos += bufsize;
235  }
236
237  public SimpleCharStream(java.io.Reader dstream, int startline,
238  int startcolumn, int buffersize)
239  {
240    inputStream = dstream;
241    line = startline;
242    column = startcolumn - 1;
243
244    available = bufsize = buffersize;
245    buffer = new char[buffersize];
246    bufline = new int[buffersize];
247    bufcolumn = new int[buffersize];
248  }
249
250  public SimpleCharStream(java.io.Reader dstream, int startline,
251                                                           int startcolumn)
252  {
253     this(dstream, startline, startcolumn, 4096);
254  }
255
256  public SimpleCharStream(java.io.Reader dstream)
257  {
258     this(dstream, 1, 1, 4096);
259  }
260  public void ReInit(java.io.Reader dstream, int startline,
261  int startcolumn, int buffersize)
262  {
263    inputStream = dstream;
264    line = startline;
265    column = startcolumn - 1;
266
267    if (buffer == null || buffersize != buffer.length)
268    {
269      available = bufsize = buffersize;
270      buffer = new char[buffersize];
271      bufline = new int[buffersize];
272      bufcolumn = new int[buffersize];
273    }
274    prevCharIsLF = prevCharIsCR = false;
275    tokenBegin = inBuf = maxNextCharInd = 0;
276    bufpos = -1;
277  }
278
279  public void ReInit(java.io.Reader dstream, int startline,
280                                                           int startcolumn)
281  {
282     ReInit(dstream, startline, startcolumn, 4096);
283  }
284
285  public void ReInit(java.io.Reader dstream)
286  {
287     ReInit(dstream, 1, 1, 4096);
288  }
289  public SimpleCharStream(java.io.InputStream dstream, int startline,
290  int startcolumn, int buffersize)
291  {
292     this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
293  }
294
295  public SimpleCharStream(java.io.InputStream dstream, int startline,
296                                                           int startcolumn)
297  {
298     this(dstream, startline, startcolumn, 4096);
299  }
300
301  public SimpleCharStream(java.io.InputStream dstream)
302  {
303     this(dstream, 1, 1, 4096);
304  }
305
306  public void ReInit(java.io.InputStream dstream, int startline,
307                          int startcolumn, int buffersize)
308  {
309     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
310  }
311
312  public void ReInit(java.io.InputStream dstream)
313  {
314     ReInit(dstream, 1, 1, 4096);
315  }
316  public void ReInit(java.io.InputStream dstream, int startline,
317                                                           int startcolumn)
318  {
319     ReInit(dstream, startline, startcolumn, 4096);
320  }
321  public String GetImage()
322  {
323     if (bufpos >= tokenBegin)
324        return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
325     else
326        return new String(buffer, tokenBegin, bufsize - tokenBegin) +
327                              new String(buffer, 0, bufpos + 1);
328  }
329
330  public char[] GetSuffix(int len)
331  {
332     char[] ret = new char[len];
333
334     if ((bufpos + 1) >= len)
335        System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
336     else
337     {
338        System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
339                                                          len - bufpos - 1);
340        System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
341     }
342
343     return ret;
344  }
345
346  public void Done()
347  {
348     buffer = null;
349     bufline = null;
350     bufcolumn = null;
351  }
352
353  /**
354   * Method to adjust line and column numbers for the start of a token.<BR>
355   */
356  public void adjustBeginLineColumn(int newLine, int newCol)
357  {
358     int start = tokenBegin;
359     int len;
360
361     if (bufpos >= tokenBegin)
362     {
363        len = bufpos - tokenBegin + inBuf + 1;
364     }
365     else
366     {
367        len = bufsize - tokenBegin + bufpos + 1 + inBuf;
368     }
369
370     int i = 0, j = 0, k = 0;
371     int nextColDiff = 0, columnDiff = 0;
372
373     while (i < len &&
374            bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
375     {
376        bufline[j] = newLine;
377        nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
378        bufcolumn[j] = newCol + columnDiff;
379        columnDiff = nextColDiff;
380        i++;
381     }
382
383     if (i < len)
384     {
385        bufline[j] = newLine++;
386        bufcolumn[j] = newCol + columnDiff;
387
388        while (i++ < len)
389        {
390           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
391              bufline[j] = newLine++;
392           else
393              bufline[j] = newLine;
394        }
395     }
396
397     line = bufline[j];
398     column = bufcolumn[j];
399  }
400
401}