SimpleCharStream.java

  1. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */
  2. /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
  3. package org.apache.commons.configuration2.plist;

  4. /**
  5.  * An implementation of interface CharStream, where the stream is assumed to
  6.  * contain only ASCII characters (without unicode processing).
  7.  */

  8. public class SimpleCharStream
  9. {
  10. /** Whether parser is static. */
  11.   public static final boolean staticFlag = false;
  12.   int bufsize;
  13.   int available;
  14.   int tokenBegin;
  15. /** Position in buffer. */
  16.   public int bufpos = -1;
  17.   protected int bufline[];
  18.   protected int bufcolumn[];

  19.   protected int column = 0;
  20.   protected int line = 1;

  21.   protected boolean prevCharIsCR = false;
  22.   protected boolean prevCharIsLF = false;

  23.   protected java.io.Reader inputStream;

  24.   protected char[] buffer;
  25.   protected int maxNextCharInd = 0;
  26.   protected int inBuf = 0;
  27.   protected int tabSize = 8;

  28.   protected void setTabSize(int i) { tabSize = i; }
  29.   protected int getTabSize(int i) { return tabSize; }


  30.   protected void ExpandBuff(boolean wrapAround)
  31.   {
  32.     char[] newbuffer = new char[bufsize + 2048];
  33.     int newbufline[] = new int[bufsize + 2048];
  34.     int newbufcolumn[] = new int[bufsize + 2048];

  35.     try
  36.     {
  37.       if (wrapAround)
  38.       {
  39.         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  40.         System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
  41.         buffer = newbuffer;

  42.         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  43.         System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  44.         bufline = newbufline;

  45.         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  46.         System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  47.         bufcolumn = newbufcolumn;

  48.         maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  49.       }
  50.       else
  51.       {
  52.         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  53.         buffer = newbuffer;

  54.         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  55.         bufline = newbufline;

  56.         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  57.         bufcolumn = newbufcolumn;

  58.         maxNextCharInd = (bufpos -= tokenBegin);
  59.       }
  60.     }
  61.     catch (Throwable t)
  62.     {
  63.       throw new Error(t.getMessage());
  64.     }


  65.     bufsize += 2048;
  66.     available = bufsize;
  67.     tokenBegin = 0;
  68.   }

  69.   protected void FillBuff() throws java.io.IOException
  70.   {
  71.     if (maxNextCharInd == available)
  72.     {
  73.       if (available == bufsize)
  74.       {
  75.         if (tokenBegin > 2048)
  76.         {
  77.           bufpos = maxNextCharInd = 0;
  78.           available = tokenBegin;
  79.         }
  80.         else if (tokenBegin < 0)
  81.           bufpos = maxNextCharInd = 0;
  82.         else
  83.           ExpandBuff(false);
  84.       }
  85.       else if (available > tokenBegin)
  86.         available = bufsize;
  87.       else if ((tokenBegin - available) < 2048)
  88.         ExpandBuff(true);
  89.       else
  90.         available = tokenBegin;
  91.     }

  92.     int i;
  93.     try {
  94.       if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
  95.       {
  96.         inputStream.close();
  97.         throw new java.io.IOException();
  98.       }
  99.       else
  100.         maxNextCharInd += i;
  101.       return;
  102.     }
  103.     catch(java.io.IOException e) {
  104.       --bufpos;
  105.       backup(0);
  106.       if (tokenBegin == -1)
  107.         tokenBegin = bufpos;
  108.       throw e;
  109.     }
  110.   }

  111. /** Start. */
  112.   public char BeginToken() throws java.io.IOException
  113.   {
  114.     tokenBegin = -1;
  115.     char c = readChar();
  116.     tokenBegin = bufpos;

  117.     return c;
  118.   }

  119.   protected void UpdateLineColumn(char c)
  120.   {
  121.     column++;

  122.     if (prevCharIsLF)
  123.     {
  124.       prevCharIsLF = false;
  125.       line += (column = 1);
  126.     }
  127.     else if (prevCharIsCR)
  128.     {
  129.       prevCharIsCR = false;
  130.       if (c == '\n')
  131.       {
  132.         prevCharIsLF = true;
  133.       }
  134.       else
  135.         line += (column = 1);
  136.     }

  137.     switch (c)
  138.     {
  139.       case '\r' :
  140.         prevCharIsCR = true;
  141.         break;
  142.       case '\n' :
  143.         prevCharIsLF = true;
  144.         break;
  145.       case '\t' :
  146.         column--;
  147.         column += (tabSize - (column % tabSize));
  148.         break;
  149.       default :
  150.         break;
  151.     }

  152.     bufline[bufpos] = line;
  153.     bufcolumn[bufpos] = column;
  154.   }

  155. /** Read a character. */
  156.   public char readChar() throws java.io.IOException
  157.   {
  158.     if (inBuf > 0)
  159.     {
  160.       --inBuf;

  161.       if (++bufpos == bufsize)
  162.         bufpos = 0;

  163.       return buffer[bufpos];
  164.     }

  165.     if (++bufpos >= maxNextCharInd)
  166.       FillBuff();

  167.     char c = buffer[bufpos];

  168.     UpdateLineColumn(c);
  169.     return c;
  170.   }

  171.   @Deprecated
  172.   /**
  173.    * @deprecated
  174.    * @see #getEndColumn
  175.    */

  176.   public int getColumn() {
  177.     return bufcolumn[bufpos];
  178.   }

  179.   @Deprecated
  180.   /**
  181.    * @deprecated
  182.    * @see #getEndLine
  183.    */

  184.   public int getLine() {
  185.     return bufline[bufpos];
  186.   }

  187.   /** Get token end column number. */
  188.   public int getEndColumn() {
  189.     return bufcolumn[bufpos];
  190.   }

  191.   /** Get token end line number. */
  192.   public int getEndLine() {
  193.      return bufline[bufpos];
  194.   }

  195.   /** Get token beginning column number. */
  196.   public int getBeginColumn() {
  197.     return bufcolumn[tokenBegin];
  198.   }

  199.   /** Get token beginning line number. */
  200.   public int getBeginLine() {
  201.     return bufline[tokenBegin];
  202.   }

  203. /** Backup a number of characters. */
  204.   public void backup(int amount) {

  205.     inBuf += amount;
  206.     if ((bufpos -= amount) < 0)
  207.       bufpos += bufsize;
  208.   }

  209.   /** Constructor. */
  210.   public SimpleCharStream(java.io.Reader dstream, int startline,
  211.   int startcolumn, int buffersize)
  212.   {
  213.     inputStream = dstream;
  214.     line = startline;
  215.     column = startcolumn - 1;

  216.     available = bufsize = buffersize;
  217.     buffer = new char[buffersize];
  218.     bufline = new int[buffersize];
  219.     bufcolumn = new int[buffersize];
  220.   }

  221.   /** Constructor. */
  222.   public SimpleCharStream(java.io.Reader dstream, int startline,
  223.                           int startcolumn)
  224.   {
  225.     this(dstream, startline, startcolumn, 4096);
  226.   }

  227.   /** Constructor. */
  228.   public SimpleCharStream(java.io.Reader dstream)
  229.   {
  230.     this(dstream, 1, 1, 4096);
  231.   }

  232.   /** Reinitialise. */
  233.   public void ReInit(java.io.Reader dstream, int startline,
  234.   int startcolumn, int buffersize)
  235.   {
  236.     inputStream = dstream;
  237.     line = startline;
  238.     column = startcolumn - 1;

  239.     if (buffer == null || buffersize != buffer.length)
  240.     {
  241.       available = bufsize = buffersize;
  242.       buffer = new char[buffersize];
  243.       bufline = new int[buffersize];
  244.       bufcolumn = new int[buffersize];
  245.     }
  246.     prevCharIsLF = prevCharIsCR = false;
  247.     tokenBegin = inBuf = maxNextCharInd = 0;
  248.     bufpos = -1;
  249.   }

  250.   /** Reinitialise. */
  251.   public void ReInit(java.io.Reader dstream, int startline,
  252.                      int startcolumn)
  253.   {
  254.     ReInit(dstream, startline, startcolumn, 4096);
  255.   }

  256.   /** Reinitialise. */
  257.   public void ReInit(java.io.Reader dstream)
  258.   {
  259.     ReInit(dstream, 1, 1, 4096);
  260.   }
  261.   /** Constructor. */
  262.   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  263.   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  264.   {
  265.     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  266.   }

  267.   /** Constructor. */
  268.   public SimpleCharStream(java.io.InputStream dstream, int startline,
  269.   int startcolumn, int buffersize)
  270.   {
  271.     this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  272.   }

  273.   /** Constructor. */
  274.   public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  275.                           int startcolumn) throws java.io.UnsupportedEncodingException
  276.   {
  277.     this(dstream, encoding, startline, startcolumn, 4096);
  278.   }

  279.   /** Constructor. */
  280.   public SimpleCharStream(java.io.InputStream dstream, int startline,
  281.                           int startcolumn)
  282.   {
  283.     this(dstream, startline, startcolumn, 4096);
  284.   }

  285.   /** Constructor. */
  286.   public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  287.   {
  288.     this(dstream, encoding, 1, 1, 4096);
  289.   }

  290.   /** Constructor. */
  291.   public SimpleCharStream(java.io.InputStream dstream)
  292.   {
  293.     this(dstream, 1, 1, 4096);
  294.   }

  295.   /** Reinitialise. */
  296.   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  297.                           int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  298.   {
  299.     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  300.   }

  301.   /** Reinitialise. */
  302.   public void ReInit(java.io.InputStream dstream, int startline,
  303.                           int startcolumn, int buffersize)
  304.   {
  305.     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  306.   }

  307.   /** Reinitialise. */
  308.   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  309.   {
  310.     ReInit(dstream, encoding, 1, 1, 4096);
  311.   }

  312.   /** Reinitialise. */
  313.   public void ReInit(java.io.InputStream dstream)
  314.   {
  315.     ReInit(dstream, 1, 1, 4096);
  316.   }
  317.   /** Reinitialise. */
  318.   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  319.                      int startcolumn) throws java.io.UnsupportedEncodingException
  320.   {
  321.     ReInit(dstream, encoding, startline, startcolumn, 4096);
  322.   }
  323.   /** Reinitialise. */
  324.   public void ReInit(java.io.InputStream dstream, int startline,
  325.                      int startcolumn)
  326.   {
  327.     ReInit(dstream, startline, startcolumn, 4096);
  328.   }
  329.   /** Get token literal value. */
  330.   public String GetImage()
  331.   {
  332.     if (bufpos >= tokenBegin)
  333.       return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  334.     else
  335.       return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  336.                             new String(buffer, 0, bufpos + 1);
  337.   }

  338.   /** Get the suffix. */
  339.   public char[] GetSuffix(int len)
  340.   {
  341.     char[] ret = new char[len];

  342.     if ((bufpos + 1) >= len)
  343.       System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  344.     else
  345.     {
  346.       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  347.                                                         len - bufpos - 1);
  348.       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  349.     }

  350.     return ret;
  351.   }

  352.   /** Reset buffer when finished. */
  353.   public void Done()
  354.   {
  355.     buffer = null;
  356.     bufline = null;
  357.     bufcolumn = null;
  358.   }

  359.   /**
  360.    * Method to adjust line and column numbers for the start of a token.
  361.    */
  362.   public void adjustBeginLineColumn(int newLine, int newCol)
  363.   {
  364.     int start = tokenBegin;
  365.     int len;

  366.     if (bufpos >= tokenBegin)
  367.     {
  368.       len = bufpos - tokenBegin + inBuf + 1;
  369.     }
  370.     else
  371.     {
  372.       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  373.     }

  374.     int i = 0, j = 0, k = 0;
  375.     int nextColDiff = 0, columnDiff = 0;

  376.     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  377.     {
  378.       bufline[j] = newLine;
  379.       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  380.       bufcolumn[j] = newCol + columnDiff;
  381.       columnDiff = nextColDiff;
  382.       i++;
  383.     }

  384.     if (i < len)
  385.     {
  386.       bufline[j] = newLine++;
  387.       bufcolumn[j] = newCol + columnDiff;

  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.     line = bufline[j];
  397.     column = bufcolumn[j];
  398.   }

  399. }
  400. /* JavaCC - OriginalChecksum=4957ec397195460f62908ce38a9ec1e1 (do not edit this line) */