1 23 24 148 149 package com.sun.enterprise.admin.meta; 150 151 import java.util.Enumeration ; 152 import java.util.Hashtable ; 153 import java.util.List ; 154 import java.text.CharacterIterator ; 155 import java.text.StringCharacterIterator ; 156 157 import java.lang.reflect.Method ; 158 import java.lang.reflect.Modifier ; 159 160 import com.sun.org.apache.commons.modeler.ManagedBean; 161 import com.sun.org.apache.commons.modeler.FeatureInfo; 162 import com.sun.org.apache.commons.modeler.FieldInfo; 163 import com.sun.org.apache.commons.modeler.AttributeInfo; 164 import com.sun.org.apache.commons.modeler.OperationInfo; 165 import com.sun.org.apache.commons.modeler.ParameterInfo; 166 167 import javax.management.Descriptor ; 169 import javax.management.ObjectName ; 170 import javax.management.Attribute ; 171 import javax.management.AttributeList ; 172 import javax.management.DynamicMBean ; 173 import javax.management.NotificationBroadcasterSupport ; 174 import com.sun.enterprise.config.ConfigBeansFactory; 175 176 179 public class MBeanMetaHelper implements MBeanMetaConstants 180 { 181 182 static int ONLY_CHILD=1; 183 static int MULTY_CHILDS=2; 184 185 static int SETTER_METHODTYPE = 1; 186 static int GETTER_METHODTYPE = 2; 187 190 public static void mergeWithConfigBean(ManagedBean managedBean, Class objectClass, int mode) 192 { 193 194 if(objectClass==null) 195 return; 196 Hashtable attrs = new Hashtable (); 197 Hashtable children = new Hashtable (); 198 int shift = 0; 199 200 Method [] methods = objectClass.getMethods(); 202 for (int j = 0; j < methods.length; ++j) 203 { 204 String methodName=methods[j].getName(); 205 206 if( methodName.startsWith("get") || methodName.startsWith("is")) 208 { 209 shift = methodName.startsWith("is")?2:3; 210 if( Modifier.isStatic(methods[j].getModifiers()) || !Modifier.isPublic(methods[j].getModifiers()) ) 211 { 212 continue; 213 } 214 Class params[]=methods[j].getParameterTypes(); 215 if( params.length != 0 ) 216 { 217 continue; 218 } 219 if( methods[j].getDeclaringClass() != objectClass ) 220 continue; 221 222 Class ret=methods[j].getReturnType(); 223 if( ! supportedType( ret ) ) 224 { 225 String childClassName = ret.getName(); 227 if(childClassName.endsWith("."+methodName.substring(shift)) || 228 childClassName.endsWith("."+methodName.substring(shift)+";")) 229 { 230 children.put(methodName.substring(shift), childClassName); 231 } 232 continue; 233 } 234 if((mode&EXPOSE_GETTERS)==0 ) 235 continue; 236 239 AttrIntro ai = (AttrIntro)attrs.get(getAttrNameFromMethodName(methodName, true)); 240 if(ai==null) 241 { 242 ai = new AttrIntro(); 243 ai.name = getAttrNameFromMethodName(methodName, true); 244 attrs.put(ai.name, ai); 245 } 246 if(ai.type!=null) 247 { 248 if(!ai.type.equals(ret.getName())) 249 continue; 250 } 251 else 252 { 253 ai.type = ret.getName(); 254 } 255 ai.getName = methodName; 256 } else if( methodName.startsWith( "set" ) ) 258 { 259 if((mode&EXPOSE_SETTERS)==0 ) 260 continue; 261 262 Class params[]=methods[j].getParameterTypes(); 263 if( params.length != 1 ) 264 { 265 continue; 266 } 267 if( ! supportedType( params[0] ) ) 268 { 269 continue; 270 } 271 if( ! Modifier.isPublic( methods[j].getModifiers() ) ) 272 { 273 continue; 274 } 275 if( methods[j].getDeclaringClass() != objectClass ) 276 continue; 277 278 AttrIntro ai = (AttrIntro)attrs.get(getAttrNameFromMethodName(methodName, true)); 279 if(ai==null) 280 { 281 ai = new AttrIntro(); 282 ai.name = getAttrNameFromMethodName(methodName, true); 283 attrs.put(ai.name, ai); 284 } 285 if(ai.type!=null) 286 { 287 if(!ai.type.equals(params[0].getName())) 288 continue; 289 } 290 else 291 { 292 ai.type = params[0].getName(); 293 } 294 ai.setName = methodName; 295 if(methodName.startsWith( "setDefault" )) { 297 ai.setName = "set"+methodName.substring(3); 298 } 299 300 } else 301 { 302 continue; 303 } 304 } 305 306 OperationInfo operationInfo; 307 308 313 if(attrs.size()>0) 314 { 315 AttributeInfo[] infos = managedBean.getAttributes(); 316 Hashtable infosTable = new Hashtable (); 317 for(int i=0; i<infos.length; i++) 318 { 319 infosTable.put(infos[i].getName(), infos[i]); 320 } 321 322 String key; 323 Enumeration keys = attrs.keys(); 324 while(keys.hasMoreElements()) 325 { 326 key = (String )keys.nextElement(); 327 AttrIntro ai = (AttrIntro)attrs.get(key); 328 AttributeInfo info = (AttributeInfo)infosTable.get(key); 329 if(info==null) 330 { 331 ai.whereType = LOCATED_IN_CONFIGBEAN; 332 info = ai.createAttributeInfo(); 333 managedBean.addAttribute(info); 334 infosTable.put(key, info); 335 } 336 else 337 { 338 ai.mergeWithAttributeInfo(info); 339 } 340 } 341 342 operationInfo = createOperationInfo("getDefaultAttributeValue", "INFO", 344 "java.lang.String", 345 new ParameterInfo("attributeName", "java.lang.String", null), 346 LOCATED_IN_CONFIGBEAN); 347 mergeWithOperationInfo(managedBean, operationInfo); 348 } 349 350 String nameClass = "javax.management.ObjectName"; 352 String attrClass = "javax.management.Attribute"; 353 String namesClass = (new ObjectName [0]).getClass().getName(); 354 String attrListClass = (new AttributeList ()).getClass().getName(); 355 String stringsClass = (new String [0]).getClass().getName(); 356 357 FieldInfo field; 358 ParameterInfo param; 359 360 if(children.get("ElementProperty")!=null) 362 { 363 children.remove("ElementProperty"); 364 365 366 operationInfo = createOperationInfo("getProperties", "INFO", 368 attrListClass, null, LOCATED_IN_CONFIGBEAN); 369 mergeWithOperationInfo(managedBean, operationInfo); 370 371 376 operationInfo = createOperationInfo("getPropertyValue", "INFO", 378 "java.lang.Object", 379 new ParameterInfo("propertyName", "java.lang.String", null), 380 LOCATED_IN_CONFIGBEAN); 381 mergeWithOperationInfo(managedBean, operationInfo); 382 383 operationInfo = createOperationInfo("setProperty", "ACTION", 385 "void", 386 new ParameterInfo("nameAndValue", attrClass, null), 387 LOCATED_IN_CONFIGBEAN); 388 mergeWithOperationInfo(managedBean, operationInfo); 389 } 390 391 if(children.get("SystemProperty")!=null) 393 { 394 children.remove("SystemProperty"); 395 396 operationInfo = createOperationInfo("getSystemProperties", "INFO", 398 attrListClass, null, LOCATED_IN_CONFIGBEAN); 399 mergeWithOperationInfo(managedBean, operationInfo); 400 401 operationInfo = createOperationInfo("getSystemPropertyValue", "INFO", 403 "java.lang.Object", 404 new ParameterInfo("propertyName", "java.lang.String", null), 405 LOCATED_IN_CONFIGBEAN); 406 mergeWithOperationInfo(managedBean, operationInfo); 407 408 operationInfo = createOperationInfo("setSystemProperty", "ACTION", 410 "void", 411 new ParameterInfo("nameAndValue", attrClass, null), 412 LOCATED_IN_CONFIGBEAN); 413 mergeWithOperationInfo(managedBean, operationInfo); 414 } 415 416 418 420 if(children.size()>0) 421 { 422 String key; 423 Enumeration keys = children.keys(); 424 while(keys.hasMoreElements()) 425 { 426 key = (String )keys.nextElement(); 427 String clazz = (String )children.get(key); 428 boolean bMulti = clazz.charAt(0)=='['?true:false; 429 String childName = getAttrNameFromMethodName(key, true, '-'); 430 if((mode&EXPOSE_GETCHILD)!=0) 432 { 433 operationInfo = createOperationInfo("get"+key, "INFO", 435 bMulti?namesClass:nameClass, 436 null, LOCATED_IN_CONFIGBEAN); 437 addDataToChildOperInfo(childName, bMulti, operationInfo); 438 mergeWithOperationInfo(managedBean, operationInfo); 439 if(bMulti) 440 { 441 operationInfo = createOperationInfo( 443 "get"+key+GET_LISTNAMES_OP_SUFFIX, 444 "INFO", stringsClass, null, null); 445 addDataToChildOperInfo(childName, bMulti, operationInfo); 446 mergeWithOperationInfo(managedBean, operationInfo); 447 } 448 449 if(bMulti) 451 { 452 String prefix = "get"+key+"By"; 453 for (int j = 0; j < methods.length; ++j) 454 { 455 String methodName=methods[j].getName(); 456 if(methodName.startsWith(prefix)) 457 { 458 operationInfo = createOperationInfo(methodName, "INFO", 459 nameClass, 460 new ParameterInfo("key", "java.lang.String", null), 461 LOCATED_IN_CONFIGBEAN); 462 addDataToChildOperInfo(childName, bMulti, operationInfo); 463 mergeWithOperationInfo(managedBean, operationInfo); 464 break; 465 } 466 } 467 } 468 } 469 if((mode&EXPOSE_CREATECHILD)!=0) 470 { 471 String prefix = bMulti?"add"+key:"set"+key; 473 for (int j = 0; j < methods.length; ++j) 474 { 475 String methodName=methods[j].getName(); 476 if(methodName.startsWith(prefix)) 477 { 478 operationInfo = createOperationInfo("create"+key, "ACTION_INFO", 479 nameClass, 480 new ParameterInfo("attribute_list", attrListClass, null), 481 LOCATED_IN_CONFIGBEAN); 482 addDataToChildOperInfo(childName, bMulti, operationInfo); 483 mergeWithOperationInfo(managedBean, operationInfo); 484 break; 485 } 486 } 487 } 488 if((mode&EXPOSE_DESTROYCHILD)!=0) 489 { 490 if(!bMulti) 491 { 492 operationInfo = createOperationInfo("remove"+key, 493 "ACTION", "void", null, LOCATED_IN_CONFIGBEAN); 494 addDataToChildOperInfo(childName, bMulti, operationInfo); 495 mergeWithOperationInfo(managedBean, operationInfo); 496 } 497 else 498 { 499 String prefix = "get"+key+"By"; 500 for (int j = 0; j < methods.length; ++j) 501 { 502 String methodName=methods[j].getName(); 503 if(methodName.startsWith(prefix)) 504 { 505 operationInfo = createOperationInfo("remove"+methodName.substring(3), 506 "ACTION", "void", 507 new ParameterInfo("key", "java.lang.String", null), 508 LOCATED_IN_CONFIGBEAN); 509 addDataToChildOperInfo(childName, bMulti, operationInfo); 510 mergeWithOperationInfo(managedBean, operationInfo); 511 break; 512 } 513 } 514 } 515 } 516 } 517 } 518 } 519 520 public static AttrIntro findOrCreateAttrInfo(Hashtable attrs, String attrName, String methodName, Class supposedTypeClass, String whereType, int methodType) 522 { 523 AttrIntro ai = (AttrIntro)attrs.get(attrName); 524 if(ai==null) 525 { 526 ai = new AttrIntro(); 527 ai.name = attrName; 528 ai.type = supposedTypeClass.getName(); 529 if((methodType&GETTER_METHODTYPE)!=0) 530 ai.getName = methodName; 531 if((methodType&SETTER_METHODTYPE)!=0) 532 ai.setName = methodName; 533 ai.getName = methodName; 534 ai.whereType = whereType; 535 attrs.put(ai.name, ai); 536 } 537 else 538 if(ai.type==null || ai.type.equals(supposedTypeClass.getName())) 539 { 540 if((methodType&GETTER_METHODTYPE)!=0) 541 ai.getName = methodName; 542 if((methodType&SETTER_METHODTYPE)!=0) 543 ai.setName = methodName; 544 } 545 else 546 { 547 return null; 548 } 549 return ai; 550 } 551 552 public static void mergeWithRuntimeModelBean(ManagedBean managedBean, Class objectClass) 554 { 555 556 if(objectClass==null) 557 return; 558 Hashtable attrs = new Hashtable (); 559 560 Method [] methods = objectClass.getMethods(); 562 for (int j = 0; j < methods.length; ++j) 563 { 564 if( Modifier.isStatic(methods[j].getModifiers())) 565 continue; 566 if( ! Modifier.isPublic( methods[j].getModifiers() ) ) 567 continue; 568 if( methods[j].getDeclaringClass() == Object .class ) 569 continue; 570 571 String methodName=methods[j].getName(); 572 Class params[]=methods[j].getParameterTypes(); 573 Class ret=methods[j].getReturnType(); 574 575 if( (methodName.startsWith("get") || methodName.startsWith("is")) && 577 params.length == 0 && 579 supportedType( ret ) ) 580 { 581 findOrCreateAttrInfo(attrs, getAttrNameFromMethodName(methodName,false), methodName, ret, LOCATED_IN_RUNTIMEBEAN, GETTER_METHODTYPE); 582 } 583 else if( methodName.startsWith( "set" ) && 585 params.length == 1 && 586 supportedType( params[0] )) 587 { 588 findOrCreateAttrInfo(attrs, getAttrNameFromMethodName(methodName,false), methodName, params[0], LOCATED_IN_RUNTIMEBEAN, SETTER_METHODTYPE); 589 590 } 591 else 593 { 594 OperationInfo operationInfo = getOperationInfo(methods[j], LOCATED_IN_RUNTIMEBEAN); 595 mergeWithOperationInfo(managedBean, operationInfo); 596 } 597 } 598 599 if(attrs.size()>0) 601 { 602 AttributeInfo[] infos = managedBean.getAttributes(); 603 Hashtable infosTable = new Hashtable (); 604 for(int i=0; i<infos.length; i++) 605 { 606 infosTable.put(infos[i].getName(), infos[i]); 607 } 608 609 String key; 610 Enumeration keys = attrs.keys(); 611 while(keys.hasMoreElements()) 612 { 613 key = (String )keys.nextElement(); 614 AttrIntro ai = (AttrIntro)attrs.get(key); 615 AttributeInfo info = (AttributeInfo)infosTable.get(key); 616 if(info==null) 617 { 618 ai.whereType = LOCATED_IN_RUNTIMEBEAN; 619 info = ai.createAttributeInfo(); 620 managedBean.addAttribute(info); 621 infosTable.put(key, info); 622 } 623 else 624 { 625 ai.mergeWithAttributeInfo(info); 626 } 627 } 628 } 629 630 } 631 632 633 private static Class forNameOrNull(String str) 635 { 636 try { 637 return Class.forName(str); 638 } catch (Exception e) { 639 return null; 640 } 641 } 642 private static boolean isMethodMatch(Method m, String name, Class [] clParams) 644 { 645 if(!name.equals(m.getName())) 646 return false; 647 Class [] cls = m.getParameterTypes(); 648 if(clParams==null) 649 if(cls==null || cls.length==0) 650 return true; 651 else 652 return false; 653 if(cls==null) 654 if(clParams.length==0) 655 return true; 656 else 657 return false; 658 if(clParams.length!=cls.length) 659 return false; 660 for(int i=0; i<cls.length; i++) 661 if(!clParams[i].equals(cls[i])) 662 return false; 663 return true; 664 } 665 static Class [] _clsStr = new Class []{(new String ()).getClass()}; 666 static Class [] _clsStrArr = new Class []{(new String [0]).getClass()}; 667 static Class [] _clsAttr = new Class []{forNameOrNull("javax.management.Attribute")}; 668 static Class [] _clsAttrList = new Class []{forNameOrNull("javax.management.AttributeList")}; 669 static Class [] _clsServAndOname = new Class []{forNameOrNull("javax.management.MBeanServer"), forNameOrNull("javax.management.ObjectName")}; 670 static Class [] _clsModelMBI = new Class []{forNameOrNull("javax.management.modelmbean.ModelMBeanInfo") }; 671 static Class [] _clsBoolean = new Class []{(new Boolean (true)).getClass()}; 672 static Class [] _clsObjAndStr = new Class []{(new Object ()).getClass(), (new String ()).getClass()}; 673 static Class [] _clsInvokeParms = new Class []{(new String ()).getClass(), (new Object [0]).getClass(), (new String [0]).getClass()}; 674 public static void mergeWithDynamicMBean(ManagedBean managedBean, Class objectClass) 676 { 677 678 if(objectClass==null) 679 return; 680 Method [] methods = objectClass.getMethods(); 682 for (int j = 0; j < methods.length; ++j) 683 { 684 if( Modifier.isStatic(methods[j].getModifiers())) 685 continue; 686 if( ! Modifier.isPublic( methods[j].getModifiers() ) ) 687 continue; 688 689 if( methods[j].getDeclaringClass() == Object .class ) 690 continue; 691 Class declaringClass = methods[j].getDeclaringClass(); 692 if( NotificationBroadcasterSupport .class.equals(declaringClass) ) 695 continue; 696 String methodName=methods[j].getName(); 697 if( isMethodMatch(methods[j], "getAttribute", _clsStr) || 698 isMethodMatch(methods[j], "getAttributes", _clsStrArr) || 699 isMethodMatch(methods[j], "setAttribute", _clsAttr) || 700 isMethodMatch(methods[j], "setAttributes", _clsAttrList) || 701 isMethodMatch(methods[j], "preRegister", _clsServAndOname) || 702 isMethodMatch(methods[j], "postRegister", _clsBoolean) || 703 isMethodMatch(methods[j], "preDeregister", null) || 704 isMethodMatch(methods[j], "postDeregister", null)|| 705 isMethodMatch(methods[j], "setManagedResource", _clsObjAndStr) || 706 isMethodMatch(methods[j], "setModelMBeanInfo", _clsModelMBI)|| 707 isMethodMatch(methods[j], "getMBeanInfo", null) || 708 isMethodMatch(methods[j], "invoke", _clsInvokeParms) ) 709 continue; 710 OperationInfo operationInfo = getOperationInfo(methods[j], LOCATED_IN_MBEAN); 711 mergeWithOperationInfo(managedBean, operationInfo); 712 } 713 714 } 715 716 private static OperationInfo getOperationInfo(Method method, String whereType) 718 { 719 OperationInfo info = new OperationInfo(); 720 info.setName(method.getName()); 721 info.setReturnType(method.getReturnType().getName()); 722 Class paramsClasses[]=method.getParameterTypes(); 723 for(int k=0; k<paramsClasses.length; k++) 724 { 725 info.addParameter(new ParameterInfo("param"+(k+1), paramsClasses[k].getName(), null)); 726 } 727 if (whereType!=null) 728 info.addField(newField(WHERE_LOCATED_FIELD_NAME, whereType)); 729 return info; 730 } 731 732 private static OperationInfo createOperationInfo(String name, String impact, 734 String returnType, ParameterInfo param, 735 String whereType) 736 { 737 OperationInfo info = new OperationInfo(); 738 info.setName(name); 739 info.setImpact(impact); 740 if(returnType!=null) 741 info.setReturnType(returnType); 742 if(param!=null) 743 info.addParameter(param); 744 if (whereType!=null) 745 info.addField(newField(WHERE_LOCATED_FIELD_NAME, whereType)); 746 return info; 747 } 748 private static void mergeWithOperationInfo(ManagedBean managedBean, OperationInfo info) 750 { 751 managedBean.addOperation(info); 754 } 755 756 private static String strArray[]=new String [0]; 757 private static boolean supportedType( Class ret ) 759 { 760 return ret == String .class || 761 ret == Integer .class || 762 ret == Integer.TYPE || 763 ret == Long .class || 764 ret == Long.TYPE || 765 ret == java.io.File .class || 766 ret == Boolean .class || 767 ret == Boolean.TYPE || 768 ret == strArray.getClass() || ret == ObjectName .class 770 ; 771 } 772 773 private static String getAttrNameFromMethodName(String name, boolean bDecamelaze) 775 { 776 return getAttrNameFromMethodName(name, bDecamelaze, ATTRIBUTE_NAME_DELIMITER_SYMBOL); 777 } 778 private static String getAttrNameFromMethodName(String name, boolean bDecamelaze, char separatorSymbol) 780 { 781 if(name.startsWith("set") || name.startsWith("get")) 782 name = name.substring(3); 783 else 784 if(name.startsWith("is")) 785 name = name.substring(2); 786 if(!bDecamelaze) 787 return name; 788 if(name.length()==0) 789 return name; 790 String attrName = name.toLowerCase(); 791 char[] arr1 = name.toCharArray(); 792 char[] arr2 = name.toLowerCase().toCharArray(); 793 StringBuffer buf = new StringBuffer (arr1.length*2); 794 buf.append(arr2[0]); 795 for(int i=1; i<arr1.length; i++) 796 { 797 if(arr1[i]!=arr2[i]) 798 buf.append(separatorSymbol); 799 buf.append(arr2[i]); 800 } 801 return buf.toString(); 802 } 803 804 public static Class getConfigBeanClass(String xPath) 806 { 807 String beanName = ConfigBeansFactory.getConfigBeanNameByXPath(xPath); 809 try 811 { 812 Class cl = Class.forName("com.sun.enterprise.config.serverbeans."+beanName); 813 return cl; 814 } 815 catch(Exception e) 816 { 817 return null; 818 } 819 } 820 821 public static Class getRuntimeModelBeanClass(String j2eeType) 823 { 824 try 825 { 826 return Class.forName("com.sun.enterprise.management.model."+j2eeType+"Mdl"); 827 } 828 catch(Exception e) 829 { 830 return null; 832 } 833 } 834 835 public static String descriptorToString(FeatureInfo descr) 837 { 838 return descriptorToString(descr.getFields()); 839 } 840 public static String descriptorToString(List fields) 842 { 843 String str = "Descriptor["; 844 for(int i=0; i<fields.size(); i++) 845 { 846 FieldInfo field = (FieldInfo)fields.get(i); 847 str = str + field.getName() + "=" + field.getValue() + " "; 848 } 849 return str + "]"; 850 } 851 852 private static FieldInfo newField(String name, Object value) 854 { 855 FieldInfo info = new FieldInfo(); 856 info.setName(name); 857 info.setValue(value); 858 return info; 859 } 860 861 private static class AttrIntro 863 { 864 public String name = null; 865 public String type = null; 866 public String getName = null; 867 public String setName = null; 868 public String whereType = null; 869 870 public AttributeInfo createAttributeInfo() 871 { 872 AttributeInfo ati = new AttributeInfo(); 873 ati.setName(name); 874 ati.setType(type); 875 if(setName!=null) 876 { 877 ati.setWriteable(true); 878 ati.addField(newField(SETTER_FIELD_NAME, setName)); 879 } 880 else 881 ati.setWriteable(false); 882 if(getName!=null) 883 { 884 ati.setReadable(true); 885 ati.addField(newField(GETTER_FIELD_NAME, getName)); 886 } 887 else 888 ati.setReadable(false); 889 890 if (whereType!=null) 891 ati.addField(newField(WHERE_LOCATED_FIELD_NAME, whereType)); 892 return ati; 893 } 894 895 896 public void mergeWithAttributeInfo(AttributeInfo ati) 897 { 898 ati.setType(type); 899 if(setName==null) 900 ati.setWriteable(false); 901 if(getName==null) 902 ati.setReadable(false); 903 } 904 } 905 906 private static void addFieldToInfo(String name, String value, FeatureInfo info) 908 { 909 FieldInfo field = new FieldInfo(); 910 field.setName(name); 911 field.setValue(value); 912 info.addField(field); 913 } 914 private static void addDataToChildOperInfo(String childName, boolean bMulti, FeatureInfo info) 916 { 917 addFieldToInfo(CHILD_FIELD_NAME, childName, info); 918 if(bMulti) 919 addFieldToInfo(MULTI_FIELD_NAME, "true", info); 920 } 921 922 public static String mapToMBeanAttributeName(String name) 925 { 926 if(ATTRIBUTE_NAME_DELIMITER_SYMBOL!='_' && name!=null) 927 return name.replace('_', ATTRIBUTE_NAME_DELIMITER_SYMBOL); 928 return name; 929 } 930 public static String mapToConfigBeanAttributeName(String name) 933 { 934 if(name!=null) 935 return name.replace('_', '-'); 936 return name; 937 } 938 public static String cutAttributeTokenFromXPath(String xpath) 940 { 941 if(!xpath.endsWith("]") && !xpath.endsWith("/")) 942 { 943 int idx = xpath.lastIndexOf('/'); 944 if(idx>0 && xpath.length()>idx+1 && xpath.charAt(idx+1)=='@') 945 { 946 return xpath.substring(0, idx); 947 } 948 } 949 return xpath; 950 } 951 public static String getMultipleElementKeyValue(String xpath) 952 { 953 if(xpath.endsWith("']")) 954 { 955 int idx = xpath.lastIndexOf('\'', xpath.length()-3); 956 if(idx>0) 957 { 958 return xpath.substring(idx+1, xpath.length()-2); 959 } 960 } 961 return null; 962 } 963 public static String extractLastElemNameFromXPath(String xpath) 964 { 965 if(xpath.endsWith("]")) 966 { 967 int idx = xpath.lastIndexOf('['); 968 if(idx>0) 969 xpath=xpath.substring(0,idx); 970 } 971 int idx = xpath.lastIndexOf('/'); 972 if(idx>=0) 973 return xpath.substring(idx+1); 974 return null; 975 } 976 public static String cutLastElementFromXPath(String xpath) 977 { 978 if(xpath.endsWith("]")) 979 { 980 int idx = xpath.lastIndexOf('['); 981 if(idx>0) 982 xpath=xpath.substring(0,idx); 983 } 984 int idx = xpath.lastIndexOf('/'); 985 if(idx>=0) 986 return xpath.substring(0, idx); 987 return null; 988 } 989 } 990 991 | Popular Tags |