1 34 35 36 package com.micronova.util; 37 38 import java.util.*; 39 import java.io.*; 40 import java.util.zip.*; 41 import java.util.regex.*; 42 43 44 45 public class StringUtil 46 { 47 48 49 public static final char DEFAULTESCAPE = '\\'; 50 51 52 53 public static final List split(StringBuffer buffer, char separator, char escape) 54 { 55 List list = new ArrayList(); 56 57 StringBuffer partBuffer = new StringBuffer (); 58 int bufferLength = buffer.length(); 59 60 boolean isEscaped = false; 61 62 for (int i = 0; i < bufferLength; i ++) 63 { 64 char c = buffer.charAt(i); 65 66 if (isEscaped) 67 { 68 if (c != separator) 69 { 70 partBuffer.append(escape); 71 } 72 73 partBuffer.append(c); 74 75 isEscaped = false; 76 } 77 else 78 { 79 if (c == escape) 80 { 81 isEscaped = true; 82 } 83 else if (c == separator) 84 { 85 list.add(partBuffer.toString()); 86 partBuffer.setLength(0); 87 } 88 else 89 { 90 partBuffer.append(c); 91 } 92 } 93 } 94 95 if (partBuffer.length() != 0) 96 { 97 list.add(partBuffer.toString()); 98 } 99 100 return list; 101 } 102 103 104 105 public static final List split(String string, char separator, char escape) 106 { 107 return split(new StringBuffer (string), separator, escape); 108 } 109 110 111 112 public static final List split(StringBuffer buffer, char separator) 113 { 114 return split(buffer, separator, DEFAULTESCAPE); 115 } 116 117 118 119 public static final List split(String string, char separator) 120 { 121 return split(string, separator, DEFAULTESCAPE); 122 } 123 124 125 126 public static final String join(List list, String glue) 127 { 128 StringBuffer output = new StringBuffer (); 129 130 boolean needsGlue = false; 131 132 Iterator iterator = list.iterator(); 133 134 while (iterator.hasNext()) 135 { 136 if (needsGlue) 137 { 138 output.append(glue); 139 } 140 141 output.append(iterator.next()); 142 143 needsGlue = true; 144 } 145 146 return output.toString(); 147 } 148 149 150 151 public static final void decodeBackslash(StringBuffer input, StringBuffer output) 152 { 153 int inputLength = input.length(); 154 155 boolean isAfterSlash = false; 156 157 for (int i = 0; i < inputLength; i ++) 158 { 159 char c = input.charAt(i); 160 161 if (c == '\\') 162 { 163 if (!isAfterSlash) 164 { 165 isAfterSlash = true; 166 } 167 else 168 { 169 output.append(c); 170 isAfterSlash = false; 171 } 172 } 173 else 174 { 175 if (isAfterSlash) 176 { 177 switch (c) 178 { 179 case 'n': 180 { 181 output.append('\n'); 182 } 183 break; 184 185 case 'r': 186 { 187 output.append('\r'); 188 } 189 break; 190 191 case 't': 192 { 193 output.append('\t'); 194 } 195 break; 196 197 case 'b': 198 { 199 output.append('\b'); 200 } 201 break; 202 203 case 'f': 204 { 205 output.append('\f'); 206 } 207 break; 208 209 case '\'': 210 { 211 output.append('\''); 212 } 213 break; 214 215 case '/': 216 { 217 output.append('/'); 218 } 219 break; 220 221 case '\"': 222 { 223 output.append('\"'); 224 } 225 break; 226 227 case 'u': 228 { 229 int u = Integer.parseInt(input.substring(i + 1, i + 5), 16); 230 output.append((char)u); 231 i += 4; 232 } 233 break; 234 } 235 236 isAfterSlash = false; 237 } 238 else 239 { 240 output.append(c); 241 } 242 } 243 } 244 } 245 246 public static final String encodeBackslash(String string) 247 { 248 StringBuffer input = new StringBuffer (string); 249 StringBuffer output = new StringBuffer (); 250 251 encodeBackslash(input, output); 252 253 return output.toString(); 254 } 255 256 257 258 public static final void encodeBackslash(StringBuffer input, StringBuffer output) 259 { 260 int inputLength = input.length(); 261 262 for (int i = 0; i < inputLength; i ++) 263 { 264 char c = input.charAt(i); 265 266 switch (c) 267 { 268 case '\n': 269 { 270 output.append("\\n"); 271 } 272 break; 273 274 case '\r': 275 { 276 output.append("\\r"); 277 } 278 break; 279 280 case '\f': 281 { 282 output.append("\\f"); 283 } 284 break; 285 286 case '\t': 287 { 288 output.append("\\t"); 289 } 290 break; 291 292 case '\b': 293 { 294 output.append("\\b"); 295 } 296 break; 297 298 case '\\': 299 { 300 output.append("\\\\"); 301 } 302 break; 303 304 case '/': 305 { 306 output.append("\\/"); 307 } 308 break; 309 310 case '\'': 311 { 312 output.append("\\'"); 313 } 314 break; 315 316 case '\"': 317 { 318 output.append("\\\""); 319 } 320 break; 321 322 default: 323 { 324 output.append(c); 325 } 326 } 327 } 328 } 329 330 331 332 public static final String decodeBackslash(String string) 333 { 334 StringBuffer input = new StringBuffer (string); 335 StringBuffer output = new StringBuffer (); 336 337 decodeBackslash(input, output); 338 339 return output.toString(); 340 } 341 342 343 344 public static byte[] compressGZIP(byte[] input) throws Exception 345 { 346 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 347 348 GZIPOutputStream zOut = new GZIPOutputStream(bOut); 349 350 zOut.write(input, 0, input.length); 351 352 zOut.close(); 353 354 return bOut.toByteArray(); 355 } 356 357 358 359 public static byte[] decompressGZIP(byte[] input) throws Exception 360 { 361 ByteArrayInputStream bIn = new ByteArrayInputStream(input); 362 363 GZIPInputStream zIn = new GZIPInputStream(bIn); 364 365 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 366 367 int ch; 368 369 while ((ch = zIn.read()) != -1) 370 { 371 bOut.write(ch); 372 } 373 374 zIn.close(); 375 376 bOut.close(); 377 378 return bOut.toByteArray(); 379 } 380 381 382 383 public static byte[] compressZip(byte[] input) throws Exception 384 { 385 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 386 387 ZipOutputStream zOut = new ZipOutputStream(bOut); 388 389 zOut.putNextEntry(new ZipEntry("ZIP")); 390 391 zOut.write(input, 0, input.length); 392 393 zOut.close(); 394 395 return bOut.toByteArray(); 396 } 397 398 399 400 public static byte[] decompressZip(byte[] input) throws Exception 401 { 402 ByteArrayInputStream bIn = new ByteArrayInputStream(input); 403 404 ZipInputStream zIn = new ZipInputStream(bIn); 405 406 zIn.getNextEntry(); 407 408 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 409 410 int ch; 411 412 while ((ch = zIn.read()) != -1) 413 { 414 bOut.write(ch); 415 } 416 417 zIn.close(); 418 419 bOut.close(); 420 421 return bOut.toByteArray(); 422 } 423 424 425 426 427 public static final String toBinaryString(byte[] b) throws Exception 428 { 429 return new String (b, "iso-8859-1"); 430 } 431 432 433 434 public static final byte[] fromBinaryString(String s) throws Exception 435 { 436 return s.getBytes("iso-8859-1"); 437 } 438 439 440 441 public static final String compressGZIP(String string) throws Exception 442 { 443 return toBinaryString(compressGZIP(fromBinaryString(string))); 444 } 445 446 447 448 public static final String decompressGZIP(String string) throws Exception 449 { 450 return toBinaryString(decompressGZIP(fromBinaryString(string))); 451 } 452 453 454 455 public static final String compressZip(String string) throws Exception 456 { 457 return toBinaryString(compressZip(fromBinaryString(string))); 458 } 459 460 461 462 public static final String decompressZip(String string) throws Exception 463 { 464 return toBinaryString(decompressZip(fromBinaryString(string))); 465 } 466 467 468 469 public static final String encodeBase64(byte[] x) throws Exception 470 { 471 return (new sun.misc.BASE64Encoder()).encode(x); 472 } 473 474 475 476 public static final byte[] decodeBase64(String s) throws Exception 477 { 478 return (new sun.misc.BASE64Decoder()).decodeBuffer(s); 479 } 480 481 482 483 public static final StringBuffer encodeHex(byte[] in, StringBuffer out) 484 { 485 for (int i = 0; i < in.length; i ++) 486 { 487 out.append(NumberUtil.toHexString((256 + in[i]) % 256, "00")); 488 } 489 490 return out; 491 } 492 493 494 495 public static final String encodeHex(byte[] in) 496 { 497 StringBuffer out = new StringBuffer (); 498 499 return encodeHex(in, out).toString(); 500 } 501 502 503 504 public static final byte[] decodeHex(StringBuffer in, byte[] out) throws Exception 505 { 506 int j = 0; 507 508 for (int i = 0; i < out.length; i ++) 509 { 510 out[i] = (byte)Integer.parseInt(in.substring(j, j + 2), 16); 511 j += 2; 512 } 513 514 return out; 515 } 516 517 518 519 public static final byte[] decodeHex(String s) throws Exception 520 { 521 int sl = s.length(); 522 523 if (sl % 2 == 0) 524 { 525 int bl = sl / 2; 526 527 byte[] out = new byte[s.length() / 2]; 528 529 return decodeHex(new StringBuffer (s), out); 530 } 531 else 532 { 533 throw new Exception ("string length must be even"); 534 } 535 } 536 537 538 539 public static final List toCharacterList(String s) throws Exception 540 { 541 return TypeUtil.isList(s.toString().toCharArray()); 542 } 543 544 545 546 public static final String fromCharacterList(List list) throws Exception 547 { 548 int length = list.size(); 549 550 char[] array = new char[length]; 551 552 for (int i = length; --i >= 0;) 553 { 554 array[i] = ((Character )(list.get(i))).charValue(); 555 } 556 557 return new String (array); 558 } 559 560 561 562 public static void applyPattern(CharSequence input, StringBuffer output, Pattern pattern, Map replacementMap) 563 { 564 Matcher matcher = pattern.matcher(input); 565 566 while (matcher.find()) 567 { 568 String group = matcher.group(); 569 570 if (replacementMap == null) 571 { 572 matcher.appendReplacement(output, ""); 573 } 574 else 575 { 576 String replacement = (String )replacementMap.get(group); 577 578 if (replacement == null) 579 { 580 replacement = group; 581 } 582 583 matcher.appendReplacement(output, replacement); 584 } 585 } 586 587 matcher.appendTail(output); 588 } 589 590 591 592 public static final String applyPattern(String string, Pattern pattern, Map replacementMap) 593 { 594 StringBuffer input = new StringBuffer (string); 595 StringBuffer output = new StringBuffer (); 596 597 applyPattern(input, output, pattern, replacementMap); 598 599 return output.toString(); 600 } 601 602 603 604 public static List decompose(CharSequence input, Pattern pattern, List list, int groupIndex, boolean includeMatch, boolean includeNoMatch) 605 { 606 Matcher matcher = pattern.matcher(input); 607 608 int position = 0; 609 int end = 0; 610 611 while (matcher.find()) 612 { 613 int start = matcher.start(); 614 end = matcher.end(); 615 616 if (start >= position) 617 { 618 if (includeNoMatch) 619 { 620 list.add(input.subSequence(position, start)); 621 } 622 } 623 624 if (includeMatch) 625 { 626 if (groupIndex >= 0) 627 { 628 list.add(matcher.group(groupIndex)); 629 } 630 else 631 { 632 List matchingGroups = new SparseList(); 633 634 for (int i = 0; i <= matcher.groupCount(); i ++) 635 { 636 matchingGroups.set(i, matcher.group(i)); 637 } 638 639 list.add(matchingGroups); 640 } 641 } 642 643 position = end; 644 } 645 646 if (includeNoMatch) 647 { 648 list.add(input.subSequence(end, input.length())); 649 } 650 651 return list; 652 } 653 654 655 656 public static void decompose(CharSequence input, Pattern pattern, List list, int groupIndex) 657 { 658 decompose(input, pattern, list, groupIndex, true, true); 659 } 660 661 662 663 public static List decompose(String string, Pattern pattern, int groupIndex) 664 { 665 StringBuffer input = new StringBuffer (string); 666 List list = new ArrayList(); 667 668 decompose(input, pattern, list, groupIndex); 669 670 return list; 671 } 672 673 674 675 public static List decompose(String string, Pattern pattern) 676 { 677 return decompose(string, pattern, 0); 678 } 679 680 681 682 public static void split(CharSequence input, Pattern pattern, List list, int groupIndex) 683 { 684 decompose(input, pattern, list, groupIndex, false, true); 685 } 686 687 688 689 public static List split(String string, Pattern pattern, int groupIndex) 690 { 691 StringBuffer input = new StringBuffer (string); 692 List list = new ArrayList(); 693 694 split(input, pattern, list, groupIndex); 695 696 return list; 697 } 698 699 700 701 public static List split(String string, Pattern pattern) 702 { 703 return split(string, pattern, 0); 704 } 705 706 707 708 public static void matchAll(CharSequence input, Pattern pattern, List list, int groupIndex) 709 { 710 decompose(input, pattern, list, groupIndex, true, false); 711 } 712 713 714 715 public static List matchAll(String string, Pattern pattern, int groupIndex) 716 { 717 StringBuffer input = new StringBuffer (string); 718 List list = new ArrayList(); 719 720 matchAll(input, pattern, list, groupIndex); 721 722 return list; 723 } 724 725 726 727 public static List matchAll(String string, Pattern pattern) 728 { 729 return matchAll(string, pattern, 0); 730 } 731 732 733 734 public static List matchingGroups(String string, Pattern pattern) 735 { 736 List list = new SparseList(); 737 738 Matcher matcher = pattern.matcher(string); 739 740 if (matcher.matches()) 741 { 742 for (int i = 0; i <= matcher.groupCount(); i ++) 743 { 744 list.set(i, matcher.group(i)); 745 } 746 } 747 748 return list; 749 } 750 751 752 753 public static String capitalize(String string) 754 { 755 StringBuffer buffer = new StringBuffer (string); 756 757 if (buffer.length() > 0) 758 { 759 char c = buffer.charAt(0); 760 761 if (Character.isLowerCase(c)) 762 { 763 buffer.setCharAt(0, Character.toUpperCase(c)); 764 } 765 766 string = buffer.toString(); 767 } 768 769 return string; 770 } 771 772 773 774 public static String reverse(String string) 775 { 776 StringBuffer buffer = new StringBuffer (string); 777 778 int bufferLength = buffer.length(); 779 int half = bufferLength / 2; 780 781 for (int i = half, j = bufferLength - half; --i >=0; j ++) 782 { 783 char c = buffer.charAt(i); 784 buffer.setCharAt(i, buffer.charAt(j)); 785 buffer.setCharAt(j, c); 786 } 787 788 return buffer.toString(); 789 } 790 791 792 793 public static String multiply(String string, int count) 794 { 795 StringBuffer buffer = new StringBuffer (); 796 797 for (int i = count; --i >= 0;) 798 { 799 buffer.append(string); 800 } 801 802 return buffer.toString(); 803 } 804 805 806 807 public static String match(CharSequence input, Pattern pattern, int groupIndex) 808 { 809 Matcher matcher = pattern.matcher(input); 810 811 if (matcher.find()) 812 { 813 return matcher.group(groupIndex); 814 } 815 else 816 { 817 return null; 818 } 819 } 820 821 822 823 public static String match(String string, Pattern pattern) 824 { 825 return match(string, pattern, 0); 826 } 827 828 829 830 public static Map countWords(String string, Pattern pattern) 831 { 832 String [] array = pattern.split(string); 833 834 Map map = new HashMap(); 835 836 for (int i = array.length; --i >= 0;) 837 { 838 String word = array[i]; 839 840 Integer count = (Integer )map.get(word); 841 842 if (count == null) 843 { 844 count = new Integer (1); 845 } 846 else 847 { 848 count = new Integer (count.intValue() + 1); 849 } 850 851 map.put(word, count); 852 } 853 854 return map; 855 } 856 857 858 859 public static String substring(String string, int start, int end) 860 { 861 if (string != null) 862 { 863 int length = string.length(); 864 865 if (start < 0) 866 { 867 start = length + start; 868 } 869 870 if (end <= 0) 871 { 872 end = length + end; 873 } 874 875 if (start < 0) 876 { 877 start = 0; 878 } 879 880 if (end > length) 881 { 882 end = length; 883 } 884 885 if (end <= start) 886 { 887 string = ""; 888 } 889 else 890 { 891 string = string.substring(start, end); 892 } 893 } 894 895 return string; 896 } 897 898 899 900 public static String substring(String string, int start) 901 { 902 return substring(string, start, 0); 903 } 904 905 906 907 public static String replaceCharacters(String string, char[] from, char[] to) 908 { 909 StringBuffer buffer = new StringBuffer (string); 910 911 for (int i = buffer.length(); --i >= 0; ) 912 { 913 char c = buffer.charAt(i); 914 915 for (int j = from.length; --j >= 0; ) 916 { 917 if (from[j] == c) 918 { 919 buffer.setCharAt(i, to[j]); 920 } 921 } 922 } 923 924 return buffer.toString(); 925 } 926 927 928 929 public static List splitCSV(Reader r, char separator, char quote, char newline) throws IOException 930 { 931 List rows = new ArrayList(); 932 List columns = new ArrayList(); 933 934 StringBuffer buffer = new StringBuffer (); 935 936 int state = 0; 937 938 int ch = -1; 939 940 while ((ch = r.read()) != -1) 941 { 942 char c = (char)ch; 943 944 if (c == quote) 945 { 946 if (state < 2) 947 { 948 state ++; 949 } 950 else 951 { 952 buffer.append(c); 953 if (state == 2) 954 { 955 state = 1; 956 } 957 } 958 } 959 else 960 { 961 if (state == 1) 962 { 963 buffer.append(c); 964 } 965 else 966 { 967 if (c == separator) 968 { 969 columns.add(buffer.toString()); 970 buffer.setLength(0); 971 state = 0; 972 } 973 else if (c == newline) 974 { 975 if (buffer.length() > 0) 976 { 977 columns.add(buffer.toString()); 978 } 979 980 rows.add(columns); 981 columns = new ArrayList(); 982 buffer.setLength(0); 983 state = 0; 984 } 985 else 986 { 987 if (state != 2) 988 { 989 buffer.append(c); 990 } 991 992 if (state == 0) 993 { 994 state = 3; 995 } 996 } 997 } 998 } 999 } 1000 1001 if (buffer.length() > 0) 1002 { 1003 columns.add(buffer.toString()); 1004 } 1005 1006 if (!columns.isEmpty()) 1007 { 1008 rows.add(columns); 1009 } 1010 1011 return rows; 1012 } 1013 1014 public final static Object decodeJSON(String json, boolean allowUnquoted) throws Exception 1015 { 1016 return com.micronova.util.cc.json.Parser.parse(json, allowUnquoted); 1017 } 1018 1019 public final static Object decodeJSON(String json) throws Exception 1020 { 1021 return decodeJSON(json, true); 1022 } 1023 1024 public final static String encodeJSON(Object object, String quote, boolean doesEncode) 1025 { 1026 String json = null; 1027 1028 if (object != null) 1029 { 1030 List list = TypeUtil.isList(object); 1031 1032 if (list != null) 1033 { 1034 StringBuffer buffer = new StringBuffer (); 1035 buffer.append("["); 1036 Iterator iterator = list.iterator(); 1037 String separator = ""; 1038 while (iterator.hasNext()) 1039 { 1040 buffer.append(separator); 1041 buffer.append(encodeJSON(iterator.next(), quote, doesEncode)); 1042 separator = ","; 1043 } 1044 buffer.append("]"); 1045 return buffer.toString(); 1046 } 1047 else if (object instanceof Map) 1048 { 1049 StringBuffer buffer = new StringBuffer (); 1050 buffer.append("{"); 1051 Iterator iterator = ((Map)object).entrySet().iterator(); 1052 String separator = ""; 1053 while (iterator.hasNext()) 1054 { 1055 Map.Entry entry = (Map.Entry)iterator.next(); 1056 buffer.append(separator); 1057 buffer.append(encodeJSON(entry.getKey().toString(), quote, doesEncode)); 1058 buffer.append(":"); 1059 buffer.append(encodeJSON(entry.getValue(), quote, doesEncode)); 1060 separator = ","; 1061 } 1062 buffer.append("}"); 1063 return buffer.toString(); 1064 } 1065 else if (object instanceof Number ) 1066 { 1067 return object.toString(); 1068 } 1069 else if (object instanceof Boolean ) 1070 { 1071 return object.toString(); 1072 } 1073 else if (object == null) 1074 { 1075 return "null"; 1076 } 1077 else 1078 { 1079 String stringValue = object.toString(); 1080 1081 if (doesEncode) 1082 { 1083 stringValue = encodeBackslash(stringValue); 1084 } 1085 1086 return quote + stringValue + quote; 1087 } 1088 } 1089 1090 return json; 1091 } 1092 1093 public final static String encodeJSON(Object object) 1094 { 1095 return encodeJSON(object, "\"", true); 1096 } 1097 1098 1099 1100 public static final void escapeUnicode(StringBuffer out, StringBuffer in, long minCode, long maxCode) 1101 { 1102 int length = in.length(); 1103 1104 for (int i = 0; i < length; i ++) 1105 { 1106 char c = in.charAt(i); 1107 long code = (long)c; 1108 1109 if (code < 0) 1110 { 1111 code = code + 65536L; 1112 } 1113 1114 if ((code >= minCode) && (code <= maxCode)) 1115 { 1116 out.append("\\u"); 1117 out.append(NumberUtil.toHexString(code, "0000")); 1118 } 1119 else 1120 { 1121 out.append(c); 1122 } 1123 } 1124 } 1125 1126 public static final String escapeUnicode(String in, long minCode, long maxCode) 1127 { 1128 StringBuffer out = new StringBuffer (); 1129 escapeUnicode(out, new StringBuffer (in), minCode, maxCode); 1130 return out.toString(); 1131 } 1132} 1133 | Popular Tags |