1 7 8 package com.ibm.icu.text; 9 10 import java.text.CharacterIterator ; 11 12 126 public abstract class SearchIterator 127 { 128 129 131 138 public static final int DONE = -1; 139 140 142 144 155 public void setIndex(int position) { 156 if (position < targetText.getBeginIndex() 157 || position > targetText.getEndIndex()) { 158 throw new IndexOutOfBoundsException ( 159 "setIndex(int) expected position to be between " + 160 targetText.getBeginIndex() + " and " + targetText.getEndIndex()); 161 } 162 m_setOffset_ = position; 163 m_reset_ = false; 164 matchLength = 0; 165 } 166 167 179 public void setOverlapping(boolean allowOverlap) 180 { 181 m_isOverlap_ = allowOverlap; 182 } 183 184 196 public void setBreakIterator(BreakIterator breakiter) 197 { 198 breakIterator = breakiter; 199 if (breakIterator != null) { 200 breakIterator.setText(targetText); 201 } 202 } 203 204 214 public void setTarget(CharacterIterator text) 215 { 216 if (text == null || text.getEndIndex() == text.getIndex()) { 217 throw new IllegalArgumentException ("Illegal null or empty text"); 218 } 219 220 targetText = text; 221 targetText.setIndex(targetText.getBeginIndex()); 222 matchLength = 0; 223 m_reset_ = true; 224 m_isForwardSearching_ = true; 225 if (breakIterator != null) { 226 breakIterator.setText(targetText); 227 } 228 } 229 230 232 256 public int getMatchStart() 257 { 258 return m_lastMatchStart_; 259 } 260 261 275 public abstract int getIndex(); 276 277 298 public int getMatchLength() 299 { 300 return matchLength; 301 } 302 303 314 public BreakIterator getBreakIterator() 315 { 316 return breakIterator; 317 } 318 319 325 public CharacterIterator getTarget() 326 { 327 return targetText; 328 } 329 330 346 public String getMatchedText() 347 { 348 if (matchLength > 0) { 349 int limit = m_lastMatchStart_ + matchLength; 350 StringBuffer result = new StringBuffer (matchLength); 351 result.append(targetText.current()); 352 targetText.next(); 353 while (targetText.getIndex() < limit) { 354 result.append(targetText.current()); 355 targetText.next(); 356 } 357 targetText.setIndex(m_lastMatchStart_); 358 return result.toString(); 359 } 360 return null; 361 } 362 363 365 387 public int next() 388 { 389 int start = targetText.getIndex(); 390 if (m_setOffset_ != DONE) { 391 start = m_setOffset_; 392 m_setOffset_ = DONE; 393 } 394 if (m_isForwardSearching_) { 395 if (!m_reset_ && 396 start + matchLength >= targetText.getEndIndex()) { 397 matchLength = 0; 399 targetText.setIndex(targetText.getEndIndex()); 400 m_lastMatchStart_ = DONE; 401 return DONE; 402 } 403 m_reset_ = false; 404 } 405 else { 406 m_isForwardSearching_ = true; 412 if (start != DONE) { 413 return start; 416 } 417 } 418 419 if (start == DONE) { 420 start = targetText.getBeginIndex(); 421 } 422 if (matchLength > 0) { 423 if (m_isOverlap_) { 425 start ++; 426 } 427 else { 428 start += matchLength; 429 } 430 } 431 m_lastMatchStart_ = handleNext(start); 432 return m_lastMatchStart_; 433 } 434 435 457 public int previous() 458 { 459 int start = targetText.getIndex(); 460 if (m_setOffset_ != DONE) { 461 start = m_setOffset_; 462 m_setOffset_ = DONE; 463 } 464 if (m_reset_) { 465 m_isForwardSearching_ = false; 466 m_reset_ = false; 467 start = targetText.getEndIndex(); 468 } 469 470 if (m_isForwardSearching_ == true) { 471 m_isForwardSearching_ = false; 477 if (start != targetText.getEndIndex()) { 478 return start; 479 } 480 } 481 else { 482 if (start == targetText.getBeginIndex()) { 483 matchLength = 0; 485 targetText.setIndex(targetText.getBeginIndex()); 486 m_lastMatchStart_ = DONE; 487 return DONE; 488 } 489 } 490 491 m_lastMatchStart_ = handlePrevious(start); 492 return m_lastMatchStart_; 493 } 494 495 502 public boolean isOverlapping() 503 { 504 return m_isOverlap_; 505 } 506 507 519 public void reset() 520 { 521 matchLength = 0; 523 setIndex(targetText.getBeginIndex()); 524 m_isOverlap_ = false; 525 m_isForwardSearching_ = true; 526 m_reset_ = true; 527 m_setOffset_ = DONE; 528 } 529 530 547 public final int first() 548 { 549 m_isForwardSearching_ = true; 550 setIndex(targetText.getBeginIndex()); 551 return next(); 552 } 553 554 572 public final int following(int position) 573 { 574 m_isForwardSearching_ = true; 575 setIndex(position); 577 return next(); 578 } 579 580 597 public final int last() 598 { 599 m_isForwardSearching_ = false; 600 setIndex(targetText.getEndIndex()); 601 return previous(); 602 } 603 604 623 public final int preceding(int position) 624 { 625 m_isForwardSearching_ = false; 626 setIndex(position); 628 return previous(); 629 } 630 631 633 642 protected BreakIterator breakIterator; 643 644 650 protected CharacterIterator targetText; 651 658 protected int matchLength; 659 660 662 676 protected SearchIterator(CharacterIterator target, BreakIterator breaker) 677 { 678 if (target == null 679 || (target.getEndIndex() - target.getBeginIndex()) == 0) { 680 throw new IllegalArgumentException ( 681 "Illegal argument target. " + 682 " Argument can not be null or of length 0"); 683 } 684 targetText = target; 685 breakIterator = breaker; 686 if (breakIterator != null) { 687 breakIterator.setText(target); 688 } 689 matchLength = 0; 690 m_lastMatchStart_ = DONE; 691 m_isOverlap_ = false; 692 m_isForwardSearching_ = true; 693 m_reset_ = true; 694 m_setOffset_ = DONE; 695 } 696 697 699 700 709 protected void setMatchLength(int length) 710 { 711 matchLength = length; 712 } 713 714 736 protected abstract int handleNext(int start); 737 738 761 protected abstract int handlePrevious(int startAt); 762 763 765 768 private boolean m_isForwardSearching_; 769 773 private boolean m_isOverlap_; 774 778 private boolean m_reset_; 779 783 private int m_setOffset_; 784 787 private int m_lastMatchStart_; 788 } 789 | Popular Tags |