1 45 46 package com.sslexplorer.util; 47 48 import java.io.EOFException ; 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 import java.io.PrintWriter ; 52 import java.io.StringWriter ; 53 import java.io.Writer ; 54 import java.util.Arrays ; 55 import java.util.Collection ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 import java.util.Map ; 59 60 public class Utils { 61 62 68 public static String zerofill(int x, int desiredWidth) { 69 StringBuffer buf = new StringBuffer (); 70 if (x < 0) { 71 buf.append("-"); 72 desiredWidth--; 73 x = -x; 74 } 75 while (desiredWidth>7) { 76 buf.append("0"); 77 desiredWidth--; 78 } 79 switch (desiredWidth) { 80 case 7: 81 if (x<1000000) buf.append("0"); 82 case 6: 83 if (x<100000) buf.append("0"); 84 case 5: 85 if (x<10000) buf.append("0"); 86 case 4: 87 if (x<1000) buf.append("0"); 88 case 3: 89 if (x<100) buf.append("0"); 90 case 2: 91 if (x<10) buf.append( "0" ); 92 } 93 buf.append(x); 94 return buf.toString(); 95 } 96 97 public static void printIndent(PrintWriter out, int indent) { 98 out.print(indent(indent)); 99 } 100 101 public static String indent(int indent) { 102 switch (indent) { 103 case 8: 104 return(" "); 105 case 7: 106 return(" "); 107 case 6: 108 return(" "); 109 case 5: 110 return(" "); 111 case 4: 112 return(" "); 113 case 3: 114 return(" "); 115 case 2: 116 return(" "); 117 case 1: 118 return(" "); 119 default: 120 StringBuffer buf = new StringBuffer (); 121 for (int i=0; i<indent; ++i) { buf.append(" "); } 122 return buf.toString(); 123 } 124 } 125 126 129 public static String commaList(Object [] a) { 130 return commaList(Arrays.asList(a).iterator()); 131 } 132 133 136 public static String commaList(Collection c) { 137 return commaList(c.iterator()); 138 } 139 140 143 public static String commaList(Iterator i) { 144 StringWriter sw = new StringWriter (); 145 PrintWriter pw = new PrintWriter (sw); 146 printCommaList(pw, i); 147 pw.close(); 148 return sw.toString(); 149 } 150 151 152 161 public static void printCommaList(PrintWriter out, Iterator i) { 162 boolean first = true; 163 while (i.hasNext()) { 164 if (first) first = false; 165 else out.print(", "); 166 out.print(i.next()); 167 } 168 } 169 170 173 public static boolean isWhitespace(String s) { 174 for (int i=0; i<s.length(); ++i) { 175 if (!Character.isWhitespace(s.charAt(i))) return false; 176 } 177 return true; 178 } 179 180 184 public static class ExecInfo { 185 public int exit; 186 public String stdout; 187 public String stderr; 188 189 public String toString() { 190 return "ExecInfo[exit=" + exit + "," + 191 "stdout=" + javaEscape(stdout) + "," + 192 "stderr=" + javaEscape(stderr) + "]"; 193 } 194 } 195 196 204 244 245 257 public static String abbreviate(String s, int max) { 258 if (max < 4) 259 throw new IllegalArgumentException ("Minimum abbreviation is 3 chars"); 260 if (s.length() <= max) return s; 261 return s.substring(0, max-3) + "..."; 263 } 264 265 268 public static String pad(String s, int length) { 269 if (s.length() < length) return s + indent(length - s.length()); 270 else return s.substring(0,length); 271 } 272 273 281 public static String strdiff(String s1, String s2) { 282 int at = strdiffat(s1, s2); 283 if (at == -1) 284 return ""; 285 return s2.substring(at); 286 } 287 288 294 public static int strdiffat(String s1, String s2) 295 { 296 int i; 297 for (i=0; i<s1.length() && i<s2.length(); ++i) 298 { 299 if (s1.charAt(i) != s2.charAt(i)) { 300 break; 301 } 302 } 303 if (i<s2.length() || i<s1.length()) 304 return i; 305 return -1; 306 } 307 308 320 321 public static String strdiffVerbose(String expected, String actual) 322 { 323 int at = Utils.strdiffat(actual, expected); 324 if (at == -1) 325 return null; 326 int length = 60; int back = 20; int start = at - back; 329 if (start < 3) start = 0; 330 331 StringBuffer buf = new StringBuffer (length*2 + 100); 332 buf.append("strings differ at character ").append(at); 333 buf.append("\n"); 334 335 buf.append("Expected: "); 336 appendWithEllipses(buf, expected, start, length); 337 buf.append("\n"); 338 339 buf.append(" Actual: "); 340 appendWithEllipses(buf, actual, start, length); 341 buf.append("\n"); 342 343 return buf.toString(); 344 } 345 346 private static void appendWithEllipses(StringBuffer buf, String s, int start, int length) 347 { 348 if (start > 0) buf.append("..."); 349 buf.append 350 (javaEscape (abbreviate (s.substring(start), length))); 353 } 354 355 359 public static int count(String s, char ch) { 360 int c=0; 361 for (int i=0; i<s.length(); ++i) { 362 if (s.charAt(i) == ch) c++; 363 } 364 return c; 365 } 366 367 371 public static String replace(String source, String target, String replacement) 372 { 373 StringBuffer result = new StringBuffer (source.length()); 375 int i = 0, j = 0; 376 int len = source.length(); 377 while (i < len) { 378 j = source.indexOf(target, i); 379 if (j == -1) { 380 result.append( source.substring(i,len) ); 381 break; 382 } 383 else { 384 result.append( source.substring(i,j) ); 385 result.append( replacement ); 386 i = j + target.length(); 387 } 388 } 389 return result.toString(); 390 } 391 392 400 public static String rtrim(String orig) { 401 int len = orig.length(); 402 int st = 0; 403 int off = 0; 404 char[] val = orig.toCharArray(); 405 406 while ((st < len) && (val[off + len - 1] <= ' ')) { 407 len--; 408 } 409 return ((st > 0) || (len < orig.length())) ? orig.substring(st, len) : orig; 410 } 411 412 420 public static String ltrim(String orig) { 421 int len = orig.length(); 422 int st = 0; 423 int off = 0; 424 char[] val = orig.toCharArray(); 425 426 while ((st < len) && (val[off + st] <= ' ')) { 427 st++; 428 } 429 return ((st > 0) || (len < orig.length())) ? orig.substring(st, len) : orig; 430 } 431 432 436 public static int getMaxLength(Iterator i) { 437 int max = 0; 438 while (i.hasNext()) { 439 String s = i.next().toString(); 440 int c = s.length(); 441 if (c>max) max=c; 442 } 443 return max; 444 } 445 446 static Object [][] entities = { 448 {"quot", new Integer (34)}, {"amp", new Integer (38)}, {"lt", new Integer (60)}, {"gt", new Integer (62)}, {"nbsp", new Integer (160)}, {"copy", new Integer (169)}, {"reg", new Integer (174)}, {"Agrave", new Integer (192)}, {"Aacute", new Integer (193)}, {"Acirc", new Integer (194)}, {"Atilde", new Integer (195)}, {"Auml", new Integer (196)}, {"Aring", new Integer (197)}, {"AElig", new Integer (198)}, {"Ccedil", new Integer (199)}, {"Egrave", new Integer (200)}, {"Eacute", new Integer (201)}, {"Ecirc", new Integer (202)}, {"Euml", new Integer (203)}, {"Igrave", new Integer (204)}, {"Iacute", new Integer (205)}, {"Icirc", new Integer (206)}, {"Iuml", new Integer (207)}, {"ETH", new Integer (208)}, {"Ntilde", new Integer (209)}, {"Ograve", new Integer (210)}, {"Oacute", new Integer (211)}, {"Ocirc", new Integer (212)}, {"Otilde", new Integer (213)}, {"Ouml", new Integer (214)}, {"Oslash", new Integer (216)}, {"Ugrave", new Integer (217)}, {"Uacute", new Integer (218)}, {"Ucirc", new Integer (219)}, {"Uuml", new Integer (220)}, {"Yacute", new Integer (221)}, {"THORN", new Integer (222)}, {"szlig", new Integer (223)}, {"agrave", new Integer (224)}, {"aacute", new Integer (225)}, {"acirc", new Integer (226)}, {"atilde", new Integer (227)}, {"auml", new Integer (228)}, {"aring", new Integer (229)}, {"aelig", new Integer (230)}, {"ccedil", new Integer (231)}, {"egrave", new Integer (232)}, {"eacute", new Integer (233)}, {"ecirc", new Integer (234)}, {"euml", new Integer (235)}, {"igrave", new Integer (236)}, {"iacute", new Integer (237)}, {"icirc", new Integer (238)}, {"iuml", new Integer (239)}, {"igrave", new Integer (236)}, {"iacute", new Integer (237)}, {"icirc", new Integer (238)}, {"iuml", new Integer (239)}, {"eth", new Integer (240)}, {"ntilde", new Integer (241)}, {"ograve", new Integer (242)}, {"oacute", new Integer (243)}, {"ocirc", new Integer (244)}, {"otilde", new Integer (245)}, {"ouml", new Integer (246)}, {"oslash", new Integer (248)}, {"ugrave", new Integer (249)}, {"uacute", new Integer (250)}, {"ucirc", new Integer (251)}, {"uuml", new Integer (252)}, {"yacute", new Integer (253)}, {"thorn", new Integer (254)}, {"yuml", new Integer (255)}, {"euro", new Integer (8364)}, }; 524 static Map e2i = new HashMap (); 525 static Map i2e = new HashMap (); 526 static { 527 for (int i=0; i<entities.length; ++i) { 528 e2i.put(entities[i][0], entities[i][1]); 529 i2e.put(entities[i][1], entities[i][0]); 530 } 531 } 532 533 539 public static String htmlescape(String s1) 540 { 541 StringBuffer buf = new StringBuffer (); 542 int i; 543 for (i=0; i<s1.length(); ++i) { 544 char ch = s1.charAt(i); 545 String entity = (String )i2e.get( new Integer ((int)ch) ); 546 if (entity == null) { 547 if (((int)ch) > 128) { 548 buf.append("&#" + ((int)ch) + ";"); 549 } 550 else { 551 buf.append(ch); 552 } 553 } 554 else { 555 buf.append("&" + entity + ";"); 556 } 557 } 558 return buf.toString(); 559 } 560 561 572 public static String htmlunescape(String s1) { 573 StringBuffer buf = new StringBuffer (); 574 int i; 575 for (i=0; i<s1.length(); ++i) { 576 char ch = s1.charAt(i); 577 if (ch == '&') { 578 int semi = s1.indexOf(';', i+1); 579 if (semi == -1) { 580 buf.append(ch); 581 continue; 582 } 583 String entity = s1.substring(i+1, semi); 584 Integer iso; 585 if (entity.charAt(0) == '#') { 586 iso = new Integer (entity.substring(1)); 587 } 588 else { 589 iso = (Integer )e2i.get(entity); 590 } 591 if (iso == null) { 592 buf.append("&" + entity + ";"); 593 } 594 else { 595 buf.append((char)(iso.intValue())); 596 } 597 i = semi; 598 } 599 else { 600 buf.append(ch); 601 } 602 } 603 return buf.toString(); 604 } 605 606 621 public static String jsEscape(String source) { 622 try { 623 StringWriter sw = new StringWriter (); 624 jsEscape(source, sw); 625 sw.flush(); 626 return sw.toString(); 627 } 628 catch (IOException ioe) { 629 ioe.printStackTrace(); 631 return null; 632 } 633 } 634 635 638 public static String javaEscape(String source) { 639 try { 640 StringWriter sw = new StringWriter (); 641 javaEscape(source, sw); 642 sw.flush(); 643 return sw.toString(); 644 } 645 catch (IOException ioe) { 646 ioe.printStackTrace(); 648 return null; 649 } 650 } 651 652 public static String readLine(InputStream in) throws IOException , EOFException { 653 654 StringBuffer buf = new StringBuffer (); 655 656 int ch; 657 658 while((ch = in.read()) > -1) { 659 660 if(ch=='\n') 661 break; 662 buf.append((char)ch); 663 } 664 665 if(ch != '\n' && buf.length()==0) 666 return null; 667 668 669 return buf.toString(); 670 } 671 672 673 687 public static void jsEscape(String source, Writer out) throws IOException { 688 stringEscape(source, out, true); 689 } 690 691 704 public static void javaEscape(String source, Writer out) throws IOException { 705 stringEscape(source, out, false); 706 } 707 708 private static void stringEscape(String source, Writer out, boolean escapeSingleQuote) throws IOException { 709 char[] chars = source.toCharArray(); 710 for (int i=0; i<chars.length; ++i) { 711 char ch = chars[i]; 712 switch (ch) { 713 case '\b': out.write("\\b"); 715 break; 716 case '\t': out.write("\\t"); 718 break; 719 case '\n': out.write("\\n"); 721 break; 722 case 11: out.write("\\v"); 724 break; 725 case '\f': out.write("\\f"); 727 break; 728 case '\r': out.write("\\r"); 730 break; 731 case '"': out.write("\\\""); 733 break; 734 case '\'': if (escapeSingleQuote) out.write("\\'"); 736 else out.write("'"); 737 break; 738 case '\\': out.write("\\\\"); 740 break; 741 default: 742 if ((int) ch < 32 || (int) ch > 127) 743 { 744 out.write("\\u"); 745 zeropad(out, Integer.toHexString(ch).toUpperCase(), 4); 746 } 747 else 748 { 749 out.write(ch); 750 } 751 break; 752 } 753 } 754 } 755 756 private static void zeropad(Writer out, String s, int width) throws IOException 757 { 758 while (width > s.length()) { 759 out.write('0'); 760 width--; 761 } 762 out.write(s); 763 } 764 765 766 778 public static String uncurlQuotes(String input) 779 { 780 if (input==null) 781 return ""; 782 StringBuffer sb = new StringBuffer (); 783 for (int i = 0; i < input.length(); i++) 784 { 785 char ch = input.charAt(i); 786 int code = (int) ch; 787 if (code == 210 || code == 211 || code == 147 || code == 148) 788 { 789 ch = (char) 34; } 791 else if (code == 212 || code == 213 || code == 145 || code == 146) 792 { 793 ch = (char) 39; } 795 sb.append(ch); 796 } 797 return sb.toString(); 798 } 799 800 803 public static String capitalize(String s) { 804 return s.substring(0,1).toUpperCase() + s.substring(1); 805 } 806 807 810 public static String lowerize(String s) { 811 return s.substring(0,1).toLowerCase() + s.substring(1); 812 } 813 814 818 public static String pluralize(String s) { 819 if (s.endsWith("y")) 820 return s.substring(0, s.length()-1) + "ies"; 821 822 else if (s.endsWith("s")) 823 return s + "es"; 824 825 else 826 return s + "s"; 827 } 828 829 public static boolean ok(String s) { 830 return (!(s == null || s.equals(""))); 831 } 832 833 836 public static String toUnderscore(String s) { 837 StringBuffer buf = new StringBuffer (); 838 char[] ch = s.toCharArray(); 839 for (int i=0; i<ch.length; ++i) { 840 if (Character.isUpperCase(ch[i])) { 841 buf.append('_'); 842 buf.append(Character.toLowerCase(ch[i])); 843 } 844 else { 845 buf.append(ch[i]); 846 } 847 } 848 return buf.toString(); 850 } 851 852 855 public static String stripWhitespace(String s) { 856 StringBuffer buf = new StringBuffer (); 857 char[] ch = s.toCharArray(); 858 for (int i=0; i<ch.length; ++i) { 859 if (Character.isWhitespace(ch[i])) { 860 continue; 861 } 862 else { 863 buf.append(ch[i]); 864 } 865 } 866 return buf.toString(); 867 } 868 869 public static String getStackTrace(Throwable t) { 870 StringWriter s = new StringWriter (); 871 PrintWriter p = new PrintWriter (s); 872 t.printStackTrace(p); 873 p.close(); 874 return s.toString(); 875 } 876 877 public static void sleep(long msec) { 878 try { 879 Thread.sleep(msec); 880 } 881 catch (InterruptedException ie) {} 882 } 883 } 885 | Popular Tags |