1 package org.apache.oro.text.awk; 2 3 59 60 import java.io.*; 61 62 import org.apache.oro.text.regex.*; 63 64 83 public final class AwkMatcher implements PatternMatcher { 84 private int __lastMatchedBufferOffset; 85 private AwkMatchResult __lastMatchResult = null; 86 private AwkStreamInput __scratchBuffer, __streamSearchBuffer; 87 private AwkPattern __awkPattern; 88 private int __offsets[] = new int[2]; 89 90 public AwkMatcher() { 91 __scratchBuffer = new AwkStreamInput(); 92 __scratchBuffer._endOfStreamReached = true; 93 } 94 95 110 public boolean matchesPrefix(char[] input, Pattern pattern, int offset){ 114 int result = -1; 115 116 __awkPattern = (AwkPattern)pattern; 117 118 __scratchBuffer._buffer = input; 119 __scratchBuffer._bufferSize = input.length; 120 __scratchBuffer._bufferOffset = 0; 121 __scratchBuffer._endOfStreamReached = true; 122 __streamSearchBuffer = __scratchBuffer; 123 __offsets[0] = offset; 124 try { 125 result = __streamMatchPrefix(); 126 } catch(IOException e){ 127 result = -1; 129 } 130 131 if(result < 0) { 132 __lastMatchResult = null; 133 return false; 134 } 135 136 __lastMatchResult = 137 new AwkMatchResult(new String (input, 0, result), offset); 138 139 return true; 140 } 141 142 143 157 public boolean matchesPrefix(char[] input, Pattern pattern){ 158 return matchesPrefix(input, pattern, 0); 159 } 160 161 162 175 public boolean matchesPrefix(String input, Pattern pattern) { 176 return matchesPrefix(input.toCharArray(), pattern, 0); 177 } 178 179 180 198 public boolean matchesPrefix(PatternMatcherInput input, Pattern pattern){ 199 int result = -1; 200 201 __awkPattern = (AwkPattern)pattern; 202 __scratchBuffer._buffer = input.getBuffer(); 203 __scratchBuffer._bufferOffset = 0; 204 __offsets[0] = input.getCurrentOffset(); 205 206 __scratchBuffer._bufferSize = input.length(); 207 __scratchBuffer._endOfStreamReached = true; 208 __streamSearchBuffer = __scratchBuffer; 209 try { 210 result = __streamMatchPrefix(); 211 } catch(IOException e) { 212 result = -1; 214 } 215 216 if(result < 0) { 217 __lastMatchResult = null; 218 return false; 219 } 220 221 __lastMatchResult = 222 new AwkMatchResult(new String (__scratchBuffer._buffer, __offsets[0], 223 result), __offsets[0]); 224 225 return true; 226 } 227 228 229 230 248 public boolean matches(char[] input, Pattern pattern) { 249 int result = -1; 250 251 __awkPattern = (AwkPattern)pattern; 252 __scratchBuffer._buffer = input; 253 __scratchBuffer._bufferSize = input.length; 254 __scratchBuffer._bufferOffset = 0; 255 __scratchBuffer._endOfStreamReached = true; 256 __streamSearchBuffer = __scratchBuffer; 257 __offsets[0] = 0; 258 try { 259 result = __streamMatchPrefix(); 260 } catch(IOException e){ 261 result = -1; 263 } 264 265 if(result != input.length) { 266 __lastMatchResult = null; 267 return false; 268 } 269 270 __lastMatchResult = 271 new AwkMatchResult(new String (input, 0, result), 0); 272 273 return true; 274 } 275 276 277 278 279 296 public boolean matches(String input, Pattern pattern){ 297 return matches(input.toCharArray(), pattern); 298 } 299 300 301 325 public boolean matches(PatternMatcherInput input, Pattern pattern){ 326 int result = -1; 327 328 __awkPattern = (AwkPattern)pattern; 329 __scratchBuffer._buffer = input.getBuffer(); 330 __scratchBuffer._bufferSize = input.length(); 331 __scratchBuffer._bufferOffset = 0; 332 __offsets[0] = input.getBeginOffset(); 333 __scratchBuffer._endOfStreamReached = true; 334 __streamSearchBuffer = __scratchBuffer; 335 try { 336 result = __streamMatchPrefix(); 337 } catch(IOException e){ 338 result = -1; 340 } 341 342 if(result != __scratchBuffer._bufferSize) { 343 __lastMatchResult = null; 344 return false; 345 } 346 347 __lastMatchResult = 348 new AwkMatchResult(new String (__scratchBuffer._buffer, __offsets[0], 349 __scratchBuffer._bufferSize), __offsets[0]); 350 351 return true; 352 } 353 354 355 356 380 public boolean contains(char[] input, Pattern pattern) { 381 __awkPattern = (AwkPattern)pattern; 382 383 if(__awkPattern._hasBeginAnchor && !__awkPattern._fastMap[input[0]]){ 385 __lastMatchResult = null; 386 return false; 387 } 388 389 __scratchBuffer._buffer = input; 390 __scratchBuffer._bufferSize = input.length; 391 __scratchBuffer._bufferOffset = 0; 392 __scratchBuffer._endOfStreamReached = true; 393 __streamSearchBuffer = __scratchBuffer; 394 __lastMatchedBufferOffset = 0; 395 try { 396 _search(); 397 } catch(IOException e) { 398 } 400 return (__lastMatchResult != null); 401 } 402 403 404 427 public boolean contains(String input, Pattern pattern){ 428 return contains(input.toCharArray(), pattern); 429 } 430 431 432 433 496 public boolean contains(PatternMatcherInput input, Pattern pattern) { 497 __awkPattern = (AwkPattern)pattern; 498 __scratchBuffer._buffer = input.getBuffer(); 499 __scratchBuffer._bufferOffset = 0; 500 __lastMatchedBufferOffset = input.getCurrentOffset(); 501 502 if(__awkPattern._hasBeginAnchor) { 505 int begin; 506 507 begin = input.getBeginOffset(); 508 if(begin != __lastMatchedBufferOffset || 509 !__awkPattern._fastMap[__scratchBuffer._buffer[begin]]) { 510 __lastMatchResult = null; 511 return false; 512 } 513 } 514 515 __scratchBuffer._bufferSize = input.length(); 516 __scratchBuffer._endOfStreamReached = true; 517 __streamSearchBuffer = __scratchBuffer; 518 try { 519 _search(); 520 } catch(IOException e) { 521 } 523 input.setCurrentOffset(__lastMatchedBufferOffset); 524 525 if(__lastMatchResult == null) 526 return false; 527 528 input.setMatchOffsets(__lastMatchResult.beginOffset(0), 529 __lastMatchResult.endOffset(0)); 530 531 return true; 532 } 533 534 535 591 public boolean contains(AwkStreamInput input, Pattern pattern) 592 throws IOException 593 { 594 __awkPattern = (AwkPattern)pattern; 595 596 if(__awkPattern._hasBeginAnchor) { 598 if(input._bufferOffset == 0) { 600 if(input.read() && !__awkPattern._fastMap[input._buffer[0]]) { 601 __lastMatchResult = null; 602 return false; 603 } 604 } else { 605 __lastMatchResult = null; 606 return false; 607 } 608 } 609 610 __lastMatchedBufferOffset = input._currentOffset; 611 __streamSearchBuffer = input; 612 _search(); 613 input._currentOffset = __lastMatchedBufferOffset; 614 return (__lastMatchResult != null); 615 } 616 617 618 private int __streamMatchPrefix() throws IOException { 619 int token, current = AwkPattern._START_STATE, lastState, transition; 620 int offset, initialOffset; 621 int lastMatchedOffset = -1; 622 int[] tstateArray; 623 624 offset = initialOffset = __offsets[0]; 625 test: 626 while(offset < __streamSearchBuffer._bufferSize) { 627 token = __streamSearchBuffer._buffer[offset++]; 628 629 if(current < __awkPattern._numStates) { 630 lastState = current; 631 tstateArray = __awkPattern._getStateArray(current); 632 current = tstateArray[token]; 633 634 if(current == 0){ 635 __awkPattern._createNewState(lastState, token, tstateArray); 636 current = tstateArray[token]; 637 } 638 if(current == AwkPattern._INVALID_STATE){ 639 break test; 640 } 641 else if(__awkPattern._endStates.get(current)){ 642 lastMatchedOffset = offset; 643 } 644 if(offset == __streamSearchBuffer._bufferSize){ 645 offset = __streamSearchBuffer._reallocate(initialOffset); 646 647 if(offset != __streamSearchBuffer._bufferSize){ 649 if(lastMatchedOffset != -1) 650 lastMatchedOffset-=initialOffset; 651 initialOffset = 0; 652 } 653 654 } 655 } 656 else 657 break; 658 } 659 660 __offsets[0] = initialOffset; 661 __offsets[1] = lastMatchedOffset - 1; 662 663 if(lastMatchedOffset == -1 && __awkPattern._matchesNullString) 664 return 0; 665 666 if(__awkPattern._hasEndAnchor && 668 (!__streamSearchBuffer._endOfStreamReached || 669 lastMatchedOffset < __streamSearchBuffer._bufferSize)) 670 return -1; 671 672 return (lastMatchedOffset - initialOffset); 673 } 674 675 676 677 void _search() throws IOException { 678 char[] currentLine; 679 int position, tokensMatched; 680 String currentString; 681 682 __lastMatchResult = null; 683 684 while(true){ 685 if(__lastMatchedBufferOffset >= __streamSearchBuffer._bufferSize){ 686 if(__streamSearchBuffer._endOfStreamReached){ 687 __streamSearchBuffer = null; 689 return; 690 } else { 691 if(!__streamSearchBuffer.read()) 692 return; 693 __lastMatchedBufferOffset = 0; 694 } 695 } 696 697 for(position=__lastMatchedBufferOffset; 698 position < __streamSearchBuffer._bufferSize; 699 position = __offsets[0] + 1) { 700 701 __offsets[0] = position; 702 if(__awkPattern._fastMap[__streamSearchBuffer._buffer[position]] && 703 (tokensMatched = __streamMatchPrefix()) > -1) { 704 705 __lastMatchResult = new AwkMatchResult( 706 new String (__streamSearchBuffer._buffer, __offsets[0], 707 tokensMatched), 708 __offsets[0] + __streamSearchBuffer._bufferOffset); 709 710 __lastMatchedBufferOffset = 711 (tokensMatched > 0 ? __offsets[1] + 1 : __offsets[0] + 1); 712 713 return; 714 } else if(__awkPattern._matchesNullString) { 715 __lastMatchResult = new AwkMatchResult(new String (), 716 position + __streamSearchBuffer._bufferOffset); 717 718 __lastMatchedBufferOffset = position + 1; 719 720 return; 721 } 722 } 723 724 __lastMatchedBufferOffset = position; 725 } 726 } 727 728 729 738 public MatchResult getMatch() { return __lastMatchResult; } 739 740 } 741 | Popular Tags |