1 28 29 package com.idaremedia.antx.helpers; 30 31 import java.lang.reflect.Field ; 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.Modifier ; 34 import java.io.BufferedReader ; 35 import java.io.File ; 36 import java.io.InputStream ; 37 import java.io.InputStreamReader ; 38 import java.io.OutputStream ; 39 import java.net.URL ; 40 import java.util.ArrayList ; 41 import java.util.Collections ; 42 import java.util.List ; 43 import java.util.Locale ; 44 import java.util.Map ; 45 import java.util.StringTokenizer ; 46 47 import org.apache.tools.ant.Location; 48 import org.apache.tools.ant.Project; 49 import org.apache.tools.ant.taskdefs.Property; 50 import org.apache.tools.ant.types.EnumeratedAttribute; 51 import org.apache.tools.ant.util.LoaderUtils; 52 53 import com.idaremedia.apis.FixtureStarter; 54 55 import com.idaremedia.antx.apis.Requester; 56 57 67 68 public final class Tk 69 { 70 71 public static final int NO_INT= Integer.MAX_VALUE; 72 73 74 public static final long NO_NUM= Long.MAX_VALUE; 75 76 77 81 public static final Long NO_NUM_NUM= new Long (Long.MAX_VALUE); 82 83 84 85 92 public static boolean isWhitespace(String s) 93 { 94 int n=0; 95 if (s==null || (n=s.length())==0) { 96 return true; 97 } 98 for (int i=0; i<n;i++) { 99 if (!Character.isWhitespace(s.charAt(i))) { 100 return false; 101 } 102 } 103 return true; 104 } 105 106 107 108 115 public static String stripWhitespace(String s) 116 { 117 StringBuffer sb=null; 118 119 int n=0; 120 if (s==null || (n=s.length())==0) { 121 return s; 122 } 123 124 char c; 125 for (int i=0; i<n;i++) { 126 c = s.charAt(i); 127 if (!Character.isWhitespace(c)) { 128 if (sb==null) { 129 sb= new StringBuffer (n); 130 } 131 sb.append(c); 132 } 133 } 134 return (sb!=null) ? sb.toString() : s; 135 } 136 137 138 139 146 public final static List splitList(String delimitedList, String delim) 147 { 148 StringTokenizer st= new StringTokenizer (delimitedList,delim); 149 int N= st.countTokens(); 150 List l= new ArrayList (Math.max(N,4)); 151 while (st.hasMoreTokens()) { 152 l.add(st.nextToken()); 153 } 154 return l; 155 } 156 157 158 163 public final static List splitList(String delimitedList) 164 { 165 return splitList(delimitedList,","); 166 } 167 168 169 173 public static String cvlabelFrom(String iam) 174 { 175 if (iam==null) { 176 iam=""; 177 } else if (iam.endsWith(".")) { 178 iam = iam.substring(0,iam.length()-1)+":"; 179 } else if (!iam.endsWith(":")){ 180 iam += ":"; 181 } 182 return iam; 183 } 184 185 186 192 public static String lowercaseFrom(String s) 193 { 194 if (s==null) { 195 return null; 196 } 197 return String.valueOf(s).toLowerCase(Locale.US); 198 } 199 200 201 202 210 public static String lowercaseFromNoNull(String s) 211 { 212 return String.valueOf(s).toLowerCase(Locale.US); 213 } 214 215 216 217 222 public static String identityStringFrom(Object thing) 223 { 224 if (thing==null) { 225 return Strings.NULL; 226 } 227 return thing.getClass().getName() 228 +"@" 229 +System.identityHashCode(thing); 230 } 231 232 233 234 243 public static String stringFrom(Object thing, Project P) 244 { 245 try { 246 return String.valueOf(thing); 247 } catch(RuntimeException rtX) { 248 if (P!=null) { 249 P.log(rtX.getMessage(),Project.MSG_INFO); 250 } 251 return identityStringFrom(thing); 252 } 253 } 254 255 256 266 public static int integerFrom(Object o, int defi) 267 { 268 int i= defi; 269 if (o!=null) { 270 try { 271 if (o instanceof Number ) { 272 i= ((Number )o).intValue(); 273 } else if (o instanceof Boolean ) { 274 i= ((Boolean )o).booleanValue()==true ? 1 : 0; 275 } else { 276 i= Integer.decode(o.toString()).intValue(); 277 } 278 } 279 catch (NumberFormatException nx) {} 280 } 281 return i; 282 } 283 284 285 293 public static long longFrom(Object o, long defl) 294 { 295 long l= defl; 296 if (o!=null) { 297 try { 298 if (o instanceof Number ) { 299 l= ((Number )o).longValue(); 300 } else if (o instanceof Boolean ) { 301 l= ((Boolean )o).booleanValue()==true ? 1 : 0; 302 } else { 303 l= Long.decode(o.toString()).longValue(); 304 } 305 } 306 catch (NumberFormatException nx) {} 307 } 308 return l; 309 } 310 311 312 313 319 public static Number numberFrom(String valu) 320 { 321 Number n = Tk.NO_NUM_NUM; 322 long l = Tk.longFrom(valu,Tk.NO_NUM); 323 if (l!=Tk.NO_NUM) { 324 n = new Long (l); 325 } else { 326 try { 327 n = Double.valueOf(valu); 328 } catch(NumberFormatException nfx) {} 329 } 330 return n; 331 } 332 333 334 335 340 public static boolean booleanFrom(String s) 341 { 342 Boolean B = string2PosBool(s); 343 return (B==null) ? false : B.booleanValue(); 344 } 345 346 347 357 public static final Boolean string2PosBool(String s) 358 { 359 if (Tk.isWhitespace(s)) { 360 return null; 361 } 362 s= Tk.lowercaseFrom(s); 363 if (s.length()>4) { return Boolean.FALSE; 365 } 366 if (s.equals("true")) { 367 return Boolean.TRUE; 368 } 369 if (s.equals("1")) { 370 return Boolean.TRUE; 371 } 372 if (s.equals("on")) { 373 return Boolean.TRUE; 374 } 375 if (s.equals("yes")) { 376 return Boolean.TRUE; 377 } 378 if (s.equals("t")) { 379 return Boolean.TRUE; 380 } 381 if (s.equals("ok")) { 382 return Boolean.TRUE; 383 } 384 return Boolean.FALSE; 385 } 386 387 388 397 public static final Boolean string2NegBool(String s) 398 { 399 if (Tk.isWhitespace(s)) { 400 return null; 401 } 402 s= Tk.lowercaseFrom(s); 403 if (s.length()>5) { return Boolean.TRUE; 405 } 406 if (s.equals("false")) { 407 return Boolean.FALSE; 408 } 409 if (s.equals("0")) { 410 return Boolean.FALSE; 411 } 412 if (s.equals("off")) { 413 return Boolean.FALSE; 414 } 415 if (s.equals("no")) { 416 return Boolean.FALSE; 417 } 418 if (s.equals("none")) { 419 return Boolean.FALSE; 420 } 421 if (s.equals("f")) { 422 return Boolean.FALSE; 423 } 424 if (s.equals("notok")) { 425 return Boolean.FALSE; 426 } 427 return Boolean.TRUE; 428 } 429 430 431 438 public static String leafNameFrom(Class cls) 439 { 440 if (cls==null) { 441 throw new IllegalArgumentException ("leafFrom- NULL class"); 442 } 443 String cn = cls.getName(); 444 int i= cn.lastIndexOf('.'); 445 if (i != -1) { 446 return cn.substring(i+1).replace('$','-'); 447 } 448 return cn; 449 } 450 451 452 457 public static boolean initEnum(EnumeratedAttribute e, String ini) 458 { 459 try { 460 e.setValue(ini); 461 } catch(Exception x) { 462 return false; 463 } 464 return true; 465 } 466 467 468 469 472 public static String toFilename(URL url) 473 { 474 if (!url.getProtocol().equals("file")) { 475 if (url.getProtocol().equals("jar")) { 476 String fn = url.getPath(); 477 if (fn.startsWith("file:")) { 478 return fn.substring(5).replace 479 ('/', File.separatorChar).replace('|', ':'); 480 } 481 } 482 return null; 483 } 484 return url.getPath().replace('/', File.separatorChar).replace('|', ':'); 485 } 486 487 488 489 492 public static File toFile(URL url) 493 { 494 String fn = toFilename(url); 495 return (fn==null) ? null : new File (fn); 496 } 497 498 499 500 507 public static boolean closeQuietly(InputStream input) 508 { 509 try { 510 input.close(); 511 return true; 512 } catch(Exception ioX) { 513 return false; 514 } 515 } 516 517 518 519 526 public static boolean closeQuietly(OutputStream output) 527 { 528 try { 529 output.close(); 530 return true; 531 } catch(Exception ioX) { 532 return false; 533 } 534 } 535 536 537 538 545 public static void transferProperty(Property source, Property dest) 546 { 547 if (source==null || dest==null) { 548 throw new IllegalArgumentException ("transferProperty- NULL source or dest"); 549 } 550 551 if (source.getName()!=null) { dest.setName(source.getName()); 553 } 554 if (source.getRefid()!=null) { 555 dest.setRefid(source.getRefid()); 556 } 557 if (source.getValue()!=null) { dest.setValue(source.getValue()); 559 } 560 if (source.getFile()!=null) { 561 dest.setFile(source.getFile()); 562 } 563 if (source.getUrl()!=null) { 564 dest.setUrl(source.getUrl()); } 566 if (source.getResource()!=null) { 567 dest.setResource(source.getResource()); 568 } 569 if (source.getEnvironment()!=null) { 570 dest.setEnvironment(source.getEnvironment()); 571 } 572 if (source.getPrefix()!=null) { 573 dest.setPrefix(source.getPrefix()); 574 } 575 if (source.getClasspath()!=null) { 576 dest.setClasspath(source.getClasspath()); 577 } 578 579 } 580 581 582 583 591 public static final String getTheProperty(Project P, String property) 592 { 593 String value=null; 594 595 if (P!=null) { 596 value = P.getProperty(property); 597 } 598 if (value==null) { 599 value = FixtureStarter.getProperty(property); 600 } 601 return value; 602 } 603 604 605 606 617 public static final void setTheProperty(Project P, String property, 618 String value, boolean passthru) 619 { 620 if (P!=null) { 621 if (passthru) { 622 P.setUserProperty(property,value); } else { 624 P.setNewProperty(property, value); 625 } 626 } else { 627 System.setProperty(property,value); FixtureStarter.unsetProperty(property); } 630 } 631 632 633 637 public static String shortStringFrom(Location l) 638 { 639 if (l==null || Location.UNKNOWN_LOCATION.equals(l)) { 640 return Strings.UNDEFINED; 641 } 642 String fileline = l.toString(); 643 int i= fileline.lastIndexOf(':'); 644 if (i>0) { 645 fileline= fileline.substring(0,i); 646 } 647 int N= fileline.length(); 648 if (N>41) { 649 fileline= "..."+fileline.substring(N-38); 650 } 651 return fileline; 652 } 653 654 655 662 public static String shortStringFrom(boolean prefer, Location l) 663 { 664 if (prefer) { 665 return shortStringFrom(l); 666 } 667 if (Location.UNKNOWN_LOCATION.equals(l)) { 668 return Strings.UNDEFINED; 669 } 670 return l.toString(); 671 } 672 673 674 683 public static String resolveString(Project P, String value) 684 { 685 if (value==null || P==null) { 686 return value; 687 } 688 if (value.indexOf("${")>=0) { 689 value = P.replaceProperties(value); } 691 return value; 692 } 693 694 695 708 public static String resolveString(Project P, String value, 709 boolean altForm) 710 { 711 if (value==null || P==null) { 712 return value; 713 } 714 if (altForm) { 716 if (value.indexOf("@(")>=0) { 718 StringBuffer sb = new StringBuffer (value.length()); 719 char[] cs = getChars(value); 720 int i,j=0,n; 721 do { 722 i= value.indexOf("@(",j); 723 if (i>=0) { 724 n= value.indexOf(')',i+2); 725 if (n<0) { 726 return value; } 728 sb.append(cs,j,i-j); 729 sb.append("${"); 730 i += 2; 731 sb.append(cs,i,n-i); 732 sb.append("}"); 733 j= n+1; 734 } else { 735 sb.append(cs,j,cs.length-j); 736 break; 737 } 738 } while(true); 739 value = sb.toString(); 740 } 741 } 742 if (altForm && value.indexOf("@{",0)>=0) { StringBuffer sb = new StringBuffer (value.length()); 745 char[] cs = getChars(value); 746 int i=0,j=0; 747 do { 748 i= value.indexOf("@{",j); 749 if (i>=0) { 750 sb.append(cs,j,i-j); 751 sb.append("${"); 752 j= i+2; 753 } else { 754 sb.append(cs,j,cs.length-j); 755 break; 756 } 757 } while (true); 758 value = P.replaceProperties(sb.toString()); 759 760 } else { 761 if (value.indexOf("${",0)>=0) { 762 value = P.replaceProperties(value); 763 } 764 } 765 return value; 766 } 767 768 769 774 public static char[] getChars(String s) 775 { 776 char[] cs = new char[s.length()]; 777 s.getChars(0,s.length(),cs,0); 778 return cs; 779 } 780 781 782 783 795 public static Map readonlyFrom(Map in, boolean noNull) 796 { 797 if (in==null) { 798 return noNull ? Collections.EMPTY_MAP : null; 799 } 800 if (in==Collections.EMPTY_MAP) { 801 return in; 802 } 803 if (in.getClass()==READONLYMAP.getClass()) { 804 return in; 805 } 806 return Collections.unmodifiableMap(in); 807 } 808 809 810 811 826 public final static Object invokeit(Object object, String methodname, 827 Class [] signature, Object [] argv) 828 throws Exception 829 { 830 Class objectclass = object.getClass(); 831 Method method=null; 832 try { 833 method = objectclass.getMethod(methodname,signature); } catch(NoSuchMethodException nsmx) { 835 try { 836 method = objectclass.getDeclaredMethod(methodname,signature); } catch(NoSuchMethodException nsdmx) { 838 Class parent = objectclass.getSuperclass(); while (parent!=null) { 840 try { 841 method = parent.getDeclaredMethod(methodname,signature); 842 if (Modifier.isPrivate(method.getModifiers())) { method=null; 844 } 845 break; 846 } catch(NoSuchMethodException igx) {} 847 parent = parent.getSuperclass(); 848 } 849 if (method==null) { 850 throw nsmx; 851 } 852 } } 855 boolean is = method.isAccessible(); 856 if (!is) { 857 method.setAccessible(true); 858 } 859 try { 860 return method.invoke(object,argv); 861 } finally { 862 if (!is) { 863 method.setAccessible(false); 864 } 865 } 866 } 867 868 869 870 882 public static Field findField(Class clazz, final String fieldname) 883 throws NoSuchFieldException 884 { 885 if (clazz==null || isWhitespace(fieldname)) { 886 throw new IllegalArgumentException ("findField- invalid class or fieldname"); 887 } 888 do { 889 try { 890 return clazz.getDeclaredField(fieldname); 891 } catch(NoSuchFieldException nsfx) {} 892 893 clazz = clazz.getSuperclass(); 894 if (clazz==null) { 895 throw new NoSuchFieldException (fieldname); 896 } 897 } while(true); 898 } 899 900 901 902 912 public static boolean equalStrings(String a, String b, boolean caseins) 913 { 914 if (a==null) { 915 return b==null; 916 } 917 if (b==null) { 918 return false; 919 } 920 if (a==b) { 921 return true; 922 } 923 return caseins ? a.equalsIgnoreCase(b) : a.equals(b); 924 } 925 926 927 931 public static boolean equalStrings(String a, String b) 932 { 933 return equalStrings(a,b,false); 934 } 935 936 937 938 939 940 948 public static int hashOf(Object o) 949 { 950 if (o==null) { 951 return 191; } 953 if (o.getClass().isArray()) { 954 return o.getClass().getComponentType().hashCode(); 955 } 956 return o.hashCode(); 957 } 958 959 960 961 972 public final static String loadSpiImpl(String spiName, Requester clnt) 973 { 974 String classname = System.getProperty(spiName); 975 if (!Tk.isWhitespace(classname)) { 976 return classname; 977 } 978 979 String serviceId = "META-INF/services/"+spiName; 980 try { 981 ClassLoader cL = LoaderUtils.getContextClassLoader(); 982 InputStream is = null; 983 if (cL!=null) { 984 is = cL.getResourceAsStream(serviceId); 985 } 986 if (is==null) { 987 is = ClassLoader.getSystemResourceAsStream(serviceId); 988 } 989 if (is!=null) { 993 InputStreamReader isr; 994 try { 995 isr = new InputStreamReader (is,"UTF-8"); 996 } catch (java.io.UnsupportedEncodingException e) { 997 isr = new InputStreamReader (is); 998 } 999 BufferedReader rd= new BufferedReader (isr); 1000 classname = rd.readLine(); 1001 rd.close(); 1002 1003 if (!Tk.isWhitespace(classname)) { 1004 return classname; 1005 } 1006 } 1007 } catch (Exception anyX) { 1008 clnt.problem(anyX.getLocalizedMessage(),Project.MSG_WARN); 1009 } 1010 1011 return null; 1012 } 1013 1014 1015 1016 1026 public final static String loadSpiImpl(Class spiClass, Requester clnt) 1027 { 1028 return loadSpiImpl(spiClass.getName(),clnt); 1029 } 1030 1031 1032 1033 1034 private Tk() 1035 { 1036 } 1037 1038 1039 1040 1043 private static final Map READONLYMAP= 1044 Collections.unmodifiableMap(Collections.EMPTY_MAP); 1045} 1046 1047 1048 | Popular Tags |