Coverage Report - org.apache.commons.ognl.JavaCharStream
 
Classes in this File Line Coverage Branch Coverage Complexity
JavaCharStream
38%
99/254
26%
37/140
3.513
 
 1  
 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 5.0 */
 2  
 /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
 3  
 package org.apache.commons.ognl;
 4  
 
 5  
 /**
 6  
  * An implementation of interface CharStream, where the stream is assumed to
 7  
  * contain only ASCII characters (with java-like unicode escape processing).
 8  
  */
 9  
 
 10  
 public
 11  
 class JavaCharStream
 12  
 {
 13  
   /** Whether parser is static. */
 14  
   public static final boolean staticFlag = false;
 15  
 
 16  
   static final int hexval(char c) throws java.io.IOException {
 17  24
     switch(c)
 18  
     {
 19  
        case '0' :
 20  12
           return 0;
 21  
        case '1' :
 22  0
           return 1;
 23  
        case '2' :
 24  0
           return 2;
 25  
        case '3' :
 26  0
           return 3;
 27  
        case '4' :
 28  2
           return 4;
 29  
        case '5' :
 30  0
           return 5;
 31  
        case '6' :
 32  2
           return 6;
 33  
        case '7' :
 34  0
           return 7;
 35  
        case '8' :
 36  4
           return 8;
 37  
        case '9' :
 38  0
           return 9;
 39  
 
 40  
        case 'a' :
 41  
        case 'A' :
 42  2
           return 10;
 43  
        case 'b' :
 44  
        case 'B' :
 45  0
           return 11;
 46  
        case 'c' :
 47  
        case 'C' :
 48  2
           return 12;
 49  
        case 'd' :
 50  
        case 'D' :
 51  0
           return 13;
 52  
        case 'e' :
 53  
        case 'E' :
 54  0
           return 14;
 55  
        case 'f' :
 56  
        case 'F' :
 57  0
           return 15;
 58  
     }
 59  
 
 60  0
     throw new java.io.IOException(); // Should never come here
 61  
   }
 62  
 
 63  
 /** Position in buffer. */
 64  1148
   public int bufpos = -1;
 65  
   int bufsize;
 66  
   int available;
 67  
   int tokenBegin;
 68  
   protected int bufline[];
 69  
   protected int bufcolumn[];
 70  
 
 71  1148
   protected int column = 0;
 72  1148
   protected int line = 1;
 73  
 
 74  1148
   protected boolean prevCharIsCR = false;
 75  1148
   protected boolean prevCharIsLF = false;
 76  
 
 77  
   protected java.io.Reader inputStream;
 78  
 
 79  
   protected char[] nextCharBuf;
 80  
   protected char[] buffer;
 81  1148
   protected int maxNextCharInd = 0;
 82  1148
   protected int nextCharInd = -1;
 83  1148
   protected int inBuf = 0;
 84  1148
   protected int tabSize = 8;
 85  
 
 86  0
   protected void setTabSize(int i) { tabSize = i; }
 87  0
   protected int getTabSize(int i) { return tabSize; }
 88  
 
 89  
   protected void ExpandBuff(boolean wrapAround)
 90  
   {
 91  0
     char[] newbuffer = new char[bufsize + 2048];
 92  0
     int newbufline[] = new int[bufsize + 2048];
 93  0
     int newbufcolumn[] = new int[bufsize + 2048];
 94  
 
 95  
     try
 96  
     {
 97  0
       if (wrapAround)
 98  
       {
 99  0
         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
 100  0
         System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
 101  0
         buffer = newbuffer;
 102  
 
 103  0
         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
 104  0
         System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
 105  0
         bufline = newbufline;
 106  
 
 107  0
         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
 108  0
         System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
 109  0
         bufcolumn = newbufcolumn;
 110  
 
 111  0
         bufpos += (bufsize - tokenBegin);
 112  
     }
 113  
     else
 114  
     {
 115  0
         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
 116  0
         buffer = newbuffer;
 117  
 
 118  0
         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
 119  0
         bufline = newbufline;
 120  
 
 121  0
         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
 122  0
         bufcolumn = newbufcolumn;
 123  
 
 124  0
         bufpos -= tokenBegin;
 125  
       }
 126  
     }
 127  0
     catch (Throwable t)
 128  
     {
 129  0
       throw new Error(t.getMessage());
 130  0
     }
 131  
 
 132  0
     available = (bufsize += 2048);
 133  0
     tokenBegin = 0;
 134  0
   }
 135  
 
 136  
   protected void FillBuff() throws java.io.IOException
 137  
   {
 138  
     int i;
 139  2845
     if (maxNextCharInd == 4096)
 140  0
       maxNextCharInd = nextCharInd = 0;
 141  
 
 142  
     try {
 143  2845
       if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
 144  
                                           4096 - maxNextCharInd)) == -1)
 145  
       {
 146  1145
         inputStream.close();
 147  1145
         throw new java.io.IOException();
 148  
       }
 149  
       else
 150  1148
          maxNextCharInd += i;
 151  1148
       return;
 152  
     }
 153  1697
     catch(java.io.IOException e) {
 154  1697
       if (bufpos != 0)
 155  
       {
 156  550
         --bufpos;
 157  550
         backup(0);
 158  
       }
 159  
       else
 160  
       {
 161  1147
         bufline[bufpos] = line;
 162  1147
         bufcolumn[bufpos] = column;
 163  
       }
 164  1697
       throw e;
 165  
     }
 166  
   }
 167  
 
 168  
   protected char ReadByte() throws java.io.IOException
 169  
   {
 170  26171
     if (++nextCharInd >= maxNextCharInd)
 171  2845
       FillBuff();
 172  
 
 173  24474
     return nextCharBuf[nextCharInd];
 174  
   }
 175  
 
 176  
 /** @return starting character for token. */
 177  
   public char BeginToken() throws java.io.IOException
 178  
   {
 179  9805
     if (inBuf > 0)
 180  
     {
 181  3841
       --inBuf;
 182  
 
 183  3841
       if (++bufpos == bufsize)
 184  0
         bufpos = 0;
 185  
 
 186  3841
       tokenBegin = bufpos;
 187  3841
       return buffer[bufpos];
 188  
     }
 189  
 
 190  5964
     tokenBegin = 0;
 191  5964
     bufpos = -1;
 192  
 
 193  5964
     return readChar();
 194  
   }
 195  
 
 196  
   protected void AdjustBuffSize()
 197  
   {
 198  0
     if (available == bufsize)
 199  
     {
 200  0
       if (tokenBegin > 2048)
 201  
       {
 202  0
         bufpos = 0;
 203  0
         available = tokenBegin;
 204  
       }
 205  
       else
 206  0
         ExpandBuff(false);
 207  
     }
 208  0
     else if (available > tokenBegin)
 209  0
       available = bufsize;
 210  0
     else if ((tokenBegin - available) < 2048)
 211  0
       ExpandBuff(true);
 212  
     else
 213  0
       available = tokenBegin;
 214  0
   }
 215  
 
 216  
   protected void UpdateLineColumn(char c)
 217  
   {
 218  24450
     column++;
 219  
 
 220  24450
     if (prevCharIsLF)
 221  
     {
 222  0
       prevCharIsLF = false;
 223  0
       line += (column = 1);
 224  
     }
 225  24450
     else if (prevCharIsCR)
 226  
     {
 227  0
       prevCharIsCR = false;
 228  0
       if (c == '\n')
 229  
       {
 230  0
         prevCharIsLF = true;
 231  
       }
 232  
       else
 233  0
         line += (column = 1);
 234  
     }
 235  
 
 236  24450
     switch (c)
 237  
     {
 238  
       case '\r' :
 239  0
         prevCharIsCR = true;
 240  0
         break;
 241  
       case '\n' :
 242  0
         prevCharIsLF = true;
 243  0
         break;
 244  
       case '\t' :
 245  0
         column--;
 246  0
         column += (tabSize - (column % tabSize));
 247  0
         break;
 248  
       default :
 249  
         break;
 250  
     }
 251  
 
 252  24450
     bufline[bufpos] = line;
 253  24450
     bufcolumn[bufpos] = column;
 254  24450
   }
 255  
 
 256  
 /** Read a character. */
 257  
   public char readChar() throws java.io.IOException
 258  
   {
 259  26141
     if (inBuf > 0)
 260  
     {
 261  31
       --inBuf;
 262  
 
 263  31
       if (++bufpos == bufsize)
 264  0
         bufpos = 0;
 265  
 
 266  31
       return buffer[bufpos];
 267  
     }
 268  
 
 269  
     char c;
 270  
 
 271  26110
     if (++bufpos == available)
 272  0
       AdjustBuffSize();
 273  
 
 274  26110
     if ((buffer[bufpos] = c = ReadByte()) == '\\')
 275  
     {
 276  33
       UpdateLineColumn(c);
 277  
 
 278  33
       int backSlashCnt = 1;
 279  
 
 280  
       for (;;) // Read all the backslashes
 281  
       {
 282  37
         if (++bufpos == available)
 283  0
           AdjustBuffSize();
 284  
 
 285  
         try
 286  
         {
 287  37
           if ((buffer[bufpos] = c = ReadByte()) != '\\')
 288  
           {
 289  33
             UpdateLineColumn(c);
 290  
             // found a non-backslash char.
 291  33
             if ((c == 'u') && ((backSlashCnt & 1) == 1))
 292  
             {
 293  6
               if (--bufpos < 0)
 294  0
                 bufpos = bufsize - 1;
 295  
 
 296  6
               break;
 297  
             }
 298  
 
 299  27
             backup(backSlashCnt);
 300  27
             return '\\';
 301  
           }
 302  
         }
 303  0
         catch(java.io.IOException e)
 304  
         {
 305  
           // We are returning one backslash so we should only backup (count-1)
 306  0
           if (backSlashCnt > 1)
 307  0
             backup(backSlashCnt-1);
 308  
 
 309  0
           return '\\';
 310  4
         }
 311  
 
 312  4
         UpdateLineColumn(c);
 313  4
         backSlashCnt++;
 314  
       }
 315  
 
 316  
       // Here, we have seen an odd number of backslash's followed by a 'u'
 317  
       try
 318  
       {
 319  6
         while ((c = ReadByte()) == 'u')
 320  0
           ++column;
 321  
 
 322  6
         buffer[bufpos] = c = (char)(hexval(c) << 12 |
 323  
                                     hexval(ReadByte()) << 8 |
 324  
                                     hexval(ReadByte()) << 4 |
 325  
                                     hexval(ReadByte()));
 326  
 
 327  6
         column += 4;
 328  
       }
 329  0
       catch(java.io.IOException e)
 330  
       {
 331  0
         throw new Error("Invalid escape character at line " + line +
 332  
                                          " column " + column + ".");
 333  6
       }
 334  
 
 335  6
       if (backSlashCnt == 1)
 336  6
         return c;
 337  
       else
 338  
       {
 339  0
         backup(backSlashCnt - 1);
 340  0
         return '\\';
 341  
       }
 342  
     }
 343  
     else
 344  
     {
 345  24380
       UpdateLineColumn(c);
 346  24380
       return c;
 347  
     }
 348  
   }
 349  
 
 350  
   @Deprecated
 351  
   /**
 352  
    * @deprecated
 353  
    * @see #getEndColumn
 354  
    */
 355  
   public int getColumn() {
 356  0
     return bufcolumn[bufpos];
 357  
   }
 358  
 
 359  
   @Deprecated
 360  
   /**
 361  
    * @deprecated
 362  
    * @see #getEndLine
 363  
    */
 364  
   public int getLine() {
 365  0
     return bufline[bufpos];
 366  
   }
 367  
 
 368  
 /** Get end column. */
 369  
   public int getEndColumn() {
 370  8279
     return bufcolumn[bufpos];
 371  
   }
 372  
 
 373  
 /** Get end line. */
 374  
   public int getEndLine() {
 375  8279
     return bufline[bufpos];
 376  
   }
 377  
 
 378  
 /** @return column of token start */
 379  
   public int getBeginColumn() {
 380  8275
     return bufcolumn[tokenBegin];
 381  
   }
 382  
 
 383  
 /** @return line number of token start */
 384  
   public int getBeginLine() {
 385  8275
     return bufline[tokenBegin];
 386  
   }
 387  
 
 388  
 /** Retreat. */
 389  
   public void backup(int amount) {
 390  
 
 391  11562
     inBuf += amount;
 392  11562
     if ((bufpos -= amount) < 0)
 393  0
       bufpos += bufsize;
 394  11562
   }
 395  
 
 396  
 /** Constructor. */
 397  
   public JavaCharStream(java.io.Reader dstream,
 398  
                  int startline, int startcolumn, int buffersize)
 399  1148
   {
 400  1148
     inputStream = dstream;
 401  1148
     line = startline;
 402  1148
     column = startcolumn - 1;
 403  
 
 404  1148
     available = bufsize = buffersize;
 405  1148
     buffer = new char[buffersize];
 406  1148
     bufline = new int[buffersize];
 407  1148
     bufcolumn = new int[buffersize];
 408  1148
     nextCharBuf = new char[4096];
 409  1148
   }
 410  
 
 411  
 /** Constructor. */
 412  
   public JavaCharStream(java.io.Reader dstream,
 413  
                                         int startline, int startcolumn)
 414  
   {
 415  1148
     this(dstream, startline, startcolumn, 4096);
 416  1148
   }
 417  
 
 418  
 /** Constructor. */
 419  
   public JavaCharStream(java.io.Reader dstream)
 420  
   {
 421  0
     this(dstream, 1, 1, 4096);
 422  0
   }
 423  
 /** Reinitialise. */
 424  
   public void ReInit(java.io.Reader dstream,
 425  
                  int startline, int startcolumn, int buffersize)
 426  
   {
 427  0
     inputStream = dstream;
 428  0
     line = startline;
 429  0
     column = startcolumn - 1;
 430  
 
 431  0
     if (buffer == null || buffersize != buffer.length)
 432  
     {
 433  0
       available = bufsize = buffersize;
 434  0
       buffer = new char[buffersize];
 435  0
       bufline = new int[buffersize];
 436  0
       bufcolumn = new int[buffersize];
 437  0
       nextCharBuf = new char[4096];
 438  
     }
 439  0
     prevCharIsLF = prevCharIsCR = false;
 440  0
     tokenBegin = inBuf = maxNextCharInd = 0;
 441  0
     nextCharInd = bufpos = -1;
 442  0
   }
 443  
 
 444  
 /** Reinitialise. */
 445  
   public void ReInit(java.io.Reader dstream,
 446  
                                         int startline, int startcolumn)
 447  
   {
 448  0
     ReInit(dstream, startline, startcolumn, 4096);
 449  0
   }
 450  
 
 451  
 /** Reinitialise. */
 452  
   public void ReInit(java.io.Reader dstream)
 453  
   {
 454  0
     ReInit(dstream, 1, 1, 4096);
 455  0
   }
 456  
 /** Constructor. */
 457  
   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
 458  
   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
 459  
   {
 460  0
     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
 461  0
   }
 462  
 
 463  
 /** Constructor. */
 464  
   public JavaCharStream(java.io.InputStream dstream, int startline,
 465  
   int startcolumn, int buffersize)
 466  
   {
 467  0
     this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
 468  0
   }
 469  
 
 470  
 /** Constructor. */
 471  
   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
 472  
                         int startcolumn) throws java.io.UnsupportedEncodingException
 473  
   {
 474  0
     this(dstream, encoding, startline, startcolumn, 4096);
 475  0
   }
 476  
 
 477  
 /** Constructor. */
 478  
   public JavaCharStream(java.io.InputStream dstream, int startline,
 479  
                         int startcolumn)
 480  
   {
 481  0
     this(dstream, startline, startcolumn, 4096);
 482  0
   }
 483  
 
 484  
 /** Constructor. */
 485  
   public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
 486  
   {
 487  0
     this(dstream, encoding, 1, 1, 4096);
 488  0
   }
 489  
 
 490  
 /** Constructor. */
 491  
   public JavaCharStream(java.io.InputStream dstream)
 492  
   {
 493  0
     this(dstream, 1, 1, 4096);
 494  0
   }
 495  
 
 496  
 /** Reinitialise. */
 497  
   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
 498  
   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
 499  
   {
 500  0
     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
 501  0
   }
 502  
 
 503  
 /** Reinitialise. */
 504  
   public void ReInit(java.io.InputStream dstream, int startline,
 505  
   int startcolumn, int buffersize)
 506  
   {
 507  0
     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
 508  0
   }
 509  
 /** Reinitialise. */
 510  
   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
 511  
                      int startcolumn) throws java.io.UnsupportedEncodingException
 512  
   {
 513  0
     ReInit(dstream, encoding, startline, startcolumn, 4096);
 514  0
   }
 515  
 /** Reinitialise. */
 516  
   public void ReInit(java.io.InputStream dstream, int startline,
 517  
                      int startcolumn)
 518  
   {
 519  0
     ReInit(dstream, startline, startcolumn, 4096);
 520  0
   }
 521  
 /** Reinitialise. */
 522  
   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
 523  
   {
 524  0
     ReInit(dstream, encoding, 1, 1, 4096);
 525  0
   }
 526  
 
 527  
 /** Reinitialise. */
 528  
   public void ReInit(java.io.InputStream dstream)
 529  
   {
 530  0
     ReInit(dstream, 1, 1, 4096);
 531  0
   }
 532  
 
 533  
   /** @return token image as String */
 534  
   public String GetImage()
 535  
   {
 536  3205
     if (bufpos >= tokenBegin)
 537  3205
       return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
 538  
     else
 539  0
       return new String(buffer, tokenBegin, bufsize - tokenBegin) +
 540  
                               new String(buffer, 0, bufpos + 1);
 541  
   }
 542  
 
 543  
   /** @return suffix */
 544  
   public char[] GetSuffix(int len)
 545  
   {
 546  3758
     char[] ret = new char[len];
 547  
 
 548  3758
     if ((bufpos + 1) >= len)
 549  3758
       System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
 550  
     else
 551  
     {
 552  0
       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
 553  
                                                         len - bufpos - 1);
 554  0
       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
 555  
     }
 556  
 
 557  3758
     return ret;
 558  
   }
 559  
 
 560  
   /** Set buffers back to null when finished. */
 561  
   public void Done()
 562  
   {
 563  0
     nextCharBuf = null;
 564  0
     buffer = null;
 565  0
     bufline = null;
 566  0
     bufcolumn = null;
 567  0
   }
 568  
 
 569  
   /**
 570  
    * Method to adjust line and column numbers for the start of a token.
 571  
    */
 572  
   public void adjustBeginLineColumn(int newLine, int newCol)
 573  
   {
 574  0
     int start = tokenBegin;
 575  
     int len;
 576  
 
 577  0
     if (bufpos >= tokenBegin)
 578  
     {
 579  0
       len = bufpos - tokenBegin + inBuf + 1;
 580  
     }
 581  
     else
 582  
     {
 583  0
       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
 584  
     }
 585  
 
 586  0
     int i = 0, j = 0, k = 0;
 587  0
     int nextColDiff = 0, columnDiff = 0;
 588  
 
 589  0
     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
 590  
     {
 591  0
       bufline[j] = newLine;
 592  0
       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
 593  0
       bufcolumn[j] = newCol + columnDiff;
 594  0
       columnDiff = nextColDiff;
 595  0
       i++;
 596  
     }
 597  
 
 598  0
     if (i < len)
 599  
     {
 600  0
       bufline[j] = newLine++;
 601  0
       bufcolumn[j] = newCol + columnDiff;
 602  
 
 603  0
       while (i++ < len)
 604  
       {
 605  0
         if (bufline[j = start % bufsize] != bufline[++start % bufsize])
 606  0
           bufline[j] = newLine++;
 607  
         else
 608  0
           bufline[j] = newLine;
 609  
       }
 610  
     }
 611  
 
 612  0
     line = bufline[j];
 613  0
     column = bufcolumn[j];
 614  0
   }
 615  
 
 616  
 }
 617  
 /* JavaCC - OriginalChecksum=7fed424457d5835b27d8aba99b626820 (do not edit this line) */