1 22 package org.jboss.util; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.net.URL ; 30 import java.net.URLEncoder ; 31 import java.util.Map ; 32 33 42 public final class Strings 43 { 44 45 public static final String EMPTY = ""; 46 47 48 private static final long MSEC = 1; 49 private static final long SECS = 1000; 50 private static final long MINS = 60 * 1000; 51 private static final long HOUR = 60 * 60 * 1000; 52 53 57 private static final String [] keywords = 58 { 59 "abstract", 60 "boolean", 61 "break", 62 "byte", 63 "case", 64 "catch", 65 "char", 66 "class", 67 "const", 68 "continue", 69 "default", 70 "do", 71 "double", 72 "else", 73 "extends", 74 "final", 75 "finally", 76 "float", 77 "for", 78 "goto", 79 "if", 80 "implements", 81 "import", 82 "instanceof", 83 "int", 84 "interface", 85 "long", 86 "native", 87 "new", 88 "package", 89 "private", 90 "protected", 91 "public", 92 "return", 93 "short", 94 "static", 95 "strictfp", 96 "super", 97 "switch", 98 "synchronized", 99 "this", 100 "throw", 101 "throws", 102 "transient", 103 "try", 104 "void", 105 "volatile", 106 "while", 107 108 "true", "false", 110 "null" 111 }; 112 113 117 private static final String [] ejbQlIdentifiers = 118 { 119 "AND", 120 "AS", 121 "BETWEEN", 122 "DISTINCT", 123 "EMPTY", 124 "FALSE", 125 "FROM", 126 "IN", 127 "IS", 128 "LIKE", 129 "MEMBER", 130 "NOT", 131 "NULL", 132 "OBJECT", 133 "OF", 134 "OR", 135 "SELECT", 136 "UNKNOWN", 137 "TRUE", 138 "WHERE", 139 }; 140 141 145 154 public static String subst(final StringBuffer buff, final String from, 155 final String to, final String string) 156 { 157 int begin = 0, end = 0; 158 159 while ((end = string.indexOf(from, end)) != -1) 160 { 161 buff.append(string.substring(begin, end)); 163 164 buff.append(to); 166 167 begin = end + from.length(); 169 end = begin; 170 } 171 172 buff.append(string.substring(begin, string.length())); 174 175 return buff.toString(); 176 } 177 178 186 public static String subst(final String from, final String to, 187 final String string) 188 { 189 return subst(new StringBuffer (), from, to, string); 190 } 191 192 202 public static String subst(final StringBuffer buff, final String string, 203 final Map map, final String beginToken, 204 final String endToken) 205 { 206 int begin = 0, rangeEnd = 0; 207 Range range; 208 209 while ((range = rangeOf(beginToken, endToken, string, rangeEnd)) != null) 210 { 211 buff.append(string.substring(begin, range.begin)); 213 214 String key = string.substring(range.begin + beginToken.length(), 216 range.end); 217 Object value = map.get(key); 218 if (value == null) value = EMPTY; 220 221 buff.append(value); 223 224 begin = range.end + endToken.length(); 226 rangeEnd = begin; 227 } 228 229 buff.append(string.substring(begin, string.length())); 231 232 return buff.toString(); 233 } 234 235 244 public static String subst(final String string, final Map map, 245 final String beginToken, final String endToken) 246 { 247 return subst(new StringBuffer (), string, map, beginToken, endToken); 248 } 249 250 264 public static String subst(final StringBuffer buff, final String string, 265 final String replace[], final char token) 266 { 267 int i = string.length(); 268 for (int j = 0; j >= 0 && j < i; j++) 269 { 270 char c = string.charAt(j); 271 272 if (c == token) 274 { 275 276 if (j != i) 278 { 279 int k = Character.digit(string.charAt(j + 1), 10); 280 281 if (k == -1) 282 { 283 buff.append(string.charAt(j + 1)); 284 } 285 else if (k < replace.length) 286 { 287 buff.append(replace[k]); 288 } 289 290 j++; 291 } 292 } 293 else 294 { 295 buff.append(c); 296 } 297 } 298 299 return buff.toString(); 300 } 301 302 314 public static String subst(final String string, final String replace[], 315 final char token) 316 { 317 return subst(new StringBuffer (), string, replace, token); 318 } 319 320 331 public static String subst(final String string, final String replace[]) 332 { 333 return subst(new StringBuffer (), string, replace, '%'); 334 } 335 336 340 343 public static class Range 344 { 345 346 public int begin; 347 348 349 public int end; 350 351 357 public Range(int begin, int end) 358 { 359 this.begin = begin; 360 this.end = end; 361 } 362 363 366 public Range() 367 { 368 } 369 } 370 371 380 public static Range rangeOf(final String beginToken, final String endToken, 381 final String string, final int fromIndex) 382 { 383 int begin = string.indexOf(beginToken, fromIndex); 384 385 if (begin != -1) 386 { 387 int end = string.indexOf(endToken, begin + 1); 388 if (end != -1) 389 { 390 return new Range(begin, end); 391 } 392 } 393 394 return null; 395 } 396 397 405 public static Range rangeOf(final String beginToken, final String endToken, 406 final String string) 407 { 408 return rangeOf(beginToken, endToken, string, 0); 409 } 410 411 412 416 425 public static String [] split(final String string, final String delim, 426 final int limit) 427 { 428 int count = count(string, delim) + 1; 432 if (limit > 0 && count > limit) 433 { 434 count = limit; 435 } 436 437 String strings[] = new String [count]; 438 int begin = 0; 439 440 for (int i = 0; i < count; i++) 441 { 442 int end = string.indexOf(delim, begin); 444 445 if (end == -1 || i + 1 == count) 448 end = string.length(); 449 450 if (end == 0) 452 strings[i] = EMPTY; 453 else 454 strings[i] = string.substring(begin, end); 455 456 begin = end + 1; 458 } 459 460 return strings; 461 } 462 463 470 public static String [] split(final String string, final String delim) 471 { 472 return split(string, delim, -1); 473 } 474 475 476 480 488 public static String join(final StringBuffer buff, final Object array[], 489 final String delim) 490 { 491 boolean haveDelim = (delim != null); 492 493 for (int i = 0; i < array.length; i++) 494 { 495 buff.append(array[i]); 496 497 if (haveDelim && (i + 1) < array.length) 499 { 500 buff.append(delim); 501 } 502 } 503 504 return buff.toString(); 505 } 506 507 514 public static String join(final Object array[], final String delim) 515 { 516 return join(new StringBuffer (), array, delim); 517 } 518 519 525 public static String join(final Object array[]) 526 { 527 return join(array, null); 528 } 529 530 536 public static String join(final byte array[]) 537 { 538 Byte bytes[] = new Byte [array.length]; 539 for (int i = 0; i < bytes.length; i++) 540 { 541 bytes[i] = new Byte (array[i]); 542 } 543 544 return join(bytes, null); 545 } 546 547 558 public static String join(final StringBuffer buff, final Object [] array, 559 final String prefix, final String separator, 560 final String suffix) 561 { 562 buff.append(prefix); 563 join(buff, array, separator); 564 buff.append(suffix); 565 566 return buff.toString(); 567 } 568 569 579 public static String join(final Object [] array, final String prefix, 580 final String separator, final String suffix) 581 { 582 return join(new StringBuffer (), array, prefix, separator, suffix); 583 } 584 585 586 590 597 public static int count(final String string, final String substring) 598 { 599 int count = 0; 600 int idx = 0; 601 602 while ((idx = string.indexOf(substring, idx)) != -1) 603 { 604 idx++; 605 count++; 606 } 607 608 return count; 609 } 610 611 618 public static int count(final String string, final char c) 619 { 620 return count(string, String.valueOf(c)); 621 } 622 623 624 628 636 public static String pad(final StringBuffer buff, final String string, 637 final int count) 638 { 639 for (int i = 0; i < count; i++) 640 { 641 buff.append(string); 642 } 643 644 return buff.toString(); 645 } 646 647 654 public static String pad(final String string, final int count) 655 { 656 return pad(new StringBuffer (), string, count); 657 } 658 659 667 public static String pad(final Object obj, final int count) 668 { 669 return pad(new StringBuffer (), String.valueOf(obj), count); 670 } 671 672 673 677 684 public static boolean compare(final String me, final String you) 685 { 686 if (me == you) 688 return true; 689 690 if (me == null && you != null) 692 return false; 693 694 return me.equals(you); 696 } 697 698 704 public static boolean isEmpty(final String string) 705 { 706 return string.equals(EMPTY); 707 } 708 709 717 public static int nthIndexOf(final String string, final String token, 718 final int index) 719 { 720 int j = 0; 721 722 for (int i = 0; i < index; i++) 723 { 724 j = string.indexOf(token, j + 1); 725 if (j == -1) break; 726 } 727 728 return j; 729 } 730 731 739 public static String capitalize(final String string) 740 { 741 if (string == null) 742 throw new NullArgumentException("string"); 743 if (string.equals("")) 744 throw new EmptyStringException("string"); 745 746 return Character.toUpperCase(string.charAt(0)) + string.substring(1); 747 } 748 749 757 public static String [] trim(final String [] strings) 758 { 759 for (int i = 0; i < strings.length; i++) 760 { 761 strings[i] = strings[i].trim(); 762 } 763 764 return strings; 765 } 766 767 785 public static URL toURL(String urlspec, final String relativePrefix) throws MalformedURLException 786 { 787 urlspec = urlspec.trim(); 788 789 URL url; 790 791 try 792 { 793 url = new URL (urlspec); 794 if (url.getProtocol().equals("file")) 795 { 796 url = makeURLFromFilespec(url.getFile(), relativePrefix); 797 } 798 } 799 catch (Exception e) 800 { 801 try 803 { 804 url = makeURLFromFilespec(urlspec, relativePrefix); 805 } 806 catch (IOException n) 807 { 808 throw new MalformedURLException (n.toString()); 812 } 813 } 814 815 return url; 816 } 817 818 public static URI toURI(String urispec, final String relativePrefix) 819 throws URISyntaxException 820 { 821 urispec = urispec.trim(); 822 823 URI uri; 824 825 if( urispec.startsWith("file:") ) 826 { 827 uri = makeURIFromFilespec(urispec.substring(5), relativePrefix); 828 } 829 else 830 { 831 uri = new URI (urispec); 832 } 833 834 return uri; 835 } 836 837 838 private static URL makeURLFromFilespec(final String filespec, final String relativePrefix) 839 throws IOException 840 { 841 File file = new File (filespec); 843 844 if (relativePrefix != null && !file.isAbsolute()) 846 { 847 file = new File (relativePrefix, filespec); 848 } 849 850 file = file.getCanonicalFile(); 852 853 return file.toURL(); 854 } 855 private static URI makeURIFromFilespec(final String filespec, final String relativePrefix) 856 { 857 File file = new File (filespec); 859 860 if (relativePrefix != null && !file.isAbsolute()) 862 { 863 file = new File (relativePrefix, filespec); 864 } 865 866 return file.toURI(); 867 } 868 869 879 public static URL toURL(final String urlspec) throws MalformedURLException 880 { 881 return toURL(urlspec, null); 882 } 883 884 890 public static URI toURI(final String urispec) 891 throws URISyntaxException 892 { 893 return toURI(urispec, null); 894 } 895 896 905 public final static boolean isJavaKeyword(String s) 906 { 907 if (s == null || s.length() == 0) 908 { 909 return false; 910 } 911 912 for (int i = 0; i < keywords.length; i++) 913 { 914 if (keywords[i].equals(s)) 915 { 916 return true; 917 } 918 } 919 920 return false; 921 } 922 923 932 public final static boolean isEjbQlIdentifier(String s) 933 { 934 if (s == null || s.length() == 0) 935 { 936 return false; 937 } 938 939 for (int i = 0; i < ejbQlIdentifiers.length; i++) 940 { 941 if (ejbQlIdentifiers[i].equalsIgnoreCase(s)) 942 { 943 return true; 944 } 945 } 946 947 return false; 948 } 949 950 962 public final static boolean isValidJavaIdentifier(String s) 963 { 964 if (s == null || s.length() == 0) 966 { 967 return false; 968 } 969 970 char[] c = s.toCharArray(); 971 if (!Character.isJavaIdentifierStart(c[0])) 972 { 973 return false; 974 } 975 976 for (int i = 1; i < c.length; i++) 977 { 978 if (!Character.isJavaIdentifierPart(c[i])) 979 { 980 return false; 981 } 982 } 983 984 return true; 985 } 986 987 993 public static String removeWhiteSpace(String s) 994 { 995 String retn = null; 996 997 if (s != null) 998 { 999 int len = s.length(); 1000 StringBuffer sbuf = new StringBuffer (len); 1001 1002 for (int i = 0; i < len; i++) 1003 { 1004 char c = s.charAt(i); 1005 1006 if (!Character.isWhitespace(c)) 1007 sbuf.append(c); 1008 } 1009 retn = sbuf.toString(); 1010 } 1011 return retn; 1012 } 1013 1014 1020 public static final String defaultToString(Object object) 1021 { 1022 if (object == null) 1023 return "null"; 1024 else 1025 return object.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(object)); 1026 } 1027 1028 1035 public static final void defaultToString(JBossStringBuilder buffer, Object object) 1036 { 1037 if (object == null) 1038 buffer.append("null"); 1039 else 1040 { 1041 buffer.append(object.getClass().getName()); 1042 buffer.append('@'); 1043 buffer.append(Integer.toHexString(System.identityHashCode(object))); 1044 } 1045 } 1046 1047 1054 public static final void defaultToString(StringBuffer buffer, Object object) 1055 { 1056 if (object == null) 1057 buffer.append("null"); 1058 else 1059 { 1060 buffer.append(object.getClass().getName()); 1061 buffer.append('@'); 1062 buffer.append(Integer.toHexString(System.identityHashCode(object))); 1063 } 1064 } 1065 1066 1084 public static long parseTimePeriod(String period) 1085 { 1086 try 1087 { 1088 String s = period.toLowerCase(); 1089 long factor; 1090 1091 if (s.endsWith("msec")) 1093 { 1094 s = s.substring(0, s.lastIndexOf("msec")); 1095 factor = MSEC; 1096 } 1097 else if (s.endsWith("sec")) 1098 { 1099 s = s.substring(0, s.lastIndexOf("sec")); 1100 factor = SECS; 1101 } 1102 else if (s.endsWith("min")) 1103 { 1104 s = s.substring(0, s.lastIndexOf("min")); 1105 factor = MINS; 1106 } 1107 else if (s.endsWith("h")) 1108 { 1109 s = s.substring(0, s.lastIndexOf("h")); 1110 factor = HOUR; 1111 } 1112 else 1113 { 1114 factor = 1; 1115 } 1116 return Long.parseLong(s) * factor; 1117 } 1118 catch (RuntimeException e) 1119 { 1120 throw new NumberFormatException ("For input time period: '" + period + "'"); 1122 } 1123 } 1124 1125 1132 public static long parsePositiveTimePeriod(String period) 1133 { 1134 long retval = parseTimePeriod(period); 1135 if (retval < 0) 1136 { 1137 throw new NumberFormatException ("Negative input time period: '" + period + "'"); 1138 } 1139 return retval; 1140 } 1141} 1142 | Popular Tags |