1 19 20 package org.netbeans.modules.schema2beans; 21 22 import java.util.*; 23 import java.io.*; 24 import java.beans.*; 25 26 import org.w3c.dom.*; 27 import java.lang.reflect.Constructor ; 28 29 80 public abstract class BaseBean implements Cloneable , Bean { 81 82 87 public class IterateChoiceProperties implements java.util.Iterator { 88 private ArrayList groups; 89 private int index; 90 91 public IterateChoiceProperties() { 92 this.groups = new ArrayList(); 93 this.index = 0; 94 } 95 96 void add(BeanProp prop) { 97 if (prop.group != null && !this.groups.contains(prop.group)) 98 this.groups.add(prop.group); 99 } 100 101 public boolean hasNext() { 102 return (this.index < this.groups.size()); 103 } 104 105 public Object next() throws NoSuchElementException { 106 if (hasNext()) { 107 BeanProp.GroupProp gp = 108 (BeanProp.GroupProp)this.groups.get(this.index++); 109 return (BaseProperty[])gp.list(); 110 } else 111 throw new NoSuchElementException(); 112 } 113 114 public void remove() 115 throws UnsupportedOperationException , IllegalStateException { 116 throw new UnsupportedOperationException (); 117 } 118 } 119 120 protected DOMBinding binding; 123 124 protected GraphManager graphManager; 126 127 private Map propByName; 132 private Map propByOrder; 133 134 144 private HashMap attrCache; 150 151 private ArrayList comparators; 154 155 160 private boolean isRoot; 162 163 private int propertyOrder; 168 169 private String defaultNamespace; 170 171 178 179 183 public BaseBean(Vector comps, Version version) { 184 init(comps, version); 185 } 186 187 protected void init(Vector comps, Version version) { 188 if (version.getMajor() < 3) { 189 initPropertyTables(13); 190 } 191 this.comparators = new ArrayList(2); 192 this.isRoot = false; 194 this.propertyOrder = 0; 195 this.attrCache = null; 196 197 if ((comps == null) || (comps.size()==0)) { 198 this.comparators.add(new BeanComparator()); 200 } else { 201 int size = comps.size(); 202 for (int i=0; i<size; i++) 203 this.comparators.add(comps.get(i)); 204 } 205 } 206 207 protected void initPropertyTables(int propertyCount) { 208 int hashTableSize = propertyCount * 2; 219 this.propByName = new HashMap(hashTableSize, 1.0f); 220 this.propByOrder = new HashMap(hashTableSize, 1.0f); 221 } 223 224 public synchronized void addBeanComparator(BeanComparator cmp) { 229 if (cmp != null) 230 this.comparators.add(cmp); 231 } 232 233 public synchronized void removeBeanComparator(BeanComparator cmp) { 234 int i = this.comparators.indexOf(cmp); 235 if (i != -1) 236 this.comparators.remove(i); 237 } 238 239 240 244 public void createProperty(String dtdName, String beanName, Class type) { 245 int o = Common.TYPE_0_1; 246 247 if (type.isInstance(java.lang.String .class)) 248 o |= Common.TYPE_STRING; 249 else 250 o |= Common.TYPE_BEAN; 251 252 this.createProperty(dtdName, beanName, o, type); 253 } 254 255 258 public void createRoot(String dtdName, String beanName, 259 int option, Class type) throws Schema2BeansRuntimeException { 260 BeanProp prop = new BeanProp(this, dtdName, beanName, 261 option, type, true); 262 try { 263 this.graphManager.createRootBinding(this, prop, null); 264 } catch (Schema2BeansException e) { 265 throw new Schema2BeansRuntimeException(e); 266 } 267 this.isRoot = true; 268 } 269 270 277 public void createProperty(String dtdName, String beanName, 278 int option, Class type) throws 279 Schema2BeansRuntimeException { 280 281 this.propertyOrder++; 287 288 BeanProp prop = new BeanProp(this, dtdName, beanName, option, type); 289 prop.setOrder(this.propertyOrder); 290 Object obj1 = this.propByName.put(beanName, prop); 291 this.propByOrder.put(String.valueOf(this.propertyOrder), prop); 293 294 if (obj1 != null) throw new Schema2BeansRuntimeException(Common. 296 getMessage("DuplicateProperties_msg")); 297 298 prop.initialize(); 299 } 300 301 public void setDefaultNamespace(String namespace) { 302 defaultNamespace = namespace; 303 createAttribute("xmlns", "xmlns", AttrProp.CDATA | AttrProp.IMPLIED, 304 null, namespace); 305 setAttributeValue("xmlns", namespace); 306 if (beanProp().getAttrProp("xsi:schemaLocation", true) == null) { 307 createAttribute("xmlns:xsi", "xmlns:xsi", AttrProp.CDATA | AttrProp.IMPLIED, null, null); 308 createAttribute("xsi:schemaLocation", "xsi:schemaLocation", AttrProp.CDATA | AttrProp.IMPLIED, null, null); 309 } 310 } 311 312 public String getDefaultNamespace() { 313 return defaultNamespace; 314 } 315 316 319 public BeanProp[] beanProps() { 320 int size = this.propByOrder.size(); 321 BeanProp[] ret = new BeanProp[size]; 322 for (int i=1; i<=size; i++) 323 ret[i-1] = (BeanProp)this.propByOrder.get(String.valueOf(i)); 324 return ret; 325 } 326 327 330 protected Iterator beanPropsIterator() { 331 return propByName.values().iterator(); 332 } 333 334 339 public BeanProp beanProp(String name) { 340 BeanProp prop = (BeanProp)this.propByName.get(name); 341 342 if (prop == null) { 343 String beanName = Common.convertName(name); 345 prop = (BeanProp)this.propByName.get(beanName); 346 347 if (prop == null) 348 throw new IllegalArgumentException (Common. 349 getMessage("BeanPropertyDoesntExist_msg", 350 this.getClass().getName(), name)); 351 } 352 return prop; 353 } 354 355 360 public BeanProp beanProp(int order) { 361 return (BeanProp)this.propByOrder.get(String.valueOf(order)); 362 } 363 364 367 public Object getValue(String name) { 368 return this.beanProp(name).getValue(0); 369 } 370 371 374 public Object getValue(String name, int index) { 375 return this.beanProp(name).getValue(index); 376 } 377 378 385 public Object getValueById(String name, int id) { 386 return this.beanProp(name).getValueById(id); 387 } 388 389 395 public int idToIndex(String name, int id) { 396 return this.beanProp(name).idToIndex(id); 397 } 398 399 403 public int indexToId(String name, int index) { 404 return this.beanProp(name).indexToId(index); 405 } 406 407 410 public boolean isNull(String name) { 411 return (this.getValue(name) == null); 412 } 413 414 417 public boolean isNull(String name, int index) { 418 return (this.getValue(name, index) == null); 419 } 420 421 425 public Object [] getValues(String name) { 426 return this.beanProp(name).getValues(); 427 } 428 429 432 public void setValue(String name, Object value) { 433 setValue(beanProp(name), 0, value); 434 } 435 436 439 public void setValue(String name, int index, Object value) { 440 setValue(beanProp(name), index, value); 441 } 442 443 protected void setValue(BeanProp prop, int index, Object value) { 444 prop.setValue(index, value); 445 } 446 447 protected int addValue(BeanProp prop, Object value) { 448 return prop.addValue(value); 449 } 450 451 protected int removeValue(BeanProp prop, Object value) { 452 return prop.removeValue(value); 453 } 454 455 protected void removeValue(BeanProp prop, int index) { 456 prop.removeValue(index); 457 } 458 459 463 public void setValueById(String name, int id, Object value) { 464 BeanProp bp = this.beanProp(name); 465 int index = bp.idToIndex(id); 466 bp.setValue(index, value); 467 } 468 469 472 public void setValue(String name, Object [] value) { 473 this.beanProp(name).setValue(value); 474 } 475 476 479 public int addValue(String name, Object value) { 480 return addValue(beanProp(name), value); 481 } 482 483 486 public int removeValue(String name, Object value) { 487 return removeValue(beanProp(name), value); 488 } 489 490 493 public void removeValue(String name, int index) { 494 removeValue(beanProp(name), index); 495 } 496 497 503 public int indexOf(String name, Object value) throws 504 Schema2BeansRuntimeException { 505 BeanProp bp = this.beanProp(name); 506 507 if (bp == null) 508 throw new Schema2BeansRuntimeException(Common. 509 getMessage("UnknownPropertyName_msg", name)); 510 511 if (Common.isArray(bp.type)) { 512 boolean isBean = Common.isBean(bp.type); 513 514 int size = bp.size(); 515 for (int i=0; i<size; i++) { 516 Object obj = bp.getValue(i); 517 if (isBean && (obj == value)) 518 return i; 519 else 520 if (!isBean && (obj.equals(value))) 521 return i; 522 } 523 } 524 return -1; 525 } 526 527 532 public int size(String name) { 533 return this.beanProp(name).size(); 534 } 535 536 540 public boolean isChoiceProperty(String name) { 541 return this.beanProp(name).isChoiceProperty(); 542 } 543 544 547 public boolean isChoiceProperty() { 548 return this.beanProp().isChoiceProperty(); 549 } 550 551 556 public BaseProperty[] listChoiceProperties(String name) { 557 return this.beanProp(name).getChoiceProperties(); 558 } 559 560 565 public Iterator listChoiceProperties() { 566 IterateChoiceProperties it = new IterateChoiceProperties(); 567 Iterator i = beanPropsIterator(); 568 while (i.hasNext()) 569 it.add((BeanProp)i.next()); 570 return it; 571 } 572 573 576 public BaseProperty[] listProperties() { 577 return (BaseProperty[])this.beanProps(); 578 } 579 580 583 public BaseProperty getProperty() { 584 return (BaseProperty)this.beanProp(); 585 } 586 587 590 public BaseProperty getProperty(String propName) { 591 return (BaseProperty)this.beanProp(propName); 592 } 593 594 597 public Object [] knownValues(String name) { 598 return this.beanProp(name).knownValues(); 599 } 600 601 protected void addKnownValue(String name, Object value) { 602 this.beanProp(name).addKnownValue(value); 603 } 604 605 608 public void createAttribute(String dtdName, String name, int type, 609 String [] values, String defValue) { 610 BeanProp bp = this.beanProp(); 611 if (bp != null) 612 bp.createAttribute(dtdName, name, type, values, defValue); 613 else 614 System.out.println(Common.getMessage("beanPropIsNull_msg", name)); 615 } 616 617 621 public void createAttribute(String propName, String dtdName, String name, 622 int type, String [] values, String defValue) { 623 this.beanProp(propName).createAttribute(dtdName, name, type, 624 values, defValue); 625 } 626 627 630 public void setAttributeValue(String propName, String name, 631 String value) { 632 this.beanProp(propName).setAttributeValue(0, name, value); 633 } 634 635 638 public void setAttributeValue(String name, String value) { 639 if (name == null) 640 return; 641 642 BeanProp bp = this.beanProp(); 643 if (bp != null) { 644 int i = bp.idToIndex(this.binding.getId()); 646 bp.setAttributeValue(i, name, value); 647 } else { 648 if (this.attrCache == null) 653 this.attrCache = new HashMap(); 654 this.attrCache.put(name, value); 655 } 656 } 657 658 665 String [] cachedAttributeNames() { 666 int size = (this.attrCache==null)?0:this.attrCache.size(); 667 String [] ret = new String [size]; 668 if (size >0) { 669 Iterator it = this.attrCache.keySet().iterator(); 670 int i = 0; 671 while (it.hasNext()) 672 ret[i++] = it.next().toString(); 673 } 674 675 return ret; 676 } 677 678 681 String cachedAttributeValue(String name) { 682 if (this.attrCache != null) 683 return (String )this.attrCache.get(name); 684 else 685 return null; 686 } 687 688 691 void cachedAttributeClear() { 692 this.attrCache = null; 693 } 694 695 700 public String getAttributeValue(String name) { 701 BeanProp bp = this.beanProp(); 702 if (bp != null) { 703 int i = bp.idToIndex(this.binding.getId()); 705 if (i < 0) return null; 707 return bp.getAttributeValue(i, name); 708 } else { 709 if (this.attrCache != null) 714 return (String )this.attrCache.get(name); 715 else 716 return null; 717 } 718 } 719 720 723 public String getAttributeValue(String propName, String name) { 724 return this.beanProp(propName).getAttributeValue(0, name); 725 } 726 727 730 public void setAttributeValue(String propName, int index, String name, 731 String value) { 732 this.beanProp(propName).setAttributeValue(index, name, value); 733 } 734 735 738 public String getAttributeValue(String propName, int index, String name) { 739 return this.beanProp(propName).getAttributeValue(index, name); 740 } 741 742 746 public String [] getAttributeNames(String propName) { 747 return this.beanProp(propName).getAttributeNames(); 748 } 749 750 753 public String [] getAttributeNames() { 754 BeanProp bp = this.beanProp(); 755 if (bp != null) 756 return bp.getAttributeNames(); 757 else 758 return null; 759 } 760 761 762 766 public BaseAttribute[] listAttributes(String propName) { 767 return this.beanProp(propName).getAttributes(); 768 } 769 770 773 public BaseAttribute[] listAttributes() { 774 BeanProp bp = this.beanProp(); 775 if (bp != null) 776 return bp.getAttributes(); 777 else 778 return null; 779 } 780 781 782 private void lookForAttribute(ArrayList found, BeanProp bp, BaseBean bean, 784 BaseAttribute[] attrs, String attrName, 785 Object value) { 786 787 for (int j=0; j<attrs.length; j++) { 788 if (attrName == null || attrs[j].hasName(attrName)) { 789 String name = attrs[j].getName(); 790 791 if (DDLogFlags.debug) { 792 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 793 DDLogFlags.DBG_UBN, 1, DDLogFlags.FINDATTR, 794 bp.getName() + "." + name + 795 " for value " + value); 796 } 797 798 int size = bp.size(); 799 if (bp.isIndexed()) { 800 for (int k=0; k<size; k++) { 801 String v = bp.getAttributeValue(k, name); 802 v = (String )Common.getComparableObject(v); 803 804 if (DDLogFlags.debug) { 805 TraceLogger.put(TraceLogger.DEBUG, 806 TraceLogger.SVC_DD, 807 DDLogFlags.DBG_UBN, 1, 808 DDLogFlags.FINDCMP, 809 bp.getName() + "." + 810 name + " = " + v); 811 } 812 813 if ((bean == null || bean == bp.getValue(k)) 814 && value.equals(v)) { 815 if (DDLogFlags.debug) { 816 TraceLogger.put(TraceLogger.DEBUG, 817 TraceLogger.SVC_DD, 818 DDLogFlags.DBG_UBN, 1, 819 DDLogFlags.FNDATTR); 820 } 821 found.add(bp.buildFullName(k, name)); 822 } 823 } 824 } else { 825 String v = bp.getAttributeValue(0, name); 826 v = (String )Common.getComparableObject(v); 827 828 if (DDLogFlags.debug) { 829 TraceLogger.put(TraceLogger.DEBUG, 830 TraceLogger.SVC_DD, 831 DDLogFlags.DBG_UBN, 1, 832 DDLogFlags.FINDCMP, 833 bp.getName() + "." + 834 name + " = " + v); 835 } 836 837 if (value.equals(v)) { 838 if (DDLogFlags.debug) { 839 TraceLogger.put(TraceLogger.DEBUG, 840 TraceLogger.SVC_DD, 841 DDLogFlags.DBG_UBN, 1, 842 DDLogFlags.FNDATTR); 843 } 844 845 found.add(bp.buildFullName(0, name)); 846 } 847 } 848 } 849 } 850 } 851 852 860 void find(BaseBean bean, ArrayList found, String propName, 861 String attrName, Object value) { 862 863 if (DDLogFlags.debug) { 864 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 865 DDLogFlags.DBG_UBN, 1, DDLogFlags.FIND, 866 (bean==null?"<null>":bean.getClass().getName()) + 867 " - " + propName + "/" + attrName + " for value " + 868 ((value == null)?"<null>":value.toString())); 869 } 870 871 if (bean == null || value == null) 872 return; 873 874 BaseProperty[] props = bean.listProperties(); 875 876 BaseAttribute[] attrs = bean.listAttributes(); 881 if (propName == null && attrs != null && attrs.length > 0) { 882 BeanProp bp = bean.beanProp(); 883 this.lookForAttribute(found, bp, bean, attrs, attrName, value); 884 } 885 886 for (int i=0; i<props.length; i++) { 892 BaseProperty p = props[i]; 893 String name = p.getName(); 894 BeanProp bp = (BeanProp)p; 895 int size = p.size(); 896 897 if (!p.isBean()) { 903 if (((propName != null && p.hasName(propName)) || 905 (propName == null && attrName == null))) { 906 if (DDLogFlags.debug) { 907 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 908 DDLogFlags.DBG_UBN, 1, 909 DDLogFlags.FINDPROP, 910 name + " for value " + value); 911 } 912 913 if (p.isIndexed()) { 914 for (int j=0; j<size; j++) { 916 Object v = bp.getValue(j); 917 v = Common.getComparableObject(v); 918 919 if (DDLogFlags.debug) { 920 TraceLogger.put(TraceLogger.DEBUG, 921 TraceLogger.SVC_DD, 922 DDLogFlags.DBG_UBN, 1, 923 DDLogFlags.FINDCMP, 924 name + "[" + j + "] = " + 925 v.toString()); 926 927 } 928 if (value.equals(v)) { 929 if (DDLogFlags.debug) 930 TraceLogger.put(TraceLogger.DEBUG, 931 TraceLogger.SVC_DD, 932 DDLogFlags.DBG_UBN, 1, 933 DDLogFlags.FNDPROP); 934 935 found.add(bp.getFullName(j)); 936 } 937 } 938 } else { 939 Object v = bp.getValue(0); 940 v = Common.getComparableObject(v); 941 942 if (DDLogFlags.debug) { 943 TraceLogger.put(TraceLogger.DEBUG, 944 TraceLogger.SVC_DD, 945 DDLogFlags.DBG_UBN, 1, 946 DDLogFlags.FINDCMP, 947 name + " = " + 948 ((v==null)?"null":v.toString())); 949 } 950 951 if (value.equals(v)) { 952 if (DDLogFlags.debug) 953 TraceLogger.put(TraceLogger.DEBUG, 954 TraceLogger.SVC_DD, 955 DDLogFlags.DBG_UBN, 1, 956 DDLogFlags.FNDPROP); 957 958 found.add(bp.getFullName()); 959 } 960 } 961 } 962 963 attrs = p.getAttributes(); 965 if (propName == null && attrs.length > 0) 966 this.lookForAttribute(found, bp, null, attrs, 967 attrName, value); 968 } 969 970 if (p.isBean() && p.isIndexed()) { 972 BaseBean[] ba = (BaseBean[])bean.getValues(name); 973 for (int k=0; k<ba.length; k++) 974 find(ba[k], found, propName, attrName, value); 975 } else 976 if (p.isBean()) { 977 BaseBean b = (BaseBean)bean.getValue(name); 978 find(b, found, propName, attrName, value); 979 } 980 } 981 } 982 983 986 public String [] findAttributeValue(String attrName, String value) { 987 ArrayList list = new ArrayList(); 988 this.find(this, list, null, attrName, value); 989 String [] ret = new String [list.size()]; 990 return (String [])list.toArray(ret); 991 } 992 993 996 public String [] findPropertyValue(String propName, Object value) { 997 ArrayList list = new ArrayList(); 998 this.find(this, list, propName, null, value); 999 String [] ret = new String [list.size()]; 1000 return (String [])list.toArray(ret); 1001 } 1002 1003 1006 public String [] findValue(Object value) { 1007 ArrayList list = new ArrayList(); 1008 this.find(this, list, null, null, value); 1009 String [] ret = new String [list.size()]; 1010 return (String [])list.toArray(ret); 1011 } 1012 1013 public void write(File f) throws IOException, Schema2BeansRuntimeException { 1014 OutputStream out = new FileOutputStream(f); 1015 try { 1016 write(out); 1017 } finally { 1018 out.close(); 1019 } 1020 } 1021 1022 1025 public void write(OutputStream out) throws IOException, Schema2BeansRuntimeException { 1026 try { 1027 reindent(); 1028 if (this.graphManager != null) { 1029 this.graphManager.write(out); 1030 } else 1031 throw new IllegalStateException (Common. 1032 getMessage("CantWriteBeanNotInDOMTree_msg")); 1033 } catch (Schema2BeansException e) { 1034 throw new Schema2BeansRuntimeException(e); 1035 } 1036 } 1037 1038 1043 public void write(OutputStream out, String encoding) throws IOException, Schema2BeansException { 1044 reindent(); 1045 if (this.graphManager != null) { 1046 this.graphManager.write(out, encoding); 1047 } else 1048 throw new IllegalStateException (Common. 1049 getMessage("CantWriteBeanNotInDOMTree_msg")); 1050 } 1051 1052 1056 public void write(java.io.Writer w) throws IOException, Schema2BeansException { 1057 write(w, null); 1058 } 1059 1060 1066 public void write(java.io.Writer w, String encoding) throws IOException, Schema2BeansException { 1067 reindent(); 1068 if (this.graphManager != null) { 1069 this.graphManager.write(w, encoding); 1070 } else 1071 throw new IllegalStateException (Common. 1072 getMessage("CantWriteBeanNotInDOMTree_msg")); 1073 } 1074 1075 public void writeNoReindent(OutputStream out) throws IOException, Schema2BeansException { 1076 if (this.graphManager != null) { 1077 this.graphManager.write(out); 1078 } else 1079 throw new IllegalStateException (Common. 1080 getMessage("CantWriteBeanNotInDOMTree_msg")); 1081 } 1082 1083 public void writeNode(java.io.Writer out) throws IOException { 1084 if (graphManager == null) { 1085 throw new IllegalStateException (Common. 1086 getMessage("CantWriteBeanNotInDOMTree_msg")); 1087 } 1088 Node myNode = binding.getNode(); 1089 try { 1090 graphManager.write(out, myNode); 1091 } catch (Schema2BeansException e) { 1092 throw new RuntimeException (e); 1094 } 1095 } 1096 1097 1101 public void reindent() { 1102 reindent(" "); 1103 } 1104 1105 public void reindent(String indent) { 1106 if (graphManager != null) { 1107 graphManager.reindent(indent); 1108 } else 1109 throw new IllegalStateException (Common. 1110 getMessage("CantWriteBeanNotInDOMTree_msg")); 1111 } 1112 1113 protected boolean hasDomNode() { 1114 if (this.binding == null) 1115 return false; 1116 else 1117 return this.binding.hasDomNode(); 1118 } 1119 1120 protected DOMBinding domBinding() { 1121 return this.binding; 1122 } 1123 1124 protected void setDomBinding(DOMBinding binding) { 1125 this.binding = binding; 1126 } 1127 1128 1131 public GraphManager graphManager() { 1132 return this.graphManager; 1133 } 1134 1135 protected void setGraphManager(GraphManager graphMgr) { 1136 this.graphManager = graphMgr; 1137 if (this.changeListeners != null) { 1138 } 1140 } 1141 1142 1148 1149 1159 void syncNodes(BeanProp.Action a) { 1160 Iterator i = beanPropsIterator(); 1161 1162 while (i.hasNext()) { 1163 BeanProp prop = (BeanProp)i.next(); 1164 if (prop != null) 1165 prop.syncNodes(a); 1166 } 1167 } 1168 1169 1173 protected void buildPathName(StringBuffer str) { 1174 if (this.binding != null) { 1175 BeanProp p = this.binding.getBeanProp(this); 1176 if (p != null) 1177 p.buildPathName(this.binding, str); 1178 } 1179 } 1180 1181 1186 void notifyInternal(BeanProp.InternalEvent ie) { 1187 if (this.changeListeners != null 1188 && ie.type == BeanProp.InternalEvent.CHANGED) { 1189 1190 boolean addedGM = false; 1191 1192 if (this.graphManager == null) { 1193 this.graphManager = new GraphManager(this); 1194 addedGM = true; 1195 } 1196 this.changeListeners. 1197 firePropertyChange(ie.getPropertyChangeEvent()); 1198 if (addedGM) { 1199 this.graphManager = null; 1200 } 1201 } 1202 if (this.binding != null) { 1203 BeanProp p = this.binding.getBeanProp(this); 1204 if (p != null) 1205 p.notifyInternal(ie, true); 1206 } 1207 } 1208 1209 1210 1213 public Object clone() { 1214 BaseBean bean = null; 1215 1216 try { 1217 bean = (BaseBean)this.getClass().newInstance(); 1219 } catch(Exception e) { 1220 TraceLogger.error(e); 1221 throw new Schema2BeansRuntimeException(Common. 1222 getMessage("CantInstantiateBean_msg", e.getMessage())); 1223 } 1224 1225 if (this.graphManager != null && this.graphManager.root == this) { 1227 BeanProp p = this.binding.getBeanProp(this); 1229 String dtdName = p.getDtdName(); 1230 String beanName = p.getDtdName(); 1231 Class beanClass = p.getPropClass(); 1232 1233 Node n = GraphManager.createRootElementNode(dtdName); 1235 1236 bean.graphManager.setXmlDocument(n); 1238 n = GraphManager.getElementNode(dtdName, n); 1239 bean.graphManager.completeRootBinding(bean, n); 1240 } 1241 1242 String [] attrs = this.getAttributeNames(); 1244 if (attrs != null) { 1245 for(int j=0; j<attrs.length; j++) { 1246 String a = attrs[j]; 1247 if (!this.beanProp().getAttrProp(a).isFixed()) { 1248 String v = this.getAttributeValue(a); 1249 if (bean.getAttributeValue(a) != v) 1250 bean.setAttributeValue(a, v); 1251 } 1252 } 1253 } 1254 1255 if (attrCache != null) 1256 bean.attrCache = (HashMap) attrCache.clone(); 1258 Iterator it = beanPropsIterator(); 1259 1260 while (it.hasNext()) { 1262 BeanProp prop = (BeanProp)it.next(); 1263 1264 if (prop == null) 1265 continue; 1266 1267 String name = prop.getBeanName(); 1268 1269 if (Common.isArray(prop.type)) { 1270 int size = prop.size(); 1271 if (Common.isBean(prop.type)) { 1272 for(int i=0; i<size; i++) { 1273 BaseBean b = (BaseBean)prop.getValue(i); 1274 if (b != null) 1275 b = (BaseBean)b.clone(); 1276 bean.addValue(name, b); 1277 } 1278 } else { 1279 for(int i=0; i<size; i++) 1280 bean.addValue(name, prop.getValue(i)); 1281 1282 attrs = prop.getAttributeNames(); 1284 for(int j=0; j<attrs.length; j++) { 1285 String a = attrs[j]; 1286 if (!prop.getAttrProp(a).isFixed()) { 1287 for(int i=0; i<size; i++) { 1288 String v = prop.getAttributeValue(i, a); 1289 if (bean.getAttributeValue(name, i, a) != v) 1290 bean.setAttributeValue(name, i, a, v); 1291 1292 } 1293 } 1294 } 1295 } 1296 } else { 1297 if (Common.isBean(prop.type)) { 1298 BaseBean b = (BaseBean)prop.getValue(0); 1299 if (b != null) 1300 b = (BaseBean)b.clone(); 1301 bean.setValue(name, b); 1302 } else { 1303 bean.setValue(name, prop.getValue(0)); 1304 1305 attrs = prop.getAttributeNames(); 1307 for(int j=0; j<attrs.length; j++) { 1308 String a = attrs[j]; 1309 if (!prop.getAttrProp(a).isFixed()) { 1310 String v = prop.getAttributeValue(0, a); 1311 if (bean.getAttributeValue(name, 0, a) != v) 1312 bean.setAttributeValue(name, a, v); 1313 } 1314 } 1315 } 1316 } 1317 } 1318 1319 return bean; 1320 } 1321 1322 1335 public static final int MERGE_NONE = 0x00; 1336 public static final int MERGE_INTERSECT = 0x01; 1337 public static final int MERGE_UNION = 0x02; 1338 public static final int MERGE_UPDATE = (MERGE_UNION|MERGE_INTERSECT); 1339 public static final int MERGE_COMPARE = 0x04; 1340 1341 1342 static String mergeModeToString(int mode) { 1343 switch(mode) { 1344 case MERGE_NONE: return "MERGE_NONE"; case MERGE_INTERSECT: return "MERGE_INTERSECT"; case MERGE_UNION: return "MERGE_UNION"; case MERGE_UPDATE: return "MERGE_UPDATE"; case MERGE_COMPARE: return "MERGE_COMPARE"; default: return "Unknown merge mode: " + mode; } 1351 } 1352 1353 1357 public void merge(BaseBean bean, int mode) { 1358 if (mode == MERGE_UPDATE) 1359 mergeUpdate(bean); 1360 else 1361 mergeTreeRoot(bean, mode); 1362 } 1363 1364 1368 public void merge(BaseBean bean) { 1369 mergeUpdate(bean); 1370 } 1371 1372 1377 public void mergeUpdate(BaseBean sourceBean) { 1378 mergeTreeRoot(sourceBean, MERGE_UPDATE); 1379 } 1380 1381 private boolean setHasKeyDefaultValue(BeanProp prop) { 1383 BeanComparator cmp = (BeanComparator)this.comparators.get(0); 1384 return cmp.hasKeyDefined(prop); 1385 } 1386 1387 1393 protected void copyProperty(BeanProp prop, BaseBean bean, 1394 int index, Object value) { 1395 1396 boolean isArray = Common.isArray(prop.type); 1397 String name = prop.getName(); 1398 1399 if (value == null) { 1401 if (isArray) 1402 value = bean.getValue(name, index); 1403 else 1404 value = bean.getValue(name, 0); 1405 } 1406 1407 int newIndex = 0; 1408 1409 if (isArray) { 1410 newIndex = addValue(prop, value); 1411 } else { 1412 setValue(prop, 0, value); 1413 index = 0; 1414 } 1415 1416 this.copyAttributes(prop, newIndex, bean, index); 1417 } 1418 1419 1424 private void copyAttributes(BeanProp prop, int propIndex, 1425 BaseBean bean, int beanIndex) { 1426 1427 String name = prop.getName(); 1429 BaseAttribute[] ba = bean.listAttributes(name); 1430 if (ba != null) { 1431 for(int j=0; j<ba.length; j++) { 1432 if (!ba[j].isFixed()) { 1433 String attrName = ba[j].getName(); 1434 String v = 1435 bean.getAttributeValue(name, beanIndex, attrName); 1436 if (v != prop.getAttributeValue(propIndex, attrName)) { 1437 prop.setAttributeValue(propIndex, attrName, v); 1438 } 1439 } 1440 } 1441 } 1442 } 1443 1444 1450 synchronized boolean mergeTreeRoot(BaseBean bean, int mode) { 1451 1452 return this.mergeTree(bean, mode); 1454 } 1455 1456 private boolean mergeAttributes(BaseBean bean, int mode) { 1457 boolean result = true; 1458 BaseAttribute[] ba = bean.listAttributes(); 1460 1461 if (ba != null) { 1463 for (int j = 0; j < ba.length; j++) { 1464 BaseAttribute baseAttribute = ba[j]; 1465 if (!baseAttribute.isFixed() && !baseAttribute.isTransient()) { 1466 String attrName = baseAttribute.getName(); 1467 String curValue = this.getAttributeValue(attrName); 1468 String otherValue = bean.getAttributeValue(attrName); 1469 1470 if (curValue != otherValue) { 1471 if (curValue == null || otherValue == null || !curValue.equals(otherValue)) { 1473 if ((mode & MERGE_COMPARE) == MERGE_COMPARE) { 1474 return false; 1475 } 1476 if ((mode & MERGE_UNION) == MERGE_UNION) { 1477 this.setAttributeValue(attrName, otherValue); 1478 } 1479 } 1480 } 1481 } 1482 } 1483 } 1484 return result; 1485 } 1486 1487 1490 synchronized boolean mergeTree(BaseBean bean, int mode) { 1501 if (DDLogFlags.debug) { 1502 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 1503 DDLogFlags.DBG_UBN, 1, DDLogFlags.MERGE, 1504 this.getClass().getName() + "/" + 1505 (bean==null?"<getAttributeValue(\"Id\")null>":bean.getClass().getName()) + 1506 " - " + mergeModeToString(mode)); 1507 } 1508 1509 if (this.getClass().isInstance(bean)) { 1516 if (!mergeAttributes(bean, mode)) { 1517 return false; 1518 } 1519 1520 1521 Iterator it = beanPropsIterator(); 1523 1524 while (it.hasNext()) { 1536 BeanProp prop = (BeanProp)it.next(); 1538 1539 if (prop == null) 1540 continue; 1541 1542 String name = prop.getBeanName(); 1543 boolean isArray = Common.isArray(prop.type); 1544 boolean isBean = Common.isBean(prop.type); 1545 Object o1, o2, o3; 1546 boolean hasKey = false; 1547 boolean hasKeyDefined = false; 1548 1549 if (isArray) { 1550 int i, j = 0; 1559 int size1 = prop.size(); 1560 int size2 = bean.size(name); 1561 boolean toRemove[] = new boolean[size1]; 1562 boolean toAdd[] = new boolean[size2]; 1563 boolean compared[] = new boolean[size2]; 1564 1565 Arrays.fill(toRemove, false); 1567 1568 Arrays.fill(toAdd, true); 1570 1571 Arrays.fill(compared, false); 1573 1574 if (DDLogFlags.debug) { 1575 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 1576 DDLogFlags.DBG_UBN, 5, 1577 DDLogFlags.MERGEPROP, 1578 this.getClass().getName() + "." + 1579 name + "[" + size1 + "] / " + 1580 bean.getClass().getName() + "." + 1581 name + "[" + size2 + "]"); 1582 } 1583 1584 for (i=0; i<size1; i++) { 1586 o1 = prop.getValue(i); 1587 1589 if (isBean && o1 == null) 1590 continue; 1592 boolean found = false; 1593 1594 for (int c=0; c<this.comparators.size() && !found; c++){ 1596 BeanComparator cmp = 1597 (BeanComparator)this.comparators.get(c); 1598 1599 for (j=0; j<size2; j++) { 1601 if (!compared[j]) { 1602 o2 = bean.getValue(name, j); 1603 1604 if (isBean) { 1605 if (o2 == null) { 1606 compared[j] = true; 1608 toAdd[j] = false; 1609 continue; 1610 } 1611 1612 o3 = cmp.compareBean(name, 1613 (BaseBean)o1, 1614 (BaseBean)o2); 1615 1616 if (!hasKey) { 1617 hasKey = cmp.hasKey(); 1618 hasKeyDefined = true; 1619 } 1620 1621 if (o3 == o1) { 1622 boolean ret = ((BaseBean)o1). 1624 mergeTree((BaseBean)o2, mode); 1625 1626 if (!ret) return ret; 1627 compared[j] = true; 1628 found = true; 1629 break; 1630 } 1631 } else { 1632 o3 = cmp.compareProperty(name, 1633 this, o1, i, 1634 bean, o2, j); 1635 if (!hasKey) { 1636 hasKey = cmp.hasKey(); 1637 hasKeyDefined = true; 1638 } 1639 1640 if (o3 == o1) { 1641 compared[j] = true; 1642 found = true; 1643 break; 1644 } 1645 } 1646 } 1647 } 1648 } 1649 1650 if (found) { 1651 toAdd[j] = false; 1653 if (DDLogFlags.debug) { 1654 TraceLogger.put(TraceLogger.DEBUG, 1655 TraceLogger.SVC_DD, 1656 DDLogFlags.DBG_UBN, 5, 1657 DDLogFlags.MERGEFOUND, 1658 name + "[" + i + "] <=> " + 1659 name + "[" + j + "]"); 1660 } 1661 } else { 1662 toRemove[i] = true; 1664 if (DDLogFlags.debug) { 1665 TraceLogger.put(TraceLogger.DEBUG, 1666 TraceLogger.SVC_DD, 1667 DDLogFlags.DBG_UBN, 5, 1668 DDLogFlags.MERGENTFND, 1669 name + "[" + i + 1670 "] to be removed"); 1671 } 1672 } 1673 } 1674 1675 if (!hasKeyDefined) 1681 hasKey = this.setHasKeyDefaultValue(prop); 1682 1683 if ((mode & MERGE_COMPARE) == MERGE_COMPARE) { 1684 for (i=0; i<size1; i++) 1686 if (toRemove[i] && hasKey) 1687 return false; 1688 1689 for (j=0; j<size2; j++) 1690 if (toAdd[j] && hasKey) 1691 return false; 1692 } 1693 1694 if ((mode & MERGE_INTERSECT) == MERGE_INTERSECT) { 1696 for (i=0, j=0; i<size1; i++) { 1697 if (toRemove[i] && hasKey) { 1698 removeValue(prop, i-j); 1700 j++; 1701 } 1702 } 1703 } 1704 1705 if ((mode & MERGE_UNION) == MERGE_UNION) { 1707 for (j=0; j < size2; j++) { 1708 if (toAdd[j] && hasKey) { 1709 if (isBean) { 1711 BaseBean srcBean = (BaseBean) bean.getValue(name, j); 1713 o2 = srcBean.clone(); 1714 addValue(prop, o2); 1715 ((BaseBean)o2).mergeTree(srcBean, mode); 1717 } else { 1718 this.copyProperty(prop, bean, j, null); 1720 } 1721 } 1722 } 1723 } 1724 } else { 1725 Object newValue = null; 1726 boolean found = false; 1727 1728 if (DDLogFlags.debug) { 1729 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 1730 DDLogFlags.DBG_UBN, 5, 1731 DDLogFlags.MERGEPROP, 1732 this.getClass().getName() + "." + 1733 name); 1734 } 1735 1736 o1 = prop.getValue(0); o2 = bean.getValue(name); 1740 for (int c=0; c<this.comparators.size() && !found; c++) { 1753 BeanComparator cmp = 1754 (BeanComparator)this.comparators.get(c); 1755 1756 if (isBean) { 1757 if (o1 != null && o2 != null) { 1758 o3 = cmp.compareBean(name, (BaseBean)o1, 1760 (BaseBean)o2); 1761 1762 if (!hasKey) 1763 hasKey = cmp.hasKey(); 1764 1765 if (o3 != o1) 1766 newValue = (c==0)?o3:newValue; 1767 else { 1768 found = true; 1769 boolean ret = ((BaseBean)o1). 1770 mergeTree((BaseBean)o2, mode); 1771 if (!ret) return ret; 1772 } 1773 } else { 1774 if (o1 == o2) 1775 found = true; 1776 else { 1777 hasKey = cmp.hasKeyDefined(prop); 1778 newValue = (c==0)?o2:newValue; 1779 } 1780 } 1781 } else { 1782 o3 = cmp.compareProperty(name, this, o1, -1, 1783 bean, o2, -1); 1784 1785 if (!hasKey) 1786 hasKey = cmp.hasKey(); 1787 1788 if (o3 != o1) 1789 newValue = (c==0)?o3:newValue; 1790 else 1791 found = true; 1792 } 1793 1794 if (!found && ((mode & MERGE_COMPARE)==MERGE_COMPARE)) { 1795 return false; 1797 } 1798 1799 if (!found && ((mode & MERGE_UNION) == MERGE_UNION) 1800 && hasKey) { 1801 1802 if (isBean) { 1803 if (newValue != null) { 1804 setValue(prop, 0, 1805 ((BaseBean)newValue).clone()); 1806 } else { 1807 setValue(prop, 0, newValue); 1808 } 1809 } else { 1810 this.copyProperty(prop, bean, 0, newValue); 1812 } 1813 1814 if (DDLogFlags.debug) { 1815 TraceLogger.put(TraceLogger.DEBUG, 1816 TraceLogger.SVC_DD, 1817 DDLogFlags.DBG_UBN, 5, 1818 DDLogFlags.MERGENTFND, 1819 "updating with new value"); 1820 } 1821 } else 1822 if (found) { 1823 if (DDLogFlags.debug) { 1824 TraceLogger.put(TraceLogger.DEBUG, 1825 TraceLogger.SVC_DD, 1826 DDLogFlags.DBG_UBN, 5, 1827 DDLogFlags.MERGEFOUND, 1828 "keeping current value"); 1829 } 1830 } 1831 } 1832 } 1833 } 1834 1835 1836 if ((mode == MERGE_UPDATE)) { 1837 if (isRoot) { 1840 Schema2BeansUtil.mergeUnsupportedElements(this, bean); 1841 } 1842 } 1843 1844 return true; 1850 } else 1851 throw new IllegalArgumentException (Common.getMessage( 1852 "MergeWrongClassType_msg", this.getClass().getName(), 1853 (bean==null ? "<null>" : bean.getClass().getName()))); 1854 } 1855 1856 1860 private boolean areNodesEqual(Node node1, Node node2) { 1861 if (node1 == null && node2 == null) 1862 return true; 1863 if (node1 == null || node2 == null) 1864 return false; 1865 if (node1.getNodeType() != node2.getNodeType()) 1866 return false; 1867 if (!node1.getNodeName().equals(node2.getNodeName())) 1868 return false; 1869 String value1 = node1.getNodeValue(); 1870 String value2 = node2.getNodeValue(); 1871 if (value1 == null) { 1872 if (value2 != null) 1873 return false; 1874 } else if (!value1.equals(value2)) 1875 return false; 1876 return true; 1877 } 1878 1879 1884 private int findInNodeList(NodeList nodes, Node node, int start) { 1885 return findInNodeList(nodes, node, start, nodes.getLength()); 1886 } 1887 1888 1894 private int findInNodeList(NodeList nodes, Node node, 1895 int start, int maxPosition) { 1896 for (; start < maxPosition; ++start) { 1897 if (areNodesEqual(nodes.item(start), node)) 1898 return start; 1899 } 1900 return -1; 1901 } 1902 1903 1915 1916 public boolean isEqualTo(Object obj) { 1917 boolean ret = false; 1918 1919 try { 1920 if (this == obj) 1921 return true; 1922 else 1923 if (obj instanceof BaseBean) 1924 ret = this.mergeTreeRoot((BaseBean)obj, MERGE_COMPARE); 1925 } catch(Exception e) { 1926 if (DDLogFlags.debug) { 1928 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 1929 DDLogFlags.DBG_UBN, 1, DDLogFlags.EQUALS, 1930 "got exception while comparing: " + 1931 e + "\n"); 1932 e.printStackTrace(); 1933 } 1934 ret = false; 1935 } 1936 1937 if (DDLogFlags.debug) { 1938 TraceLogger.put(TraceLogger.DEBUG, TraceLogger.SVC_DD, 1939 DDLogFlags.DBG_UBN, 1, DDLogFlags.EQUALS, 1940 (ret?"true":"false")); 1941 } 1942 return ret; 1943 } 1944 1945 1951 public Bean propertyById(String name, int id) { 1952 BeanProp bp = this.beanProp(name); 1953 1954 if (Common.isBean(bp.type)) { 1955 if (Common.isArray(bp.type)) 1956 return (BaseBean)bp.getValueById(id); 1957 else 1958 return (BaseBean)bp.getValue(0); 1959 } else 1960 throw new IllegalStateException (Common. 1961 getMessage("PropertyIsNotABean_msg", name)); 1962 } 1963 1964 1967 public BeanProp beanProp() { 1968 if (this.binding != null) 1969 return this.binding.getBeanProp(this); 1970 else 1971 return null; 1972 } 1973 1974 1979 public BaseBean parent() { 1980 BeanProp bp = this.beanProp(); 1981 1982 if (bp != null) 1983 return this.beanProp().getBean(); 1984 else 1985 return null; 1986 } 1987 1988 public Bean _getParent() { 1989 return parent(); 1990 } 1991 1992 1996 public Bean _getRoot() { 1997 Bean b = this; 1998 Bean bParent; 1999 while (!b.isRoot()) { 2000 bParent = b._getParent(); 2001 if (bParent == null) 2002 break; 2003 b = bParent; 2004 } 2005 return b; 2006 } 2007 2008 2011 public String fullName() { 2012 StringBuffer str = new StringBuffer (); 2013 this.buildPathName(str); 2014 return str.toString(); 2015 } 2016 2017 public boolean hasName(String name) { 2018 if (name != null) 2019 return (name.equals(this.name()) || name.equals(this.dtdName())); 2020 else 2021 return false; 2022 } 2023 2024 2027 public boolean isRoot() { 2028 return this.isRoot; 2029 } 2030 2031 2034 public String name() { 2035 BeanProp bp = this.beanProp(); 2036 2037 if (bp != null) 2038 return this.beanProp().getBeanName(); 2039 else 2040 return ""; } 2042 2043 2046 public String dtdName() { 2047 return this.beanProp().getDtdName(); 2048 } 2049 2050 public void createBean(Node node, GraphManager mgr) throws Schema2BeansRuntimeException { 2051 if (this.isRoot) { 2052 mgr.completeRootBinding(this, node); 2053 } 2054 2055 this.graphManager = mgr; 2056 try { 2057 mgr.fillProperties(this.beanProps(), node); 2058 } catch (Schema2BeansException e) { 2059 throw new Schema2BeansRuntimeException(e); 2060 } 2061 } 2062 2063 2066 public BaseBean newInstance(String name) { 2067 return this.beanProp(name).newBeanInstance(); 2068 } 2069 2070 2071 public abstract void dump(StringBuffer str, String indent); 2072 2073 2077 2081 public String dumpDomNode(String nodeName, int depth) { 2082 Node n = null; 2083 2084 if (this.binding == null) 2085 return "<no binding>"; else { 2087 n = this.binding.node; 2088 if (n == null) 2089 return "<no node>"; } 2091 2092 return DDFactory.XmlToString(n, depth, nodeName); 2093 } 2094 2095 public String dumpDomNode(int depth) { 2096 return this.dumpDomNode(null, depth); 2097 } 2098 2099 public String dumpDomNode() { 2100 return this.dumpDomNode(null, 99999); 2101 } 2102 2103 public String dumpBeanNode() { 2104 return null; 2105 } 2106 2107 public void dumpAttributes(String name, int index, StringBuffer str, 2108 String indent) { 2109 String [] names = this.getAttributeNames(name); 2110 2111 for (int i=0; i<names.length; i++) { 2112 String v = this.getAttributeValue(name, index, names[i]); 2113 if (v != null) { 2114 str.append(indent + "\t attr: "); str.append(names[i]); 2116 str.append("="); str.append(v); 2118 } 2119 } 2120 } 2121 2122 public String toString() { 2123 return this.name(); 2124 } 2125 2126 2127 public void dumpXml() { 2128 try { 2129 write(System.out); 2130 } catch (IOException ee) { 2131 ee.printStackTrace(); 2132 } 2133 } 2134 2135 2136 2139 public static BaseBean createGraph(Class clazz, InputStream in) 2140 throws Schema2BeansException { 2141 return createGraph(clazz, in, false, null, null); 2142 } 2143 2144 2148 public static BaseBean createGraph(Class clazz, InputStream in, 2149 boolean validate) throws Schema2BeansException { 2150 return createGraph(clazz, in, validate, null, null); 2151 } 2152 2153 2157 public static BaseBean createGraph(Class clazz, InputStream in, 2158 boolean validate, 2159 org.xml.sax.EntityResolver er) throws 2160 Schema2BeansException { 2161 return createGraph(clazz, in, validate, er, null); 2162 } 2163 2164 2168 public static BaseBean createGraph(Class clazz, InputStream in, 2169 boolean validate, 2170 org.xml.sax.EntityResolver er, 2171 org.xml.sax.ErrorHandler eh) throws 2172 Schema2BeansException { 2173 2174 Constructor c = null; 2175 Document doc = null; 2176 BaseBean bean = null; 2177 2178 doc = GraphManager.createXmlDocument(new org.xml.sax.InputSource (in), 2179 validate, er, eh); 2180 try { 2181 Class [] cc = new Class [] {org.w3c.dom.Node .class, 2182 int.class}; 2183 c = clazz.getDeclaredConstructor(cc); 2184 } catch(NoSuchMethodException me) { 2185 throw new RuntimeException (Common. 2186 getMessage("CantGetConstructor_msg")); 2187 } 2188 2189 Object [] p = new Object [] {doc, new Integer (Common.NO_DEFAULT_VALUES)}; 2190 2191 try { 2192 bean = (BaseBean)c.newInstance(p); 2193 } catch (InstantiationException e) { 2194 throw new Schema2BeansNestedException(Common.getMessage( 2195 "CantInstanciateBeanClass_msg"), e); 2196 } catch (IllegalAccessException e) { 2197 throw new Schema2BeansNestedException(Common.getMessage( 2198 "CantInstanciateBeanClass_msg"), e); 2199 } catch (java.lang.reflect.InvocationTargetException e) { 2200 throw new Schema2BeansNestedException(Common.getMessage( 2201 "CantInstanciateBeanClass_msg"), e); 2202 } 2203 2204 return bean; 2205 } 2206 2207 2208 PropertyChangeSupport changeListeners; 2210 2211 2212 public void addPropertyChangeListener(PropertyChangeListener l) { 2214 BeanProp p = this.beanProp(); 2215 if (p != null) { 2216 p.addPCListener(l); 2217 } else { 2218 if (this.changeListeners == null) { 2219 this.changeListeners = new PropertyChangeSupport(this); 2220 } 2221 this.changeListeners.addPropertyChangeListener(l); 2222 } 2223 } 2224 2225 public void removePropertyChangeListener(PropertyChangeListener l) { 2227 BeanProp p = this.beanProp(); 2228 if (p != null) { 2229 p.removePCListener(l); 2230 } else if (this.changeListeners != null) { 2231 this.changeListeners.removePropertyChangeListener(l); 2232 } 2233 } 2234 2235 public void addPropertyChangeListener(String n, PropertyChangeListener l) { 2237 BeanProp p = this.beanProp(n); 2238 if (p != null) 2239 p.addPCListener(l); 2240 } 2241 2242 public void removePropertyChangeListener(String n, PropertyChangeListener l) { 2244 BeanProp p = this.beanProp(n); 2245 if (p != null) 2246 p.removePCListener(l); 2247 } 2248 2249 2255 public org.w3c.dom.Comment [] comments() { 2256 if (graphManager == null) 2257 return new org.w3c.dom.Comment [0]; 2258 Document doc = graphManager.getXmlDocument(); 2259 Node node = binding.getNode(); 2260 NodeList children = node.getChildNodes(); 2261 List foundComments = new LinkedList(); 2262 for (int i = 0; i < children.getLength(); ++i) { 2263 Node child = children.item(i); 2264 if (child instanceof org.w3c.dom.Comment ) { 2265 foundComments.add(child); 2266 } 2267 } 2268 org.w3c.dom.Comment [] result = new org.w3c.dom.Comment [foundComments.size()]; 2269 result = (org.w3c.dom.Comment []) foundComments.toArray(result); 2270 return result; 2271 } 2272 2273 2279 public org.w3c.dom.Comment addComment(String comment) { 2280 if (graphManager == null) { 2281 } 2283 Document doc = graphManager.getXmlDocument(); 2284 org.w3c.dom.Comment commentNode = doc.createComment(comment); 2285 Node node = binding.getNode(); 2286 Node firstChild = node.getFirstChild(); 2287 if (firstChild == null) { 2288 node.appendChild(commentNode); 2289 } else { 2290 node.insertBefore(commentNode, firstChild); 2291 } 2292 return commentNode; 2293 } 2294 2295 2298 public void removeComment(org.w3c.dom.Comment comment) { 2299 comment.getParentNode().removeChild(comment); 2300 } 2301 2302 2305 public BaseBean[] childBeans(boolean recursive) { 2306 List children = new LinkedList(); 2307 childBeans(recursive, children); 2308 BaseBean[] result = new BaseBean[children.size()]; 2309 return (BaseBean[]) children.toArray(result); 2310 } 2311 2312 2315 public void childBeans(boolean recursive, List beans) { 2316 BeanProp[] props = beanProps(); 2317 BaseBean nextBean; 2318 for (int propPosition = 0; propPosition < props.length; ++propPosition) { 2319 BeanProp prop = props[propPosition]; 2320 if (!prop.isBean()) { 2321 continue; 2322 } 2323 if (prop.isIndexed()) { 2324 for (int i = 0; i < prop.size(); ++i) { 2325 nextBean = (BaseBean) prop.getValue(i); 2326 if (nextBean == null) 2327 continue; 2328 beans.add(nextBean); 2329 if (recursive) 2330 nextBean.childBeans(true, beans); 2331 } 2332 } else { 2333 nextBean = (BaseBean) prop.getValue(0); 2334 if (nextBean == null) 2335 continue; 2336 if (recursive) 2337 nextBean.childBeans(true, beans); 2338 beans.add(nextBean); 2339 } 2340 } 2341 } 2342 2343 public String nameSelf() { 2344 return beanProp().getFullName(); 2345 } 2346 2347 public String nameChild(Object childObj) { 2348 return nameChild(childObj, false, false); 2349 } 2350 2351 public String nameChild(Object childObj, boolean returnConstName, 2352 boolean returnSchemaName) { 2353 return nameChild(childObj, returnConstName, returnSchemaName, false); 2354 } 2355 2356 public String nameChild(Object childObj, boolean returnConstName, 2357 boolean returnSchemaName, 2358 boolean returnXPathName) { 2359 BeanProp[] props = beanProps(); 2360 Object propValue; 2361 BeanProp prop = null; 2362 boolean found = false; 2363 int index = -2; 2364 propLoop: 2365 for (int propPosition = 0; propPosition < props.length; ++propPosition) { 2366 prop = props[propPosition]; 2367 if (prop.isIndexed()) { 2368 for (int i = 0; i < prop.size(); ++i) { 2369 propValue = prop.getValue(i); 2370 if (propValue == null ? childObj == null : propValue.equals(childObj)) { 2371 found = true; 2372 index = i; 2373 break propLoop; 2374 } 2375 } 2376 } else { 2377 propValue = prop.getValue(0); 2378 if (propValue == null ? childObj == null : propValue.equals(childObj)) { 2379 found = true; 2380 break propLoop; 2381 } 2382 } 2383 } 2384 if (found) { 2385 if (returnConstName) 2386 return prop.getBeanName(); 2387 else if (returnSchemaName) 2388 return prop.dtdName; 2389 else if (returnXPathName) { 2390 if (index < 0) 2391 return prop.dtdName; 2392 else 2393 return prop.dtdName+"[position()="+index+"]"; 2394 } else 2395 return prop.getBeanName()+"."+Integer.toHexString(prop.indexToId(index)); 2396 } 2397 return null; 2398 } 2399 2400 public void changeDocType(String publicId, String systemId) { 2401 graphManager().setDoctype(publicId, systemId); 2402 } 2403 2404 public void _setChanged(boolean changed) { 2405 throw new UnsupportedOperationException (); 2406 } 2407 2408 public String _getXPathExpr() { 2409 if (parent() == null) { 2410 return "/"+dtdName(); 2411 } else { 2412 String parentXPathExpr = parent()._getXPathExpr(); 2413 String myExpr = parent().nameChild(this, false, false, true); 2414 return parentXPathExpr + "/" + myExpr; 2415 } 2416 } 2417 2418 public String _getXPathExpr(Object childObj) { 2419 String childName = nameChild(childObj, false, false, true); 2420 if (childName == null) { 2421 throw new IllegalArgumentException ("childObj ("+childObj.toString()+") is not a child of this bean ("+dtdName()+")"); 2422 } 2423 return _getXPathExpr() + "/" + childName; 2424 } 2425} | Popular Tags |