1 64 65 package com.jcorporate.expresso.core.misc; 66 67 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 68 69 import java.io.PrintWriter ; 70 import java.io.StringWriter ; 71 import java.math.BigDecimal ; 72 import java.math.BigInteger ; 73 import java.util.Hashtable ; 74 import java.util.List ; 75 import java.util.Random ; 76 import java.util.StringTokenizer ; 77 import java.util.Vector ; 78 79 80 86 public final class StringUtil { 87 public static final String EMPTY_STRING = ""; 88 89 90 99 public static final void assertNotBlank(String theString, String theMessage) { 100 if (theString == null) { 101 throw new IllegalArgumentException ("Null argument not allowed: " + 102 theMessage); 103 } 104 if (theString.trim().equals(EMPTY_STRING)) { 105 throw new IllegalArgumentException ("Blank argument not allowed: " + 106 theMessage); 107 } 108 } 109 110 116 public static final String notNull(String theString) { 117 if (theString == null) { 118 return EMPTY_STRING; 119 } 120 121 return theString; 122 } 123 124 125 132 public static final void assertBoolean(String theString, String theMessage) { 133 assertNotBlank(theString, theMessage); 134 135 if (!(theString.equalsIgnoreCase("yes") || 136 theString.equalsIgnoreCase("true") || 137 theString.equalsIgnoreCase("no") || 138 theString.equalsIgnoreCase("false") || 139 theString.equals("1") || 140 theString.equals("0") || 141 theString.equalsIgnoreCase("y") || 142 theString.equalsIgnoreCase("n"))) { 143 throw new IllegalArgumentException (theMessage); 144 } 145 } 146 147 153 public static final boolean toBoolean(String theString) { 154 boolean result = false; 155 if (theString != null) { 156 result = theString.equalsIgnoreCase("true") 158 || theString.equals("1") 159 || theString.equalsIgnoreCase("yes") 160 || theString.equalsIgnoreCase("y") 161 || theString.equalsIgnoreCase("on") 162 || theString.equalsIgnoreCase("set"); 163 } 164 165 return result; 166 } 167 168 174 public static String notNull(SerializableString theString) { 175 if (theString == null) { 176 return EMPTY_STRING; 177 } 178 179 return theString.toString(); 180 } 181 182 195 public static String numberToLetter(int number, boolean upperCaseFlag) 196 throws IllegalArgumentException { 197 198 if (number < 1 || number > 26) { 200 throw new IllegalArgumentException ("The number is out of the proper range (1 to " + 201 "26) to be converted to a letter."); 202 } 203 204 int modnumber = number + 9; 205 char thechar = Character.forDigit(modnumber, 36); 206 207 if (upperCaseFlag) { 208 thechar = Character.toUpperCase(thechar); 209 } 210 211 return "" + thechar; 212 } 213 214 215 223 public static String replace(String s, String sub, String with) { 224 if (s == null) { 225 return null; 226 } 227 int c = 0; 228 int i = s.indexOf(sub, c); 229 230 if (i == -1) { 231 return s; 232 } 233 234 FastStringBuffer buf = new FastStringBuffer(s.length() + with.length()); 235 236 do { 237 buf.append(s.substring(c, i)); 238 buf.append(with); 239 c = i + sub.length(); 240 } while ((i = s.indexOf(sub, c)) != -1); 241 242 if (c < s.length()) { 243 buf.append(s.substring(c, s.length())); 244 } 245 246 return buf.toString(); 247 } 248 249 257 public static String xmlEscape(String s) { 258 int length = s.length(); 259 FastStringBuffer fsb = new FastStringBuffer(length); 260 261 for (int i = 0; i < length; i++) { 262 fsb = printEscaped(s.charAt(i), fsb); 263 } 264 265 return fsb.toString(); 266 } 267 268 275 protected static FastStringBuffer printEscaped(char ch, 276 FastStringBuffer fsb) { 277 String charRef; 278 279 charRef = getEntityRef(ch); 284 285 if (charRef != null) { 286 fsb.append('&'); 287 fsb.append(charRef); 288 fsb.append(';'); 289 290 } else if ((ch >= ' ' && ch < 0xFF && ch != 0xF7) || ch == '\n' || 292 ch == '\r' || ch == '\t') { 293 294 if (ch < 0x10000) { 298 fsb.append(ch); 299 } else { 300 fsb.append((char) ((((int) ch - 0x10000) >> 10) + 0xd800)); 301 fsb.append((char) ((((int) ch - 0x10000) & 0x3ff) + 0xdc00)); 302 } 303 } else { 304 fsb.append("&#x"); 305 fsb.append(Integer.toHexString(ch)); 306 fsb.append(';'); 307 } 308 309 return fsb; 310 } 311 312 319 protected static String getEntityRef(int ch) { 320 321 switch (ch) { 324 case '<': 325 return "lt"; 326 327 case '>': 328 return "gt"; 329 330 case '"': 331 return "quot"; 332 333 case '\'': 334 return "apos"; 335 336 case '&': 337 return "amp"; 338 } 339 340 return null; 341 } 342 343 347 public static final String ELLIPSES = "…"; 348 349 358 public static String truncate(String str, int len) { 359 String result = str; 360 if (str.length() > len) { 361 result = str.substring(0, len) + ELLIPSES; 362 } 363 return result; 364 } 365 366 367 374 public static Hashtable characterMap() { 375 Hashtable characterMap = new Hashtable (); 376 377 characterMap.put(new Character ('à'), new Character ('a')); 378 characterMap.put(new Character ('â'), new Character ('a')); 379 characterMap.put(new Character ('ä'), new Character ('a')); 380 characterMap.put(new Character ('á'), new Character ('a')); 381 characterMap.put(new Character ('À'), new Character ('A')); 382 characterMap.put(new Character ('Á'), new Character ('A')); 383 characterMap.put(new Character ('Â'), new Character ('A')); 384 characterMap.put(new Character ('Ä'), new Character ('A')); 385 characterMap.put(new Character ('è'), new Character ('e')); 386 characterMap.put(new Character ('é'), new Character ('e')); 387 characterMap.put(new Character ('ê'), new Character ('e')); 388 characterMap.put(new Character ('ë'), new Character ('e')); 389 characterMap.put(new Character ('È'), new Character ('E')); 390 characterMap.put(new Character ('É'), new Character ('E')); 391 characterMap.put(new Character ('Ê'), new Character ('E')); 392 characterMap.put(new Character ('Ë'), new Character ('E')); 393 characterMap.put(new Character ('î'), new Character ('i')); 394 characterMap.put(new Character ('ï'), new Character ('i')); 395 characterMap.put(new Character ('Î'), new Character ('I')); 396 characterMap.put(new Character ('Ï'), new Character ('I')); 397 characterMap.put(new Character ('ô'), new Character ('o')); 398 characterMap.put(new Character ('ö'), new Character ('o')); 399 characterMap.put(new Character ('Ô'), new Character ('O')); 400 characterMap.put(new Character ('Ö'), new Character ('O')); 401 characterMap.put(new Character ('û'), new Character ('u')); 402 characterMap.put(new Character ('ü'), new Character ('u')); 403 characterMap.put(new Character ('Û'), new Character ('U')); 404 characterMap.put(new Character ('Ü'), new Character ('U')); 405 characterMap.put(new Character ('ç'), new Character ('c')); 406 characterMap.put(new Character ('Ç'), new Character ('C')); 407 408 return characterMap; 409 } 410 411 420 public static String removeAccents(String s) { 421 String out = null; 422 if (s != null) { 423 Hashtable charRemove = characterMap(); 424 StringBuffer sb = new StringBuffer (); 425 for (int i = 0; i < s.length(); i++) { 426 Character c = new Character (s.charAt(i)); 427 if (charRemove.containsKey(c)) { 428 c = (Character ) charRemove.get(c); 429 } 430 sb.append(c.charValue()); 431 } 432 out = sb.toString(); 433 } 434 return out; 435 } 436 437 438 446 public static String convertCommaToDot(String str) { 447 if (str == null) { 448 return null; 449 } 450 451 return str.replace(',', '.'); 452 } 453 454 463 public static int compareDecimals(String decim1, String decim2) { 464 BigDecimal dec1 = new BigDecimal (decim1); 465 BigDecimal dec2 = new BigDecimal (decim2); 466 return dec1.compareTo(dec2); 467 } 468 469 478 public static int compareIntegers(String int1, String int2) { 479 BigInteger dec1 = new BigInteger (int1); 480 BigInteger dec2 = new BigInteger (int2); 481 return dec1.compareTo(dec2); 482 } 483 484 492 public static boolean isAlphaNumeric(String s) { 493 return isAlphaNumeric(s, ""); 494 } 495 496 505 public static boolean isAlphaNumeric(String str, String otherChars) { 506 String alphaNum = 507 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + otherChars; 508 for (int i = 0; i < str.length(); i++) { 509 if (alphaNum.indexOf(str.charAt(i)) == -1) { 510 return false; 511 } 512 } 513 return true; 514 } 515 516 524 public static boolean isBlankOrNull(String str) { 525 if (str == null) { 526 return true; 527 } 528 if (str.trim().length() < 1) { 529 return true; 530 } else { 531 return false; 532 } 533 } 534 535 543 public static boolean isDecimal(String s) { 544 try { 545 new BigDecimal (s); 546 return true; 547 } catch (NumberFormatException e) { 548 return false; 549 } 550 } 551 552 560 public static int isEmail(String email) { 561 StringTokenizer st = new StringTokenizer (email, "@"); 562 563 if (st.countTokens() != 2) { 564 return 1; 565 } 566 567 st.nextToken(); 568 if (st.nextToken().indexOf(".") == -1) { 569 return 2; 570 } 571 572 return 0; 573 } 574 575 583 public static boolean isInteger(String str) { 584 try { 585 new BigInteger (str); 586 return true; 587 } catch (NumberFormatException e) { 588 return false; 589 } 590 } 591 592 599 public static boolean isNumber(String str) { 600 try { 601 Double.parseDouble(str); 602 return true; 603 } catch (NumberFormatException ex) { 604 return false; 605 } 606 } 607 608 616 public static boolean isSiren(String siren) { 617 StringBuffer temp = new StringBuffer (""); 618 int value = 0; 619 620 try { 622 Double.parseDouble(siren); 623 } catch (NumberFormatException nbe) { 624 return false; 625 } 626 627 if (siren.length() != 9) { 629 return false; 630 } 631 632 for (int i = 0; i < 9; i++) { 634 int n = 635 Integer.parseInt(siren.substring(i, i + 1)) * (((i % 2) != 0) ? 2 : 1); 636 temp.append(n); 637 } 638 639 for (int i = 0; i < (temp.length()); i++) { 641 value += Integer.parseInt(temp.substring(i, i + 1)); 642 } 643 644 if ((value % 10) != 0) { 646 return false; 647 } 648 649 return true; 650 } 651 652 660 public static boolean isSiret(String siret) { 661 if (siret.length() != 14) { 663 return false; 664 } 665 666 try { 668 Double.parseDouble(siret); 669 return isSiren(siret.substring(0, 9)); 670 } catch (NumberFormatException nfe) { 671 return false; 672 } 673 } 674 675 683 public static String nullToEmpty(String s) { 684 if (s == null) { 685 return ""; 686 } else { 687 return s; 688 } 689 } 690 691 699 public static String reformatDecimalString(String str) { 700 if (str == null) { 701 return null; 702 } 703 704 str = str.replace(',', '.'); 706 try { 707 Double.parseDouble(str); 708 return str; 709 } catch (NumberFormatException nbe) { 710 return ""; 711 } 712 } 713 714 722 public static String convertDotToComma(String str) { 723 return str.replace('.', ','); 724 } 725 726 736 public static String replaceAll(String text, String key, String value) { 737 if (text == null) { 738 return null; 739 } 740 741 String buffer = text; 742 if (buffer != null && key != null && value != null) { 743 int length = key.length(); 744 for (int start = buffer.indexOf(key); 745 start != -1; 746 start = buffer.indexOf(key, start + value.length())) { 747 buffer = buffer.substring(0, start) + value + buffer.substring(start + length); 748 } 749 } 750 return buffer; 751 } 752 753 764 static public String replaceStringOnce(String text, String str1, String str2) { 765 return replaceString(text, str1, str2, 1); 766 } 767 768 778 static public String replaceString(String text, String str1, String str2) { 779 return replaceString(text, str1, str2, -1); 780 } 781 782 794 static public String replaceString(String text, String str1, String str2, int max) { 795 if (text == null) { 796 return null; 797 } 798 799 StringBuffer buffer = new StringBuffer (text.length()); 800 int start = 0; 801 int end = 0; 802 while ((end = text.indexOf(str1, start)) != -1) { 803 buffer.append(text.substring(start, end)).append(str2); 804 start = end + str1.length(); 805 if (--max == 0) { 806 break; 807 } 808 } 809 buffer.append(text.substring(start)); 810 return buffer.toString(); 811 } 812 813 821 public static List string2List(String s) { 822 return string2List(s, ","); 823 } 824 825 834 public static List string2List(String s, String sep) { 835 return string2List(s, sep, s != null ? s.length() : Integer.MAX_VALUE); 836 } 837 838 848 public static List string2List(String s, String sep, int maxSize) { 849 List l = null; 850 if (s != null) { 851 l = new Vector (); 852 for (int i = 0; i < maxSize;) { 853 int index = s.indexOf(sep, i); 854 String token; 855 if (index != -1) { 856 token = s.substring(i, index); 857 } else { 858 token = s.substring(i); 859 } 860 if (token.length() > 0 && !token.equals(sep)) { 861 l.add(token.trim()); 862 } 863 i += token.length() + sep.length(); 864 } 865 } 866 return l; 867 } 868 869 877 static public String unUpperFirstChar(String str) { 878 if (str == null) { 879 return null; 880 } 881 882 FastStringBuffer fsb = null; 883 ; 884 try { 885 fsb = FastStringBuffer.getInstance(); 886 fsb.append(Character.toLowerCase(str.charAt(0))); 887 fsb.append(str.substring(1)); 888 return fsb.toString(); 889 } finally { 890 if (fsb != null) { 891 fsb.release(); 892 } 893 } 894 } 895 896 904 static public String upperFirstChar(String str) { 905 if (str == null) { 906 return null; 907 } 908 909 FastStringBuffer fsb = FastStringBuffer.getInstance(); 910 try { 911 fsb.append(Character.toTitleCase(str.charAt(0))); 912 fsb.append(str.substring(1)); 913 return fsb.toString(); 914 } finally { 915 fsb.release(); 916 } 917 918 } 920 921 930 static public String repeatString(String str, int n) { 931 if (str == null) { 932 return null; 933 } 934 935 FastStringBuffer buffer = FastStringBuffer.getInstance(); 936 try { 937 int val = n * str.length(); 940 if (val > buffer.capacity()) { 941 buffer.ensureCapacity(val); 942 } 943 for (int i = 0; i < n; i++) { 945 buffer.append(str); 946 } 947 return buffer.toString(); 948 } finally { 949 buffer.release(); 950 } 951 } 952 953 963 static public String centerPad(String str, int n) { 964 return centerPad(str, n, " "); 965 } 966 967 977 static public String centerPad(String str, int n, String delim) { 978 if (str == null) { 979 return null; 980 } 981 982 int sz = str.length(); 983 int p = n - sz; 984 if (p < 1) { 985 return str; 986 } 987 str = leftPad(str, sz + p / 2, delim); 988 str = rightPad(str, n, delim); 989 return str; 990 } 991 992 1002 static public String rightPad(String str, int n, String delim) { 1003 if (str == null) { 1004 return null; 1005 } 1006 1007 int sz = str.length(); 1008 n = (n - sz) / delim.length(); 1009 if (n > 0) { 1010 str += repeatString(delim, n); 1011 } 1012 return str; 1013 } 1014 1015 1025 static public String rightPad(String str, int n) { 1026 return rightPad(str, n, " "); 1027 } 1028 1029 1039 static public String leftPad(String str, int n) { 1040 return leftPad(str, n, " "); 1041 } 1042 1043 1053 static public String leftPad(String str, int n, String delim) { 1054 if (str == null) { 1055 return null; 1056 } 1057 int sz = str.length(); 1058 n = (n - sz) / delim.length(); 1059 if (n > 0) { 1060 str = repeatString(delim, n) + str; 1061 } 1062 return str; 1063 } 1064 1065 1071 static public String reverseString(String str) { 1072 if (str == null) { 1073 return null; 1074 } 1075 FastStringBuffer fsb = FastStringBuffer.getInstance(); 1076 try { 1077 fsb.append(str); 1078 return fsb.reverse().toString(); 1079 } finally { 1080 fsb.release(); 1081 } 1082 } 1083 1084 1092 static public String swapCase(String str) { 1093 if (str == null) { 1094 return null; 1095 } 1096 1097 int sz = str.length(); 1098 FastStringBuffer buffer = FastStringBuffer.getInstance(); 1099 try { 1101 if (sz > buffer.capacity()) { 1102 buffer.ensureCapacity(sz); 1103 } 1104 boolean whiteSpace = false; 1105 char ch = 0; 1106 char tmp = 0; 1107 for (int i = 0; i < sz; i++) { 1108 ch = str.charAt(i); 1109 if (Character.isUpperCase(ch)) { 1110 tmp = Character.toLowerCase(ch); 1111 } else if (Character.isTitleCase(ch)) { 1112 tmp = Character.toLowerCase(ch); 1113 } else if (Character.isLowerCase(ch)) { 1114 if (whiteSpace) { 1115 tmp = Character.toTitleCase(ch); 1116 } else { 1117 tmp = Character.toUpperCase(ch); 1118 } 1119 } 1120 buffer.append(tmp); 1121 whiteSpace = Character.isWhitespace(ch); 1122 } 1123 return buffer.toString(); 1124 } finally { 1125 buffer.release(); 1126 } 1127 } 1128 1129 1136 static public String random(int count) { 1137 return random(count, false, false); 1138 } 1139 1140 1147 static public String randomAscii(int count) { 1148 return random(count, 32, 127, false, false); 1149 } 1150 1151 1158 static public String randomAlphabetic(int count) { 1159 return random(count, true, false); 1160 } 1161 1162 1169 static public String randomAlphanumeric(int count) { 1170 return random(count, true, true); 1171 } 1172 1173 1180 static public String randomNumeric(int count) { 1181 return random(count, false, true); 1182 } 1183 1184 1195 static public String random(int count, boolean letters, boolean numbers) { 1196 return random(count, 0, 0, letters, numbers); 1197 } 1198 1199 1212 static public String random(int count, int start, int end, boolean letters, boolean numbers) { 1213 return random(count, start, end, letters, numbers, null); 1214 } 1215 1216 1232 static public String random(int count, int start, int end, boolean letters, boolean numbers, char[] set) { 1233 if ((start == 0) && (end == 0)) { 1234 end = (int) 'z'; 1235 start = (int) ' '; 1236 if (!letters && !numbers) { 1237 start = 0; 1238 end = Integer.MAX_VALUE; 1239 } 1240 } 1241 Random rnd = new Random (); 1242 FastStringBuffer buffer = FastStringBuffer.getInstance(); 1244 try { 1245 int gap = end - start; 1246 while (count-- != 0) { 1247 char ch; 1248 if (set == null) { 1249 ch = (char) (rnd.nextInt(gap) + start); 1250 } else { 1251 ch = set[rnd.nextInt(gap) + start]; 1252 } 1253 if ((letters && numbers && Character.isLetterOrDigit(ch)) 1254 || (letters && Character.isLetter(ch)) 1255 || (numbers && Character.isDigit(ch)) 1256 || (!letters && !numbers)) { 1257 buffer.append(ch); 1258 } else { 1259 count++; 1260 } 1261 } 1262 return buffer.toString(); 1263 } finally { 1264 buffer.release(); 1265 } 1266 } 1267 1268 1276 static public String random(int count, String set) { 1277 return random(count, set.toCharArray()); 1278 } 1279 1280 1288 static public String random(int count, char[] set) { 1289 return random(count, 0, set.length - 1, false, false, set); 1290 } 1291 1292 1301 public static String substring(String str, int lg) { 1302 return substring(str, 0, lg); 1303 } 1304 1305 1315 public static String substring(String str, int start, int end) { 1316 if (str == null || str.length() <= start) { 1317 return null; 1318 } else if (str.length() >= end) { 1319 return str.substring(start, end); 1320 } else { 1321 return str.substring(start); 1322 } 1323 } 1324 1325 1331 public static String omitPackages(Object obj) { 1332 int i = obj.getClass().getPackage().getName().length(); 1333 if (i > 0) { 1334 i++; 1335 } 1336 return obj.getClass().getName().substring(i); 1337 } 1338 1339 1345 public static String omitPackages(Class theclass) { 1346 int i = theclass.getPackage().getName().length(); 1347 if (i > 0) { 1348 i++; 1349 } 1350 return theclass.getName().substring(i); 1351 } 1352 1353 1354 1359 public static String getStackTraceAsString(Throwable t) { 1360 StringWriter swriter = new StringWriter (); 1361 PrintWriter pwriter = new PrintWriter (swriter); 1362 t.printStackTrace(pwriter); 1363 pwriter.flush(); 1364 return swriter.toString(); 1365 } 1366 1367 1375 public static String join(String [] input) { 1376 return join(input, ", "); 1377 } 1378 1379 1388 public static String join(String [] input, String delimiter) { 1389 if (input == null) { 1390 return ""; 1391 } 1392 1393 StringBuffer buf = new StringBuffer (); 1394 for (int q = 0; q < input.length; ++q) { 1395 if (q > 0) { 1396 buf.append(delimiter); 1397 } 1398 1399 buf.append(input[q]); 1400 } 1401 1402 return buf.toString(); 1403 } 1404 1405 1406 1419 public static String createWordWrappedString(String input) { 1420 return createWordWrappedString(input, 72, "\n"); 1421 } 1422 1423 1435 public static String createWordWrappedString(String input, int fixedLength, String delimiter) { 1436 int inputLength = input.length(); 1437 char c; 1438 1439 if (inputLength < fixedLength) { 1441 return input; 1442 } 1443 1444 StringBuffer buf = new StringBuffer (1024); 1446 int pos = 0; int lineLength = 0; while (pos < inputLength) { 1449 1451 c = input.charAt(pos); 1453 while (pos < inputLength && Character.isWhitespace(c)) { 1454 buf.append(c); 1455 if (c == '\t') 1456 { 1458 lineLength += 8; 1459 } else if (c == '\n') 1460 { 1462 lineLength = 0; 1463 } else 1464 { 1466 ++lineLength; 1467 } 1468 1469 ++pos; 1470 if (pos < inputLength) { 1471 c = input.charAt(pos); 1472 } 1473 } 1474 1475 StringBuffer word = new StringBuffer (64); 1477 if (pos < inputLength) { 1478 c = input.charAt(pos); 1479 } 1480 while (pos < inputLength && !Character.isWhitespace(c)) { 1481 word.append(c); 1482 ++pos; 1483 if (pos < inputLength) { 1484 c = input.charAt(pos); 1485 } 1486 } 1487 1488 int wordLength = word.length(); 1489 if (lineLength + wordLength >= fixedLength) { 1490 buf.append(delimiter); 1493 lineLength = 0; 1494 } 1495 1496 buf.append(word); 1498 lineLength += wordLength; 1499 } 1500 1501 return buf.toString(); 1502 } 1503 1504 public static void main(String args[]) { 1505 String text = "CAT SAT ON THE MAT"; 1507 1508 String textResult = createWordWrappedString(text, 12, "\n"); 1509 1510 System.out.println(textResult); 1511 1512 } 1513 1514 1520 public static String unquote(String s) { 1521 String result = s; 1522 if (startsAndEnds(s, "\"") || startsAndEnds(s, "'")) { 1523 result = s.substring(1, s.length() - 1); 1524 } 1525 1526 return result; 1527 } 1528 1529 1530 1537 public static boolean startsAndEnds(String s, String search) { 1538 if (s != null && s.length() >= 2 && s.startsWith(search) && s.endsWith(search)) { 1539 return true; 1540 } 1541 1542 return false; 1543 } 1544 1545} 1546 | Popular Tags |