1 13 14 package org.netbeans.modules.schema2beansdev; 15 16 import java.util.*; 17 import java.io.*; 18 19 import org.netbeans.modules.schema2beans.*; 20 import org.netbeans.modules.schema2beansdev.metadd.*; 21 import org.netbeans.modules.schema2beansdev.gen.*; 22 23 27 43 public class BeanClass extends AbstractCodeGeneratorClass implements CodeGeneratorClass { 44 private boolean isVetoable; 45 private boolean genVetoListeners; 46 private boolean genGenericVetoListeners; 47 static HashMap listOfTags = new HashMap(); 49 protected int GET_DEFAULT_ATTR_GENERIC_SECTION = BODY_SECTION; 50 protected int GET_DEFAULT_ATTR_SECTION = ACCESS_SECTION; 51 52 static String stags = null; 60 static Properties listOfPKs = new Properties(); 61 static { 63 try { 64 System.out.println("listofpks dir=" + System.getProperty("listofpks")); 69 FileInputStream is2 = new FileInputStream(System.getProperty("listofpks")); 70 listOfPKs.load(is2); 71 System.out.println("Loading listofPks" + listOfPKs); 72 } catch(Exception e){} 73 stags = System.getProperty("servertagname"); 74 if(stags == null) stags = "ServerTags"; 75 } 76 77 private String tagFile = stags + "."; 78 79 BeanClass(BeanBuilder.BeanElement be, GenBeans.Config config) { 80 init(be, config); 81 addExtraMethods(); 82 } 83 84 void setBeanName(String n) { 85 this.className = n; 86 } 87 88 private void genVetoBegin() { 89 gencr("try"); begin(); 90 } 91 92 private void genVetoEnd() { 93 end(); 94 gencr("catch(BaseProperty.VetoException ve)"); 95 begin(); 96 gen("throw ve.getPropertyVetoException()"); eol(); 97 end(); 98 } 99 100 String getPK(String ss){ 102 String p = null; 109 110 for(Enumeration e = listOfPKs.propertyNames(); e.hasMoreElements();){ 111 String s = (String ) e.nextElement(); 112 if(ss.equalsIgnoreCase(Common.convertName(s))) { 113 p = listOfPKs.getProperty(s); 114 break; 115 } 116 } 117 120 122 System.out.println("PK for " + ss + "is " + p); 123 if("".equals(p)) return null; 124 return p; 125 } 126 127 static final String VCL_FULL_CLASS_NAME = "java.beans.VetoableChangeListener"; 128 static final String VCL = "VetoableChangeListener"; 129 static final String PCL_FULL_CLASS_NAME = "java.beans.PropertyChangeListener"; 130 static final String PCL = "PropertyChangeListener"; 131 132 135 void genHeader(int out) { 136 select(out); 137 gencr("/*"); 138 gencr(" * The contents of this file are subject to the terms "); 139 gencr(" * of the Common Development and Distribution License "); 140 gencr(" * (the License). You may not use this file except in"); 141 gencr(" * compliance with the License."); 142 gencr(" * "); 143 gencr(" * You can obtain a copy of the license at "); 144 gencr(" * https://glassfish.dev.java.net/public/CDDLv1.0.html or"); 145 gencr(" * glassfish/bootstrap/legal/CDDLv1.0.txt."); 146 gencr(" * See the License for the specific language governing "); 147 gencr(" * permissions and limitations under the License."); 148 gencr(" * "); 149 gencr(" * When distributing Covered Code, include this CDDL "); 150 gencr(" * Header Notice in each file and include the License file "); 151 gencr(" * at glassfish/bootstrap/legal/CDDLv1.0.txt. "); 152 gencr(" * If applicable, add the following below the CDDL Header, "); 153 gencr(" * with the fields enclosed by brackets [] replaced by"); 154 gencr(" * you own identifying information: "); 155 gencr(" * \"Portions Copyrighted [year] [name of copyright owner]\""); 156 gencr(" * "); 157 gencr(" * Copyright 2006 Sun Microsystems, Inc. All rights reserved."); 158 gencr(" */"); 159 gencr(" "); 160 161 gencr("/**"); 162 gencr(" * This generated bean class " + this.className + 163 " matches the DTD element " + this.beanElement.node.getName()); 164 gencr(" *"); 165 gencr(" */"); cr(); 166 } 167 void genPackage(int out) { 168 select(out); 169 if (this.packageName != null) { 170 gen(PACKAGE, this.packageName); 171 eol(); 172 cr(); 173 } 174 } 175 176 void genImports(int out) { 177 select(out); 178 gen(IMPORT, "org.w3c.dom.*"); eol(); 179 gen(IMPORT, "org.netbeans.modules.schema2beans.*"); eol(); 180 gen(IMPORT, "java.beans.*"); eol(); 181 gen(IMPORT, "java.util.*"); eol(); 182 if (this.beanElement.isRoot) { 186 gen(IMPORT, "java.io.*"); eol(); 187 } 188 189 gencr("import java.io.Serializable;"); 191 195 gencr("import com.sun.enterprise.config.ConfigBean;"); 196 gencr("import com.sun.enterprise.config.ConfigException;"); 197 gencr("import com.sun.enterprise.config.StaleWriteConfigException;"); 198 gencr("import com.sun.enterprise.util.i18n.StringManager;"); 199 200 String [] imps = null; 201 if (this.mdd != null) { 202 if (this.metaElement != null) 203 imps = this.metaElement.getImport(); 204 205 if (imps == null || imps.length==0) 206 imps = this.mdd.getImport(); 207 } 208 if (imps != null) { 209 for (int i=0; i<imps.length; i++) { 210 String imp = imps[i]; 211 imp = imp.trim(); 212 if (imp.startsWith("import")) 213 gen(imp); 214 else 215 gen(IMPORT, " ", imp); 216 if (!imp.endsWith(";")) 217 eol(); 218 else 219 cr(); 220 } 221 } 222 223 } 224 225 void genClassName(int out) { 226 String name = null; 227 String impName = null; 228 229 select(out); 230 gen(PUBLIC, CLASS, this.className); 231 232 if (this.mdd != null) { 233 if (this.metaElement != null) { 234 name = this.metaElement.getExtends(); 235 impName = this.metaElement.getImplements(); 236 } 237 if (name == null) { 238 name = this.mdd.getExtends(); 239 } 240 if (impName == null) { 241 impName = this.mdd.getImplements(); 242 } 243 } 244 245 gen(" extends "); 246 if (name != null) 247 gencr(name); 248 else 249 gencr("ConfigBean implements Serializable"); 251 if (impName != null) { 252 gentab(1); gencr(" implements ", impName); 253 } 254 255 begin(); 256 } 257 258 259 void genConstructor(int out) throws IOException { 260 select(out); 261 String thrownExceptions = null; 262 if (this.beanElement.isRoot && shouldThrowException()) { 263 thrownExceptions = "org.netbeans.modules.schema2beans.Schema2BeansException"; 264 } 265 jw.beginConstructor(className, "", thrownExceptions, jw.PUBLIC); 266 gen("this("); 267 if (this.beanElement.isRoot) 268 gen("null, "); 269 gen("Common.USE_DEFAULT_VALUES)"); 270 eol(); 271 end(); cr(); 272 273 if (this.beanElement.isRoot) { 274 jw.beginConstructor(className, "org.w3c.dom.Node doc, int options", 275 thrownExceptions, jw.PUBLIC); 276 jw.writeEol("this(Common.NO_DEFAULT_VALUES)"); 281 if (!shouldThrowException()) { 282 gen("try "); 283 begin(); 284 } 285 gen("initFromNode(doc, options)"); eol(); 286 if (!shouldThrowException()) { 287 end(); 288 gen("catch (Schema2BeansException e) "); 289 begin(); 290 gen("throw new RuntimeException(e)"); 291 eol(); 292 end(); 293 } 294 end(); 295 gen(PROTECTED, VOID, "initFromNode(org.w3c.dom.Node doc, int options) throws Schema2BeansException"); cr(); 299 begin(); 300 gencr("if (doc == null)"); 301 begin(); 302 gen("doc = GraphManager.createRootElementNode(\"", 303 this.beanElement.node.getName(), "\")"); 304 eolNoI18N(); 305 gencr("if (doc == null)"); 306 307 if (this.config.isStandalone()) { 308 tabIn(); 309 gencrNoI18N("throw new Schema2BeansException(\"Cannot create DOM root\");"); 310 } else { 311 tabIn(); 312 gencr("throw new Schema2BeansException(Common.getMessage("); 313 tabIn(); tabIn(); 314 gencr("\"CantCreateDOMRoot_msg\", \""+beanElement.node.getName()+"\"));"); 315 } 316 end(); 317 318 gen("Node n = GraphManager.getElementNode(\""); 319 gen(this.beanElement.node.getName(), "\", doc)"); 320 eolNoI18N(); 321 gencr("if (n == null)"); 322 if (this.config.isStandalone()) { 323 tabIn(); 324 gen("throw new Schema2BeansException(\"Doc root not in the DOM graph\")"); 325 eolNoI18N(); 326 } else { 327 tabIn(); 328 gencr("throw new Schema2BeansException(Common.getMessage("); 329 tabIn(); tabIn(); 330 gen("\"DocRootNotInDOMGraph_msg\", \""+beanElement.node.getName()+"\", doc.getFirstChild().getNodeName()))"); 331 } 332 eol(); 333 334 cr(); 335 gen("this.graphManager.setXmlDocument(doc)"); eol(); cr(); 336 comment("Entry point of the createBeans() recursive calls"); 337 gen("this.createBean(n, this.graphManager())"); eol(); 338 gen("this.initialize(options)"); eol(); 339 end(); 340 } 341 342 gen(PUBLIC, this.className+"(int options)"); 343 348 cr(); 349 begin(); 350 jw.writeEol("super(comparators, runtimeVersion)"); 352 if (this.beanElement.isRoot) { 353 gen("initOptions(options)"); eol(); 354 end(); 355 356 gen(PROTECTED, VOID, "initOptions(int options)"); cr(); 357 begin(); 358 comment("The graph manager is allocated in the bean root"); 359 gen("this.graphManager = new GraphManager(this)"); eol(); 360 gen("this.createRoot(\"", this.beanElement.node.getName(), "\", \""); 361 gen(this.className, "\","); 362 noI18N(); tabIn(); 363 gen("Common.TYPE_1 | Common.TYPE_BEAN, "); 364 gen(this.className, ".class)"); eol(); cr(); 365 } 366 } 367 368 372 void genInitializer() throws IOException { 373 select(INITIALIZE_SECTION); 374 comment("Setting the default values of the properties"); 375 jw.beginMethod("initialize", "int options", null, "void", jw.PACKAGE_LEVEL); 376 if (beanElement.isRoot) { 377 if (getDefaultNamespace() != null) { 378 jw.write("setDefaultNamespace("); 379 jw.write(JavaUtil.instanceFrom("java.lang.String", 380 getDefaultNamespace())); 381 jw.writeEol(")"); 382 } 383 if (mdd.getSchemaLocation() != null) { 384 jw.beginIf("(options & Common.USE_DEFAULT_VALUES) == Common.USE_DEFAULT_VALUES"); 385 jw.write("_setSchemaLocation("); 386 jw.write(JavaUtil.instanceFrom("java.lang.String", 387 mdd.getSchemaLocation())); 388 jw.writeEol(")"); 389 jw.end(); 390 } 391 } 392 393 int size = attrList.size(); 394 for (int i = 0; i < size; i++) { 395 Property a = (Property)attrList.get(i); 396 boolean indexed = a.isIndexed(); 397 String type = a.getType(); 398 String constName = a.constName; 399 String values[] = null; 401 String wrapperClass = null; 402 403 MetaElement me = getMetaElement(a); 404 MetaProperty mp = getMetaProperty(a); 405 if (me != null && !a.isBean) { 407 select(INITIALIZE_SECTION); 408 int elts = 0; 412 413 if (mp != null) 415 values = mp.getDefaultValue(); 416 417 if (values != null) 418 elts = values.length; 419 420 if (elts == 0) 422 values = me.getDefaultValue(); 423 424 if (values != null ) { 425 if (values.length > 0) 426 gencr("if ((options & Common.USE_DEFAULT_VALUES) ", 427 "== Common.USE_DEFAULT_VALUES)"); 428 if (values.length > 1) 429 begin(); 430 for (int j=0; j<values.length; j++) { 431 if (indexed) { 432 gen("this.addValue(", constName, ", "); 433 gen(JavaUtil.instanceFrom(JavaUtil.toObjectType(type), values[j])); 434 gen(")"); 435 eol(); 436 } 437 else { 438 gen("this.setValue(", constName, ", "); 439 gen(JavaUtil.instanceFrom(JavaUtil.toObjectType(type), values[j])); 440 gen(")"); 441 eol(); 442 break; 443 } 444 } 445 if (values.length > 1) 446 end(); 447 } 448 449 elts = 0; 453 values = null; 454 455 if (mp != null) 457 values = mp.getKnownValue(); 458 459 if (values != null) 460 elts = values.length; 461 462 if (elts == 0) 464 values = me.getKnownValue(); 465 466 if (values != null) { 467 for (int j = 0; j < values.length; j++) { 468 jw.writeEol("addKnownValue("+constName+", ", JavaUtil.instanceFrom(JavaUtil.toObjectType(type), values[j]), ")"); 469 } 470 } 471 } 472 } 473 474 boolean first = true; 475 for (int i = 0; i < size; i++) { 476 Property a = (Property)attrList.get(i); 477 boolean indexed = a.isIndexed(); 478 String type = a.getType(); 479 if (!indexed && a.getDefaultValue() != null && (a.elementInstance == Common.TYPE_1 || a.elementInstance == Common.TYPE_1_N)) { 480 if (!JavaUtil.checkValueToType(type, a.getDefaultValue())) { 481 config.messageOut.println(Common.getMessage("MSG_NotAGoodValue", a.getDefaultValue(), type)); 482 } 483 if (first) { 484 first = false; 485 jw.beginIf("(options & Common.USE_DEFAULT_VALUES) == Common.USE_DEFAULT_VALUES"); 486 } 487 jw.write(a.getWriteMethod(), "("); 488 jw.write(JavaUtil.instanceFrom(type, a.getDefaultValue())); 489 jw.writeEol(")"); 490 } 491 } 492 if (!first) 493 jw.end(); 494 } 495 496 497 void genDeclarations(int out) { 498 select(out); 499 cr(); 500 gen("static Vector comparators = new Vector()"); eol(); 501 if (this.metaElement != null) { 502 int size = this.metaElement.sizeComparatorClass(); 503 504 if (size >0) { 505 gen(STATIC); 506 begin(); 507 for (int i=0; i<size; i++) { 508 gen(this.className, ".addComparator(new "); 509 gen(this.metaElement.getComparatorClass(i), "())"); 510 eol(); 511 } 512 end(); 513 } 514 } 515 gen("private static final org.netbeans.modules.schema2beans.Version runtimeVersion = new org.netbeans.modules.schema2beans.Version(" + Version.MAJVER); 516 gen(", " + Version.MINVER); 517 gen(", " + Version.PTCVER, ")"); 518 eol(); 519 } 520 521 523 void genAccessors(int out) throws IOException { 524 int size = this.attrList.size(); 525 boolean defaultKey = true; 526 ArrayList attrNames = new ArrayList(); 527 528 529 select(BODY_SECTION); 531 gencr("/**"); 532 gencr("* get the xpath representation for this element"); 533 gencr("* returns something like abc[@name='value'] or abc"); 534 gencr("* depending on the type of the bean"); 535 gencr("*/"); 536 gencr("protected String getRelativeXPath() {"); 537 gencr(" String ret = null;"); 538 String thisPK = getPK(Common.convertName(this.beanElement.node.getName())); 539 if(thisPK != null) { 540 gencr(" ret = \"" + this.beanElement.node.getName() + "\" + (canHaveSiblings() ? \"[@" + thisPK + "='\" + getAttributeValue(\"" + thisPK + "\") +\"']\" : \"\") ;"); 541 } else { 542 gencr(" ret = \"" + this.beanElement.node.getName() + "\";"); 543 } 544 545 gencr(" return (null != ret ? ret.trim() : null);"); 546 gencr("}"); 547 548 549 550 557 select(TRAILER_SECTION); 558 if (this.beanElement.isRoot) { 559 comment("Special serializer: output XML as serialization"); 565 gen(PRIVATE, VOID, "writeObject(java.io.ObjectOutputStream out) throws java.io.IOException"); 566 begin(); 567 gen("ByteArrayOutputStream baos = new ByteArrayOutputStream()"); eol(); 568 572 gen("write(baos)"); eol(); 573 580 gen("String str = baos.toString();"); eol(); 581 comment("System.out.println(\"str='\"+str+\"'\");"); 582 gen("out.writeUTF(str)"); eol(); 583 end(); 584 585 comment("Special deserializer: read XML as deserialization"); 586 gen(PRIVATE, VOID, "readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException"); 587 begin(); 588 gen("try"); 589 begin(); 590 gen("init(comparators, runtimeVersion)"); 591 eol(); 592 gen("String strDocument = in.readUTF()"); eol(); 593 comment("System.out.println(\"strDocument='\"+strDocument+\"'\");"); 594 gen("ByteArrayInputStream bais = new ByteArrayInputStream(strDocument.getBytes())"); eol(); 595 gen("Document doc = GraphManager.createXmlDocument(bais, false)"); eol(); 596 if (config.isSetDefaults()) { 597 gen("initOptions(Common.USE_DEFAULT_VALUES)"); eol(); 598 gen("initFromNode(doc, Common.USE_DEFAULT_VALUES)"); eol(); 599 } else { 600 gen("initOptions(Common.NO_DEFAULT_VALUES)"); eol(); 601 gen("initFromNode(doc, Common.NO_DEFAULT_VALUES)"); eol(); 602 } 603 end(); 604 gen("catch (Schema2BeansException e) "); 605 begin(); 606 gen("throw new RuntimeException(e)"); eol(); 607 end(); 608 end(); cr(); 609 610 jw.beginMethod("_setSchemaLocation", "String location", null, "void", jw.PUBLIC); 611 jw.beginIf("beanProp().getAttrProp(\"xsi:schemaLocation\", true) == null"); 612 jw.writeEol("createAttribute(\"xmlns:xsi\", \"xmlns:xsi\", AttrProp.CDATA | AttrProp.IMPLIED, null, \"http://www.w3.org/2001/XMLSchema-instance\")"); 613 jw.writeEol("setAttributeValue(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\")"); 614 jw.writeEol("createAttribute(\"xsi:schemaLocation\", \"xsi:schemaLocation\", AttrProp.CDATA | AttrProp.IMPLIED, null, location)"); 615 jw.end(); 616 jw.writeEol("setAttributeValue(\"xsi:schemaLocation\", location)"); 617 jw.end(); 618 jw.cr(); 619 620 jw.beginMethod("_getSchemaLocation", "", null, "String", jw.PUBLIC); 621 jw.beginIf("beanProp().getAttrProp(\"xsi:schemaLocation\", true) == null"); 622 jw.writeEol("createAttribute(\"xmlns:xsi\", \"xmlns:xsi\", AttrProp.CDATA | AttrProp.IMPLIED, null, \"http://www.w3.org/2001/XMLSchema-instance\")"); 623 jw.writeEol("setAttributeValue(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\")"); 624 jw.writeEol("createAttribute(\"xsi:schemaLocation\", \"xsi:schemaLocation\", AttrProp.CDATA | AttrProp.IMPLIED, null, null)"); 625 jw.end(); 626 jw.writeEol("return getAttributeValue(\"xsi:schemaLocation\")"); 627 jw.end(); 628 jw.cr(); 629 } 630 631 select(TRAILER_SECTION); 632 comment("Dump the content of this bean returning it as a String"); 633 gen(PUBLIC, VOID, "dump(StringBuffer str, String indent)"); 634 begin(); 635 gen("String s"); eol(); 636 gen("Object o"); eol(); 637 gen("org.netbeans.modules.schema2beans.BaseBean n"); eol(); 638 639 select(CONSTRUCTOR_SECTION); 640 comment("Properties (see root bean comments for the bean graph)"); 641 642 int propertyCount = 0; 643 for (int i = 0; i < size; i++) { 644 Property a = (Property) attrList.get(i); 645 if (!a.isAttribute()) { 646 ++propertyCount; 647 } 648 } 649 jw.writeEol("initPropertyTables("+propertyCount, ")"); 650 651 select(DECL_SECTION); cr(); 652 653 this.genVetoListeners = false; 658 659 if (this.metaElement != null) { 661 MetaProperty[] mp = this.metaElement.getMetaProperty(); 662 for (int i=0; i<mp.length; i++) { 663 if (mp[i].isKey()) { 664 defaultKey = false; 666 break; 667 } 668 } 669 } 670 671 for(int i=0; i<size; i++) { 672 boolean indexed = false; 673 Property a = (Property)this.attrList.get(i); 674 String constName = a.constName; 675 boolean keyedElement = defaultKey; 676 boolean isWrapper = false; 677 MetaElement me; 678 String scalarType = a.getScalarType(); 679 boolean genVetoable = this.isVetoable; 680 Signatures sigs = getSignatures(a); 681 boolean isScalar = a.isScalar(); 682 683 684 MetaProperty mp = getMetaProperty(a); 689 690 if (mp != null) { 691 keyedElement = (mp.isKey())?true:defaultKey; 692 genVetoable = (mp.isVetoable())?true:genVetoable; 693 } 694 695 me = getMetaElement(a); 697 if (me != null) { 698 711 718 719 genVetoable = (me.isVetoable())?true:genVetoable; 720 } 721 if (!isScalar && !a.isBean) { 722 if (!JavaUtil.isPrimitiveType(a.classType)) 723 isWrapper = true; 724 } 725 726 728 if (genVetoable) { 729 this.genVetoListeners = true; 732 } 733 734 if (a.isBean) 736 genVetoable = false; 737 738 select(DECL_SECTION); 740 gen(STATIC, PUBLIC, FINAL, STRING, (String )constName); 741 gen(" = \""); 742 gen(a.name); 743 gen("\""); 744 eol(); 745 747 select(CONSTRUCTOR_SECTION); 749 if (!a.isAttribute()) { 750 String dtdName = a.dtdName; 752 if(a.dtdName.equals("element-property")) 753 dtdName = "property"; 754 gen("this.createProperty(\"", dtdName, "\", "); 755 758 gen(constName, ", "); 762 if (a.ored) 763 gen("Common.SEQUENCE_OR | "); 764 765 if (genVetoable) 766 gen("Common.TYPE_VETOABLE |"); 767 768 cr(); tabIn(); 769 770 select(BODY_SECTION); 771 if (a.elementInstance == Common.TYPE_1) { 772 if (!Common.isBoolean(a.type)) { 776 select(CONSTRUCTOR_SECTION); 777 gen("Common.TYPE_1"); 778 } 779 else { 780 select(CONSTRUCTOR_SECTION); 788 gen("Common.TYPE_0_1"); 789 } 790 } 791 else 792 if (a.elementInstance == Common.TYPE_0_1) { 793 select(CONSTRUCTOR_SECTION); 794 gen("Common.TYPE_0_1"); 795 } 796 else 797 if (a.elementInstance == Common.TYPE_1_N) { 798 indexed = true; 803 select(CONSTRUCTOR_SECTION); 804 gen("Common.TYPE_1_N"); 805 } 806 else 807 if (a.elementInstance == Common.TYPE_0_N) { 808 indexed = true; 814 select(CONSTRUCTOR_SECTION); 815 gen("Common.TYPE_0_N"); 816 } 817 } 818 select(out); 819 820 String thetype = a.name; 822 boolean isPCDATA = a.getBeanElement().isPCDATA();; 823 if(!indexed) { if (isPCDATA) 827 thetype = "String"; 828 } 831 Property attributeOwner = null; 832 if (a.isAttribute()) { 833 attributeOwner = a.getAttributeOwner(); 834 indexed = attributeOwner.isIndexed(); 835 } 836 String type; 837 if (isScalar) 838 type = scalarType; 839 else 840 type = a.classType; 841 String thrownExceptions = null; 842 if (genVetoable) { 843 thrownExceptions = "java.beans.PropertyVetoException"; 844 } 845 846 String setParameters = ""; 847 if (!isPCDATA) { 849 852 comment("This attribute is ", Common.instanceToString(a.elementInstance)); 853 if (indexed) 855 setParameters = "int index, "; 856 if (a.getPropertyInterface() == null) { 857 setParameters += type + " value"; 858 } else { 859 setParameters += a.getPropertyInterface() + " valueInterface"; 860 } 861 if (genVetoable) { 862 thrownExceptions = "java.beans.PropertyVetoException"; 863 } 864 865 jw.beginMethod(a.getWriteMethod(), setParameters, thrownExceptions, 866 "void", jw.PUBLIC | jw.BEANINFO); 867 if (a.getPropertyInterface() != null) { 868 jw.writeEol(type+" value = ("+type+") valueInterface"); 869 } 870 if (genVetoable) 871 genVetoBegin(); 872 873 SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a.searchExtraData(SchemaRep.WhiteSpace.class); 874 if (ws != null) 875 genWhiteSpaceRestriction(ws, "value", type); 876 if (a.isAttribute()) { 877 AttrProp attrProp = a.getAttrProp(); 878 if (attributeOwner != a) { 879 jw.comment("Make sure we've got a place to put this attribute."); 880 jw.beginIf("size("+attributeOwner.constName+") == 0"); 881 String valueToSetTo = "\"\""; if (Common.isBoolean(attributeOwner.type) && attributeOwner.getCanBeEmpty()) 883 valueToSetTo = "java.lang.Boolean.TRUE"; 884 if (attributeOwner.isIndexed()) { 885 jw.writeEol("addValue("+attributeOwner.constName+", ", 886 valueToSetTo, ")"); 887 } else { 888 jw.writeEol("setValue("+attributeOwner.constName+", ", 889 valueToSetTo, ")"); 890 } 891 jw.end(); 892 if (Common.isBoolean(attributeOwner.type) && attributeOwner.getCanBeEmpty() && attributeOwner.isIndexed()) { 893 jw.write("setValue(", attributeOwner.constName); 894 jw.write(", index"); 895 jw.writeEol(", java.lang.Boolean.TRUE)"); 896 } 897 jw.write("setAttributeValue("); 898 jw.write(attributeOwner.constName+", "); 899 if (attributeOwner.isIndexed()) 900 gen("index, "); 901 jw.write("\""+attrProp.getName()+"\""); 902 } else { 903 gen("setAttributeValue("); 904 gen(a.constName); 905 } 906 gen(", "); 907 if (isScalar) 908 gen("\"\"+value"); 909 else 910 gen(JavaUtil.typeToString(a.getType(), "value")); 911 geneol(")"); 912 } else { 913 gen("this.setValue(", constName, ", "); 914 915 if (indexed) gen("index, "); 916 917 genSetValue(isScalar, a.getType()); 918 919 eol(); 920 } 921 922 if (genVetoable) 923 genVetoEnd(); 924 925 if (!indexed) 926 genResetMutuallyExclusive(a, true); 927 end(); cr(); 928 929 932 comment("Get Method"); 933 String getParameters = ""; 935 if (indexed) 936 getParameters = "int index"; 937 jw.beginMethod(a.getReadMethod(indexed), getParameters, null, 938 a.getPropertyInterface() == null ? type :a.getPropertyInterface(), 939 jw.PUBLIC|jw.BEANINFO); 940 941 if (a.isAttribute()) { 943 List exceps = JavaUtil.exceptionsFromParsingText(a.getType()); 944 if (!exceps.isEmpty()) { 945 gen("try "); 946 begin(); 947 } 948 if (attributeOwner != a) { 949 AttrProp attrProp = a.getAttrProp(); 950 jw.comment("If our element does not exist, then the attribute does not exist."); 951 jw.beginIf("size("+attributeOwner.constName+") == 0"); 952 jw.writeEol("return null"); 953 jw.endElseBegin(); 954 jw.write("return "); 955 jw.writeEol(JavaUtil.genParseText(a.getType(), "getAttributeValue("+attributeOwner.constName+ ((attributeOwner.isIndexed()) ? ", index" : "") +", \""+attrProp.getName()+"\")", config.isForME())); 956 jw.end(); 957 } else { 958 String nullValue = JavaUtil.nullValueForType(a.getType()); 959 gen("return "); 960 if (!"null".equals(nullValue)) 961 gen("(getAttributeValue("+a.constName+") == null) ? "+nullValue+" : "); 962 gen(JavaUtil.genParseText(a.getType(), "getAttributeValue("+a.constName+")", config.isForME())); 963 eol(); 964 } 965 if (!exceps.isEmpty()) { 966 end(); 967 genRethrowExceptions(exceps); 968 } 969 } else if (isScalar) { 970 gen(a.classType, " ret = "); 971 gen("(", a.classType, ")"); 972 gen("this.getValue(", constName); 973 if (indexed) gen(", index"); 974 PC(); eol(); 975 gencr("if (ret == null)"); 976 if (this.config.isScalarException() && !Common.isBoolean(a.type)) { 977 if (this.config.isStandalone()) { 978 tabIn(); 979 gen("throw new NoSuchElementException(\"The element ", 980 a.name, " of type "); 981 gen(scalarType, " has no value.\")"); eol(); 982 } else { 983 tabIn(); 984 gencr("throw new RuntimeException(Common.getMessage("); 985 tabIn(); tabIn(); 986 gencr("\"NoValueForElt_msg\","); 987 tabIn(); tabIn(); 988 gen("new Object[] {\""); 989 gen(constName); 990 gen("\", "); 991 gen("\"", scalarType); 992 gen("\"}));"); 993 cr(); 994 } 995 } 996 else { 997 tabIn(); 998 gen("ret = "); 999 gen("(", a.classType, ")"); 1000 gen("Common.defaultScalarValue(Common."); 1001 gen(Common.typeToString(a.type), ")"); eol(); 1002 } 1003 gen("return "+JavaUtil.fromObject(a.getType(), "ret")); 1004 eol(); 1005 } else { 1006 gen("return (", a.classType, ")"); 1007 gen("this.getValue(", constName); 1008 if (indexed) gen(", index"); PC(); 1009 eol(); 1010 } 1011 end(); cr(); 1012 } 1014 if (indexed) { 1019 if (!a.isAttribute()) { 1020 comment("This attribute is ", Common.instanceToString(a.elementInstance)); 1022 if (a.getPropertyInterface() == null) { 1024 setParameters = type + "[] value"; 1025 } else { 1026 setParameters = a.getPropertyInterface() +"[] value"; 1027 } 1028 jw.beginMethod(a.getWriteMethod(), setParameters, thrownExceptions, "void", jw.PUBLIC|jw.BEANINFO); 1029 1030 if (isScalar) { 1032 gen(a.classType, "[] values = null"); eol(); 1033 gencr("if (value != null)"); 1034 begin(); 1035 gen("values = new ", a.classType, "[value.length]"); eol(); 1036 gencr("for (int i=0; i<value.length; i++)"); 1037 gentab(1); 1038 if (!config.isForME() && Common.isBoolean(a.type)) 1039 gen("values[i] = (value[i] ? Boolean.TRUE : Boolean.FALSE)"); 1040 else 1041 gen("values[i] = new ", a.classType, "(value[i])"); 1042 eol(); 1043 end(); 1044 } 1045 1046 if (genVetoable) 1047 genVetoBegin(); 1048 1049 gen("this.setValue(", constName, ", "); 1050 1051 if (isScalar) 1052 gen("values)"); 1053 else 1054 gen("value)"); 1055 1056 eol(); 1057 if (genVetoable) 1058 genVetoEnd(); 1059 1060 genResetMutuallyExclusive(a, true); 1061 end(); cr(); 1062 1063 comment("Getter Method"); 1065 jw.beginMethod(a.getReadMethod(false), "", null, 1067 (a.getPropertyInterface() == null ? type : a.getPropertyInterface())+"[]", 1068 jw.PUBLIC|jw.BEANINFO); 1069 1070 if (isScalar) { 1072 gen(scalarType, "[] ret = null"); eol(); 1073 gen(a.classType, "[] values = (", a.classType); 1074 gen("[])this.getValues(", constName, ")"); eol(); 1075 gencr("if (values != null)"); 1076 begin(); 1077 gen("ret = new ", scalarType, "[values.length]"); eol(); 1078 gencr("for (int i=0; i<values.length; i++)"); 1079 gentab(1); 1080 gen("ret[i] = values[i].", 1081 Common.wrapperGetMethod(a.type), "()"); eol(); 1082 end(); 1083 geneol("return ret"); 1084 } else { 1085 gen("return (", a.classType, "[])"); 1086 gen("this.getValues(", constName, ")"); 1087 eol(); 1088 } 1089 end(); cr(); 1090 1091 jw.comment("Return the number of properties"); 1092 gen(sigs.findSignature(SIZE)); 1094 sp(); 1095 begin(); 1096 if (!a.isAttribute()) 1098 gen("return this.size(", constName, ")"); 1099 else 1100 gen("return this.size(", attributeOwner.constName, ")"); 1101 eol(); end(); cr(); 1102 1103 1106 comment("Add a new element returning its index in the list"); 1107 1131 gen(PUBLIC, INT, "add", a.name); 1136 PO(); genSetValP(isScalar, scalarType, a.classType); 1137 PC(); cr(); 1138 if (genVetoable) 1139 gencr("\t\tthrows PropertyVetoException"); 1140 gen("\t\tthrows ConfigException"); 1142 begin(); 1143 gencr("return add"+a.name+"(value, true);"); 1144 end(); cr(); 1145 1147 1148 comment("Add a new element returning its index in the list with a boolean flag"); 1149 1150 1151 gen(PUBLIC, INT, "add", a.name); 1156 PO(); genSetValP(isScalar, scalarType, a.classType); 1157 gen(", boolean overwrite"); PC(); cr(); 1159 if (genVetoable) 1160 gencr("\t\tthrows PropertyVetoException"); 1161 gen("\t\tthrows ConfigException"); 1163 begin(); 1164 1165 if (genVetoable) 1167 genVetoBegin(); 1168 1170 if(!isPCDATA) { String pk = getPK(a.name); 1172 if (null == pk) { 1173 throw new NullPointerException ("primary key for " + a.name); 1174 } 1175 1176 pk = Common.convertName(pk); 1177 1178 gencr(a.name + " old = get" + a.name + "By" + pk + "(value.get" + pk + "());"); 1179 gencr("if(old != null) {"); 1180 1181 gencr("\tthrow new ConfigException(StringManager.getManager(" + 1183 this.className + 1184 ".class).getString(\"cannotAddDuplicate\", \"" + 1185 a.name + 1186 "\"));"); 1187 gencr("}"); 1188 } gen("return this.addValue(", constName, ", value, overwrite)"); 1191 eol(); 1194 1195 1196 if (genVetoable) 1197 genVetoEnd(); 1198 end(); cr(); 1199 1200 comment(); 1201 comment("Remove an element using its reference"); 1202 comment("Returns the index the element had in the list"); 1203 comment(); 1204 1209 gen(PUBLIC, INT, "remove", a.name); 1210 PO(); genSetValP(isScalar, scalarType, a.classType); 1211 PC(); 1214 if (genVetoable) { 1215 gencr("throws PropertyVetoException "); 1216 } 1217 begin(); 1218 if (a.getPropertyInterface() != null) { 1220 jw.writeEol(a.getType()+" value = ("+a.getType()+") valueInterface"); 1221 } 1222 if (genVetoable) 1223 genVetoBegin(); 1224 gen("return this.removeValue(", constName, ", "); 1225 genSetValue(isScalar, a.getType()); 1226 eol(); 1227 if (genVetoable) 1228 genVetoEnd(); 1229 end(); cr(); 1230 comment(); 1232 comment("Remove an element using its reference"); 1233 comment("Returns the index the element had in the list"); 1234 comment("with boolean overwrite"); 1235 comment(); 1236 gen(PUBLIC, INT, "remove", a.name); 1238 PO(); genSetValP(isScalar, scalarType, a.classType); 1239 gen(", boolean overwrite"); 1240 PC(); cr(); 1241 if (genVetoable) 1242 gencr("\t\tthrows PropertyVetoException"); 1243 gen("\t\tthrows StaleWriteConfigException"); 1244 1245 begin(); 1246 if (genVetoable) 1248 genVetoBegin(); 1249 gen("return this.removeValue(", constName, ", value, overwrite)"); 1250 eol(); 1252 if (genVetoable) 1253 genVetoEnd(); 1254 end(); cr(); 1255 1257 1258 if (isScalar && !"int".equals(a.getType())) { 1259 comment(); 1262 comment("Remove an element using its index"); 1263 comment(); 1264 jw.beginMethod(a.getRemoveMethod(), "int index", 1266 thrownExceptions, "void", 1267 jw.PUBLIC|jw.BEANINFO); 1268 if (genVetoable) 1270 genVetoBegin(); 1271 gen("this.removeValue(", constName, ", index)"); 1272 eol(); 1273 if (genVetoable) 1274 genVetoEnd(); 1275 end(); cr(); 1276 } 1277 } 1278 1279 } 1280 1281 1282 String plural = a.name+"s"; 1284 String tagName = Common.constName(a.dtdName); 1285 1286 if(indexed && !isPCDATA) { 1288 1289 String pk = getPK(a.name); 1290 pk = Common.convertName(pk); 1291 String pkTag = Common.constName(getPK(a.name)); 1292 1293 gencr("public "+a.name +" get"+a.name +"By"+pk+"(String id) {"); 1294 gencr(" if (null != id) { id = id.trim(); }"); 1295 gencr(a.name + "[] o = get" + a.name + "();"); 1296 gencr(" if (o == null) return null;"); 1297 gencr(""); 1298 gencr(" for (int i=0; i < o.length; i++) {"); 1299 gencr(" if(o[i].getAttributeValue(Common.convertName(" + tagFile + pkTag + ")).equals(id)) {"); 1300 gencr(" return o[i];"); 1301 gencr(" }"); 1302 gencr(" }"); 1303 gencr(""); 1304 1305 gencr(" return null;"); 1306 gencr(" "); 1307 gencr("}"); 1308 1309 1328 } 1329 1330 1331 if(!indexed && isPCDATA) { gencr("/**"); 1335 gencr("* Return the "+a.name +" of the Element "+this.beanElement.node.getName()); 1336 gencr("*/"); 1337 gencr("public String"+" get"+ a.name +"() {"); 1338 gencr("\treturn (String) getValue(" + tagFile +tagName +");"); 1339 gencr("}"); 1340 gencr("/**"); 1342 gencr("* Modify the "+a.name +" of the Element "+this.beanElement.node.getName()); 1343 gencr("* @param v the new value"); 1344 gencr("*/"); 1345 gencr("public void set"+ a.name +"(String v){"); 1346 gencr("\tsetValue(" + tagFile + tagName + ", (null != v ? v.trim() : null));"); 1347 gencr("\t}"); 1348 1349 1356 1373 1380 1381 1382 } 1383 1387 { 1388 MetaElement e = getMetaElement(a); 1390 String cls = null; 1391 if (e != null) 1392 cls = e.getBeanClass(); 1393 if (cls == null) 1394 cls = a.classType; 1395 1397 if (!a.isAttribute()) { 1398 select(CONSTRUCTOR_SECTION); 1399 gen(" | Common."); 1400 if (isScalar && (a.type != Common.TYPE_BOOLEAN)) { 1401 gen(Common.typeToString(Common.TYPE_STRING)); 1402 } else { 1403 gen(Common.typeToString(a.type)); 1404 if (a.type == Common.TYPE_BOOLEAN && !a.getCanBeEmpty()) { 1405 gen(" | Common.TYPE_SHOULD_NOT_BE_EMPTY"); 1406 } 1407 } 1408 if (keyedElement) 1409 gen(" | Common.TYPE_KEY"); 1410 gen(", "); cr(); tabIn(); 1411 gen( cls, ".class)"); eol(); 1412 } 1413 } 1414 1415 if (!a.isAttribute()) { 1417 select(TRAILER_SECTION); 1418 if (isScalar && config.isScalarException() && !Common.isBoolean(a.type)) { 1419 gen("if (this.getValue("); 1420 gen(constName); 1421 gen(") != null) "); 1422 begin(); 1423 } 1424 gen("str.append(indent)"); eol(); 1425 gen("str.append(\"",a.name); 1426 if (indexed) { 1427 gen("[\"+this.size", a.name, "()+\"]"); 1428 } 1429 gen("\")" ); 1430 eolNoI18N(); 1431 if (indexed) { 1432 gen("for(int i=0; i<this.size", a.name, "(); i++)"); 1433 cr(); 1434 begin(); 1435 gen("str.append(indent+\"\\t\")"); eol(); 1436 gen("str.append(\"#\"+i+\":\")"); eol(); 1437 } 1438 1439 boolean isCharArray = a.classType.equals("char[]"); 1440 if (Common.isBoolean(a.type)) { 1441 gen("str.append(indent+\"\\t\")"); eolNoI18N(); 1442 gen("str.append(("); 1443 gen("this.is", a.name, "("); 1444 if (indexed) 1445 gen("i"); 1446 gen(")?\"true\":\"false\"))"); eol(); 1447 } else if (Common.isString(a.type) || isScalar || isCharArray) { 1448 gen("str.append(indent+\"\\t\")"); eolNoI18N(); 1449 gen("str.append(\"<\")"); eolNoI18N(); 1450 1451 if (isWrapper && !isCharArray) 1452 gen("o = "); 1453 else 1454 gen("s = "); 1455 1456 if (isScalar || isCharArray) gen("String.valueOf("); 1457 if(indexed) 1459 { 1460 gen("this.getValue(", constName, ", i)"); 1461 } 1462 else 1463 { 1464 gen("this.get", a.name, "()"); 1465 } 1466 if (isScalar || isCharArray) gen(")"); 1467 eol(); 1468 1469 if (isWrapper && !isCharArray) 1470 gen("str.append((o==null?\"null\":o.toString()"); 1471 else 1472 gen("str.append((s==null?\"null\":s"); 1473 1474 gen(".trim()))"); 1475 eolNoI18N(); 1476 gen("str.append(\">\\n\")"); eolNoI18N(); 1477 } else if (a.isBean) { 1478 gen("n = (org.netbeans.modules.schema2beans.BaseBean) this.get", a.name, "("); 1479 if (indexed) gen("i"); 1480 gencr(");"); 1481 gencr("if (n != null)"); 1482 gen("\tn.dump(str, indent + \"\\t\")"); eolNoI18N(); 1483 gencr("else"); 1484 gen("\tstr.append(indent+\"\\tnull\")"); eolNoI18N(); 1485 } 1486 if (!a.isAttribute()) { 1487 gen("this.dumpAttributes(", constName, ", "); 1488 if (indexed) 1489 gen("i"); 1490 else 1491 gen("0"); 1492 gen(", str, indent)"); eol(); 1493 } 1494 1495 if (isScalar && config.isScalarException() && !Common.isBoolean(a.type)) { 1496 end(); 1497 } 1498 1499 if (indexed) { 1500 end(); 1503 } 1504 cr(); 1505 } 1506 1507 1508 genAttributes(CONSTRUCTOR_SECTION, a.attributes, constName); 1509 select(ACCESS_SECTION); 1510 genDefaultsAccessable(a); 1511 1512 if (a.isBean) { 1513 genNewMethod(a.getPropertyInterface(), a.getType()); 1514 } 1515 else 1516 { 1517 String ss = transformTo_(a.getBeanElement().getDTDName().toUpperCase()); 1518 if (listOfTags.containsKey(ss)) 1519 serverTagsStream.println("\t//static public final String " + ss + " = \""+ a.getBeanElement().getDTDName()+"\";"); 1520 else 1521 serverTagsStream.println("\tstatic public final String " + ss + " = \""+ a.getBeanElement().getDTDName()+"\";"); 1522 listOfTags.put(ss,ss ); 1523 } 1524 } 1525 1526 if (this.beanElement.isRoot) 1528 genAttributes(CONSTRUCTOR_SECTION, 1529 this.beanElement.node.getAttributes(), null); 1530 1531 select(DECL_SECTION); cr(); 1532 1533 select(TRAILER_SECTION); 1534 end(false); cr(); 1535 1536 1537 1540 select(GET_DEFAULT_ATTR_GENERIC_SECTION); 1542 gencr(""); 1543 gencr("/*"); 1544 gencr("* generic method to get default value from dtd"); 1545 gencr("*/"); 1546 gencr("public static String getDefaultAttributeValue(String attr) {"); 1547 gencr("\tif(attr == null) return null;"); 1548 gencr("\tattr = attr.trim();"); 1549 1550 1552 1553 GraphNode node = beanElement.getGraphNode(); 1554 AttrProp attrs[] = node.getAttributes(); 1555 select(out); 1556 1557 for(int i=0; i<attrs.length ; i++) { 1558 select(out); 1559 1563 boolean isBoolean = false; 1564 1565 if (attrs[i].getDefaultValue()==null){ 1566 } 1567 else 1568 if (attrs[i].getDefaultValue().equals("'true'") || attrs[i].getDefaultValue().equals("true")) { 1569 isBoolean =true; 1570 } 1571 else 1572 if (attrs[i].getDefaultValue().equals("'false'") || attrs[i].getDefaultValue().equals("false")){ 1573 isBoolean =true; 1574 } 1575 1577 String constantTagName = transformTo_(attrs[i].getDtdName().toUpperCase()); 1578 if (listOfTags.containsKey(constantTagName)) 1579 serverTagsStream.println("\t//static public final String "+constantTagName+" = \""+ attrs[i].getDtdName()+"\";"); 1580 else 1581 serverTagsStream.println("\tstatic public final String "+constantTagName+" = \""+ attrs[i].getDtdName()+"\";"); 1582 listOfTags.put(constantTagName, constantTagName ); 1583 String thetype ="String"; 1584 1585 if (isBoolean) 1586 thetype ="boolean"; 1587 1588 1589 gencr("/**"); 1590 gencr("* Getter for "+attrs[i].getName() +" of the Element "+this.beanElement.node.getName()); 1591 gencr("* @return the "+attrs[i].getName() +" of the Element "+this.beanElement.node.getName()); 1593 gencr("*/"); 1594 if(isBoolean){ 1595 gencr("public "+thetype+" is"+attrs[i].getName() +"() {"); 1597 gencr("\treturn toBoolean(getAttributeValue(" + tagFile + constantTagName+"));"); 1598 } 1599 else{ 1600 if (attrs[i].getOption()==BaseAttribute.OPTION_IMPLIED){ 1601 gencr("public "+thetype+" get"+ attrs[i].getName() +"() {"); gencr("\t\treturn getAttributeValue(" + tagFile + constantTagName+");"); 1605 } 1610 else{ 1611 gencr("public "+thetype+" get"+ attrs[i].getName() +"() {"); gencr("\treturn getAttributeValue(" + tagFile + constantTagName+");"); 1614 } 1615 } 1616 gencr("}"); 1617 1618 gencr("/**"); 1620 gencr("* Modify the "+attrs[i].getName() +" of the Element "+this.beanElement.node.getName()); 1621 gencr("* @param v the new value"); 1622 gencr("* @throws StaleWriteConfigException if overwrite is false and file changed on disk"); 1623 gencr("*/"); 1625 gencr("public void set"+ attrs[i].getName() +"("+thetype +" v, boolean overwrite) throws StaleWriteConfigException {"); 1626 if(isBoolean){ 1627 gencr("\tsetAttributeValue("+ tagFile + constantTagName+", \"\"+(v==true), overwrite);"); 1628 } 1629 else{ 1630 gencr("\tsetAttributeValue(" + tagFile + constantTagName+", v, overwrite);"); 1631 } 1632 gencr("}"); 1634 1636 gencr("/**"); 1637 gencr("* Modify the "+attrs[i].getName() +" of the Element "+this.beanElement.node.getName()); 1638 gencr("* @param v the new value"); 1639 gencr("*/"); 1641 gencr("public void set"+ attrs[i].getName() +"("+thetype +" v) {"); 1642 if(isBoolean){ 1643 gencr("\tsetAttributeValue("+ tagFile + constantTagName+", \"\"+(v==true));"); 1644 } 1645 else{ 1646 gencr("\tsetAttributeValue(" + tagFile + constantTagName+", v);"); 1647 } 1648 1649 gencr("}"); 1651 1652 select(GET_DEFAULT_ATTR_SECTION); 1654 1655 if(attrs[i].getDefaultValue() != null) { 1656 gencr("/**"); 1657 gencr("* Get the default value of "+attrs[i].getName() +" from dtd"); 1658 gencr("*/"); 1659 gencr("public static String getDefault" + attrs[i].getName() + "() {"); 1660 gencr("\treturn \"" + attrs[i].getDefaultValue()+ "\".trim();"); 1661 gencr("}"); 1662 } 1663 1664 select(GET_DEFAULT_ATTR_GENERIC_SECTION); 1665 if(attrs[i].getDefaultValue() != null) { 1666 gencr("\tif(attr.equals(" + tagFile + constantTagName + ")) return \"" + attrs[i].getDefaultValue() + "\".trim();"); 1667 } 1668 1669 1688 1689 1690 } 1691 1695select(GET_DEFAULT_ATTR_GENERIC_SECTION); 1696 gencr("return null;"); 1697 gencr("}"); 1698 1699 1700 1702 1703 1704 1705 } 1706 1707 static String transformTo_(String in){ 1709 return in.replace('-', '_').toUpperCase(); 1710 } 1711 1712 1714 void genAttributes(int out, AttrProp[] attributes, String name) { 1716 select(out); 1717 if (attributes == null) 1718 return; 1719 for (int j=0; j<attributes.length; j++) { 1720 AttrProp attr = attributes[j]; 1721 genAttribute(name, attr); 1722 } 1723 } 1724 1725 void genAttribute(String name, AttrProp attr) { 1726 String constName = Common.constName(attr.getDtdName()); 1727 gen("this.createAttribute("); 1728 if (name != null) 1729 gen(name, ", "); 1730 gen("\"", attr.getDtdName(), "\", "); 1731 gen("\"", attr.getName(), "\", "); cr(); gentab(4); 1732 gencr(attr.typeAsString(), ","); gentab(4); 1733 String [] values = attr.getValues(); 1734 if (values.length > 0) { 1735 gencr("new String[] {"); 1736 for (int k=0; k<values.length; k++) { 1737 if (k>0) gencr(","); 1738 gentab(5); 1739 gen("\"", values[k], "\""); 1740 } 1741 cr(); gentab(4); gen("}, "); 1742 } 1743 else 1744 gen("null, "); 1745 1746 String value = attr.getDefaultValue(); 1747 1748 if (value != null) 1749 gen("\"", value, "\")"); 1750 else 1751 gen("null)"); 1752 eol(); 1753 } 1754 1755 void genBody(int out) throws IOException { 1756 select(out); 1757 1758 comment(); 1762 jw.beginMethod("addComparator", "org.netbeans.modules.schema2beans.BeanComparator c", null, "void", jw.PUBLIC | jw.STATIC); 1763 gen("comparators.add(c)"); eol(); 1764 end(); cr(); 1765 1766 comment(); 1767 jw.beginMethod("removeComparator", "org.netbeans.modules.schema2beans.BeanComparator c", null, "void", jw.PUBLIC | jw.STATIC); 1768 gen("comparators.remove(c)"); eol(); 1769 end(); 1770 1771 1775 1779 if (this.genGenericVetoListeners) { 1780 comment(); 1781 jw.beginMethod("add"+VCL, VCL_FULL_CLASS_NAME+" l", null, "void", jw.PUBLIC); 1782 gengetprop(); 1783 gen("\tp.addVCListener(l)"); eol(); 1784 end(); cr(); 1785 1786 comment(); 1787 jw.beginMethod("remove"+VCL, VCL_FULL_CLASS_NAME+" l", null, "void", jw.PUBLIC); 1788 gengetprop(); 1789 gen("\tp.removeVCListener(l)"); eol(); 1790 end(); cr(); 1791 } 1792 1793 1797 if (this.genVetoListeners) { 1801 comment(); 1802 jw.beginMethod("add"+VCL, "String n, "+VCL_FULL_CLASS_NAME+" l", null, "void", jw.PUBLIC); 1803 gengetpropbyname(); 1804 gen("\tp.addVCListener(l)"); eol(); 1805 end(); cr(); 1806 1807 comment(); 1808 jw.beginMethod("remove"+VCL, "String n, "+VCL_FULL_CLASS_NAME+" l", null, "void", jw.PUBLIC); 1809 gengetpropbyname(); 1810 gen("\tp.removeVCListener(l)"); eol(); 1811 end(); cr(); 1812 } 1813 1814 if (this.beanElement.isRoot) { 1815 comment(); 1817 comment("This method returns the root of the bean graph"); 1818 comment("Each call creates a new bean graph from the specified DOM graph"); 1819 comment(); 1820 1821 String thrownExceptions = null; 1822 if (shouldThrowException()) { 1823 thrownExceptions = "org.netbeans.modules.schema2beans.Schema2BeansException"; 1824 } 1825 jw.beginMethod("createGraph", "org.w3c.dom.Node doc", thrownExceptions, className, jw.PUBLIC | jw.STATIC); 1826 if (config.isSetDefaults()) 1827 gen("return new ", this.className, "(doc, Common.USE_DEFAULT_VALUES)"); 1828 else 1829 gen("return new ", this.className, "(doc, Common.NO_DEFAULT_VALUES)"); 1830 eol(); 1831 end(); cr(); 1832 1833 jw.beginMethod("createGraph", "java.io.File f", 1834 (thrownExceptions == null) ? "java.io.IOException" : 1835 thrownExceptions + ", " + "java.io.IOException", 1836 className, jw.PUBLIC | jw.STATIC); 1837 jw.writeEol("java.io.InputStream in = new java.io.FileInputStream(f)"); 1838 jw.beginTry(); 1839 gen("return createGraph(in, false)"); eol(); 1840 jw.endFinallyBegin(); 1841 jw.writeEol("in.close()"); 1842 jw.end(); 1843 jw.endMethod(); 1844 1845 jw.beginMethod("createGraph", "java.io.InputStream in", thrownExceptions, className, jw.PUBLIC | jw.STATIC); 1847 gen("return createGraph(in, false)"); eol(); 1848 jw.endMethod(); 1849 1850 jw.beginMethod("createGraph", "java.io.InputStream in, boolean validate", thrownExceptions, className, jw.PUBLIC | jw.STATIC); 1852 if (!shouldThrowException()) { 1853 gen("try "); 1854 begin(); 1855 } 1856 gen("Document doc = GraphManager.createXmlDocument(in, validate)"); 1857 eol(); 1858 gen("return createGraph(doc)"); eol(); 1859 if (!shouldThrowException()) { 1860 end(); 1861 gen("catch (Exception t) "); 1862 begin(); 1863 if (this.config.isStandalone()) { 1864 gencrNoI18N("throw new RuntimeException(\"DOM graph creation failed\", t);"); 1865 } else { 1866 gencr("throw new RuntimeException(Common.getMessage("); 1867 tabIn(); 1868 gencr("\"DOMGraphCreateFailed_msg\","); 1869 tabIn(); 1870 gen("t))"); eol(); 1871 } 1872 end(); 1873 } 1874 end(); cr(); 1875 1876 1877 comment(); 1879 comment("This method returns the root for a new empty bean graph"); 1880 comment(); 1881 1882 jw.beginMethod("createGraph", "", null, className, jw.PUBLIC | jw.STATIC); 1883 if (shouldThrowException()) { 1884 gen("try "); 1885 begin(); 1886 } 1887 gen("return new ", this.className, "()"); eol(); 1888 if (shouldThrowException()) { 1889 end(); 1890 gen("catch (Schema2BeansException e) "); 1891 begin(); 1892 gen("throw new RuntimeException(e)"); eol(); 1893 end(); 1894 } 1895 end(); cr(); 1896 } 1897 1898 MetaElement me = getMetaElement(beanElement); 1899 if (me != null && me.getUserCode() != null) { 1900 String userCode = me.getUserCode(); 1901 cr(); gencr(userCode); 1902 } 1903 } 1904 1905 boolean shouldThrowException() { 1906 return (config.isThrowErrors() || (mdd != null && mdd.isThrowExceptions())); 1907 } 1908 1909 void genValidate() throws IOException { 1910 select(BODY_SECTION); 1911 jw.beginMethod("validate", "", "org.netbeans.modules.schema2beans.ValidateException", "void", jw.PUBLIC); 1912 if (config.isGenerateValidate()) { 1913 genValidateProperties(); 1914 } 1915 end(); 1916 cr(); 1917 } 1918 1919 protected void genValidateFail(String detail, String name, 1920 boolean quoteDetail, 1921 ValidateException.FailureType ft, 1922 JavaWriter out) throws IOException { 1923 out.write("throw new org.netbeans.modules.schema2beans.ValidateException("); 1924 if (quoteDetail) 1925 out.write('"'); 1926 out.write(detail); 1927 if (quoteDetail) 1928 out.write('"'); 1929 out.write(", org.netbeans.modules.schema2beans.ValidateException.FailureType.", 1930 ft.toString()); 1931 out.writeEolNoI18N(", \""+name+"\", this)"); 1932 } 1933 1934 void genTrailer(int out) { 1935 select(out); 1936 gen(PUBLIC, STRING, "dumpBeanNode()"); 1937 begin(); 1938 gen("StringBuffer str = new StringBuffer()"); eol(); 1939 gen("str.append(\"", this.className, "\\n\")"); eolNoI18N(); 1940 gen("this.dump(str, \"\\n \")"); eolNoI18N(); 1941 gen("return str.toString()"); eol(); 1942 end(false); 1943 1944 if (this.config.isDumpToString()) { 1945 cr(); 1946 gencr(PUBLIC, STRING, "toString()"); 1947 begin(); 1948 gen("return this.dumpBeanNode()"); eol(); 1949 end(); 1950 } 1951 } 1952 1953 static PrintStream serverTagsStream = null; 1955 1956 1960 public void generate(OutputStream out, MetaDD mdd) throws IOException { 1961 this.mdd = mdd; 1962 this.metaElement = getMetaElement(beanElement); 1963 1964 1965 if (this.metaElement != null && this.metaElement.isSkipGeneration()) { 1966 config.messageOut.println("Skipping generation of class " 1967 + " (as specified in the mdd file)"); return; 1969 } 1970 1971 if (serverTagsStream==null){ 1973 String genDir = System.getProperty("gendir"); 1974 if(genDir==null) 1975 { 1976 genDir = config.getRootDir()+ "/" +this.packageName.replace('.','/'); 1977 } 1978 serverTagsStream = new PrintStream(new FileOutputStream(genDir + "/" + stags + ".java")); 1979 1980 serverTagsStream.println("/*"); 1981 serverTagsStream.println(" * The contents of this file are subject to the terms "); 1982 serverTagsStream.println(" * of the Common Development and Distribution License "); 1983 serverTagsStream.println(" * (the License). You may not use this file except in"); 1984 serverTagsStream.println(" * compliance with the License."); 1985 serverTagsStream.println(" * "); 1986 serverTagsStream.println(" * You can obtain a copy of the license at "); 1987 serverTagsStream.println(" * https://glassfish.dev.java.net/public/CDDLv1.0.html or"); 1988 serverTagsStream.println(" * glassfish/bootstrap/legal/CDDLv1.0.txt."); 1989 serverTagsStream.println(" * See the License for the specific language governing "); 1990 serverTagsStream.println(" * permissions and limitations under the License."); 1991 serverTagsStream.println(" * "); 1992 serverTagsStream.println(" * When distributing Covered Code, include this CDDL "); 1993 serverTagsStream.println(" * Header Notice in each file and include the License file "); 1994 serverTagsStream.println(" * at glassfish/bootstrap/legal/CDDLv1.0.txt. "); 1995 serverTagsStream.println(" * If applicable, add the following below the CDDL Header, "); 1996 serverTagsStream.println(" * with the fields enclosed by brackets [] replaced by"); 1997 serverTagsStream.println(" * you own identifying information: "); 1998 serverTagsStream.println(" * \"Portions Copyrighted [year] [name of copyright owner]\""); 1999 serverTagsStream.println(" * "); 2000 serverTagsStream.println(" * Copyright 2006 Sun Microsystems, Inc. All rights reserved."); 2001 serverTagsStream.println(" */"); 2002 serverTagsStream.println(" "); 2003 2004 serverTagsStream.println("package " + this.packageName + ";"); 2005 serverTagsStream.println("public class " + stags + "{ "); 2006 } 2007 2008 serverTagsStream.println("// Tags for Element "+beanElement.getDTDName()); 2009 String ss = transformTo_(beanElement.getDTDName().toUpperCase()); 2010 if (listOfTags.containsKey(ss)) 2011 serverTagsStream.println("//static public final String "+ss+" = \""+ beanElement.getDTDName()+"\";"); 2012 else 2013 serverTagsStream.println("static public final String "+transformTo_(beanElement.getDTDName().toUpperCase())+" = \""+ beanElement.getDTDName()+"\";"); 2014 listOfTags.put(ss,ss ); 2015 2017 findAttributeOwners(); 2018 2019 this.isVetoable = this.config.isVetoable(); 2020 2021 if (this.mdd != null) { 2023 org.netbeans.modules.schema2beansdev.metadd.CommonBean[] beans = mdd.childBeans(true); 2025 for (int beanPos = 0; beanPos < beans.length; ++beanPos) { 2026 try { 2027 if (((Boolean )beans[beanPos].fetchPropertyByName("vetoable")).booleanValue()) { 2028 this.isVetoable = this.mdd.isVetoable(); 2030 this.genGenericVetoListeners = true; 2031 break; 2032 } 2033 } catch (IllegalArgumentException e) { 2034 } 2036 } 2037 } 2038 2039 2040 this.genHeader(HEADER_SECTION); 2041 this.genPackage(HEADER_SECTION); 2042 this.genImports(HEADER_SECTION); 2043 select(HEADER_SECTION); 2044 cr(); beginNoI18N(); cr(); 2045 this.genClassName(HEADER_SECTION); 2046 2047 this.genDeclarations(DECL_SECTION); 2048 this.genConstructor(CONSTRUCTOR_SECTION); 2049 2050 this.genAccessors(ACCESS_SECTION); 2051 2052 this.genBody(BODY_SECTION); 2053 2054 this.genInitializer(); 2055 2056 genValidate(); 2057 2058 this.genTrailer(TRAILER_SECTION); 2059 2060 select(CONSTRUCTOR_SECTION); 2061 gen("this.initialize(options)"); eol(); 2062 end(); cr(); 2063 2064 select(INITIALIZE_SECTION); 2065 cr(); end(); cr(); 2066 2067 select(TRAILER_SECTION); 2068 end(); cr(); 2069 endNoI18N(); cr(); 2070 2077 printGenBuffers(out); 2078 } 2079 2080 protected void addExtraMethods() { 2081 jw.addToMethodStore("addComparator", "org.netbeans.modules.schema2beans.BeanComparator c", null, "void", jw.PUBLIC | jw.STATIC); 2083 jw.addToMethodStore("removeComparator", "org.netbeans.modules.schema2beans.BeanComparator c", null, "void", jw.PUBLIC | jw.STATIC); 2084 if (beanElement.isRoot) { 2085 } 2086 jw.addToMethodStore("getValue", "String name", null, "Object"); 2087 jw.addToMethodStore("getValue", "String name, int index", null, "Object"); 2088 jw.addToMethodStore("isNull", "String name", null, "boolean"); 2089 jw.addToMethodStore("isNull", "String name, int index", null, "boolean"); 2090 jw.addToMethodStore("getValues", "String name", null, "Object[]"); 2091 jw.addToMethodStore("setValue", "String name, Object value", null, "void"); 2092 jw.addToMethodStore("setValue", "String name, int index, Object value", null, "void"); 2093 jw.addToMethodStore("setValue", "String name, Object[] value", null, "void"); 2094 jw.addToMethodStore("addValue", "String name, Object value", null, "int"); 2095 jw.addToMethodStore("removeValue", "String name, Object value", null, "int"); 2096 jw.addToMethodStore("removeValue", "String name, int index", null, "void"); 2097 jw.addToMethodStore("indexOf", "String name, Object value", null, "int"); 2098 jw.addToMethodStore("size", "String name", null, "int"); 2099 jw.addToMethodStore("isChoiceProperty", "String name", null, "boolean"); 2100 jw.addToMethodStore("isChoiceProperty", "", null, "boolean"); 2101 jw.addToMethodStore("getAttributeValue", "String name", null, "String"); 2102 jw.addToMethodStore("getAttributeValue", "String propName, String name", null, "String"); 2103 jw.addToMethodStore("setAttributeValue", "String propName, int index, String name, String value", null, "void"); 2104 jw.addToMethodStore("getAttributeValue", "String propName, int index, String name", null, "String"); 2105 jw.addToMethodStore("getAttributeNames", "String propName", null, "String[]"); 2106 jw.addToMethodStore("getAttributeNames", "", null, "String[]"); 2107 jw.addToMethodStore("write", "java.io.OutputStream out", "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansRuntimeException", "void"); 2108 jw.addToMethodStore("write", "java.io.OutputStream out, String encoding", "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException", "void"); 2109 jw.addToMethodStore("write", "java.io.Writer w", "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException", "void"); 2110 jw.addToMethodStore("write", "java.io.Writer w, String encoding", "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException", "void"); 2111 jw.addToMethodStore("writeNoReindent", "java.io.OutputStream out", "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException", "void"); 2112 jw.addToMethodStore("reindent", "", null, "void"); 2113 jw.addToMethodStore("clone", "", null, "Object"); 2114 jw.addToMethodStore("merge", "org.netbeans.modules.schema2beans.BaseBean bean, int mode", null, "void"); 2115 jw.addToMethodStore("merge", "org.netbeans.modules.schema2beans.BaseBean bean", null, "void"); 2116 jw.addToMethodStore("equals", "Object obj", null, "boolean"); 2117 jw.addToMethodStore("parent", "", null, "org.netbeans.modules.schema2beans.BaseBean"); 2118 jw.addToMethodStore("fullName", "", null, "String"); 2119 jw.addToMethodStore("isRoot", "", null, "boolean"); 2120 jw.addToMethodStore("name", "", null, "String"); 2121 jw.addToMethodStore("dtdName", "", null, "String"); 2122 jw.addToMethodStore("dump", "StringBuffer str, String indent", null, "void"); 2123 jw.addToMethodStore("createGraph", "Class clazz, java.io.InputStream in", "org.netbeans.modules.schema2beans.Schema2BeansException", "org.netbeans.modules.schema2beans.BaseBean", jw.PUBLIC | jw.STATIC); 2124 jw.addToMethodStore("createGraph", "Class clazz, java.io.InputStream in, boolean validate", "org.netbeans.modules.schema2beans.Schema2BeansException", "org.netbeans.modules.schema2beans.BaseBean", jw.PUBLIC | jw.STATIC); 2125 jw.addToMethodStore("createGraph", "Class clazz, java.io.InputStream in, boolean validate, org.xml.sax.EntityResolver er", "org.netbeans.modules.schema2beans.Schema2BeansException", "org.netbeans.modules.schema2beans.BaseBean", jw.PUBLIC | jw.STATIC); 2126 jw.addToMethodStore("createGraph", "Class clazz, java.io.InputStream in, boolean validate, org.xml.sax.EntityResolver er, org.xml.sax.ErrorHandler eh", "org.netbeans.modules.schema2beans.Schema2BeansException", "org.netbeans.modules.schema2beans.BaseBean", jw.PUBLIC | jw.STATIC); 2127 jw.addToMethodStore("addPropertyChangeListener", "java.beans.PropertyChangeListener l", null, "void"); 2128 jw.addToMethodStore("removePropertyChangeListener", "java.beans.PropertyChangeListener l", null, "void"); 2129 jw.addToMethodStore("addPropertyChangeListener", "String n, java.beans.PropertyChangeListener l", null, "void"); 2130 jw.addToMethodStore("removePropertyChangeListener", "String n, java.beans.PropertyChangeListener l", null, "void"); 2131 jw.addToMethodStore("comments", "", null, "org.w3c.dom.Comment[]"); 2132 jw.addToMethodStore("addComment", "String comment", null, "org.w3c.dom.Comment"); 2133 jw.addToMethodStore("removeComment", "org.w3c.dom.Comment comment", null, "void"); 2134 jw.addToMethodStore("childBeans", "boolean recursive", null, "org.netbeans.modules.schema2beans.BaseBean[]"); 2135 jw.addToMethodStore("childBeans", "boolean recursive, java.util.List beans", null, "void"); 2136 jw.addToMethodStore("setDefaultNamespace", "String namespace", null, "void"); 2137 jw.addToMethodStore("getDefaultNamespace", "", null, "String"); 2138 jw.addToMethodStore("toString", "", null, "String"); 2139 } 2141 2142 2207 2208 public void setInvalidPropertyNames(Map invalidNames) { 2209 invalidNames.put("Class", null); 2210 invalidNames.put("Property", null); 2211 invalidNames.put("AttributeNames", null); 2212 } 2213 2214 protected String testIfPropertySet(Property prop) { 2215 if (!prop.isAttribute() && prop.isScalar() && 2216 !Common.isBoolean(prop.type)) 2217 return "getValue("+prop.constName+") != null"; 2218 else 2219 return super.testIfPropertySet(prop); 2220 } 2221 2222 protected String testIfPropertyNotSet(Property prop) { 2223 if (!prop.isAttribute() && prop.isScalar() && 2224 !Common.isBoolean(prop.type)) 2225 return "getValue("+prop.constName+") == null"; 2226 else 2227 return super.testIfPropertyNotSet(prop); 2228 } 2229} 2230 2231 | Popular Tags |