001    /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */
002    /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
003    /**
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    package org.apache.activemq.selector;
021    
022    /**
023     * An implementation of interface CharStream, where the stream is assumed to
024     * contain only ASCII characters (without unicode processing).
025     */
026    
027    public class SimpleCharStream
028    {
029    /** Whether parser is static. */
030      public static final boolean staticFlag = false;
031      int bufsize;
032      int available;
033      int tokenBegin;
034    /** Position in buffer. */
035      public int bufpos = -1;
036      protected int bufline[];
037      protected int bufcolumn[];
038    
039      protected int column = 0;
040      protected int line = 1;
041    
042      protected boolean prevCharIsCR = false;
043      protected boolean prevCharIsLF = false;
044    
045      protected java.io.Reader inputStream;
046    
047      protected char[] buffer;
048      protected int maxNextCharInd = 0;
049      protected int inBuf = 0;
050      protected int tabSize = 8;
051    
052      protected void setTabSize(int i) { tabSize = i; }
053      protected int getTabSize(int i) { return tabSize; }
054    
055    
056      protected void ExpandBuff(boolean wrapAround)
057      {
058        char[] newbuffer = new char[bufsize + 2048];
059        int newbufline[] = new int[bufsize + 2048];
060        int newbufcolumn[] = new int[bufsize + 2048];
061    
062        try
063        {
064          if (wrapAround)
065          {
066            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
067            System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
068            buffer = newbuffer;
069    
070            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
071            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
072            bufline = newbufline;
073    
074            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
075            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
076            bufcolumn = newbufcolumn;
077    
078            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
079          }
080          else
081          {
082            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
083            buffer = newbuffer;
084    
085            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
086            bufline = newbufline;
087    
088            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
089            bufcolumn = newbufcolumn;
090    
091            maxNextCharInd = (bufpos -= tokenBegin);
092          }
093        }
094        catch (Throwable t)
095        {
096          throw new Error(t.getMessage());
097        }
098    
099    
100        bufsize += 2048;
101        available = bufsize;
102        tokenBegin = 0;
103      }
104    
105      protected void FillBuff() throws java.io.IOException
106      {
107        if (maxNextCharInd == available)
108        {
109          if (available == bufsize)
110          {
111            if (tokenBegin > 2048)
112            {
113              bufpos = maxNextCharInd = 0;
114              available = tokenBegin;
115            }
116            else if (tokenBegin < 0)
117              bufpos = maxNextCharInd = 0;
118            else
119              ExpandBuff(false);
120          }
121          else if (available > tokenBegin)
122            available = bufsize;
123          else if ((tokenBegin - available) < 2048)
124            ExpandBuff(true);
125          else
126            available = tokenBegin;
127        }
128    
129        int i;
130        try {
131          if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
132          {
133            inputStream.close();
134            throw new java.io.IOException();
135          }
136          else
137            maxNextCharInd += i;
138          return;
139        }
140        catch(java.io.IOException e) {
141          --bufpos;
142          backup(0);
143          if (tokenBegin == -1)
144            tokenBegin = bufpos;
145          throw e;
146        }
147      }
148    
149    /** Start. */
150      public char BeginToken() throws java.io.IOException
151      {
152        tokenBegin = -1;
153        char c = readChar();
154        tokenBegin = bufpos;
155    
156        return c;
157      }
158    
159      protected void UpdateLineColumn(char c)
160      {
161        column++;
162    
163        if (prevCharIsLF)
164        {
165          prevCharIsLF = false;
166          line += (column = 1);
167        }
168        else if (prevCharIsCR)
169        {
170          prevCharIsCR = false;
171          if (c == '\n')
172          {
173            prevCharIsLF = true;
174          }
175          else
176            line += (column = 1);
177        }
178    
179        switch (c)
180        {
181          case '\r' :
182            prevCharIsCR = true;
183            break;
184          case '\n' :
185            prevCharIsLF = true;
186            break;
187          case '\t' :
188            column--;
189            column += (tabSize - (column % tabSize));
190            break;
191          default :
192            break;
193        }
194    
195        bufline[bufpos] = line;
196        bufcolumn[bufpos] = column;
197      }
198    
199    /** Read a character. */
200      public char readChar() throws java.io.IOException
201      {
202        if (inBuf > 0)
203        {
204          --inBuf;
205    
206          if (++bufpos == bufsize)
207            bufpos = 0;
208    
209          return buffer[bufpos];
210        }
211    
212        if (++bufpos >= maxNextCharInd)
213          FillBuff();
214    
215        char c = buffer[bufpos];
216    
217        UpdateLineColumn(c);
218        return c;
219      }
220    
221      @Deprecated
222      /**
223       * @deprecated
224       * @see #getEndColumn
225       */
226    
227      public int getColumn() {
228        return bufcolumn[bufpos];
229      }
230    
231      @Deprecated
232      /**
233       * @deprecated
234       * @see #getEndLine
235       */
236    
237      public int getLine() {
238        return bufline[bufpos];
239      }
240    
241      /** Get token end column number. */
242      public int getEndColumn() {
243        return bufcolumn[bufpos];
244      }
245    
246      /** Get token end line number. */
247      public int getEndLine() {
248         return bufline[bufpos];
249      }
250    
251      /** Get token beginning column number. */
252      public int getBeginColumn() {
253        return bufcolumn[tokenBegin];
254      }
255    
256      /** Get token beginning line number. */
257      public int getBeginLine() {
258        return bufline[tokenBegin];
259      }
260    
261    /** Backup a number of characters. */
262      public void backup(int amount) {
263    
264        inBuf += amount;
265        if ((bufpos -= amount) < 0)
266          bufpos += bufsize;
267      }
268    
269      /** Constructor. */
270      public SimpleCharStream(java.io.Reader dstream, int startline,
271      int startcolumn, int buffersize)
272      {
273        inputStream = dstream;
274        line = startline;
275        column = startcolumn - 1;
276    
277        available = bufsize = buffersize;
278        buffer = new char[buffersize];
279        bufline = new int[buffersize];
280        bufcolumn = new int[buffersize];
281      }
282    
283      /** Constructor. */
284      public SimpleCharStream(java.io.Reader dstream, int startline,
285                              int startcolumn)
286      {
287        this(dstream, startline, startcolumn, 4096);
288      }
289    
290      /** Constructor. */
291      public SimpleCharStream(java.io.Reader dstream)
292      {
293        this(dstream, 1, 1, 4096);
294      }
295    
296      /** Reinitialise. */
297      public void ReInit(java.io.Reader dstream, int startline,
298      int startcolumn, int buffersize)
299      {
300        inputStream = dstream;
301        line = startline;
302        column = startcolumn - 1;
303    
304        if (buffer == null || buffersize != buffer.length)
305        {
306          available = bufsize = buffersize;
307          buffer = new char[buffersize];
308          bufline = new int[buffersize];
309          bufcolumn = new int[buffersize];
310        }
311        prevCharIsLF = prevCharIsCR = false;
312        tokenBegin = inBuf = maxNextCharInd = 0;
313        bufpos = -1;
314      }
315    
316      /** Reinitialise. */
317      public void ReInit(java.io.Reader dstream, int startline,
318                         int startcolumn)
319      {
320        ReInit(dstream, startline, startcolumn, 4096);
321      }
322    
323      /** Reinitialise. */
324      public void ReInit(java.io.Reader dstream)
325      {
326        ReInit(dstream, 1, 1, 4096);
327      }
328      /** Constructor. */
329      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
330      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
331      {
332        this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
333      }
334    
335      /** Constructor. */
336      public SimpleCharStream(java.io.InputStream dstream, int startline,
337      int startcolumn, int buffersize)
338      {
339        this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
340      }
341    
342      /** Constructor. */
343      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
344                              int startcolumn) throws java.io.UnsupportedEncodingException
345      {
346        this(dstream, encoding, startline, startcolumn, 4096);
347      }
348    
349      /** Constructor. */
350      public SimpleCharStream(java.io.InputStream dstream, int startline,
351                              int startcolumn)
352      {
353        this(dstream, startline, startcolumn, 4096);
354      }
355    
356      /** Constructor. */
357      public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
358      {
359        this(dstream, encoding, 1, 1, 4096);
360      }
361    
362      /** Constructor. */
363      public SimpleCharStream(java.io.InputStream dstream)
364      {
365        this(dstream, 1, 1, 4096);
366      }
367    
368      /** Reinitialise. */
369      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
370                              int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
371      {
372        ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
373      }
374    
375      /** Reinitialise. */
376      public void ReInit(java.io.InputStream dstream, int startline,
377                              int startcolumn, int buffersize)
378      {
379        ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
380      }
381    
382      /** Reinitialise. */
383      public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
384      {
385        ReInit(dstream, encoding, 1, 1, 4096);
386      }
387    
388      /** Reinitialise. */
389      public void ReInit(java.io.InputStream dstream)
390      {
391        ReInit(dstream, 1, 1, 4096);
392      }
393      /** Reinitialise. */
394      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
395                         int startcolumn) throws java.io.UnsupportedEncodingException
396      {
397        ReInit(dstream, encoding, startline, startcolumn, 4096);
398      }
399      /** Reinitialise. */
400      public void ReInit(java.io.InputStream dstream, int startline,
401                         int startcolumn)
402      {
403        ReInit(dstream, startline, startcolumn, 4096);
404      }
405      /** Get token literal value. */
406      public String GetImage()
407      {
408        if (bufpos >= tokenBegin)
409          return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
410        else
411          return new String(buffer, tokenBegin, bufsize - tokenBegin) +
412                                new String(buffer, 0, bufpos + 1);
413      }
414    
415      /** Get the suffix. */
416      public char[] GetSuffix(int len)
417      {
418        char[] ret = new char[len];
419    
420        if ((bufpos + 1) >= len)
421          System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
422        else
423        {
424          System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
425                                                            len - bufpos - 1);
426          System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
427        }
428    
429        return ret;
430      }
431    
432      /** Reset buffer when finished. */
433      public void Done()
434      {
435        buffer = null;
436        bufline = null;
437        bufcolumn = null;
438      }
439    
440      /**
441       * Method to adjust line and column numbers for the start of a token.
442       */
443      public void adjustBeginLineColumn(int newLine, int newCol)
444      {
445        int start = tokenBegin;
446        int len;
447    
448        if (bufpos >= tokenBegin)
449        {
450          len = bufpos - tokenBegin + inBuf + 1;
451        }
452        else
453        {
454          len = bufsize - tokenBegin + bufpos + 1 + inBuf;
455        }
456    
457        int i = 0, j = 0, k = 0;
458        int nextColDiff = 0, columnDiff = 0;
459    
460        while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
461        {
462          bufline[j] = newLine;
463          nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
464          bufcolumn[j] = newCol + columnDiff;
465          columnDiff = nextColDiff;
466          i++;
467        }
468    
469        if (i < len)
470        {
471          bufline[j] = newLine++;
472          bufcolumn[j] = newCol + columnDiff;
473    
474          while (i++ < len)
475          {
476            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
477              bufline[j] = newLine++;
478            else
479              bufline[j] = newLine;
480          }
481        }
482    
483        line = bufline[j];
484        column = bufcolumn[j];
485      }
486    
487    }
488    /* JavaCC - OriginalChecksum=0d87faf7268f57eee4264d5490bb66fc (do not edit this line) */