1 2 12 13 package com.versant.core.ejb.query; 14 15 19 20 public class JavaCharStream 21 { 22 public static final boolean staticFlag = false; 23 static final int hexval(char c) throws java.io.IOException { 24 switch(c) 25 { 26 case '0' : 27 return 0; 28 case '1' : 29 return 1; 30 case '2' : 31 return 2; 32 case '3' : 33 return 3; 34 case '4' : 35 return 4; 36 case '5' : 37 return 5; 38 case '6' : 39 return 6; 40 case '7' : 41 return 7; 42 case '8' : 43 return 8; 44 case '9' : 45 return 9; 46 47 case 'a' : 48 case 'A' : 49 return 10; 50 case 'b' : 51 case 'B' : 52 return 11; 53 case 'c' : 54 case 'C' : 55 return 12; 56 case 'd' : 57 case 'D' : 58 return 13; 59 case 'e' : 60 case 'E' : 61 return 14; 62 case 'f' : 63 case 'F' : 64 return 15; 65 } 66 67 throw new java.io.IOException (); } 69 70 public int bufpos = -1; 71 int bufsize; 72 int available; 73 int tokenBegin; 74 protected int bufline[]; 75 protected int bufcolumn[]; 76 77 protected int column = 0; 78 protected int line = 1; 79 80 protected boolean prevCharIsCR = false; 81 protected boolean prevCharIsLF = false; 82 83 protected java.io.Reader inputStream; 84 85 protected char[] nextCharBuf; 86 protected char[] buffer; 87 protected int maxNextCharInd = 0; 88 protected int nextCharInd = -1; 89 protected int inBuf = 0; 90 91 protected void ExpandBuff(boolean wrapAround) 92 { 93 char[] newbuffer = new char[bufsize + 2048]; 94 int newbufline[] = new int[bufsize + 2048]; 95 int newbufcolumn[] = new int[bufsize + 2048]; 96 97 try 98 { 99 if (wrapAround) 100 { 101 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 102 System.arraycopy(buffer, 0, newbuffer, 103 bufsize - tokenBegin, bufpos); 104 buffer = newbuffer; 105 106 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 107 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); 108 bufline = newbufline; 109 110 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 111 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); 112 bufcolumn = newbufcolumn; 113 114 bufpos += (bufsize - tokenBegin); 115 } 116 else 117 { 118 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); 119 buffer = newbuffer; 120 121 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); 122 bufline = newbufline; 123 124 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); 125 bufcolumn = newbufcolumn; 126 127 bufpos -= tokenBegin; 128 } 129 } 130 catch (Throwable t) 131 { 132 throw new Error (t.getMessage()); 133 } 134 135 available = (bufsize += 2048); 136 tokenBegin = 0; 137 } 138 139 protected void FillBuff() throws java.io.IOException 140 { 141 int i; 142 if (maxNextCharInd == 4096) 143 maxNextCharInd = nextCharInd = 0; 144 145 try { 146 if ((i = inputStream.read(nextCharBuf, maxNextCharInd, 147 4096 - maxNextCharInd)) == -1) 148 { 149 inputStream.close(); 150 throw new java.io.IOException (); 151 } 152 else 153 maxNextCharInd += i; 154 return; 155 } 156 catch(java.io.IOException e) { 157 if (bufpos != 0) 158 { 159 --bufpos; 160 backup(0); 161 } 162 else 163 { 164 bufline[bufpos] = line; 165 bufcolumn[bufpos] = column; 166 } 167 throw e; 168 } 169 } 170 171 protected char ReadByte() throws java.io.IOException 172 { 173 if (++nextCharInd >= maxNextCharInd) 174 FillBuff(); 175 176 return nextCharBuf[nextCharInd]; 177 } 178 179 public char BeginToken() throws java.io.IOException 180 { 181 if (inBuf > 0) 182 { 183 --inBuf; 184 185 if (++bufpos == bufsize) 186 bufpos = 0; 187 188 tokenBegin = bufpos; 189 return buffer[bufpos]; 190 } 191 192 tokenBegin = 0; 193 bufpos = -1; 194 195 return readChar(); 196 } 197 198 protected void AdjustBuffSize() 199 { 200 if (available == bufsize) 201 { 202 if (tokenBegin > 2048) 203 { 204 bufpos = 0; 205 available = tokenBegin; 206 } 207 else 208 ExpandBuff(false); 209 } 210 else if (available > tokenBegin) 211 available = bufsize; 212 else if ((tokenBegin - available) < 2048) 213 ExpandBuff(true); 214 else 215 available = tokenBegin; 216 } 217 218 protected void UpdateLineColumn(char c) 219 { 220 column++; 221 222 if (prevCharIsLF) 223 { 224 prevCharIsLF = false; 225 line += (column = 1); 226 } 227 else if (prevCharIsCR) 228 { 229 prevCharIsCR = false; 230 if (c == '\n') 231 { 232 prevCharIsLF = true; 233 } 234 else 235 line += (column = 1); 236 } 237 238 switch (c) 239 { 240 case '\r' : 241 prevCharIsCR = true; 242 break; 243 case '\n' : 244 prevCharIsLF = true; 245 break; 246 case '\t' : 247 column--; 248 column += (8 - (column & 07)); 249 break; 250 default : 251 break; 252 } 253 254 bufline[bufpos] = line; 255 bufcolumn[bufpos] = column; 256 } 257 258 public char readChar() throws java.io.IOException 259 { 260 if (inBuf > 0) 261 { 262 --inBuf; 263 264 if (++bufpos == bufsize) 265 bufpos = 0; 266 267 return buffer[bufpos]; 268 } 269 270 char c; 271 272 if (++bufpos == available) 273 AdjustBuffSize(); 274 275 if ((buffer[bufpos] = c = ReadByte()) == '\\') 276 { 277 UpdateLineColumn(c); 278 279 int backSlashCnt = 1; 280 281 for (;;) { 283 if (++bufpos == available) 284 AdjustBuffSize(); 285 286 try 287 { 288 if ((buffer[bufpos] = c = ReadByte()) != '\\') 289 { 290 UpdateLineColumn(c); 291 if ((c == 'u') && ((backSlashCnt & 1) == 1)) 293 { 294 if (--bufpos < 0) 295 bufpos = bufsize - 1; 296 297 break; 298 } 299 300 backup(backSlashCnt); 301 return '\\'; 302 } 303 } 304 catch(java.io.IOException e) 305 { 306 if (backSlashCnt > 1) 307 backup(backSlashCnt); 308 309 return '\\'; 310 } 311 312 UpdateLineColumn(c); 313 backSlashCnt++; 314 } 315 316 try 318 { 319 while ((c = ReadByte()) == 'u') 320 ++column; 321 322 buffer[bufpos] = c = (char)(hexval(c) << 12 | 323 hexval(ReadByte()) << 8 | 324 hexval(ReadByte()) << 4 | 325 hexval(ReadByte())); 326 327 column += 4; 328 } 329 catch(java.io.IOException e) 330 { 331 throw new Error ("Invalid escape character at line " + line + 332 " column " + column + "."); 333 } 334 335 if (backSlashCnt == 1) 336 return c; 337 else 338 { 339 backup(backSlashCnt - 1); 340 return '\\'; 341 } 342 } 343 else 344 { 345 UpdateLineColumn(c); 346 return (c); 347 } 348 } 349 350 354 355 public int getColumn() { 356 return bufcolumn[bufpos]; 357 } 358 359 363 364 public int getLine() { 365 return bufline[bufpos]; 366 } 367 368 public int getEndColumn() { 369 return bufcolumn[bufpos]; 370 } 371 372 public int getEndLine() { 373 return bufline[bufpos]; 374 } 375 376 public int getBeginColumn() { 377 return bufcolumn[tokenBegin]; 378 } 379 380 public int getBeginLine() { 381 return bufline[tokenBegin]; 382 } 383 384 public void backup(int amount) { 385 386 inBuf += amount; 387 if ((bufpos -= amount) < 0) 388 bufpos += bufsize; 389 } 390 391 public JavaCharStream(java.io.Reader dstream, 392 int startline, int startcolumn, int buffersize) 393 { 394 inputStream = dstream; 395 line = startline; 396 column = startcolumn - 1; 397 398 available = bufsize = buffersize; 399 buffer = new char[buffersize]; 400 bufline = new int[buffersize]; 401 bufcolumn = new int[buffersize]; 402 nextCharBuf = new char[4096]; 403 } 404 405 public JavaCharStream(java.io.Reader dstream, 406 int startline, int startcolumn) 407 { 408 this(dstream, startline, startcolumn, 4096); 409 } 410 411 public JavaCharStream(java.io.Reader dstream) 412 { 413 this(dstream, 1, 1, 4096); 414 } 415 public void ReInit(java.io.Reader dstream, 416 int startline, int startcolumn, int buffersize) 417 { 418 inputStream = dstream; 419 line = startline; 420 column = startcolumn - 1; 421 422 if (buffer == null || buffersize != buffer.length) 423 { 424 available = bufsize = buffersize; 425 buffer = new char[buffersize]; 426 bufline = new int[buffersize]; 427 bufcolumn = new int[buffersize]; 428 nextCharBuf = new char[4096]; 429 } 430 prevCharIsLF = prevCharIsCR = false; 431 tokenBegin = inBuf = maxNextCharInd = 0; 432 nextCharInd = bufpos = -1; 433 } 434 435 public void ReInit(java.io.Reader dstream, 436 int startline, int startcolumn) 437 { 438 ReInit(dstream, startline, startcolumn, 4096); 439 } 440 441 public void ReInit(java.io.Reader dstream) 442 { 443 ReInit(dstream, 1, 1, 4096); 444 } 445 public JavaCharStream(java.io.InputStream dstream, int startline, 446 int startcolumn, int buffersize) 447 { 448 this(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 449 } 450 451 public JavaCharStream(java.io.InputStream dstream, int startline, 452 int startcolumn) 453 { 454 this(dstream, startline, startcolumn, 4096); 455 } 456 457 public JavaCharStream(java.io.InputStream dstream) 458 { 459 this(dstream, 1, 1, 4096); 460 } 461 462 public void ReInit(java.io.InputStream dstream, int startline, 463 int startcolumn, int buffersize) 464 { 465 ReInit(new java.io.InputStreamReader (dstream), startline, startcolumn, 4096); 466 } 467 public void ReInit(java.io.InputStream dstream, int startline, 468 int startcolumn) 469 { 470 ReInit(dstream, startline, startcolumn, 4096); 471 } 472 public void ReInit(java.io.InputStream dstream) 473 { 474 ReInit(dstream, 1, 1, 4096); 475 } 476 477 public String GetImage() 478 { 479 if (bufpos >= tokenBegin) 480 return new String (buffer, tokenBegin, bufpos - tokenBegin + 1); 481 else 482 return new String (buffer, tokenBegin, bufsize - tokenBegin) + 483 new String (buffer, 0, bufpos + 1); 484 } 485 486 public char[] GetSuffix(int len) 487 { 488 char[] ret = new char[len]; 489 490 if ((bufpos + 1) >= len) 491 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); 492 else 493 { 494 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, 495 len - bufpos - 1); 496 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); 497 } 498 499 return ret; 500 } 501 502 public void Done() 503 { 504 nextCharBuf = null; 505 buffer = null; 506 bufline = null; 507 bufcolumn = null; 508 } 509 510 513 public void adjustBeginLineColumn(int newLine, int newCol) 514 { 515 int start = tokenBegin; 516 int len; 517 518 if (bufpos >= tokenBegin) 519 { 520 len = bufpos - tokenBegin + inBuf + 1; 521 } 522 else 523 { 524 len = bufsize - tokenBegin + bufpos + 1 + inBuf; 525 } 526 527 int i = 0, j = 0, k = 0; 528 int nextColDiff = 0, columnDiff = 0; 529 530 while (i < len && 531 bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) 532 { 533 bufline[j] = newLine; 534 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; 535 bufcolumn[j] = newCol + columnDiff; 536 columnDiff = nextColDiff; 537 i++; 538 } 539 540 if (i < len) 541 { 542 bufline[j] = newLine++; 543 bufcolumn[j] = newCol + columnDiff; 544 545 while (i++ < len) 546 { 547 if (bufline[j = start % bufsize] != bufline[++start % bufsize]) 548 bufline[j] = newLine++; 549 else 550 bufline[j] = newLine; 551 } 552 } 553 554 line = bufline[j]; 555 column = bufcolumn[j]; 556 } 557 558 } 559 | Popular Tags |