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