1 21 22 package org.opensubsystems.core.util; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.StringTokenizer ; 29 30 import org.opensubsystems.core.error.OSSInvalidDataException; 31 import org.opensubsystems.core.error.OSSException; 32 33 41 public final class StringUtils 42 { 43 45 48 public static final String EMPTY_STRING = ""; 49 50 53 public static final String COMMA_STRING = ","; 54 55 58 public static final int CASE_ORIGINAL = 0; 59 60 63 public static final int CASE_TOUPPER = 1; 64 65 68 public static final int CASE_TOLOWER = 2; 69 70 72 75 private StringUtils( 76 ) 77 { 78 } 80 81 83 90 public static int count( 91 String text, 92 char lookup 93 ) 94 { 95 int iCount = 0; 96 97 if (text != null) 98 { 99 int iIndex = text.indexOf(lookup); 100 101 while (iIndex != -1) 102 { 103 iCount++; 104 iIndex = text.indexOf(lookup, iIndex + 1); 105 } 106 } 107 108 return iCount; 109 } 110 111 121 public static double parseFraction( 122 String textToParse, 123 double defaultValue, 124 boolean bIgnoreRest 125 ) 126 { 127 double parsed = defaultValue; 128 int iLength; 129 int iIndex; 130 int iIndexStart; 131 int iIndexEnd; 132 int iNumber; 133 134 137 iIndexStart = 0; 138 iLength = textToParse.length(); 139 if (bIgnoreRest) 140 { 141 while ((iIndexStart < iLength) 143 && (!Character.isDigit(textToParse.charAt(iIndexStart)))) 144 { 145 iIndexStart++; 146 } 147 } 149 150 if (iIndexStart < iLength) 152 { 153 iIndex = iIndexStart; 155 while ((iIndex < iLength) 156 && (Character.isDigit(textToParse.charAt(iIndex)))) 157 { 158 iIndex++; 159 } 160 iIndexEnd = iIndex; 161 164 if (iIndexStart != iIndexEnd) 165 { 166 iNumber = Integer.parseInt(textToParse.substring(iIndexStart, 168 iIndexEnd)); 169 171 while ((iIndex < iLength) 174 && ((textToParse.charAt(iIndex) == ' ') 175 || (textToParse.charAt(iIndex) == '-'))) 176 { 177 iIndex++; 178 } 179 181 if (iIndex == iLength) 185 { 186 parsed = iNumber; 188 } 189 else 190 { 191 int iQuotient = 0; 192 int iDivisor = Integer.MAX_VALUE; 193 194 if (Character.isDigit(textToParse.charAt(iIndex))) 195 { 196 int iWholePart = 0; 197 198 iWholePart = iNumber; 200 201 iIndexStart = iIndex; while ((iIndex < iLength) 204 && (Character.isDigit(textToParse.charAt(iIndex)))) 205 { 206 iIndex++; 207 } 208 iIndexEnd = iIndex; 209 212 iQuotient = Integer.parseInt(textToParse.substring(iIndexStart, 213 iIndexEnd)); 214 216 while ((iIndex < iLength) 218 && (textToParse.charAt(iIndex) == ' ')) 219 { 220 iIndex++; 221 } 222 224 if (textToParse.charAt(iIndex) == '/') 225 { 226 228 iIndexStart = iIndex + 1; 229 while ((iIndexStart < iLength) 230 && (textToParse.charAt(iIndexStart) == ' ')) 231 { 232 iIndexStart++; 233 } 234 236 if (iIndexStart < iLength) 238 { 239 iIndex = iIndexStart; 241 while ((iIndex < iLength) 242 && (Character.isDigit(textToParse.charAt(iIndex)))) 243 { 244 iIndex++; 245 } 246 iIndexEnd = iIndex; 247 250 if (iIndexStart != iIndexEnd) 251 { 252 iDivisor = Integer.parseInt(textToParse.substring(iIndexStart, 253 iIndexEnd)); 254 if (iDivisor != 0) 256 { 257 if (iIndexEnd == iLength) 258 { 259 parsed = ((double)(iWholePart)) 261 + (((double)iQuotient) / ((double)iDivisor)); 262 } 263 else 264 { 265 if (bIgnoreRest) 266 { 267 parsed = ((double)(iWholePart)) 269 + (((double)iQuotient) / ((double)iDivisor)); 270 } 271 else 272 { 273 } 276 } 277 } 278 } 279 else 280 { 281 } 283 } 284 else 285 { 286 } 288 } 289 else 290 { 291 } 293 } 294 else 295 { 296 if (textToParse.charAt(iIndex) == '/') 297 { 298 300 iQuotient = iNumber; 302 304 iIndexStart = iIndex + 1; 305 while ((iIndexStart < iLength) 306 && (textToParse.charAt(iIndexStart) == ' ')) 307 { 308 iIndexStart++; 309 } 310 312 if (iIndexStart < iLength) 314 { 315 iIndex = iIndexStart; 317 while ((iIndex < iLength) 318 && (Character.isDigit(textToParse.charAt(iIndex)))) 319 { 320 iIndex++; 321 } 322 iIndexEnd = iIndex; 323 326 if (iIndexStart != iIndexEnd) 327 { 328 iDivisor = Integer.parseInt(textToParse.substring(iIndexStart, 329 iIndexEnd)); 330 332 if (iDivisor != 0) 333 { 334 if (iIndexEnd == iLength) 335 { 336 parsed = ((double)iQuotient) / ((double)iDivisor); 338 } 339 else 340 { 341 if (bIgnoreRest) 342 { 343 parsed = ((double)iQuotient) / ((double)iDivisor); 345 } 346 else 347 { 348 } 351 } 352 } 353 } 354 else 355 { 356 } 358 } 359 else 360 { 361 } 363 } 364 else 365 { 366 if (bIgnoreRest) 368 { 369 parsed = iNumber; 371 } 372 else 373 { 374 } 377 } 378 } 379 } 380 } 381 } 382 383 return parsed; 384 } 385 386 397 public static String [] parseQuotedStringToStringArray( 398 String strParse, 399 String strDel, 400 boolean bAllowSingleQuote, 401 boolean bAllowDoubleQuote 402 ) throws OSSInvalidDataException 403 { 404 String [] arrayStrings; 405 406 if (strParse == null) 407 { 408 arrayStrings = null; 409 } 410 else 411 { 412 List lstElements = new ArrayList (); 413 int iCurrentIndex = 0; 414 int iNextIndex; 415 int iDelLength = strDel.length(); 416 int iParseLength = strParse.length(); 417 418 while (iCurrentIndex < iParseLength) 419 { 420 if ((bAllowSingleQuote) && (strParse.charAt(iCurrentIndex) == '\'')) 421 { 422 iNextIndex = strParse.indexOf('\'', iCurrentIndex + 1); 425 if (iNextIndex == -1) 426 { 427 throw new OSSInvalidDataException( 428 "Incorrect input. " + strParse 429 + " No single quote following the one" 430 + " at location " + iCurrentIndex); 431 } 432 lstElements.add(strParse.substring(iCurrentIndex + 1, iNextIndex)); 433 iCurrentIndex = iNextIndex + 1; 434 if (strParse.substring(iCurrentIndex).startsWith(strDel)) 435 { 436 iCurrentIndex += iDelLength; 437 } 438 } 439 else if ((bAllowDoubleQuote) && (strParse.charAt(iCurrentIndex) == '"')) 440 { 441 iNextIndex = strParse.indexOf('"', iCurrentIndex + 1); 444 if (iNextIndex == -1) 445 { 446 throw new OSSInvalidDataException( 447 "Incorrect input. " + strParse 448 + " No double quote following the one" 449 + " at location " + iCurrentIndex); 450 } 451 lstElements.add(strParse.substring(iCurrentIndex + 1, iNextIndex)); 452 iCurrentIndex = iNextIndex + 1; 453 if (strParse.substring(iCurrentIndex).startsWith(strDel)) 454 { 455 iCurrentIndex += iDelLength; 456 } 457 } 458 else 459 { 460 iNextIndex = strParse.indexOf(strDel, iCurrentIndex); 463 if (iNextIndex == -1) 464 { 465 lstElements.add(strParse.substring(iCurrentIndex)); 467 iCurrentIndex = iParseLength; 468 } 469 else 470 { 471 lstElements.add(strParse.substring(iCurrentIndex, iNextIndex)); 472 iCurrentIndex = iNextIndex + iDelLength; 473 } 474 } 475 } 476 arrayStrings = (String [])lstElements.toArray(new String [lstElements.size()]); 477 } 478 479 return arrayStrings; 480 } 481 482 490 public static int[] parseStringToIntArray( 491 String strParse, 492 String strDel 493 ) throws OSSException 494 { 495 int[] arrayInts; 496 try 497 { 498 if (strParse == null) 499 { 500 arrayInts = null; 501 } 502 else 503 { 504 List lstInts = parseStringToList(strParse, strDel); 509 510 if (lstInts == null || lstInts.size() < 1) 511 { 512 arrayInts = null; 513 } 514 else 515 { 516 Iterator items; 517 int iCount; 518 519 arrayInts = new int[lstInts.size()]; 520 for (iCount = 0, items = lstInts.iterator(); items.hasNext();) 521 { 522 arrayInts[iCount++] = Integer.parseInt(((String )items.next()).trim()); 523 } 524 } 525 } 526 } 527 catch (NumberFormatException eExc) 528 { 529 throw new OSSInvalidDataException( 530 "Problems with parsing String to array of int.", 531 eExc); 532 } 533 return arrayInts; 534 } 535 536 544 public static Integer [] parseStringToIntegerArray( 545 String strParse, 546 String strDel 547 ) throws OSSException 548 { 549 Integer [] arrayInts; 550 try 551 { 552 if (strParse == null) 553 { 554 arrayInts = null; 555 } 556 else 557 { 558 List strInts = parseStringToList(strParse, strDel); 563 564 if (strInts == null || strInts.size() < 1) 565 { 566 arrayInts = null; 567 } 568 else 569 { 570 arrayInts = new Integer [strInts.size()]; 571 for (int iCount = 0; iCount < strInts.size(); iCount++) 572 { 573 arrayInts[iCount] = Integer.valueOf((String ) strInts.get(iCount)); 574 } 575 } 576 } 577 } 578 catch (NumberFormatException eExc) 579 { 580 throw new OSSInvalidDataException( 581 "Problems with parsing String to array of int.", 582 eExc); 583 } 584 return arrayInts; 585 } 586 587 594 public static String [] parseStringToStringArray( 595 String strParse, 596 String strDel 597 ) 598 { 599 String [] arrayStrings; 600 601 if (strParse == null) 602 { 603 arrayStrings = null; 604 } 605 else 606 { 607 List lstStrings = parseStringToList(strParse, strDel); 612 613 if ((lstStrings == null) || (lstStrings.isEmpty())) 614 { 615 arrayStrings = null; 616 } 617 else 618 { 619 arrayStrings = (String [])lstStrings.toArray(new String [lstStrings.size()]); 620 } 621 } 622 623 return arrayStrings; 624 } 625 626 633 public static String parseIntArrayToString( 634 int[] arrParse, 635 String strDel 636 ) 637 { 638 StringBuffer strbInts = new StringBuffer (); 639 if ((arrParse != null) && (arrParse.length > 0)) 640 { 641 for (int iCount = 0; iCount < arrParse.length; iCount++) 642 { 643 if (iCount > 0) 644 { 645 strbInts.append(strDel); 646 } 647 strbInts.append(arrParse[iCount]); 648 } 649 } 650 return strbInts.toString(); 651 } 652 653 660 public static String parseCollectionToString( 661 Collection colObjects, 662 String strDel 663 ) 664 { 665 StringBuffer strbInts = new StringBuffer (); 666 if ((colObjects != null) && (!colObjects.isEmpty())) 667 { 668 for (Iterator items = colObjects.iterator(); 669 items.hasNext();) 670 { 671 if (strbInts.length() > 0) 672 { 673 strbInts.append(strDel); 674 } 675 strbInts.append(items.next().toString()); 676 } 677 } 678 return strbInts.toString(); 679 } 680 681 688 public static List parseStringToList( 689 String strParse, 690 String strDel 691 ) 692 { 693 return (List )parseStringToCollection(strParse, strDel, false, 694 CASE_ORIGINAL, null); 695 } 696 697 714 public static Collection parseStringToCollection( 715 String strParse, 716 String strDel, 717 boolean bTrim, 718 int iConvertCase, 719 Collection container 720 ) 721 { 722 Collection colReturn; 723 724 if (strParse == null || strParse.length() < 1) 725 { 726 if (container != null) 727 { 728 colReturn = container; 729 } 730 else 731 { 732 colReturn = null; 733 } 734 } 735 else 736 { 737 StringTokenizer strTokParse = new StringTokenizer (strParse, strDel); 741 String strTemp; 742 743 if (container == null) 744 { 745 colReturn = new ArrayList (); 747 } 748 else 749 { 750 container.clear(); 751 colReturn = container; 752 } 753 754 if (strParse.startsWith(strDel)) 755 { 756 colReturn.add(""); 759 } 760 761 while (strTokParse.hasMoreTokens()) 762 { 763 strTemp = (String )strTokParse.nextToken(); 764 if (bTrim) 765 { 766 strTemp = strTemp.trim(); 767 } 768 switch (iConvertCase) 769 { 770 case (CASE_ORIGINAL) : 771 { 772 break; 774 } 775 case (CASE_TOUPPER) : 776 { 777 strTemp = strTemp.toUpperCase(); 778 break; 779 } 780 case (CASE_TOLOWER) : 781 { 782 strTemp = strTemp.toLowerCase(); 783 break; 784 } 785 default : 786 { 787 assert false : "Incorrect case specification."; 788 } 789 } 790 colReturn.add(strTemp); 791 } 792 } 793 return colReturn; 794 } 795 796 803 public static String limitStringLength( 804 int limitLength, 805 String strValue 806 ) 807 { 808 StringBuffer sbReturn = new StringBuffer (); 809 810 if ((limitLength > 0) && (strValue.length() > limitLength)) 811 { 812 if (limitLength < 5) 814 { 815 sbReturn.append(strValue.substring(0, limitLength)); 816 } 817 else if (limitLength < 15) 820 { 821 sbReturn.append(strValue.substring(0, limitLength - 3)); 822 sbReturn.append("..."); 823 } 824 else 827 { 828 if ((strValue.indexOf(" ", limitLength - 12) > (limitLength - 4)) 831 || (strValue.indexOf(" ", limitLength - 12) < 0)) 832 { 833 sbReturn.append(strValue.substring(0, limitLength - 3)); 834 sbReturn.append("..."); 835 } 836 else 839 { 840 sbReturn.append(strValue.substring(0, strValue.indexOf( 841 " ", limitLength - 12))); 842 sbReturn.append(" ..."); 843 } 844 } 845 } 846 else 847 { 848 sbReturn.append(strValue); 849 } 850 851 return sbReturn.toString(); 852 } 853 854 861 public static String removeComma( 862 String strToRemoveFrom 863 ) 864 { 865 if (GlobalConstants.ERROR_CHECKING) 866 { 867 assert strToRemoveFrom.charAt(0) == ',' 868 : "String should to start with character ','"; 869 assert strToRemoveFrom.charAt(strToRemoveFrom.length() - 1) == ',' 870 : "String should to end with character ','"; 871 } 872 if (strToRemoveFrom.length() > 2) 875 { 876 strToRemoveFrom = strToRemoveFrom.substring(1, strToRemoveFrom.length() - 1); 877 } 878 else 879 { 880 strToRemoveFrom = ""; 881 } 882 883 return strToRemoveFrom; 884 } 885 886 895 public static String concat( 896 String [] strings, 897 String separator, 898 String quote 899 ) 900 { 901 StringBuffer output = new StringBuffer (); 902 903 if (strings != null) 904 { 905 int iIndex; 906 boolean bSeparator; 907 boolean bQuote; 908 909 bSeparator = (separator != null) && (separator.length() > 0); 910 bQuote = (quote != null) && (quote.length() > 0); 911 for (iIndex = 0; iIndex < strings.length; iIndex++) 912 { 913 if ((strings[iIndex] != null) && (strings[iIndex].length() > 0)) 914 { 915 if ((output.length() > 0) && (bSeparator)) 916 { 917 output.append(separator); 918 } 919 if (bQuote) 920 { 921 output.append(quote); 922 } 923 output.append(strings[iIndex]); 924 if (bQuote) 925 { 926 output.append(quote); 927 } 928 } 929 } 930 } 931 932 return output.toString(); 933 } 934 935 944 public static boolean isContained( 945 Collection container, 946 String strSearch 947 ) 948 { 949 boolean bReturn = false; 950 951 if ((container != null) && (!container.isEmpty())) 952 { 953 for (Iterator itrElements = container.iterator(); 954 (itrElements.hasNext() && (!bReturn));) 955 { 956 if (((String )itrElements.next()).indexOf(strSearch) != -1) 957 { 958 bReturn = true; 959 } 960 } 961 } 962 963 return bReturn; 964 } 965 966 975 public static boolean contains( 976 Collection container, 977 String strSearch 978 ) 979 { 980 boolean bReturn = false; 981 982 if ((container != null) && (!container.isEmpty())) 983 { 984 for (Iterator itrElements = container.iterator(); 985 (itrElements.hasNext() && (!bReturn));) 986 { 987 if (strSearch.indexOf((String )itrElements.next()) != -1) 988 { 989 bReturn = true; 990 } 991 } 992 } 993 994 return bReturn; 995 } 996 997 1007 public static boolean containsInSeparatedString( 1008 String strSearchIn, 1009 String strSearchFor, 1010 String strSeparator 1011 ) 1012 { 1013 boolean bReturn = false; 1014 1015 StringBuffer sbInputString = new StringBuffer (); 1016 StringBuffer sbSearchString = new StringBuffer (); 1017 1018 if (strSearchIn.length() > 0) 1019 { 1020 sbInputString.append(strSeparator); 1022 sbInputString.append(strSearchIn); 1023 sbInputString.append(strSeparator); 1024 1025 sbSearchString.append(strSeparator); 1027 sbSearchString.append(strSearchFor); 1028 sbSearchString.append(strSeparator); 1029 1030 if (sbInputString.indexOf(sbSearchString.toString()) != -1) 1032 { 1033 bReturn = true; 1034 } 1035 } 1036 1037 return bReturn; 1038 } 1039} 1040 | Popular Tags |