Home | History | Annotate | Download | only in parser
      1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
      2 /*
      3  *  Copyright 2004 the mime4j project
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package org.apache.james.mime4j.field.address.parser;
     18 
     19 /**
     20  * An implementation of interface CharStream, where the stream is assumed to
     21  * contain only ASCII characters (without unicode processing).
     22  */
     23 
     24 public class SimpleCharStream
     25 {
     26   public static final boolean staticFlag = false;
     27   int bufsize;
     28   int available;
     29   int tokenBegin;
     30   public int bufpos = -1;
     31   protected int bufline[];
     32   protected int bufcolumn[];
     33 
     34   protected int column = 0;
     35   protected int line = 1;
     36 
     37   protected boolean prevCharIsCR = false;
     38   protected boolean prevCharIsLF = false;
     39 
     40   protected java.io.Reader inputStream;
     41 
     42   protected char[] buffer;
     43   protected int maxNextCharInd = 0;
     44   protected int inBuf = 0;
     45   protected int tabSize = 8;
     46 
     47   protected void setTabSize(int i) { tabSize = i; }
     48   protected int getTabSize(int i) { return tabSize; }
     49 
     50 
     51   protected void ExpandBuff(boolean wrapAround)
     52   {
     53      char[] newbuffer = new char[bufsize + 2048];
     54      int newbufline[] = new int[bufsize + 2048];
     55      int newbufcolumn[] = new int[bufsize + 2048];
     56 
     57      try
     58      {
     59         if (wrapAround)
     60         {
     61            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
     62            System.arraycopy(buffer, 0, newbuffer,
     63                                              bufsize - tokenBegin, bufpos);
     64            buffer = newbuffer;
     65 
     66            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
     67            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
     68            bufline = newbufline;
     69 
     70            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
     71            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
     72            bufcolumn = newbufcolumn;
     73 
     74            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
     75         }
     76         else
     77         {
     78            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
     79            buffer = newbuffer;
     80 
     81            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
     82            bufline = newbufline;
     83 
     84            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
     85            bufcolumn = newbufcolumn;
     86 
     87            maxNextCharInd = (bufpos -= tokenBegin);
     88         }
     89      }
     90      catch (Throwable t)
     91      {
     92         throw new Error(t.getMessage());
     93      }
     94 
     95 
     96      bufsize += 2048;
     97      available = bufsize;
     98      tokenBegin = 0;
     99   }
    100 
    101   protected void FillBuff() throws java.io.IOException
    102   {
    103      if (maxNextCharInd == available)
    104      {
    105         if (available == bufsize)
    106         {
    107            if (tokenBegin > 2048)
    108            {
    109               bufpos = maxNextCharInd = 0;
    110               available = tokenBegin;
    111            }
    112            else if (tokenBegin < 0)
    113               bufpos = maxNextCharInd = 0;
    114            else
    115               ExpandBuff(false);
    116         }
    117         else if (available > tokenBegin)
    118            available = bufsize;
    119         else if ((tokenBegin - available) < 2048)
    120            ExpandBuff(true);
    121         else
    122            available = tokenBegin;
    123      }
    124 
    125      int i;
    126      try {
    127         if ((i = inputStream.read(buffer, maxNextCharInd,
    128                                     available - maxNextCharInd)) == -1)
    129         {
    130            inputStream.close();
    131            throw new java.io.IOException();
    132         }
    133         else
    134            maxNextCharInd += i;
    135         return;
    136      }
    137      catch(java.io.IOException e) {
    138         --bufpos;
    139         backup(0);
    140         if (tokenBegin == -1)
    141            tokenBegin = bufpos;
    142         throw e;
    143      }
    144   }
    145 
    146   public char BeginToken() throws java.io.IOException
    147   {
    148      tokenBegin = -1;
    149      char c = readChar();
    150      tokenBegin = bufpos;
    151 
    152      return c;
    153   }
    154 
    155   protected void UpdateLineColumn(char c)
    156   {
    157      column++;
    158 
    159      if (prevCharIsLF)
    160      {
    161         prevCharIsLF = false;
    162         line += (column = 1);
    163      }
    164      else if (prevCharIsCR)
    165      {
    166         prevCharIsCR = false;
    167         if (c == '\n')
    168         {
    169            prevCharIsLF = true;
    170         }
    171         else
    172            line += (column = 1);
    173      }
    174 
    175      switch (c)
    176      {
    177         case '\r' :
    178            prevCharIsCR = true;
    179            break;
    180         case '\n' :
    181            prevCharIsLF = true;
    182            break;
    183         case '\t' :
    184            column--;
    185            column += (tabSize - (column % tabSize));
    186            break;
    187         default :
    188            break;
    189      }
    190 
    191      bufline[bufpos] = line;
    192      bufcolumn[bufpos] = column;
    193   }
    194 
    195   public char readChar() throws java.io.IOException
    196   {
    197      if (inBuf > 0)
    198      {
    199         --inBuf;
    200 
    201         if (++bufpos == bufsize)
    202            bufpos = 0;
    203 
    204         return buffer[bufpos];
    205      }
    206 
    207      if (++bufpos >= maxNextCharInd)
    208         FillBuff();
    209 
    210      char c = buffer[bufpos];
    211 
    212      UpdateLineColumn(c);
    213      return (c);
    214   }
    215 
    216   /**
    217    * @deprecated
    218    * @see #getEndColumn
    219    */
    220   @Deprecated
    221   public int getColumn() {
    222      return bufcolumn[bufpos];
    223   }
    224 
    225   /**
    226    * @deprecated
    227    * @see #getEndLine
    228    */
    229   @Deprecated
    230   public int getLine() {
    231      return bufline[bufpos];
    232   }
    233 
    234   public int getEndColumn() {
    235      return bufcolumn[bufpos];
    236   }
    237 
    238   public int getEndLine() {
    239      return bufline[bufpos];
    240   }
    241 
    242   public int getBeginColumn() {
    243      return bufcolumn[tokenBegin];
    244   }
    245 
    246   public int getBeginLine() {
    247      return bufline[tokenBegin];
    248   }
    249 
    250   public void backup(int amount) {
    251 
    252     inBuf += amount;
    253     if ((bufpos -= amount) < 0)
    254        bufpos += bufsize;
    255   }
    256 
    257   public SimpleCharStream(java.io.Reader dstream, int startline,
    258   int startcolumn, int buffersize)
    259   {
    260     inputStream = dstream;
    261     line = startline;
    262     column = startcolumn - 1;
    263 
    264     available = bufsize = buffersize;
    265     buffer = new char[buffersize];
    266     bufline = new int[buffersize];
    267     bufcolumn = new int[buffersize];
    268   }
    269 
    270   public SimpleCharStream(java.io.Reader dstream, int startline,
    271                           int startcolumn)
    272   {
    273      this(dstream, startline, startcolumn, 4096);
    274   }
    275 
    276   public SimpleCharStream(java.io.Reader dstream)
    277   {
    278      this(dstream, 1, 1, 4096);
    279   }
    280   public void ReInit(java.io.Reader dstream, int startline,
    281   int startcolumn, int buffersize)
    282   {
    283     inputStream = dstream;
    284     line = startline;
    285     column = startcolumn - 1;
    286 
    287     if (buffer == null || buffersize != buffer.length)
    288     {
    289       available = bufsize = buffersize;
    290       buffer = new char[buffersize];
    291       bufline = new int[buffersize];
    292       bufcolumn = new int[buffersize];
    293     }
    294     prevCharIsLF = prevCharIsCR = false;
    295     tokenBegin = inBuf = maxNextCharInd = 0;
    296     bufpos = -1;
    297   }
    298 
    299   public void ReInit(java.io.Reader dstream, int startline,
    300                      int startcolumn)
    301   {
    302      ReInit(dstream, startline, startcolumn, 4096);
    303   }
    304 
    305   public void ReInit(java.io.Reader dstream)
    306   {
    307      ReInit(dstream, 1, 1, 4096);
    308   }
    309   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
    310   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
    311   {
    312      this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
    313   }
    314 
    315   public SimpleCharStream(java.io.InputStream dstream, int startline,
    316   int startcolumn, int buffersize)
    317   {
    318      this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
    319   }
    320 
    321   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
    322                           int startcolumn) throws java.io.UnsupportedEncodingException
    323   {
    324      this(dstream, encoding, startline, startcolumn, 4096);
    325   }
    326 
    327   public SimpleCharStream(java.io.InputStream dstream, int startline,
    328                           int startcolumn)
    329   {
    330      this(dstream, startline, startcolumn, 4096);
    331   }
    332 
    333   public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
    334   {
    335      this(dstream, encoding, 1, 1, 4096);
    336   }
    337 
    338   public SimpleCharStream(java.io.InputStream dstream)
    339   {
    340      this(dstream, 1, 1, 4096);
    341   }
    342 
    343   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
    344                           int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
    345   {
    346      ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
    347   }
    348 
    349   public void ReInit(java.io.InputStream dstream, int startline,
    350                           int startcolumn, int buffersize)
    351   {
    352      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
    353   }
    354 
    355   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
    356   {
    357      ReInit(dstream, encoding, 1, 1, 4096);
    358   }
    359 
    360   public void ReInit(java.io.InputStream dstream)
    361   {
    362      ReInit(dstream, 1, 1, 4096);
    363   }
    364   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
    365                      int startcolumn) throws java.io.UnsupportedEncodingException
    366   {
    367      ReInit(dstream, encoding, startline, startcolumn, 4096);
    368   }
    369   public void ReInit(java.io.InputStream dstream, int startline,
    370                      int startcolumn)
    371   {
    372      ReInit(dstream, startline, startcolumn, 4096);
    373   }
    374   public String GetImage()
    375   {
    376      if (bufpos >= tokenBegin)
    377         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
    378      else
    379         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
    380                               new String(buffer, 0, bufpos + 1);
    381   }
    382 
    383   public char[] GetSuffix(int len)
    384   {
    385      char[] ret = new char[len];
    386 
    387      if ((bufpos + 1) >= len)
    388         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
    389      else
    390      {
    391         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
    392                                                           len - bufpos - 1);
    393         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
    394      }
    395 
    396      return ret;
    397   }
    398 
    399   public void Done()
    400   {
    401      buffer = null;
    402      bufline = null;
    403      bufcolumn = null;
    404   }
    405 
    406   /**
    407    * Method to adjust line and column numbers for the start of a token.
    408    */
    409   public void adjustBeginLineColumn(int newLine, int newCol)
    410   {
    411      int start = tokenBegin;
    412      int len;
    413 
    414      if (bufpos >= tokenBegin)
    415      {
    416         len = bufpos - tokenBegin + inBuf + 1;
    417      }
    418      else
    419      {
    420         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
    421      }
    422 
    423      int i = 0, j = 0, k = 0;
    424      int nextColDiff = 0, columnDiff = 0;
    425 
    426      while (i < len &&
    427             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
    428      {
    429         bufline[j] = newLine;
    430         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
    431         bufcolumn[j] = newCol + columnDiff;
    432         columnDiff = nextColDiff;
    433         i++;
    434      }
    435 
    436      if (i < len)
    437      {
    438         bufline[j] = newLine++;
    439         bufcolumn[j] = newCol + columnDiff;
    440 
    441         while (i++ < len)
    442         {
    443            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
    444               bufline[j] = newLine++;
    445            else
    446               bufline[j] = newLine;
    447         }
    448      }
    449 
    450      line = bufline[j];
    451      column = bufcolumn[j];
    452   }
    453 
    454 }
    455