1 18 19 package com.Ostermiller.util; 20 21 124 public class StringTokenizer implements java.util.Enumeration , java.util.Iterator { 125 126 132 protected String text; 133 134 141 protected int strLength; 142 143 148 protected String nontokenDelims; 149 150 155 protected String tokenDelims; 156 157 168 protected int position; 169 170 182 protected boolean emptyReturned; 183 184 199 protected char maxDelimChar; 200 201 209 protected boolean returnEmptyTokens; 210 211 220 protected int delimsChangedPosition; 221 222 229 protected int tokenCount; 230 231 246 public StringTokenizer(String text, String nontokenDelims, String tokenDelims){ 247 this(text, nontokenDelims, tokenDelims, false); 248 } 249 250 272 public StringTokenizer(String text, String nontokenDelims, String tokenDelims, boolean returnEmptyTokens){ 273 setDelims(nontokenDelims, tokenDelims); 274 setText(text); 275 setReturnEmptyTokens(returnEmptyTokens); 276 } 277 278 302 public StringTokenizer(String text, String delims, boolean delimsAreTokens){ 303 this(text, (delimsAreTokens ? null : delims), (delimsAreTokens ? delims : null)); 304 } 305 306 319 public StringTokenizer(String text, String nontokenDelims){ 320 this(text, nontokenDelims, null); 321 } 322 323 335 public StringTokenizer(String text){ 336 this(text, " \t\n\r\f", null); 337 } 338 339 352 public void setText(String text){ 353 if (text == null){ 354 throw new NullPointerException (); 355 } 356 this.text = text; 357 strLength = text.length(); 358 emptyReturned = false; 359 position = (strLength > 0 ? 0: -1); 363 delimsChangedPosition = 0; 366 tokenCount = -1; 368 } 369 370 380 private void setDelims(String nontokenDelims, String tokenDelims){ 381 this.nontokenDelims = nontokenDelims; 382 this.tokenDelims = tokenDelims; 383 delimsChangedPosition = (position != -1 ? position : strLength); 388 maxDelimChar = 0; 390 for (int i=0; nontokenDelims != null && i < nontokenDelims.length(); i++){ 391 if (maxDelimChar < nontokenDelims.charAt(i)){ 392 maxDelimChar = nontokenDelims.charAt(i); 393 } 394 } 395 for (int i=0; tokenDelims != null && i < tokenDelims.length(); i++){ 396 if (maxDelimChar < tokenDelims.charAt(i)){ 397 maxDelimChar = tokenDelims.charAt(i); 398 } 399 } 400 tokenCount = -1; 402 } 403 404 405 417 public boolean hasMoreTokens(){ 418 419 if (tokenCount == 0){ 422 return false; 423 } else if (tokenCount > 0){ 424 return true; 425 } 426 427 int savedPosition = position; 432 boolean savedEmptyReturned = emptyReturned; 433 434 int workingPosition = position; 435 boolean workingEmptyReturned = emptyReturned; 436 boolean onToken = advancePosition(); 437 while(position != workingPosition || 438 emptyReturned != workingEmptyReturned){ 439 if (onToken){ 440 position = savedPosition; 442 emptyReturned = savedEmptyReturned; 443 return true; 444 } 445 workingPosition = position; 446 workingEmptyReturned = emptyReturned; 447 onToken = advancePosition(); 448 } 449 450 position = savedPosition; 452 emptyReturned = savedEmptyReturned; 453 return false; 454 } 455 456 466 public String nextToken(){ 467 int workingPosition = position; 468 boolean workingEmptyReturned = emptyReturned; 469 boolean onToken = advancePosition(); 470 while(position != workingPosition || 471 emptyReturned != workingEmptyReturned){ 472 if (onToken){ 473 tokenCount--; 475 return (emptyReturned ? "" : text.substring(workingPosition, (position != -1) ? position : strLength)); 476 } 477 workingPosition = position; 478 workingEmptyReturned = emptyReturned; 479 onToken = advancePosition(); 480 } 481 throw new java.util.NoSuchElementException (); 482 } 483 484 497 public boolean skipDelimiters(){ 498 int workingPosition = position; 499 boolean workingEmptyReturned = emptyReturned; 500 boolean onToken = advancePosition(); 501 502 tokenCount = -1; 504 505 while(position != workingPosition || 506 emptyReturned != workingEmptyReturned){ 507 if (onToken){ 508 position = workingPosition; 511 emptyReturned = workingEmptyReturned; 512 return true; 513 } 514 workingPosition = position; 515 workingEmptyReturned = emptyReturned; 516 onToken = advancePosition(); 517 } 518 519 return false; 522 } 523 524 535 public int countTokens(){ 536 537 if (this.tokenCount >=0){ 540 return this.tokenCount; 541 } 542 543 int tokenCount = 0; 544 545 int savedPosition = position; 550 boolean savedEmptyReturned = emptyReturned; 551 552 int workingPosition = position; 553 boolean workingEmptyReturned = emptyReturned; 554 boolean onToken = advancePosition(); 555 while(position != workingPosition || 556 emptyReturned != workingEmptyReturned){ 557 if (onToken){ 558 tokenCount++; 559 } 560 workingPosition = position; 561 workingEmptyReturned = emptyReturned; 562 onToken = advancePosition(); 563 } 564 565 position = savedPosition; 567 emptyReturned = savedEmptyReturned; 568 569 this.tokenCount = tokenCount; 572 573 return tokenCount; 574 } 575 576 583 public void setDelimiters(String delims){ 584 setDelims(delims, null); 585 } 586 587 598 public void setDelimiters(String delims, boolean delimsAreTokens){ 599 setDelims((delimsAreTokens ? null : delims), (delimsAreTokens ? delims : null)); 600 } 601 602 610 public void setDelimiters(String nontokenDelims, String tokenDelims){ 611 setDelims(nontokenDelims, tokenDelims); 612 } 613 614 623 public void setDelimiters(String nontokenDelims, String tokenDelims, boolean returnEmptyTokens){ 624 setDelims(nontokenDelims, tokenDelims); 625 setReturnEmptyTokens(returnEmptyTokens); 626 } 627 628 642 public int countTokens(String delims){ 643 setDelims(delims, null); 644 return countTokens(); 645 } 646 647 665 public int countTokens(String delims, boolean delimsAreTokens){ 666 setDelims((delimsAreTokens ? null : delims), (delimsAreTokens ? delims : null)); 667 return countTokens(); 668 } 669 670 685 public int countTokens(String nontokenDelims, String tokenDelims){ 686 setDelims(nontokenDelims, tokenDelims); 687 return countTokens(); 688 } 689 690 706 public int countTokens(String nontokenDelims, String tokenDelims, boolean returnEmptyTokens){ 707 setDelims(nontokenDelims, tokenDelims); 708 setReturnEmptyTokens(returnEmptyTokens); 709 return countTokens(); 710 } 711 712 722 private boolean advancePosition(){ 723 if (returnEmptyTokens && !emptyReturned && 728 (delimsChangedPosition == position || 729 (position == -1 && strLength == delimsChangedPosition))){ 730 if (strLength == delimsChangedPosition){ 731 emptyReturned = true; 735 736 return true; 737 } else { 738 char c = text.charAt(position); 739 if (c <= maxDelimChar && 740 (nontokenDelims != null && nontokenDelims.indexOf(c) != -1) || 741 (tokenDelims != null && tokenDelims.indexOf(c) != -1)){ 742 emptyReturned = true; 745 746 return true; 747 } 748 } 749 } 750 while (position != -1){ 753 char c = text.charAt(position); 754 if (returnEmptyTokens && !emptyReturned && position > delimsChangedPosition){ 755 char c1 = text.charAt(position - 1); 756 if (c <= maxDelimChar && c1 <= maxDelimChar && 761 ((nontokenDelims != null && nontokenDelims.indexOf(c) != -1) || 762 (tokenDelims != null && tokenDelims.indexOf(c) != -1)) && 763 ((nontokenDelims != null && nontokenDelims.indexOf(c1) != -1) || 764 (tokenDelims != null && tokenDelims.indexOf(c1) != -1))){ 765 emptyReturned = true; 766 767 return true; 768 } 769 } 770 771 int nextDelimiter = (position < strLength - 1 ? indexOfNextDelimiter(position + 1) : -1); 772 if (c > maxDelimChar || 773 ((nontokenDelims == null || nontokenDelims.indexOf(c) == -1) && 774 (tokenDelims == null || tokenDelims.indexOf(c) == -1))){ 775 779 position = nextDelimiter; 780 emptyReturned = false; 781 return true; 782 } else if (tokenDelims != null && tokenDelims.indexOf(c) != -1) { 783 emptyReturned = false; 785 786 position = (position < strLength -1 ? position +1 : -1); 787 return true; 788 } else { 789 emptyReturned = false; 791 position = (position < strLength -1 ? position +1 : -1); 792 return false; 793 } 794 } 795 if (returnEmptyTokens && !emptyReturned && strLength > 0){ 798 char c = text.charAt(strLength - 1); 799 if (c <= maxDelimChar && 800 (nontokenDelims != null && nontokenDelims.indexOf(c) != -1) || 801 (tokenDelims != null && tokenDelims.indexOf(c) != -1)){ 802 emptyReturned = true; 804 805 return true; 806 } 807 } 808 return false; 809 } 810 811 831 public String nextToken(String nontokenDelims, String tokenDelims){ 832 setDelims(nontokenDelims, tokenDelims); 833 return nextToken(); 834 } 835 836 859 public String nextToken(String nontokenDelims, String tokenDelims, boolean returnEmptyTokens){ 860 setDelims(nontokenDelims, tokenDelims); 861 setReturnEmptyTokens(returnEmptyTokens); 862 return nextToken(); 863 } 864 865 889 public String nextToken(String delims, boolean delimsAreTokens){ 890 return (delimsAreTokens ? nextToken(null, delims) : nextToken(delims, null)); 891 } 892 893 907 public String nextToken(String nontokenDelims){ 908 return nextToken(nontokenDelims, null); 909 } 910 911 921 private int indexOfNextDelimiter(int start){ 922 char c; 923 int next; 924 for (next = start; (c = text.charAt(next)) > maxDelimChar || 925 ((nontokenDelims == null || nontokenDelims.indexOf(c) == -1) && 926 (tokenDelims == null || tokenDelims.indexOf(c) == -1)); next++){ 927 if (next == strLength - 1){ 928 return (-1); 931 } 932 } 933 return next; 934 } 935 936 947 public boolean hasMoreElements(){ 948 return hasMoreTokens(); 949 } 950 951 964 public Object nextElement(){ 965 return nextToken(); 966 } 967 968 979 public boolean hasNext(){ 980 return hasMoreTokens(); 981 } 982 983 996 public Object next(){ 997 return nextToken(); 998 } 999 1000 1009 public void remove(){ 1010 throw new UnsupportedOperationException (); 1011 } 1012 1013 1034 public void setReturnEmptyTokens(boolean returnEmptyTokens){ 1035 tokenCount = -1; 1037 this.returnEmptyTokens = returnEmptyTokens; 1038 } 1039 1040 1049 public int getCurrentPosition(){ 1050 return this.position; 1051 } 1052 1053 1063 public String [] toArray(){ 1064 String [] tokenArray = new String [countTokens()]; 1065 for(int i=0; hasMoreTokens(); i++) { 1066 tokenArray[i] = nextToken(); 1067 } 1068 return tokenArray; 1069 } 1070 1071 1079 public String restOfText(){ 1080 return nextToken(null, null); 1081 } 1082 1083 1094 public String peek(){ 1095 int savedPosition = position; 1100 boolean savedEmptyReturned = emptyReturned; 1101 int savedtokenCount = tokenCount; 1102 1103 String retval = nextToken(); 1105 1106 position = savedPosition; 1108 emptyReturned = savedEmptyReturned; 1109 tokenCount = savedtokenCount; 1110 1111 return(retval); 1113 } 1114} 1115 | Popular Tags |