1 19 20 package org.netbeans.modules.schema2beansdev; 21 22 import java.util.*; 23 import java.io.*; 24 25 import org.netbeans.modules.schema2beans.*; 26 import org.netbeans.modules.schema2beansdev.metadd.*; 27 import org.netbeans.modules.schema2beansdev.gen.*; 28 29 public abstract class AbstractCodeGeneratorClass { 30 static class Property { 31 String dtdName; 32 private String namespace; 33 String name; 34 String classType; 35 int type; 36 int elementInstance; 37 int groupInstance; 38 int level; 39 boolean isBean; 40 boolean ored; 41 boolean isUnion = false; 42 String constName; 43 private boolean nillable; 44 private boolean _isAttribute; 45 private AttrProp attrProp; 46 private Property attributeOwner = null; 47 private String defaultValue; 48 private boolean directChild; 49 private GraphNode graphNode; 50 private GraphLink graphLink; 51 private List extraData; 52 private boolean canBeEmpty = false; 53 private String propertyInterface = null; 54 private transient List mutuallyExclusiveProperties; 55 private BeanBuilder.BeanElement beanElement; 56 58 AttrProp[] attributes; 60 61 Property(String propName, String dtdName, String namespace, 62 GraphNode graphNode, 63 GraphLink graphLink, String classType, 64 int eInst, int gInst, int level, int type, boolean ored, 65 AttrProp[] attrs, String constName, String defaultValue, 66 boolean directChild, List extraData, boolean isUnion) { 67 if (gInst == Common.TYPE_0_N) 72 eInst = gInst; 73 else 74 if (gInst == Common.TYPE_1_N) { 75 if (eInst == Common.TYPE_0_1) 76 eInst = Common.TYPE_0_N; 77 else 78 eInst = Common.TYPE_1_N; 79 } 80 81 this.name = propName; 82 this.isUnion = isUnion; 83 this.dtdName = dtdName; 84 this.namespace = namespace; 85 this.graphNode = graphNode; 86 this.graphLink = graphLink; 87 this.classType = classType; 88 this.type = type; 89 this.elementInstance = eInst; 90 this.groupInstance = gInst; 91 this.level = level; 92 this.attributes = attrs; 93 this.ored = ored; 94 this.constName = constName; 95 this.isBean = Common.isBean(type); 96 if (isScalar()) { 97 this.classType = Common.wrapperClass(type); 98 } 99 this.defaultValue = defaultValue; 100 this.directChild = directChild; 101 this.extraData = extraData; 102 } 104 105 public boolean isIndexed() { 106 if (elementInstance == Common.TYPE_1_N) { 107 return true; 108 } else if (elementInstance == Common.TYPE_0_N) { 109 return true; 110 } 111 return false; 112 } 113 114 public void setIndexed(boolean value) { 115 if (isIndexed() == value) 116 return; 117 if (value) 118 elementInstance = Common.TYPE_0_N; 119 else 120 elementInstance = Common.TYPE_0_1; 121 } 122 123 public boolean isNillable() { 124 return nillable; 125 } 126 127 public void setNillable(boolean value) { 128 nillable = value; 129 } 130 131 public boolean isUnion() { 132 return isUnion; 133 } 134 135 public void setUnion(boolean value) { 136 isUnion = value; 137 } 138 139 public boolean isScalar() { 140 return Common.isScalar(type); 141 } 142 143 public String getScalarType() { 144 if (!isScalar()) 145 return null; 146 return Common.scalarType(type); 147 } 148 149 152 public String getType() { 153 if (isScalar()) 154 return getScalarType(); 155 else 156 return classType; 157 } 158 159 public void setAttribute(boolean value) { 160 _isAttribute = value; 161 } 162 163 public boolean isAttribute() { 164 return _isAttribute; 165 } 166 167 public void setAttrProp(AttrProp ap) { 168 attrProp = ap; 169 setAttribute(ap != null); 170 } 171 172 public AttrProp getAttrProp() { 173 return attrProp; 174 } 175 176 public void setAttributeOwner(Property owner) { 177 attributeOwner = owner; 178 } 179 180 public Property getAttributeOwner() { 181 return attributeOwner; 182 } 183 184 public String getDefaultValue() { 185 return defaultValue; 186 } 187 188 public boolean isDirectChild() { 189 return directChild; 190 } 191 192 public int getGroupInstance() { 193 return groupInstance; 194 } 195 196 201 public void setCanBeEmpty(boolean value) { 202 canBeEmpty = value; 203 } 204 205 public boolean getCanBeEmpty() { 206 return canBeEmpty; 207 } 208 209 212 public GraphNode getGraphNode() { 213 return graphNode; 214 } 215 216 222 public GraphLink getGraphLink() { 223 return graphLink; 224 } 225 226 public void setPropertyInterface(String iface) { 227 propertyInterface = iface; 228 } 229 230 234 public String getPropertyInterface() { 235 return propertyInterface; 236 } 237 238 243 public String getSignatureType(String packageName) { 244 if (getPropertyInterface() == null) 245 return getTypeFullClassName(packageName); 246 else 247 return getPropertyInterface(); 248 } 249 250 public String getTypeFullClassName(String packageName) { 251 return getTypeFullClassName(packageName, getType()); 252 } 253 254 public String getTypeFullClassName(String packageName, String typeName) { 255 if (isBean && getGraphNode().isCreated() && packageName != null) 256 return packageName+"."+typeName; 257 return typeName; 258 } 259 260 public String getDTDName() { 261 return dtdName; 262 } 263 264 public String getNamespace() { 265 return namespace; 266 } 267 268 public BeanBuilder.BeanElement getBeanElement() { 269 return beanElement; 270 } 271 272 public void setBeanElement(BeanBuilder.BeanElement be) { 273 beanElement = be; 274 } 275 276 public String instanceOf() { 277 String result = java.beans.Introspector.decapitalize(name); 278 while (JavaUtil.reservedWord(result)) 279 result = "a_"+result; 280 return result; 281 } 282 283 public String beanIntrospectorName() { 284 return java.beans.Introspector.decapitalize(name); 285 } 286 287 public String getEventName() { 292 return name; 293 } 294 295 300 public String getReadMethod(boolean withIndexParameter) { 301 if (!withIndexParameter && isIndexed()) 302 return "get"+name; 303 else if (isScalar() && Common.isBoolean(type)) 304 return "is"+name; 305 else 306 return "get"+name; 307 } 308 309 public String getWriteMethod() { 310 return "set"+name; 311 } 312 313 public String getAddMethod() { 314 return "add"+name; 315 } 316 317 public String getRemoveMethod() { 318 return "remove"+name; 319 } 320 321 public String getSizeMethod() { 322 return "size"+name; 323 } 324 325 public String getScalarIsSet() { 326 return "_isSet_"+name; 327 } 328 329 public List getMutuallyExclusiveProperties() { 330 if (mutuallyExclusiveProperties == null && graphLink != null) { 331 List mutuallyExclusiveGraphLinks = new ArrayList(); 334 graphLink.getMutuallyExclusiveLinks(mutuallyExclusiveGraphLinks); 335 mutuallyExclusiveProperties = new ArrayList(mutuallyExclusiveGraphLinks.size()); 336 for (Iterator it = mutuallyExclusiveGraphLinks.iterator(); it.hasNext(); ) { 337 GraphLink l = (GraphLink) it.next(); 338 if (l.getObject() != null) 339 mutuallyExclusiveProperties.add(l.getObject()); 340 } 341 } 342 return mutuallyExclusiveProperties; 343 } 344 345 361 362 public boolean canBeNull() { 363 boolean canBeNull = false; 364 boolean isPrimitiveType = JavaUtil.isPrimitiveType(getType()); 365 if (ored) { 366 canBeNull = !isPrimitiveType; 367 } else { 368 switch (elementInstance & Common.MASK_INSTANCE) { 369 case Common.TYPE_0_1: 370 case Common.TYPE_1_N: 371 case Common.TYPE_0_N: 372 canBeNull = !isPrimitiveType; 373 break; 374 } 375 } 376 return canBeNull; 378 } 379 380 public Object searchExtraData(Class type) { 381 for (Iterator it = extraData.iterator(); it.hasNext(); ) { 382 Object o = it.next(); 383 if (type.isAssignableFrom(o.getClass())) 385 return o; 386 } 387 return null; 388 } 389 390 public Iterator extraDataIterator() { 391 return extraData.iterator(); 392 } 393 } 394 395 class PropertyVisitor { 396 protected Property curProp; 397 protected int propNum; 398 protected int propCount; 399 400 public PropertyVisitor() { 401 } 402 403 public void generate() throws IOException { 404 propCount = attrList.size(); 405 preGenerate(); 406 for (propNum = 0; propNum < propCount; ++propNum) { 407 curProp = (Property) attrList.get(propNum); 408 if (skip()) 409 continue; 410 preGenerateProp(); 411 generateProp(); 412 postGenerateProp(); 413 } 414 postGenerate(); 415 } 416 417 public void preGenerate() throws IOException {} 418 419 public boolean skip() { 420 return false; 421 } 422 423 public void preGenerateProp() throws IOException {} 424 425 public void generateProp() throws IOException {} 426 427 public void postGenerateProp() throws IOException {} 428 429 public void postGenerate() throws IOException {} 430 } 431 432 class NamePropertyVisitor extends PropertyVisitor { 433 protected String nameVar; 434 435 public NamePropertyVisitor(String nameVar) { 436 this.nameVar = nameVar; 437 } 438 439 public void preGenerate() throws IOException { 440 jw.writeEol(nameVar+" = "+nameVar+".intern()"); 441 } 442 443 public void preGenerateProp() throws IOException { 444 if (curProp.name.equals(curProp.dtdName)) 445 jw.beginIf(nameVar+" == "+curProp.constName); 446 else 447 jw.beginIf(nameVar+" == "+curProp.constName+" || "+nameVar+" == \""+curProp.dtdName+"\""); 448 } 449 450 public void postGenerateProp() throws IOException { 451 jw.endElse(); 452 } 453 454 public void postGenerate() throws IOException { 455 genInvalidName(nameVar); 456 } 457 } 458 459 protected void genInvalidName(String var) throws IOException { 460 jw.writeEol("throw new IllegalArgumentException(", 461 var, "+\" is not a valid property name for ", 462 className+"\")"); 463 } 464 465 protected GenBeans.Config config; 466 protected String packageName; 467 protected String className; 468 protected String fullClassName; 469 470 protected BeanBuilder.BeanElement beanElement; 471 protected BeanBuilder.BeanElement rootBeanElement; 472 473 private String defaultNamespace = null; 474 475 protected MetaDD mdd; 477 478 protected MetaElement metaElement; 480 481 protected Map generatedNewMethods = new HashMap(); 484 protected ArrayList attrList; 486 487 protected PrefixGuesser prefixGuesser; 488 489 protected int HEADER_SECTION = 0; 491 protected int DECL_SECTION = 1; 492 protected int CONSTRUCTOR_SECTION = 2; 493 protected int INITIALIZE_SECTION = 3; 494 protected int ACCESS_SECTION = 4; 495 protected int NEW_METHOD_SECTION = 5; 496 protected int BODY_SECTION = 6; 497 protected int EQUALS_SECTION = 7; 498 protected int HASHCODE_SECTION = 8; 499 protected int TRAILER_SECTION = 9; 500 protected int MAXVALUE = 10; 501 502 JavaWriter jw; 503 504 private Map mutableTypes = new HashMap(); 505 private Map needToCallClone = new HashMap(); 506 507 protected boolean isMutableType(Property prop) { 508 String type = prop.getType(); 509 Boolean mutable = (Boolean ) mutableTypes.get(type); 510 if (mutable == null) { 511 if (prop.isBean) 512 mutable = Boolean.TRUE; 513 else if (prop.isScalar() || JavaUtil.isImmutable(type)) 514 mutable = Boolean.FALSE; 515 else 516 mutable = Boolean.TRUE; 517 mutableTypes.put(type, mutable); 518 } 519 return mutable.booleanValue(); 520 } 521 522 protected boolean isCloneCallNeededOnType(Property prop) { 523 String type = prop.getType(); 524 Boolean callClone = (Boolean ) needToCallClone.get(type); 525 if (callClone == null) { 526 if (prop.isBean || !isMutableType(prop)) 527 callClone = Boolean.FALSE; 528 else 529 callClone = JavaUtil.isCloneable(type) ? Boolean.TRUE : Boolean.FALSE; 530 needToCallClone.put(type, callClone); 531 } 532 return callClone.booleanValue(); 533 } 534 535 protected boolean genCopyWillCopy(Property a) { 536 String type = a.getType(); 537 boolean mutable = isMutableType(a); 538 boolean needToCallClone = isCloneCallNeededOnType(a); 539 if ((mutable || needToCallClone) 540 && !a.isBean && !JavaUtil.isInstantiable(type)) { 541 mutable = false; 542 needToCallClone = false; 543 } 544 if (type == "java.io.File") { 545 return true; 546 } else if (needToCallClone) { 547 return true; 548 } else if (mutable) { 549 return true; 550 } else { 551 return false; 552 } 553 } 554 555 protected void genCopy(String src, Property a, String justData) throws IOException { 556 String type = a.getType().intern(); 557 String fullClassType = getTypeFullClassName(a); 558 boolean mutable = isMutableType(a); 559 boolean needToCallClone = isCloneCallNeededOnType(a); 560 if ((mutable || needToCallClone) 561 && !a.isBean && !JavaUtil.isInstantiable(type)) { 562 mutable = false; 563 needToCallClone = false; 564 } 565 if (type == "java.io.File") { 566 jw.write("(", src, " == null) ? null : "); 567 gen("new "); 568 gen(fullClassType); 569 PO(); 570 jw.write(src); 571 jw.write(".getAbsolutePath()"); 572 PC(); 573 } else if (needToCallClone) { 574 jw.write("(", src, " == null) ? null : "); 575 PO(); 576 gen(fullClassType); 577 PC(); 578 gen(src); 579 gen(".clone()"); 580 } else if (mutable) { 581 jw.write("(", src, " == null) ? null : "); 582 String castSourceTo = null; 583 if (a.isBean && hasDeepCopyConstructor()) { 584 castSourceTo = a.getPropertyInterface(); 585 if (a.getPropertyInterface() != null && !type.equals(a.getPropertyInterface())) 586 jw.write("(", type, ") "); 587 jw.write(genNewMethod(a.getPropertyInterface(), type)); 588 } else { 589 gen("new "); 590 gen(fullClassType); 591 } 592 593 PO(); 594 if (castSourceTo != null) 595 jw.write("(", castSourceTo, ") "); 596 jw.write(src); 597 if (a.isBean) { 598 if (config.isGenerateParentRefs()) { 599 jw.write(", this"); 600 } 601 jw.write(", ", justData); 602 } 603 PC(); 604 } else { 605 jw.write(src); 606 } 607 } 608 609 protected boolean isTypeQName(String type) { 610 type = type.intern(); 611 if (type == "javax.xml.namespace.QName") 612 return true; 613 if (type == "org.netbeans.modules.schema2beans.QName") 614 return true; 615 return false; 616 } 617 618 623 static protected final int PUBLIC = 0; 624 static protected final int PRIVATE = 1; 625 static protected final int VOID = 2; 626 static protected final int CLASS = 3; 627 static protected final int IMPORT = 4; 628 static protected final int PACKAGE = 5; 629 static protected final int STRING = 6; 630 static protected final int BOOLEAN = 7; 631 static protected final int STATIC = 8; 632 static protected final int FINAL = 9; 633 static protected final int INT = 10; 634 static protected final int PROTECTED = 11; 635 636 static protected final String arrKeywords[] = { 637 "public", 638 "private", 639 "void", 640 "class", 641 "import", 642 "package", 643 "String", 644 "boolean", 645 "static", 646 "final", 647 "int", 648 "protected" 649 }; 650 651 protected void init(BeanBuilder.BeanElement be, GenBeans.Config config) { 652 this.config = config; 653 beanElement = be; 654 packageName = null; 655 className = be.getClassType(); 656 fullClassName = className; 657 attrList = new ArrayList(); 658 jw = new JavaWriter(); 659 HEADER_SECTION = jw.HEADER_SECTION; 660 DECL_SECTION = jw.DECL_SECTION; 661 CONSTRUCTOR_SECTION = jw.CONSTRUCTOR_SECTION; 662 INITIALIZE_SECTION = jw.insertSectionAfter(jw.CONSTRUCTOR_SECTION); 663 ACCESS_SECTION = jw.insertSectionAfter(INITIALIZE_SECTION); 664 NEW_METHOD_SECTION = jw.insertSectionAfter(ACCESS_SECTION); 665 BODY_SECTION = jw.BODY_SECTION; 666 EQUALS_SECTION = jw.insertSectionAfter(BODY_SECTION); 667 HASHCODE_SECTION = jw.insertSectionAfter(EQUALS_SECTION); 668 resetGenBuffers(); 669 } 670 671 protected void resetGenBuffers() { 672 jw.reset(); 673 jw.select(HEADER_SECTION); 674 } 675 676 protected void printGenBuffers(OutputStream out) throws IOException { 677 jw.writeTo(out); 678 } 679 680 public void setPackageName(String n) { 681 if (n == null || "".equals(n)) { 682 packageName = null; 683 fullClassName = className; 684 } else { 685 packageName = n.replace('/', '.'); 686 fullClassName = packageName + "." + className; 687 } 688 } 689 690 public void setDefaultNamespace(String ns) { 691 defaultNamespace = ns; 692 } 693 694 public String getDefaultNamespace() { 695 return defaultNamespace; 696 } 697 698 704 protected String getTypeFullClassName(Property a) { 705 return a.getTypeFullClassName(packageName); 706 } 707 708 protected String getTypeFullClassName(Property a, String typeName) { 709 return a.getTypeFullClassName(packageName, typeName); 710 } 711 712 public void setIndent(String indent) { 713 jw.setIndent(indent); 714 } 715 716 public void setRootBeanElement(BeanBuilder.BeanElement element) { 717 rootBeanElement = element; 718 } 719 720 private transient String cachedRootClassName = null; 721 protected String getRootClassName() { 722 if (cachedRootClassName == null) { 723 String rootClassName = rootBeanElement.getClassType(); 724 if (packageName == null) 725 cachedRootClassName = rootClassName; 726 else 727 cachedRootClassName = packageName + "." + rootClassName; 728 } 729 return cachedRootClassName; 730 } 731 732 737 protected void findAttributeOwners() { 738 Property attributeOwner; 739 int size = attrList.size(); 740 for (int i = 0; i < size; i++) { 741 Property a = (Property)attrList.get(i); 742 if (a.isAttribute()) { 743 attributeOwner = a; 744 AttrProp attrProp = a.getAttrProp(); 745 propertyLoop: 746 for (int propNum = 0; propNum < size; ++propNum) { 747 Property prop = (Property) attrList.get(propNum); 748 if (prop.attributes == null) 749 continue; 750 for (int attrNum = 0; attrNum < prop.attributes.length; ++attrNum) { 751 AttrProp propAttr = prop.attributes[attrNum]; 752 if (propAttr == attrProp) { 753 attributeOwner = prop; 755 break propertyLoop; 756 } 757 } 758 } 759 a.setAttributeOwner(attributeOwner); 760 a.setIndexed(attributeOwner.isIndexed()); 761 } 762 } 763 } 764 765 768 protected Property findProperty(AttrProp attr) { 769 int size = attrList.size(); 770 for (int j = 0; j < size; j++) { 771 Property p = (Property)attrList.get(j); 772 if (p.getAttrProp() == attr) { 773 return p; 775 } 776 } 777 return null; 778 } 779 780 783 protected void select(int o) { 784 jw.select(o); 785 } 786 787 protected void gen(String s) { 788 try { 789 jw.write(s); 790 } catch (IOException e) { 791 throw new RuntimeException (e); 792 } 793 } 794 795 protected void gen(StringBuffer s) { 796 try { 797 jw.write(s); 798 } catch (IOException e) { 799 throw new RuntimeException (e); 800 } 801 } 802 803 protected void gen(String s1, String s2) { gen(s1); gen(s2); } 804 805 protected void gen(String s1, String s2, String s3) { 806 gen(s1); gen(s2); gen(s3); 807 } 808 809 protected void gen(int a) { gen(arrKeywords[a]); } 810 811 protected void gen(int a, String s) { gen(a); sp(); gen(s); } 812 813 protected void gen(int a, String s, String s2) { gen(a, s); sp(); gen(s2); } 814 815 protected void gen(int a, int b, String s, String s2) { 816 gen(a, b, s); gen(s2); 817 } 818 819 protected void gen(int a, int b, String s) { 820 gen(a); sp(); gen(b); sp(); gen(s); 821 } 822 823 protected void gen(int a, int b, int c, String s) { 824 gen(a); sp(); gen(b); sp(); gen(c); sp(); gen(s); 825 } 826 827 protected void gen(int a, int b, int c, int d, String s) { 828 gen(a); sp(); gen(b); sp(); gen(c); sp(); gen(d); sp(); gen(s); 829 } 830 831 protected void gencr(String s) { gen(s); cr(); } 832 833 protected void gencr(String s, String s2) { gen(s, s2); cr(); } 834 835 protected void gencr(int a, String s) { gen(a, s); cr(); } 836 837 protected void gencr(int a, String s, String s2) { gen(a, s, s2); cr(); } 838 839 protected void gencr(int a, int b, String s, String s2) { 840 gen(a, b, s, s2); cr(); 841 } 842 843 protected void gencr(int a, int b, String s) { gen(a, b, s); cr(); } 844 845 protected void gencr(int a, int b, int c, String s) {gen(a, b, c, s); cr();} 846 847 protected void get(String s) {gen(" get"); gen(s); PO(); PC();} 848 protected void geti(String s) {gen(" get"); gen(s, "(int index)");} 849 protected void getidx(String s, boolean b) {if (b) geti(s); else get(s); } 850 851 protected void cr() { 852 try { 853 jw.cr(); 854 } catch (IOException e) { 855 throw new RuntimeException (e); 856 } 857 } 858 859 protected void geneol(String s) { gen(s); eol(); } 860 861 protected void PO() { gen("("); } 862 863 protected void PC() { gen(")"); } 864 865 protected void begin() { 866 try { 867 jw.begin(); 868 } catch (IOException e) { 869 throw new RuntimeException (e); 870 } 871 } 872 873 protected void end() { end(true); }; 874 875 protected void end(boolean cr) { 876 try { 877 jw.end(cr); 878 } catch (IOException e) { 879 throw new RuntimeException (e); 880 } 881 } 882 883 protected void comment() { gencr("//"); } 884 885 protected void gencrNoI18N(String s) {gen(s); noI18N();} 886 protected void gencrNoI18N(String s, String s2) {gen(s); gen(s2); noI18N();} 887 888 protected void beginNoI18N() { gencr("// BEGIN" + "_NOI18N"); } 889 protected void endNoI18N() { gencr("// END" + "_NOI18N"); } 890 protected void noI18N() { 891 try { 892 jw.noI18N(); 893 } catch (IOException e) { 894 throw new RuntimeException (e); 895 } 896 } 897 898 protected void comment(String s) { 899 try { 900 jw.comment(s); 901 } catch (IOException e) { 902 throw new RuntimeException (e); 903 } 904 } 905 906 protected void comment(String s, String s2) { comment(s + s2); } 907 908 protected void eol() { 909 try { 910 jw.eol(); 911 } catch (IOException e) { 912 throw new RuntimeException (e); 913 } 914 } 915 916 protected void eol(boolean cr) { gen(";"); if (cr) cr(); } 917 918 protected void eolNoI18N() { 919 try { 920 jw.eolNoI18N(); 921 } catch (IOException e) { 922 throw new RuntimeException (e); 923 } 924 } 925 926 protected void sp() { gen(" "); } 927 928 protected void genforprop() {gencr("for (int i=0; i<this.prop.length; i++)");} 929 protected void gengetprop() { 930 gencr("BeanProp p = this.beanProp();"); 931 gencr("if (p != null)"); } 932 933 protected void gengetpropbyname() { 934 gencr("BeanProp p = this.beanProp(n);"); 935 gencr("if (p != null)"); } 936 937 protected void gensig1(String s) {gen("(", s, " l)"); cr();} 938 939 protected void gensig2(String s) {gen("(String n, ", s, " l)"); cr();} 940 941 protected void tabIn() { jw.indentOneLevel(); } 942 943 protected void gentab(int n) {for(int i=0; i<n; i++) tabIn();} 944 945 protected void genSetValue(boolean isScalar, String classType) { 946 gen(JavaUtil.toObject("value", classType, config.isForME())); 947 gen(")"); 948 } 949 950 protected void genSetValP(boolean b, String s1, String s2) { 951 if(b) gen(s1); else gen(s2); gen(" value"); } 952 953 957 public Property addProperty(String name, String dtdName, String namespace, 958 GraphNode node, 959 GraphLink l, String classType, int nestedLevel, 960 int eltInstance, int groupInstance, 961 int type, boolean ored, 962 AttrProp[] propAttributes, 963 String constName, String defaultValue, 964 boolean directChild, List extraData, 965 boolean isUnion) { 966 if (config.isTraceGen()) { 967 config.messageOut.println("AbstractCodeGen.addProperty: name="+name+" dtdName="+dtdName+" classType="+classType+" type="+type+" eltInstance="+eltInstance+" groupInstance="+groupInstance+" isUnion="+isUnion); 968 973 } 974 Property attr = new Property(name, dtdName, namespace, 975 node, l, classType, 976 eltInstance, 977 groupInstance, nestedLevel, type, 978 ored, propAttributes, constName, 979 defaultValue, directChild, extraData, isUnion); 980 attrList.add(attr); 981 return attr; 982 } 983 984 public List getPropertyList() { 985 return attrList; 986 } 987 988 992 public void generate(String filename, MetaDD mdd) throws IOException { 993 this.mdd = mdd; 994 if (className == null) 995 throw new NullPointerException (); 996 metaElement = getMetaElement(beanElement); 997 998 if (metaElement != null && metaElement.isSkipGeneration()) { 999 config.messageOut.println("Skipping generation of class " + filename 1000 + " (as specified in the mdd file)"); return; 1002 } 1003 1004 config.messageOut.println("Generating class " + filename); OutputStream out = new FileOutputStream(filename); 1006 generate(out, mdd); 1007 out.close(); 1008 } 1009 1010 public abstract void generate(OutputStream out, MetaDD mdd) throws IOException; 1011 1012 protected MetaElement getMetaElement(BeanBuilder.BeanElement be) { 1013 return getMetaElement(be.getDTDName(), be.getNamespace()); 1014 } 1015 1016 protected MetaElement getMetaElement(Property prop) { 1017 BeanBuilder.BeanElement be = prop.getBeanElement(); 1018 if (be != null) 1019 return getMetaElement(be); 1020 return getMetaElement(prop.getDTDName(), prop.getNamespace()); 1021 } 1022 1023 protected MetaElement getMetaElement(String dtdName, String namespace) { 1024 if (mdd == null) 1025 return null; 1026 int size = mdd.sizeMetaElement(); 1027 for (int i=0; i<size; i++) { 1028 MetaElement e = mdd.getMetaElement(i); 1029 if (e == null) 1030 continue; 1031 if (namespace != null && !namespace.equals(e.getNamespace())) 1032 continue; 1033 if (e.getDtdName().equals(dtdName)) 1034 return e; 1035 } 1036 return null; 1037 } 1038 1039 protected MetaProperty getMetaProperty(Property prop) { 1040 return getMetaProperty(prop.name); 1041 } 1042 1043 protected MetaProperty getMetaProperty(String name) { 1045 MetaProperty ret = null; 1046 if (metaElement != null) { 1048 MetaProperty[] mp = this.metaElement.getMetaProperty(); 1049 for (int i=0; i<mp.length; i++) { 1050 if (mp[i] != null && mp[i].getBeanName().equals(name)) { 1051 ret = mp[i]; 1052 break; 1053 } 1054 } 1055 } 1056 return ret; 1057 } 1058 1059 1062 protected void printSchema() throws IOException { 1063 if (config.getFilename() == null) 1064 return; 1065 File f = config.getFilename(); 1066 if (f.length() < 16384L) { 1067 FileInputStream fi = new FileInputStream(f); 1068 byte[] r = new byte[(int)f.length()]; 1069 fi.read(r); 1070 cr(); gencr("/*"); gentab(2); 1071 gencr("The following schema file has been used for generation:"); 1072 cr(); 1073 gen(new String (r)); 1074 fi.close(); 1075 cr(); 1076 gencr("*/"); 1077 } 1078 } 1079 1080 protected void printComment(String indent) throws IOException { 1081 String comment = (String ) beanElement.node.getExtendedProperty("comment"); 1082 if (comment == null) 1083 return; 1084 gen(indent); 1085 gencr("==============================================================="); 1086 gen(indent); 1087 int length = comment.length(); 1089 for (int i = 0; i < length; ++i) { 1090 char c = comment.charAt(i); 1091 if (c == '\n') { 1092 cr(); 1093 gen(indent); 1094 } else if (c == '*' && i+1 < length && comment.charAt(i+1) == '/') { 1095 jw.write("* /"); 1096 ++i; 1097 } else { 1098 jw.write(c); 1099 } 1100 } 1101 cr(); 1102 gen(indent); 1103 gencr("==============================================================="); 1104 } 1105 1106 public void dumpBeanTree(java.io.Writer out, String indent, String indentBy) throws java.io.IOException { 1107 out.write(indent); 1108 out.write(java.beans.Introspector.decapitalize(Common.convertName(beanElement.node.getName()))); 1109 out.write(" <"); 1110 out.write(beanElement.node.getName()); 1111 out.write(">"); 1112 out.write(" : "); 1113 out.write(className); 1114 if (!beanElement.node.getExtraData().isEmpty()) { 1115 out.write(" \t"); 1116 out.write(beanElement.node.getExtraData().toString()); 1117 } 1118 out.write("\n"); 1119 StringBuffer str = new StringBuffer (); 1120 int lineCount = 1; 1121 lineCount = dumpAttributes(beanElement.getGraphNode(), str, indent+indentBy, lineCount); 1122 lineCount = dumpBeanTree(beanElement, str, 1123 indent+indentBy, indentBy, lineCount); 1124 out.write(str.toString()); 1125 if (lineCount >= MAX_DUMP_TREE_LINE_COUNT) { 1126 out.write(indent); 1127 out.write("... etc ...\n"); 1128 } 1129 } 1130 1131 protected int dumpBeanTree(BeanBuilder.BeanElement be, StringBuffer str, 1132 String indent, String indentBy, int lineCount) { 1133 lineCount = beanTree(be.getGraphNode().getGraphLink(), str, 1134 indent, indentBy, lineCount); 1135 if (be.getExtension() != null) { 1136 lineCount = dumpBeanTree(be.getExtension(), str, indent, indentBy, lineCount); 1138 } 1139 return lineCount; 1140 } 1141 1142 protected int dumpAttributes(GraphNode elt, StringBuffer str, String indent, int lineCount) { 1143 AttrProp[] attrList = elt.getAttributes(); 1144 1145 for (int i = 0; i < attrList.length; i++) { 1146 AttrProp attr = attrList[i]; 1147 str.append(indent + "[attr: " + attr + "]"); 1148 if (!attr.getExtraData().isEmpty()) { 1149 str.append(" \t"); 1150 str.append(attr.getExtraData().toString()); 1151 } 1152 str.append("\n"); 1153 ++lineCount; 1154 } 1155 if (false && elt.getExtension() != null) 1156 lineCount = dumpAttributes(elt.getExtension(), str, indent, lineCount); 1157 return lineCount; 1158 } 1159 1160 protected int beanTree(GraphLink l, StringBuffer str, 1161 String indent, String indentBy, int lineCount) { 1162 if (lineCount >= MAX_DUMP_TREE_LINE_COUNT) 1163 return lineCount; for (; l != null; l = l.getSibling()) { 1165 String nextIndent = indent + indentBy; 1169 if (l.element != null) { 1170 BeanBuilder.BeanElement be = 1171 (BeanBuilder.BeanElement)l.element.getObject(); 1172 1173 if (be == null) 1174 continue; 1175 Property prop = (Property) l.getObject(); 1176 String type = be.getClassType(); 1177 String name = l.name; 1178 if ("#PCDATA".equals(l.name) && "String".equals(type)) 1179 continue; 1180 if (prop != null) { 1181 type = prop.getType(); 1182 if ("EMPTY".equals(prop.dtdName)) 1183 name = prop.dtdName; 1184 else 1185 name = prop.beanIntrospectorName() + " <"+prop.dtdName+">"; 1186 } 1187 1188 str.append(indent); 1189 str.append(name); 1190 str.append(" : "); 1191 str.append(type); 1192 str.append(TreeBuilder.instanceToString(l.getElementInstance(), 1193 true)); 1194 if (l.element.getMarked()) 1195 str.append("..."); 1196 if (!l.extraData.isEmpty()) { 1197 str.append(" \t"); 1198 str.append(l.extraData.toString()); 1199 } 1200 if (!be.node.getExtraData().isEmpty()) { 1201 str.append(" \t"); 1202 str.append(be.node.getExtraData().toString()); 1203 } 1204 str.append("\n"); 1205 ++lineCount; 1206 1207 if (config.isRespectExtension() && be.getExtension() != null) 1208 lineCount = dumpBeanTree(be.getExtension(), 1209 str, nextIndent, indentBy, lineCount); 1210 1211 lineCount = this.dumpAttributes(l.element, str, nextIndent, lineCount); 1212 1213 if (l.element.getGraphLink() != null) { 1214 if (l.element.getMarked() == false) { 1215 l.element.setMarked(true); 1216 lineCount = 1217 beanTree(l.element.getGraphLink(), str, nextIndent + 1218 TreeBuilder.instanceToString(l.getGroupInstance(), 1219 true) + 1220 (l.isSequenceOr() ? "| " : ""), indentBy, lineCount); 1221 l.element.setMarked(false); 1222 } 1223 } 1224 } 1225 1226 String groupInstance = TreeBuilder.instanceToString(l.getGroupInstance(), true); 1227 if (!"".equals(groupInstance)) { 1228 str.append(indent); 1229 str.append("(\n"); 1230 ++lineCount; 1231 } 1232 if (l.getFirstChild() != null) { 1233 lineCount = beanTree(l.getFirstChild(), str, indent + (("".equals(groupInstance)) ? "" : " ") + (l.isSequenceOr() ? "| " : ""), indentBy, lineCount); 1234 } 1235 if (!"".equals(groupInstance)) { 1236 str.append(indent); 1237 str.append(")"); 1238 str.append(groupInstance); 1239 str.append("\n"); 1240 ++lineCount; 1241 } 1242 } 1243 return lineCount; 1244 } 1245 1246 public String toString() { 1247 if (packageName == null) 1248 return "Class " + className; 1249 else 1250 return "Class " + packageName + "." + className; 1251 } 1252 1253 protected void gen(Signature sig) throws IOException { 1254 sig.writeMethod(jw); 1255 } 1256 1257 public static final int MAX_DUMP_TREE_LINE_COUNT = 512; 1258 1259 protected static final Signature.TypeOfMethod SETTER = Signature.TypeOfMethod.SETTER; 1260 protected static final Signature.TypeOfMethod SETTERINDEXED = Signature.TypeOfMethod.SETTERINDEXED; 1261 protected static final Signature.TypeOfMethod GETTER = Signature.TypeOfMethod.GETTER; 1262 protected static final Signature.TypeOfMethod GETTERLIST = Signature.TypeOfMethod.GETTERLIST; 1263 protected static final Signature.TypeOfMethod GETTERINDEXED = Signature.TypeOfMethod.GETTERINDEXED; 1264 protected static final Signature.TypeOfMethod SIZE = Signature.TypeOfMethod.SIZE; 1265 protected static final Signature.TypeOfMethod ADD = Signature.TypeOfMethod.ADD; 1266 protected static final Signature.TypeOfMethod REMOVE = Signature.TypeOfMethod.REMOVE; 1267 protected static final Signature.TypeOfMethod OTHER = Signature.TypeOfMethod.OTHER; 1268 1269 protected static class Signature { 1270 public static class TypeOfMethod { 1271 private final String name; 1272 private TypeOfMethod(String name) { this.name = name; } 1273 public String toString() { return "TypeOfMethod "+name; } 1274 public static final TypeOfMethod SETTER = new TypeOfMethod("setter"); 1275 public static final TypeOfMethod SETTERINDEXED = new TypeOfMethod("setterindexed"); 1276 public static final TypeOfMethod GETTER = new TypeOfMethod("getter"); 1277 public static final TypeOfMethod GETTERLIST = new TypeOfMethod("getterlist"); 1278 public static final TypeOfMethod GETTERINDEXED = new TypeOfMethod("getterindexed"); 1279 public static final TypeOfMethod SIZE = new TypeOfMethod("size"); 1280 public static final TypeOfMethod ADD = new TypeOfMethod("add"); 1281 public static final TypeOfMethod REMOVE = new TypeOfMethod("remove"); 1282 public static final TypeOfMethod OTHER = new TypeOfMethod("other"); 1283 } 1284 1285 private String returnType; 1286 private String methodName; 1287 private TypeOfMethod methodType; 1288 private List parameterTypes; 1289 private List parameterNames; 1290 private List throwTypes; 1291 private boolean _isStatic; 1292 1293 public Signature(String returnType, String methodName, TypeOfMethod methodType) { 1294 this.returnType = returnType; 1295 this.methodName = methodName; 1296 this.methodType = methodType; 1297 this.parameterTypes = new LinkedList(); 1298 this.parameterNames = new LinkedList(); 1299 this.throwTypes = new LinkedList(); 1300 this._isStatic = false; 1301 } 1302 1303 public Signature(String returnType, String methodName) { 1304 this.returnType = returnType; 1305 this.methodName = methodName; 1306 this.methodType = OTHER; 1307 this.parameterTypes = new LinkedList(); 1308 this.parameterNames = new LinkedList(); 1309 this.throwTypes = new LinkedList(); 1310 this._isStatic = false; 1311 } 1312 1313 public String getMethodName() { 1314 return methodName; 1315 } 1316 1317 public String getReturnType() { 1318 return returnType; 1319 } 1320 1321 public TypeOfMethod getMethodType() { 1322 return methodType; 1323 } 1324 1325 public boolean isStatic() { 1326 return _isStatic; 1327 } 1328 1329 public void setStatic() { 1330 _isStatic = true; 1331 } 1332 1333 public boolean isVoidReturnType() { 1334 return "void".equals(returnType); 1335 } 1336 1337 public void addParameter(String type, String name) { 1338 parameterTypes.add(type); 1339 parameterNames.add(name); 1340 } 1341 1342 public void addThrows(String type) { 1343 throwTypes.add(type); 1344 } 1345 1346 public void writeMethod(JavaWriter jw) throws IOException { 1347 StringBuffer parameters = new StringBuffer (); 1348 Iterator itTypes = parameterTypes.iterator(); 1349 Iterator itNames = parameterNames.iterator(); 1350 boolean first = true; 1351 while (itTypes.hasNext() && itNames.hasNext()) { 1352 if (first) 1353 first = false; 1354 else 1355 parameters.append(", "); 1356 parameters.append((String ) itTypes.next()); 1357 parameters.append(" "); 1358 parameters.append((String ) itNames.next()); 1359 } 1360 String exceptions = null; 1361 Iterator itThrows = throwTypes.iterator(); 1362 if (itThrows.hasNext()) { 1363 exceptions = ""; 1364 first = true; 1365 do { 1366 if (first) 1367 first = false; 1368 else 1369 exceptions += ", "; 1370 exceptions += ((String ) itThrows.next()); 1371 } while (itThrows.hasNext()); 1372 } 1373 int options = jw.PUBLIC | jw.BEANINFO; 1374 if (_isStatic) 1375 options |= jw.STATIC; 1376 jw.writeMethod(methodName, parameters.toString(), exceptions, 1377 returnType, options); 1378 } 1379 1380 1384 public String toString() { 1385 StringBuffer str = new StringBuffer (); 1386 if (_isStatic) 1387 str.append("static "); 1388 str.append(returnType); 1389 str.append(" "); 1390 str.append(methodName); 1391 str.append("("); 1392 Iterator itTypes = parameterTypes.iterator(); 1393 Iterator itNames = parameterNames.iterator(); 1394 boolean first = true; 1395 while (itTypes.hasNext() && itNames.hasNext()) { 1396 if (first) 1397 first = false; 1398 else 1399 str.append(", "); 1400 str.append((String ) itTypes.next()); 1401 str.append(" "); 1402 str.append((String ) itNames.next()); 1403 } 1404 str.append(")"); 1405 Iterator itThrows = throwTypes.iterator(); 1406 if (itThrows.hasNext()) { 1407 str.append(" throws "); 1408 first = true; 1409 do { 1410 if (first) 1411 first = false; 1412 else 1413 str.append(", "); 1414 str.append((String ) itThrows.next()); 1415 } while (itThrows.hasNext()); 1416 } 1417 return str.toString(); 1418 } 1419 1420 1425 public String callMethod() { 1426 StringBuffer str = new StringBuffer (); 1427 str.append(methodName); 1428 str.append("("); 1429 Iterator itTypes = parameterTypes.iterator(); 1430 Iterator itNames = parameterNames.iterator(); 1431 boolean first = true; 1432 while (itTypes.hasNext() && itNames.hasNext()) { 1433 if (first) 1434 first = false; 1435 else 1436 str.append(", "); 1437 str.append((String ) itNames.next()); 1438 } 1439 str.append(")"); 1440 return str.toString(); 1441 } 1442 } 1443 1444 protected static class Signatures { 1445 Map signatureTable; 1446 List others; 1447 1448 public Signatures() { 1449 signatureTable = new HashMap(); 1450 others = new LinkedList(); 1451 } 1452 1453 public void add(Signature sig) { 1454 if (sig.getMethodType() == OTHER) 1455 others.add(sig); 1456 else 1457 signatureTable.put(sig.getMethodType(), sig); 1458 } 1459 1460 public Signature findSignature(Signature.TypeOfMethod methodType) { 1461 Signature sig = null; 1462 if (methodType == OTHER) { 1463 Iterator it = others.iterator(); 1464 if (it.hasNext()) 1465 sig = (Signature) it.next(); 1466 } else 1467 sig = (Signature) signatureTable.get(methodType); 1468 if (sig == null) 1469 throw new IllegalStateException ("Unable to find "+methodType); 1470 return sig; 1471 } 1472 1473 public Iterator iterator() { 1474 return new SignaturesIterator(); 1475 } 1477 1478 public class SignaturesIterator implements Iterator { 1479 Iterator tableIterator; 1480 Iterator othersIterator; 1481 1482 public SignaturesIterator() { 1483 tableIterator = signatureTable.values().iterator(); 1484 othersIterator = others.iterator(); 1485 } 1486 1487 public boolean hasNext() { 1488 if (tableIterator.hasNext()) 1489 return true; 1490 return othersIterator.hasNext(); 1491 } 1492 1493 public Object next() { 1494 if (tableIterator.hasNext()) 1495 return tableIterator.next(); 1496 return othersIterator.next(); 1497 } 1498 1499 public void remove() { 1500 throw new UnsupportedOperationException (); 1501 } 1502 } 1503 } 1504 1505 protected Signatures getSignatures(Property a) { 1506 Signatures result = new Signatures(); 1507 Signature sig; 1508 1509 boolean indexed = a.isIndexed(); 1510 boolean isScalar = a.isScalar(); 1511 String type = getTypeFullClassName(a); 1512 String typeVariable = "value"; 1513 if (a.isBean && config.isUseInterfaces()) { 1515 MetaElement me = getMetaElement(a); 1516 if (me != null) { 1517 String firstInterface = firstInCommaSeparatedList(me.getImplements()); 1518 if (firstInterface != null && 1519 !firstInterface.equals(config.getGenerateCommonInterface()) && 1520 !firstInterface.equals(packageName+"."+config.getGenerateCommonInterface())) { 1521 type = firstInterface; 1522 typeVariable = "valueInterface"; 1523 a.setPropertyInterface(firstInterface); 1524 } 1525 } 1526 } 1527 String baseType = type; 1528 if (indexed) 1529 type = baseType + "[]"; 1530 1531 sig = new Signature("void", "set"+a.name, SETTER); 1533 sig.addParameter(type, typeVariable); 1534 if (config.isVetoable()) 1535 sig.addThrows("java.beans.PropertyVetoException"); 1536 result.add(sig); 1537 if (indexed) { 1538 sig = new Signature("void", "set"+a.name, SETTERINDEXED); 1539 sig.addParameter("int", "index"); 1540 sig.addParameter(baseType, typeVariable); 1541 if (config.isVetoable()) 1542 sig.addThrows("java.beans.PropertyVetoException"); 1543 result.add(sig); 1544 } 1545 1546 String getterName = a.getReadMethod(false); 1548 result.add(new Signature(type, getterName, GETTER)); 1549 if (indexed) { 1550 String returnType = "java.util.List"; 1551 if (config.jdkTarget >= 150) 1552 returnType += "<"+JavaUtil.toObjectType(baseType)+">"; 1553 result.add(new Signature(returnType, "fetch"+a.name+"List", GETTERLIST)); 1554 sig = new Signature(baseType, a.getReadMethod(true), GETTERINDEXED); 1555 sig.addParameter("int", "index"); 1556 result.add(sig); 1557 result.add(new Signature("int", "size"+a.name, SIZE)); 1558 sig = new Signature("int", "add"+a.name, ADD); 1559 sig.addParameter(baseType, typeVariable); 1560 if (config.isVetoable()) 1561 sig.addThrows("java.beans.PropertyVetoException"); 1562 result.add(sig); 1563 sig = new Signature("int", "remove"+a.name, REMOVE); 1564 sig.addParameter(baseType, typeVariable); 1565 if (config.isVetoable()) 1566 sig.addThrows("java.beans.PropertyVetoException"); 1567 result.add(sig); 1568 } 1569 1570 return result; 1571 } 1572 1573 public void generateDelegator(OutputStream out, MetaDD mdd, 1574 String delegatorClassName, 1575 String delegatorPackageName) throws IOException { 1576 Collection generatedMethods = jw.getStoredMethods(); 1577 resetGenBuffers(); 1578 1579 select(HEADER_SECTION); 1580 jw.bigComment("Delegate for "+className+"\n\n@"+Common.GENERATED_TAG); 1581 jw.cr(); 1582 if (delegatorPackageName != null) { 1583 jw.writePackage(delegatorPackageName); 1584 cr(); 1585 } 1586 gen(PUBLIC, CLASS, delegatorClassName); 1587 if (metaElement.getDelegatorExtends() != null) { 1588 jw.write(" extends "); 1589 jw.write(metaElement.getDelegatorExtends()); 1590 } 1591 if (config.getGenerateCommonInterface() != null) { 1592 jw.write(" implements "); 1593 jw.write(config.getGenerateCommonInterface()); 1594 } 1595 sp(); 1596 begin(); 1597 1598 select(DECL_SECTION); 1599 String delegator = "_"+className; gen(PROTECTED, fullClassName, delegator); 1601 eol(); 1602 cr(); 1603 1604 generateDelegatorConstructors(delegatorClassName); 1605 1606 List sortedMethods = new LinkedList(generatedMethods); 1607 Collections.sort(sortedMethods); 1608 for (Iterator it = sortedMethods.iterator(); it.hasNext(); ) { 1609 JavaWriter.Method method = (JavaWriter.Method) it.next(); 1610 if (method.isPublic()) 1611 generateDelegator(method, delegatorClassName); 1612 } 1613 1614 select(TRAILER_SECTION); 1615 end(); 1616 1617 printGenBuffers(out); 1618 } 1619 1620 protected void generateDelegatorConstructors(String delegatorClassName) throws IOException { 1621 select(CONSTRUCTOR_SECTION); 1622 String delegator = "_"+className; gen(PUBLIC, delegatorClassName); 1624 PO(); 1625 gen(fullClassName); 1626 gen(" delegator"); 1627 PC(); sp(); 1628 begin(); 1629 geneol(delegator+" = delegator"); 1630 end(); 1631 cr(); 1632 } 1633 1634 protected void generateDelegator(JavaWriter.Method method, String delegatorClassName) throws IOException { 1635 if ("".equals(method.getReturnType())) { 1636 if (method.getParameters().indexOf(',') < 0 && 1637 (method.getParameters().startsWith(className+" ") || 1638 method.getParameters().startsWith(fullClassName+" "))) { 1639 return; 1641 } 1642 select(CONSTRUCTOR_SECTION); 1643 String delegator = "_"+className; jw.write("public "); 1645 jw.write(delegatorClassName); 1646 jw.write("("); 1647 jw.write(method.getParameters()); 1648 jw.write(") "); 1649 if (method.getExceptions() != null) { 1650 jw.write("throws ", method.getExceptions(), " "); 1651 } 1652 jw.begin(); 1653 jw.write(delegator, " = new ", fullClassName); 1654 jw.write("("); 1655 method.writeParametersNoTypes(jw); 1656 jw.writeEol(")"); 1657 jw.end(); 1658 jw.cr(); 1659 return; 1660 } 1661 select(BODY_SECTION); 1662 boolean returnsDelegation = false; 1663 if (className.equals(method.getReturnType()) || 1664 fullClassName.equals(method.getReturnType())) { 1665 returnsDelegation = true; 1666 } 1667 jw.beginMethod(method.getName(), method.getParameters(), 1668 method.getExceptions(), 1669 returnsDelegation ? delegatorClassName : method.getReturnType(), 1670 method.getOptions()); 1671 if (!"void".equals(method.getReturnType())) { 1672 gen("return "); 1673 } 1674 if (returnsDelegation) 1675 jw.write("new ", delegatorClassName, "("); 1676 if (!method.isStatic()) 1677 jw.write("_", className); 1678 else 1679 jw.write(fullClassName); 1680 jw.write("."); 1681 method.writeCall(jw); 1682 if (returnsDelegation) 1683 jw.write(")"); 1684 jw.eol(); 1685 jw.end(); 1686 jw.cr(); 1687 } 1688 1689 1692 protected void genWhiteSpaceRestriction(SchemaRep.WhiteSpace ws, String var, 1693 String type) throws IOException { 1694 if (!("String".equals(type) || "java.lang.String".equals(type))) 1695 return; 1696 if (ws.isPreserve()) 1697 return; 1698 if (ws.isReplace()) { 1699 jw.beginIf(var+" != null"); 1700 jw.comment("Whitespace Replace due to whitespace restriction."); 1701 jw.writeEol(var+" = "+var+".replace('\\n', ' ').replace('\\t', ' ')"); 1702 jw.end(); 1703 } else if (ws.isCollapse()) { 1704 jw.beginIf(var+" != null"); 1705 jw.comment("Whitespace Collapse due to whitespace restriction."); 1706 jw.writeEol(var+" = "+var+".replace('\\n', ' ').replace('\\t', ' ').trim()"); 1707 jw.beginFor("int pos = "+var+".indexOf(\" \")", "pos >= 0", 1708 "pos = "+var+".indexOf(\" \", pos)"); 1709 jw.writeEol(var+" = "+var+".substring(0, pos) + "+var+".substring(pos+1, "+var+".length())"); 1710 jw.end(); 1711 jw.end(); 1712 } 1713 } 1714 1715 protected void genRethrowExceptions(List exceps) { 1716 for (Iterator it = exceps.iterator(); it.hasNext(); ) { 1717 gen("catch ("); 1718 gen((String ) it.next()); 1719 gen(" e) "); 1720 begin(); 1721 geneol("throw new java.lang.RuntimeException(e)"); 1722 end(); 1723 } 1724 } 1725 1726 protected void genValidateProperties() throws IOException { 1727 jw.writeEol("boolean restrictionFailure = false"); 1728 jw.writeEol("boolean restrictionPassed = false"); 1729 Set requiredPropertySet = new HashSet(); 1730 Map requiredProperty = new HashMap(); 1731 int size = attrList.size(); 1732 for (int i = 0; i < size; i++) { 1733 Property a = (Property)attrList.get(i); 1734 boolean indexed = a.isIndexed(); 1735 String attr = a.getReadMethod(false)+"()"; 1736 String type = a.getType().intern(); 1737 boolean mightBeNull = a.canBeNull(); 1738 boolean isPrimitiveType = JavaUtil.isPrimitiveType(type); 1739 String propertyName = a.beanIntrospectorName(); 1740 boolean hasDataEnumRestriction = false; 1741 String enumArray = "enumRestriction"+a.name; 1742 boolean checkOrCount = a.ored; 1744 if (checkOrCount) { 1745 if (a.getGroupInstance() == Common.TYPE_0_N || 1748 a.getGroupInstance() == Common.TYPE_0_1) { 1749 checkOrCount = false; 1750 } 1751 } 1752 jw.comment("Validating property "+propertyName); 1753 if (a.isUnion()) 1755 jw.writeEol("restrictionPassed = false"); 1756 if (a.ored) { 1757 } else { 1758 switch (a.elementInstance & Common.MASK_INSTANCE) { 1759 case Common.TYPE_1: 1760 if (isPrimitiveType) { 1761 break; 1763 } 1764 if (a.isNillable()) 1765 break; 1766 genValidateIf(attr+" == null", propertyName, 1767 ValidateException.FailureType.NULL_VALUE, 1768 jw); 1769 break; 1770 case Common.TYPE_0_1: 1771 break; 1772 case Common.TYPE_0_N: 1773 break; 1775 case Common.TYPE_1_N: 1776 genValidateIf("size"+a.name+"() == 0", propertyName, 1777 ValidateException.FailureType.NULL_VALUE, jw); 1778 break; 1779 } 1780 } 1781 if (indexed && a.isAttribute()) 1782 continue; 1783 int markedPosition = -1; 1785 if (indexed) { 1786 for (Iterator it = a.extraDataIterator(); it.hasNext(); ) { 1787 Object extraData = it.next(); 1788 if (extraData instanceof DataListRestriction) { 1789 DataListRestriction dlr = (DataListRestriction) extraData; 1790 dlr.genRestriction(jw, "size"+a.name+"()", a.getReadMethod(false), type, "restrictionFailure", false); 1791 jw.beginIf("restrictionFailure"); 1792 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr+" "+dlr.toString()), 1793 propertyName, false, 1794 ValidateException.FailureType.DATA_RESTRICTION, 1795 jw); 1796 jw.end(); 1797 } 1798 } 1799 markedPosition = jw.getCurrentPosition(); 1800 jw.beginFor("int _index = 0", 1801 "_index < size"+a.name+"()", "++_index"); 1802 jw.write(getTypeFullClassName(a)); 1803 jw.write(" element = "); 1804 if (a.getPropertyInterface() != null) { 1805 jw.write("(", getTypeFullClassName(a), ") "); 1806 } 1807 jw.writeEol(a.getReadMethod(true)+"(_index)"); 1808 attr = "element"; 1809 } 1810 if (mightBeNull) { 1811 if (markedPosition == -1) 1812 markedPosition = jw.getCurrentPosition(); 1813 jw.beginIf(attr+" != null"); 1814 } 1815 1816 int marked2Position = jw.getCurrentPosition(); 1817 if (a.isBean) { 1818 if (a.getPropertyInterface() == null) { 1819 if (!a.getGraphNode().isCreated()) { 1820 jw.beginTry(); 1821 jw.comment("Catch and deal with any foreign validate exceptions."); 1822 } 1823 jw.writeEol(attr, ".validate()"); 1824 if (!a.getGraphNode().isCreated()) { 1825 jw.endCatch("java.lang.Exception e"); 1826 jw.writeEol("throw new java.lang.RuntimeException(e)"); 1827 jw.end(); 1828 } 1829 } else 1830 jw.writeEol("(("+type+")", attr, ").validate()"); 1831 } 1832 genForRestrictions(a, attr); 1833 for (Iterator it = a.extraDataIterator(); it.hasNext(); ) { 1835 Object extraData = it.next(); 1836 if (extraData instanceof DataTypeRestriction) { 1839 DataTypeRestriction dtr = (DataTypeRestriction) extraData; 1840 dtr.genRestriction(jw, attr, type, "restrictionFailure", false); 1841 jw.beginIf("restrictionFailure"); 1842 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr+" "+dtr.toString()), 1843 propertyName, false, 1844 ValidateException.FailureType.DATA_RESTRICTION, 1845 jw); 1846 jw.end(); 1847 } 1848 } 1849 for (Iterator it = a.extraDataIterator(); it.hasNext(); ) { 1850 Object extraData = it.next(); 1851 if (extraData instanceof DataEnumRestriction) { 1852 DataEnumRestriction der = (DataEnumRestriction) extraData; 1853 if (!hasDataEnumRestriction) { 1854 hasDataEnumRestriction = true; 1856 jw.write("final "+type+"[] "+enumArray+" = {"); 1857 } else { 1858 jw.write(", "); 1859 } 1860 der.genRestriction(jw, type); 1861 } 1862 } 1863 if (hasDataEnumRestriction) { 1864 jw.writeEol("}"); 1865 jw.writeEol("restrictionFailure = true"); 1866 jw.beginFor("int _index2 = 0", "_index2 < "+enumArray+".length", 1867 "++_index2"); 1868 jw.beginIf(JavaUtil.genEquals(type, enumArray+"[_index2]", 1869 attr, false)); 1870 jw.writeEol("restrictionFailure = false"); 1871 jw.writeEol("break"); 1872 jw.end(); 1873 jw.end(); 1874 jw.beginIf("restrictionFailure"); 1875 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr+" enumeration test"), 1876 propertyName, false, 1877 ValidateException.FailureType.ENUM_RESTRICTION, 1878 jw); 1879 jw.end(); 1880 } 1881 1882 if (marked2Position != jw.getCurrentPosition()) { 1883 if (mightBeNull) { 1884 jw.end(); 1885 } 1886 if (indexed) { 1887 jw.end(); 1888 } 1889 } else { 1890 if (markedPosition >= 0) { 1891 jw.truncateAtPosition(markedPosition); 1894 } 1895 if (mightBeNull) 1896 jw.indentLeft(); 1897 if (indexed) 1898 jw.indentLeft(); 1899 } 1900 1901 if (checkOrCount) { 1902 GraphLink gl = a.getGraphLink(); 1903 GraphLink parentgl = null; 1905 if (gl != null) { 1906 parentgl = gl.getParent(); 1907 } 1908 List requiredPropertyTest = new ArrayList(); 1910 jw.beginIf(testIfPropertySet(a)); 1911 requiredPropertyTest.add(testIfPropertyNotSet(a)); 1912 for (Iterator it = a.getMutuallyExclusiveProperties().iterator(); it.hasNext(); ) { 1913 Property prop = (Property) it.next(); 1914 jw.beginIf(testIfPropertySet(prop)); 1915 requiredPropertyTest.add(testIfPropertyNotSet(prop)); 1916 genValidateFail("mutually exclusive properties: "+a.name+" and "+prop.name, 1917 prop.name, true, 1918 ValidateException.FailureType.MUTUALLY_EXCLUSIVE, 1919 jw); 1920 jw.end(); 1921 } 1922 jw.end(); 1923 1924 Collections.sort(requiredPropertyTest); 1932 StringBuffer requiredExpr = new StringBuffer (); 1933 boolean first = true; 1934 for (Iterator it = requiredPropertyTest.iterator(); it.hasNext(); ) { 1935 if (first) 1936 first = false; 1937 else 1938 requiredExpr.append(" && "); 1939 requiredExpr.append((String ) it.next()); 1940 } 1941 String requiredExprString = requiredExpr.toString(); 1942 requiredPropertySet.add(requiredExprString); 1943 requiredProperty.put(requiredExprString, a); 1944 } 1945 } 1946 for (Iterator it = requiredPropertySet.iterator(); it.hasNext(); ) { 1947 String required = (String ) it.next(); 1948 Property prop = (Property) requiredProperty.get(required); 1949 jw.beginIf(required); 1950 genValidateFail("required properties: "+required, 1951 prop.name, true, 1952 ValidateException.FailureType.NULL_VALUE, 1953 jw); 1954 jw.end(); 1955 } 1956 } 1957 1958 protected void genForRestrictions(Property a, String attr) 1959 throws IOException { 1960 boolean isUnion = a.isUnion(); 1961 String propertyName = a.beanIntrospectorName(); 1962 String enumArray = "enumRestriction"+a.name; 1963 String type = a.getType().intern(); 1964 SchemaRep.Restriction restrict = null; 1965 for (Iterator it = a.extraDataIterator(); it.hasNext(); ) { 1966 boolean hasDataEnumRestriction = false; 1967 Object extraData = it.next(); 1968 if (extraData instanceof SchemaRep.Restriction) 1969 restrict = (SchemaRep.Restriction)extraData; 1970 else 1971 continue; 1972 if (isUnion) { 1973 String tempType = restrict.getJavaTypeName(); 1976 if (tempType != null) 1977 type = tempType.intern(); 1978 jw.write("{\n"); 1979 } 1980 boolean firstPattern = true; 1981 for (Iterator itr = restrict.subElementsIterator(); itr.hasNext(); ) { 1982 Object rType = itr.next(); 1983 if (rType instanceof SchemaRep.Pattern) { 1984 if (firstPattern) { 1985 jw.write("{\n"); 1986 jw.writeEol("boolean patternPassed = false"); 1987 firstPattern = false; 1988 } 1989 DataTypeRestriction dtr = (DataTypeRestriction) rType; 1990 dtr.genRestriction(jw, attr, type, "patternPassed", true); 1991 } 1992 } 1993 if (!firstPattern) { 1994 jw.writeEol("restrictionFailure = !patternPassed"); 1995 jw.write("}\n"); 1996 if (!isUnion) { 1997 jw.beginIf("restrictionFailure"); 1998 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr), 1999 propertyName, false, 2000 ValidateException.FailureType.DATA_RESTRICTION, 2001 jw); 2002 jw.end(); 2003 } 2004 } 2005 for (Iterator itr = restrict.subElementsIterator(); itr.hasNext(); ) { 2006 Object rType = itr.next(); 2007 if (rType instanceof DataListRestriction) { 2008 DataListRestriction dlr = (DataListRestriction) rType; 2009 dlr.genRestriction(jw, "size"+a.name+"()", a.getReadMethod(false), type, "restrictionFailure", false); 2010 if (!isUnion) { 2011 jw.beginIf("restrictionFailure"); 2012 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr+" "+dlr.toString()), 2013 propertyName, false, 2014 ValidateException.FailureType.DATA_RESTRICTION, 2015 jw); 2016 jw.end(); 2017 } 2018 } 2019 if (rType instanceof DataTypeRestriction && !(rType instanceof SchemaRep.Pattern)) { 2020 DataTypeRestriction dtr = (DataTypeRestriction) rType; 2021 String typeAttr = attr; 2022 if (isUnion) { 2023 jw.beginTry(); 2027 typeAttr = "("+JavaUtil.genParseText(type, attr)+")"; 2028 } 2029 dtr.genRestriction(jw, typeAttr, type, "restrictionFailure", false); 2030 if (isUnion) { 2031 jw.endCatch("Exception e"); 2036 jw.writeEol("restrictionFailure = true"); 2037 jw.end(); 2038 } 2039 if (!isUnion) { 2040 jw.beginIf("restrictionFailure"); 2041 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr+" "+dtr.toString()), 2042 propertyName, false, 2043 ValidateException.FailureType.DATA_RESTRICTION, 2044 jw); 2045 jw.end(); 2046 } 2047 } 2048 } 2049 for (Iterator itr = restrict.subElementsIterator(); itr.hasNext(); ) { 2050 Object rType = itr.next(); 2051 if (rType instanceof DataEnumRestriction) { 2052 DataEnumRestriction der = (DataEnumRestriction) rType; 2053 if (!hasDataEnumRestriction) { 2054 hasDataEnumRestriction = true; 2056 jw.write("final "+type+"[] "+enumArray+" = {"); 2057 } else { 2058 jw.write(", "); 2059 } 2060 der.genRestriction(jw, type); 2061 } 2062 } 2063 if (hasDataEnumRestriction) { 2064 jw.writeEol("}"); 2065 jw.writeEol("restrictionFailure = true"); 2066 jw.beginFor("int _index2 = 0", "_index2 < "+enumArray+".length", 2067 "++_index2"); 2068 jw.beginIf(JavaUtil.genEquals(type, enumArray+"[_index2]", 2069 attr, false)); 2070 jw.writeEol("restrictionFailure = false"); 2071 jw.writeEol("break"); 2072 jw.end(); 2073 jw.end(); 2074 if (!isUnion) { 2075 jw.beginIf("restrictionFailure"); 2076 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr+" enumeration test"), 2077 propertyName, false, 2078 ValidateException.FailureType.ENUM_RESTRICTION, 2079 jw); 2080 jw.end(); 2081 } 2082 } 2083 if (isUnion) { 2084 jw.beginIf("!restrictionPassed"); 2086 jw.writeEol("restrictionPassed = !restrictionFailure"); 2087 jw.end(); 2088 jw.writeEol("restrictionFailure = false"); 2089 jw.write("}\n"); 2090 } 2091 } 2092 if (isUnion) { 2093 jw.beginIf("!restrictionPassed"); 2094 genValidateFail(JavaUtil.instanceFrom("java.lang.String", attr), 2095 propertyName, false, 2096 ValidateException.FailureType.ALL_RESTRICTIONS, 2097 jw); 2098 jw.end(); 2099 } 2100 } 2101 2102 protected void genValidateIf(String test, String name, 2103 ValidateException.FailureType ft, 2104 JavaWriter out) throws IOException { 2105 jw.beginIf(test); 2106 genValidateFail(test, name, true, ft, out); 2107 jw.end(); 2108 } 2109 2110 protected abstract void genValidateFail(String detail, String name, 2111 boolean quoteDetail, 2112 ValidateException.FailureType ft, 2113 JavaWriter out) throws IOException; 2114 2115 public Collection getGeneratedMethods() { 2116 return jw.getStoredMethods(); 2117 } 2118 2119 protected void genDefaultsAccessable(Property a) throws IOException { 2120 String type = a.getType(); 2121 boolean mustHaveOneInstance = (!a.ored && a.elementInstance == Common.TYPE_1); 2122 if (config.isDefaultsAccessable() && !a.isIndexed() && 2123 (a.isScalar() || a.getDefaultValue() != null || 2124 mustHaveOneInstance)) { 2125 jw.beginMethod("fetchDefault"+a.name, "", null, type, jw.PUBLIC); 2126 List exceps = JavaUtil.exceptionsFromParsingText(type, false); 2127 if (!exceps.isEmpty()) { 2128 jw.beginTry(); 2129 } 2130 jw.write("return "); 2131 if (a.getDefaultValue() != null) 2132 jw.write(JavaUtil.instanceFrom(type, a.getDefaultValue())); 2133 else if (config.isMakeDefaults()) 2134 jw.write(JavaUtil.genNewDefault(type)); 2135 else 2136 jw.write(JavaUtil.nullValueForType(type)); 2137 jw.eol(); 2138 if (!exceps.isEmpty()) { 2139 end(); 2140 genRethrowExceptions(exceps); 2141 } 2142 jw.end(); 2143 jw.cr(); 2144 } 2145 } 2146 2147 protected void generateSwitches() throws IOException { 2148 select(jw.BODY_SECTION); 2149 jw.bigComment("@return true if error, then should display help"); 2150 jw.beginMethod("parseArguments", "String[] args", null, "boolean"); 2151 jw.beginFor("int argNum = 0, size = args.length", "argNum < size", 2152 "++argNum"); 2153 jw.writeEol("String arg = args[argNum].toLowerCase().intern()"); 2154 int size = attrList.size(); 2155 Map mandatoryProperties = new LinkedHashMap(); 2156 StringBuffer helpParams = new StringBuffer (); 2157 int lastHelpParamsPos = 0; 2158 StringBuffer helpText = new StringBuffer (); 2159 for (int i = 0; i < size; i++) { 2160 Property a = (Property)attrList.get(i); 2161 XMLSchemaParser.SwitchData sd = (XMLSchemaParser.SwitchData) a.searchExtraData(XMLSchemaParser.SwitchData.class); 2162 if (sd == null) 2163 continue; 2164 if (sd.isMandatory()) 2165 mandatoryProperties.put(sd.getName(), a); 2166 String sw = sd.getName(); 2167 sw = sw.toLowerCase(); 2168 String type = a.getType().intern(); 2169 boolean indexed = a.isIndexed(); 2170 String writeMethod; 2171 if (indexed) 2172 writeMethod = a.getAddMethod(); 2173 else 2174 writeMethod = a.getWriteMethod(); 2175 jw.beginIf("arg == "+JavaUtil.instanceFrom("String", "-"+sw)); 2177 boolean isBoolean = (type == "boolean" || type == "Boolean" || type == "java.lang.Boolean"); 2178 helpParams.append(" "); 2179 if (!sd.isMandatory()) 2180 helpParams.append("["); 2181 helpParams.append("-"); 2182 if (isBoolean) { 2183 jw.writeEol(writeMethod+"(true)"); 2184 helpParams.append(sd.getName()); 2185 } else { 2186 jw.beginIf("argNum+1 >= size"); 2187 jw.writeEol("missingArgument(args, arg)"); 2188 jw.writeEol("continue"); 2189 jw.end(); 2190 jw.writeEol(writeMethod+"("+JavaUtil.genParseText(type, 2191 "args[++argNum]", 2192 config.isForME())+")"); 2193 helpParams.append(sd.getName()); 2194 helpParams.append(" "); 2195 if (type == "java.io.File") 2196 helpParams.append("filename"); 2197 else 2198 helpParams.append(a.dtdName); 2199 } 2200 if (!sd.isMandatory()) 2201 helpParams.append("]"); 2202 jw.writeEol("continue"); 2203 jw.end(); 2204 helpText.append(" -"); 2205 helpText.append(sd.getName()); 2206 helpText.append("\t"); 2207 if (sd.getHelp() != null) { 2208 helpText.append(sd.getHelp()); 2209 } else { 2210 helpText.append(a.name); 2211 } 2212 helpText.append('\n'); 2213 if (isBoolean) { 2214 jw.beginIf("arg == "+JavaUtil.instanceFrom("String", "-no"+sw)); 2215 jw.writeEol(writeMethod+"(false)"); 2216 jw.writeEol("continue"); 2217 jw.end(); 2218 helpParams.append(" [-no"); 2219 helpParams.append(sd.getName()); 2220 helpParams.append("]"); 2221 } 2222 if (helpParams.length() - lastHelpParamsPos > 63) { 2223 helpParams.append("\n"); 2224 lastHelpParamsPos = helpParams.length(); 2225 } 2226 } 2227 jw.beginIf("arg == \"-help\" || arg == \"--help\""); 2228 jw.writeEol("return true"); 2229 jw.end(); 2230 jw.writeEol("argNum = unknownArgument(args, arg, argNum)"); 2231 jw.end(); 2232 for (Iterator it = mandatoryProperties.keySet().iterator(); it.hasNext(); ) { 2233 String sw = (String ) it.next(); 2234 Property a = (Property) mandatoryProperties.get(sw); 2235 jw.beginIf(testIfPropertyNotSet(a)); 2236 jw.writeEol("missingMandatoryArgument(", JavaUtil.instanceFrom("java.lang.String", "-"+sw), 2237 ")"); 2238 jw.end(); 2239 } 2240 jw.writeEol("return false"); 2241 jw.end(); 2242 jw.cr(); 2243 jw.beginMethod("unknownArgument", 2244 "String[] args, String arg, int argNum", null, "int", 2245 jw.PROTECTED); 2246 jw.writeEol("throw new IllegalArgumentException(\"Found unknown argument '\"+arg+\"'\")"); 2247 jw.end(); 2248 jw.cr(); 2249 jw.beginMethod("missingArgument", 2250 "String[] args, String arg", null, "void", 2251 jw.PROTECTED); 2252 jw.writeEol("throw new IllegalArgumentException(\"Not enough arguments. Need 1 more for '\"+arg+\"'\")"); 2253 jw.end(); 2254 jw.cr(); 2255 jw.beginMethod("missingMandatoryArgument", 2256 "String arg", null, "void", 2257 jw.PROTECTED); 2258 jw.writeEol("throw new IllegalArgumentException(\"Missing argument '\"+arg+\"'\")"); 2259 jw.end(); 2260 jw.cr(); 2261 jw.beginMethod("showHelp", "java.io.PrintStream out"); 2262 if (helpParams.length() > 0) { 2263 if (lastHelpParamsPos < helpParams.length()) 2264 helpParams.append("\n"); 2265 jw.writeEol("out.println(", 2266 JavaUtil.instanceFrom("java.lang.String", 2267 helpParams.toString()), 2268 ")"); 2269 } 2270 if (helpText.length() > 0) { 2271 jw.writeEol("out.print(", 2272 JavaUtil.instanceFrom("java.lang.String", 2273 helpText.toString()), 2274 ")"); 2275 } 2276 jw.end(); 2277 jw.cr(); 2278 } 2279 2280 protected boolean isMutuallyExclusive(Property a) { 2281 return (a.ored && !a.getMutuallyExclusiveProperties().isEmpty()); 2282 } 2283 2284 2288 protected void genResetMutuallyExclusive(Property a, boolean genTest) throws IOException { 2289 if (isMutuallyExclusive(a)) { 2290 if (genTest) { 2291 if (a.isIndexed()) { 2292 jw.beginIf("value != null && value.length > 0"); 2293 } else { 2294 jw.beginIf("value != "+JavaUtil.nullValueForType(a.getType())); 2295 } 2296 } 2297 jw.comment("It's a mutually exclusive property."); 2298 for (Iterator it = a.getMutuallyExclusiveProperties().iterator(); it.hasNext(); ) { 2299 Property prop = (Property) it.next(); 2300 jw.writeEol(prop.getWriteMethod(), "(", 2301 prop.isIndexed() ? "null" : JavaUtil.nullValueForType(prop.getType()), 2302 ")"); 2303 } 2304 if (genTest) 2305 jw.end(); 2306 } 2307 } 2308 2309 2312 protected String commonBeanType() { 2313 String cb = "java.lang.Object"; 2314 if (config.getGenerateCommonInterface() != null) { 2315 if (packageName == null) 2316 cb = config.getGenerateCommonInterface(); 2317 else 2318 cb = packageName + "." + config.getGenerateCommonInterface(); 2319 } 2320 return cb; 2321 } 2322 2323 protected String parentBeanType() { 2324 if (config.isExtendBaseBean()) 2325 return "org.netbeans.modules.schema2beans.BaseBean"; 2326 else 2327 return commonBeanType(); 2328 } 2329 2330 2333 protected static String firstInCommaSeparatedList(String lst) { 2334 if (lst == null) 2335 return null; 2336 int pos = lst.indexOf(','); 2337 if (pos < 0) 2338 return lst; 2339 return lst.substring(0, pos); 2340 } 2341 2342 protected String genNewMethod(String abstractType, String concreteType) throws IOException { 2343 return genNewMethod(abstractType, concreteType, concreteType); 2344 } 2345 2346 2354 protected String genNewMethod(String abstractType, String concreteType, 2355 String concreteFullTypeName) throws IOException { 2356 if (abstractType == null) 2357 abstractType = concreteType; 2358 abstractType = abstractType.trim(); 2359 concreteType = concreteType.trim(); 2360 String returnType = abstractType; 2361 boolean abstractConcreteSame = abstractType.equals(concreteType); 2362 if (abstractConcreteSame) 2363 returnType = concreteFullTypeName; 2364 String methodName = (String ) generatedNewMethods.get(abstractType); 2365 if (methodName == null) { 2366 if (abstractType.indexOf('.') >= 0) { 2367 String base = JavaUtil.baseName(abstractType); 2368 if (generatedNewMethods.containsKey(base)) { 2369 methodName = "new"+(abstractType.replace('.', '_')); 2370 } else { 2371 methodName = "new"+base; 2372 } 2373 } else { 2374 methodName = "new"+abstractType; 2375 } 2376 jw.pushSelect(NEW_METHOD_SECTION); 2377 jw.bigComment("Create a new bean using it's default constructor.\nThis does not add it to any bean graph."); 2378 jw.beginMethod(methodName, "", null, returnType, jw.PUBLIC | jw.BEANINFO); 2379 jw.writeEol("return new ", concreteFullTypeName, "()"); 2380 jw.endMethod(); 2381 2382 if (hasDeepCopyConstructor()) { 2383 jw.bigComment("Create a new bean, copying from another one.\nThis does not add it to any bean graph."); 2384 String formalParam = abstractType + " source"; 2385 String actualParam; 2386 if (abstractConcreteSame) 2387 actualParam = "source"; 2388 else 2389 actualParam = "("+concreteType+") source"; 2390 if (config.isGenerateParentRefs()) { 2391 formalParam += ", "+parentBeanType()+" parent"; 2392 actualParam += ", parent"; 2393 } 2394 formalParam += ", boolean justData"; 2395 actualParam += ", justData"; 2396 jw.beginMethod(methodName, formalParam, null, returnType, jw.PUBLIC | jw.BEANINFO); 2397 jw.write("return new ", concreteFullTypeName); 2398 jw.writeEol("(", actualParam, ")"); 2399 jw.endMethod(); 2400 } 2401 jw.popSelect(); 2402 generatedNewMethods.put(abstractType, methodName); 2403 } 2404 return methodName; 2405 } 2406 2407 protected void genNewDefault(Property prop, 2408 boolean wantConcreteType) throws IOException { 2409 String type = prop.getType(); 2410 if (prop.isBean) { 2411 String abstractType = prop.getPropertyInterface(); 2412 String methodName = genNewMethod(abstractType, type, 2413 getTypeFullClassName(prop)); 2414 if (wantConcreteType) { 2415 if (abstractType != null && !type.equals(abstractType)) 2416 jw.write("(", type, ") "); 2417 } 2418 jw.write(methodName, "()"); 2419 } else { 2420 jw.write(JavaUtil.genNewDefault(type)); 2421 } 2422 } 2423 2424 protected boolean hasDeepCopyConstructor() { 2425 return false; 2426 } 2427 2428 protected String testIfPropertySet(Property prop) { 2429 if (prop.isIndexed()) { 2430 return "size"+prop.name+"() > 0"; 2431 } else { 2432 return prop.getReadMethod(false)+"() != "+JavaUtil.nullValueForType(prop.getType()); 2433 } 2434 } 2435 2436 protected String testIfPropertyNotSet(Property prop) { 2437 if (prop.isIndexed()) { 2438 return "size"+prop.name+"() == 0"; 2439 } else { 2440 return prop.getReadMethod(false)+"() == "+JavaUtil.nullValueForType(prop.getType()); 2441 } 2442 } 2443 2444 protected int countNumberOfNonAttributeProperties() { 2445 int count = beanElement.getNonAttributePropertyCount(); 2446 2455 if (config.isRespectExtension()) { 2456 count += countNumberOfNonAttributePropertiesRecurse(beanElement.getExtension()); 2457 } 2458 return count; 2459 } 2460 2461 protected int countNumberOfNonAttributePropertiesRecurse(BeanBuilder.BeanElement be) { 2462 int count = 0; 2463 while (be != null) { 2464 count += be.getNonAttributePropertyCount(); 2466 be = be.getExtension(); 2467 } 2469 return count; 2470 } 2471 2472 public void setPrefixGuesser(PrefixGuesser guesser) { 2473 if (guesser == null) { 2474 if (prefixGuesser == null) { 2475 prefixGuesser = new PrefixGuesser() { 2476 public String guessPrefixFromURI(String uri) { 2477 return SchemaRep.guessPrefix(uri); 2478 } 2479 }; 2480 } 2481 } else { 2482 prefixGuesser = guesser; 2483 } 2484 } 2485} 2486 | Popular Tags |