0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041:
0042: package org.netbeans.modules.schema2beansdev;
0043:
0044: import java.util.*;
0045: import java.io.*;
0046:
0047: import org.netbeans.modules.schema2beans.*;
0048: import org.netbeans.modules.schema2beansdev.metadd.*;
0049: import org.netbeans.modules.schema2beansdev.gen.*;
0050:
0051: //******************************************************************************
0052: // BEGIN_NOI18N
0053: //******************************************************************************
0054:
0055: /**
0056: * This class generates the bean classes code. There is one instance of
0057: * this class per bean to generate.
0058: *
0059: * The BeanBuilder creates one instance of the BeanClass class for every
0060: * bean class to generate, and for each of these instances,
0061: * the BeanBuilder calls the addPropertyMethod() in order to populate the
0062: * list of properties contained by the bean. Then it calls the process()
0063: * method, which makes the class to generate its java output into
0064: * the output stream specified in the process parameter.
0065: *
0066: * The BeanBuilder might give an optional MetaDD bean graph which contains
0067: * extra information that didn't exist in the dtd. The content of this graph
0068: * is used to generate extra information about the properties of the bean
0069: * (for example the default values, well known values, wrapper class)
0070: */
0071: public class BeanClass extends AbstractCodeGeneratorClass implements
0072: CodeGeneratorClass {
0073: private boolean isVetoable;
0074: private boolean genVetoListeners;
0075: private boolean genGenericVetoListeners;
0076:
0077: BeanClass(BeanBuilder.BeanElement be, GenBeans.Config config) {
0078: init(be, config);
0079: addExtraMethods();
0080: }
0081:
0082: void setBeanName(String n) {
0083: this .className = n;
0084: }
0085:
0086: private void genVetoBegin() {
0087: gencr("try");
0088: begin();
0089: }
0090:
0091: private void genVetoEnd() {
0092: end();
0093: gencr("catch(BaseProperty.VetoException ve)");
0094: begin();
0095: gen("throw ve.getPropertyVetoException()");
0096: eol();
0097: end();
0098: }
0099:
0100: static final String VCL_FULL_CLASS_NAME = "java.beans.VetoableChangeListener";
0101: static final String VCL = "VetoableChangeListener";
0102: static final String PCL_FULL_CLASS_NAME = "java.beans.PropertyChangeListener";
0103: static final String PCL = "PropertyChangeListener";
0104:
0105: /**
0106: * Generate the different parts of the class
0107: */
0108: void genHeader(int out) throws IOException {
0109: select(out);
0110: gencr("/**");
0111: gencr(" * This generated bean class " + this .className
0112: + " matches the schema element '"
0113: + this .beanElement.node.getName() + "'.");
0114: if (!beanElement.isRoot) {
0115: jw.writecr(" * The root bean class is "
0116: + rootBeanElement.getClassType());
0117: }
0118: gencr(" *");
0119: printComment(" * ");
0120: if (config.isGenerateTimeStamp())
0121: gencr(" * Generated on " + new Date());
0122: if (this .beanElement.isRoot) {
0123: gencr(" *");
0124: gencr(" * This class matches the root element of the "
0125: + (config.getSchemaTypeNum() == GenBeans.Config.DTD ? "DTD"
0126: : "XML Schema") + ",");
0127: gencr(" * and is the root of the following bean graph:");
0128: gencr(" *");
0129:
0130: dumpBeanTree(jw, " *" + jw.getIndent(), jw.getIndent());
0131: gencr(" *");
0132: }
0133: gencr(" * @" + Common.GENERATED_TAG);
0134: gencr(" */");
0135: cr();
0136: }
0137:
0138: void genPackage(int out) {
0139: select(out);
0140: if (this .packageName != null) {
0141: gen(PACKAGE, this .packageName);
0142: eol();
0143: cr();
0144: }
0145: }
0146:
0147: void genImports(int out) {
0148: select(out);
0149: gen(IMPORT, "org.w3c.dom.*");
0150: eol();
0151: gen(IMPORT, "org.netbeans.modules.schema2beans.*");
0152: eol();
0153: gen(IMPORT, "java.beans.*");
0154: eol();
0155: gen(IMPORT, "java.util.*");
0156: eol();
0157: //if (!this.config.isStandalone()) {
0158: // gen(IMPORT, "org.openide.util.NbBundle"); eol();
0159: //}
0160: if (this .beanElement.isRoot) {
0161: gen(IMPORT, "java.io.*");
0162: eol();
0163: }
0164:
0165: String[] imps = null;
0166: if (this .mdd != null) {
0167: if (this .metaElement != null)
0168: imps = this .metaElement.getImport();
0169:
0170: if (imps == null || imps.length == 0)
0171: imps = this .mdd.getImport();
0172: }
0173: if (imps != null) {
0174: for (int i = 0; i < imps.length; i++) {
0175: String imp = imps[i];
0176: imp = imp.trim();
0177: if (imp.startsWith("import"))
0178: gen(imp);
0179: else
0180: gen(IMPORT, " ", imp);
0181: if (!imp.endsWith(";"))
0182: eol();
0183: else
0184: cr();
0185: }
0186: }
0187:
0188: }
0189:
0190: void genClassName(int out) {
0191: String name = null;
0192: String impName = null;
0193:
0194: select(out);
0195: gen(PUBLIC, CLASS, this .className);
0196:
0197: if (this .mdd != null) {
0198: if (this .metaElement != null) {
0199: name = this .metaElement.getExtends();
0200: impName = this .metaElement.getImplements();
0201: }
0202: if (name == null) {
0203: name = this .mdd.getExtends();
0204: }
0205: if (impName == null) {
0206: impName = this .mdd.getImplements();
0207: }
0208: }
0209:
0210: gen(" extends ");
0211: if (name != null)
0212: gencr(name);
0213: else
0214: gencr("org.netbeans.modules.schema2beans.BaseBean");
0215:
0216: if (impName != null) {
0217: gentab(1);
0218: gencr(" implements ", impName);
0219: }
0220:
0221: begin();
0222: }
0223:
0224: void genConstructor(int out) throws IOException {
0225: select(out);
0226: String thrownExceptions = null;
0227: if (this .beanElement.isRoot && shouldThrowException()) {
0228: thrownExceptions = "org.netbeans.modules.schema2beans.Schema2BeansException";
0229: }
0230: jw.beginConstructor(className, "", thrownExceptions, jw.PUBLIC);
0231: gen("this(");
0232: if (this .beanElement.isRoot)
0233: gen("null, ");
0234: gen("Common.USE_DEFAULT_VALUES)");
0235: eol();
0236: end();
0237: cr();
0238:
0239: if (this .beanElement.isRoot) {
0240: jw.beginConstructor(className,
0241: "org.w3c.dom.Node doc, int options",
0242: thrownExceptions, jw.PUBLIC);
0243: //
0244: // This call should never pass anything but NO_DEFAULT_VALUES, since
0245: // initFromNode will get the options that are specified.
0246: //
0247: jw.writeEol("this(Common.NO_DEFAULT_VALUES)");
0248: if (!shouldThrowException()) {
0249: gen("try ");
0250: begin();
0251: }
0252: gen("initFromNode(doc, options)");
0253: eol();
0254: if (!shouldThrowException()) {
0255: end();
0256: gen("catch (Schema2BeansException e) ");
0257: begin();
0258: gen("throw new RuntimeException(e)");
0259: eol();
0260: end();
0261: }
0262: end();
0263: // Make it so that initFromNode can be called from other
0264: // methods in this class (like ones used for deserializing
0265: // from a DOM tree).
0266: gen(PROTECTED, VOID,
0267: "initFromNode(org.w3c.dom.Node doc, int options) throws Schema2BeansException");
0268: cr();
0269: begin();
0270: gencr("if (doc == null)");
0271: begin();
0272: gen("doc = GraphManager.createRootElementNode(\"",
0273: this .beanElement.node.getName(), "\")");
0274: eolNoI18N();
0275: gencr("if (doc == null)");
0276:
0277: if (this .config.isStandalone()) {
0278: tabIn();
0279: gencrNoI18N("throw new Schema2BeansException(\"Cannot create DOM root\");");
0280: } else {
0281: tabIn();
0282: gencr("throw new Schema2BeansException(Common.getMessage(");
0283: tabIn();
0284: tabIn();
0285: gencr("\"CantCreateDOMRoot_msg\", \""
0286: + beanElement.node.getName() + "\"));");
0287: }
0288: end();
0289:
0290: gen("Node n = GraphManager.getElementNode(\"");
0291: gen(this .beanElement.node.getName(), "\", doc)");
0292: eolNoI18N();
0293: gencr("if (n == null)");
0294: if (this .config.isStandalone()) {
0295: tabIn();
0296: gen("throw new Schema2BeansException(\"Doc root not in the DOM graph\")");
0297: eolNoI18N();
0298: } else {
0299: tabIn();
0300: gencr("throw new Schema2BeansException(Common.getMessage(");
0301: tabIn();
0302: tabIn();
0303: gen("\"DocRootNotInDOMGraph_msg\", \""
0304: + beanElement.node.getName()
0305: + "\", doc.getFirstChild().getNodeName()))");
0306: }
0307: eol();
0308:
0309: cr();
0310: gen("this.graphManager.setXmlDocument(doc)");
0311: eol();
0312: cr();
0313: comment("Entry point of the createBeans() recursive calls");
0314: gen("this.createBean(n, this.graphManager())");
0315: eol();
0316: gen("this.initialize(options)");
0317: eol();
0318: end();
0319: }
0320:
0321: gen(PUBLIC, this .className + "(int options)");
0322: /*
0323: if (this.beanElement.isRoot && shouldThrowException()) {
0324: gen(" throws Schema2BeansException ");
0325: }
0326: */
0327: cr();
0328: begin();
0329: //gen("super(", this.className, ".comparators");
0330: jw.writeEol("super(comparators, runtimeVersion)");
0331: if (this .beanElement.isRoot) {
0332: gen("initOptions(options)");
0333: eol();
0334: end();
0335:
0336: gen(PROTECTED, VOID, "initOptions(int options)");
0337: cr();
0338: begin();
0339: comment("The graph manager is allocated in the bean root");
0340: gen("this.graphManager = new GraphManager(this)");
0341: eol();
0342: gen("this.createRoot(\"", this .beanElement.node.getName(),
0343: "\", \"");
0344: gen(this .className, "\",");
0345: noI18N();
0346: tabIn();
0347: gen("Common.TYPE_1 | Common.TYPE_BEAN, ");
0348: gen(this .className, ".class)");
0349: eol();
0350: cr();
0351: }
0352: }
0353:
0354: /**
0355: * This method needs to be run after genAccessors is run, so that
0356: * all of the a.isIndexed values are correct (for attributes).
0357: */
0358: void genInitializer() throws IOException {
0359: select(INITIALIZE_SECTION);
0360: comment("Setting the default values of the properties");
0361: jw.beginMethod("initialize", "int options", null, "void",
0362: jw.PACKAGE_LEVEL);
0363: if (beanElement.isRoot) {
0364: if (getDefaultNamespace() != null) {
0365: jw.write("setDefaultNamespace(");
0366: jw.write(JavaUtil.instanceFrom("java.lang.String",
0367: getDefaultNamespace()));
0368: jw.writeEol(")");
0369: }
0370: if (mdd.getSchemaLocation() != null) {
0371: jw
0372: .beginIf("(options & Common.USE_DEFAULT_VALUES) == Common.USE_DEFAULT_VALUES");
0373: jw.write("_setSchemaLocation(");
0374: jw.write(JavaUtil.instanceFrom("java.lang.String", mdd
0375: .getSchemaLocation()));
0376: jw.writeEol(")");
0377: jw.end();
0378: }
0379: }
0380:
0381: int size = attrList.size();
0382: for (int i = 0; i < size; i++) {
0383: Property a = (Property) attrList.get(i);
0384: boolean indexed = a.isIndexed();
0385: String type = a.getType();
0386: String constName = a.constName;
0387: // Generate the initializer (default & well known property values)
0388: String values[] = null;
0389: String wrapperClass = null;
0390:
0391: MetaElement me = getMetaElement(a);
0392: MetaProperty mp = getMetaProperty(a);
0393: // no default value for a bean element (only for final property)
0394: if (me != null && !a.isBean) {
0395: select(INITIALIZE_SECTION);
0396: //
0397: // Generate the default values
0398: //
0399: int elts = 0;
0400:
0401: // Get the current property default values first
0402: if (mp != null)
0403: values = mp.getDefaultValue();
0404:
0405: if (values != null)
0406: elts = values.length;
0407:
0408: // If no default value, try to get the generic ones
0409: if (elts == 0)
0410: values = me.getDefaultValue();
0411:
0412: if (values != null) {
0413: if (values.length > 0)
0414: gencr(
0415: "if ((options & Common.USE_DEFAULT_VALUES) ",
0416: "== Common.USE_DEFAULT_VALUES)");
0417: if (values.length > 1)
0418: begin();
0419: for (int j = 0; j < values.length; j++) {
0420: if (indexed) {
0421: gen("this.addValue(", constName, ", ");
0422: gen(JavaUtil.instanceFrom(JavaUtil
0423: .toObjectType(type), values[j]));
0424: gen(")");
0425: eol();
0426: } else {
0427: gen("this.setValue(", constName, ", ");
0428: gen(JavaUtil.instanceFrom(JavaUtil
0429: .toObjectType(type), values[j]));
0430: gen(")");
0431: eol();
0432: break;
0433: }
0434: }
0435: if (values.length > 1)
0436: end();
0437: }
0438:
0439: //
0440: // Generate the well-known values
0441: //
0442: elts = 0;
0443: values = null;
0444:
0445: // Get the current property default values first
0446: if (mp != null)
0447: values = mp.getKnownValue();
0448:
0449: if (values != null)
0450: elts = values.length;
0451:
0452: // If no default value, try to get the generic ones
0453: if (elts == 0)
0454: values = me.getKnownValue();
0455:
0456: if (values != null) {
0457: for (int j = 0; j < values.length; j++) {
0458: jw
0459: .writeEol("addKnownValue(" + constName
0460: + ", ", JavaUtil.instanceFrom(
0461: JavaUtil.toObjectType(type),
0462: values[j]), ")");
0463: }
0464: }
0465: }
0466: }
0467:
0468: boolean first = true;
0469: for (int i = 0; i < size; i++) {
0470: Property a = (Property) attrList.get(i);
0471: boolean indexed = a.isIndexed();
0472: String type = a.getType();
0473: if (!indexed
0474: && a.getDefaultValue() != null
0475: && (a.elementInstance == Common.TYPE_1 || a.elementInstance == Common.TYPE_1_N)) {
0476: if (!JavaUtil.checkValueToType(type, a
0477: .getDefaultValue())) {
0478: config.messageOut.println(Common.getMessage(
0479: "MSG_NotAGoodValue", a.getDefaultValue(),
0480: type));
0481: }
0482: if (first) {
0483: first = false;
0484: jw
0485: .beginIf("(options & Common.USE_DEFAULT_VALUES) == Common.USE_DEFAULT_VALUES");
0486: }
0487: jw.write(a.getWriteMethod(), "(");
0488: jw.write(JavaUtil.instanceFrom(type, a
0489: .getDefaultValue()));
0490: jw.writeEol(")");
0491: }
0492: }
0493: if (!first)
0494: jw.end();
0495: }
0496:
0497: void genDeclarations(int out) {
0498: select(out);
0499: cr();
0500: gen("static Vector comparators = new Vector()");
0501: eol();
0502: if (this .metaElement != null) {
0503: int size = this .metaElement.sizeComparatorClass();
0504:
0505: if (size > 0) {
0506: gen(STATIC);
0507: begin();
0508: for (int i = 0; i < size; i++) {
0509: gen(this .className, ".addComparator(new ");
0510: gen(this .metaElement.getComparatorClass(i), "())");
0511: eol();
0512: }
0513: end();
0514: }
0515: }
0516: gen("private static final org.netbeans.modules.schema2beans.Version runtimeVersion = new org.netbeans.modules.schema2beans.Version("
0517: + Version.MAJVER);
0518: gen(", " + Version.MINVER);
0519: gen(", " + Version.PTCVER, ")");
0520: eol();
0521: }
0522:
0523: /**
0524: */
0525: void genAccessors(int out) throws IOException {
0526: int size = this .attrList.size();
0527: boolean defaultKey = true;
0528: ArrayList attrNames = new ArrayList();
0529:
0530: select(TRAILER_SECTION);
0531: if (this .beanElement.isRoot) {
0532: //
0533: // Put in some writeObject/readObject serializer/deserializer
0534: // methods, so that this object can implement java.io.Serializable
0535: // correctly.
0536: //
0537: comment("Special serializer: output XML as serialization");
0538: gen(PRIVATE, VOID,
0539: "writeObject(java.io.ObjectOutputStream out) throws java.io.IOException");
0540: begin();
0541:
0542: gen("out.defaultWriteObject()");
0543: eol();
0544: gen("final int MAX_SIZE = 0XFFFF");
0545: eol();
0546: gen("final ByteArrayOutputStream baos = new ByteArrayOutputStream()");
0547: eol();
0548: gen("write(baos)");
0549: eol();
0550: gen("final byte [] array = baos.toByteArray()");
0551: eol();
0552: gen("final int numStrings = array.length / MAX_SIZE");
0553: eol();
0554: gen("final int leftover = array.length % MAX_SIZE");
0555: eol();
0556: gen("out.writeInt(numStrings + (0 == leftover ? 0 : 1))");
0557: eol();
0558: gen("out.writeInt(MAX_SIZE)");
0559: eol();
0560: gen("int offset = 0");
0561: eol();
0562: gen("for (int i = 0; i < numStrings; i++)");
0563: begin();
0564: gen("out.writeUTF(new String(array, offset, MAX_SIZE))");
0565: eol();
0566: gen("offset += MAX_SIZE");
0567: eol();
0568: end();
0569: gen("if (leftover > 0)");
0570: begin();
0571: gen("final int count = array.length - offset");
0572: eol();
0573: gen("out.writeUTF(new String(array, offset, count))");
0574: eol();
0575: end();
0576: end();
0577:
0578: comment("Special deserializer: read XML as deserialization");
0579: gen(
0580: PRIVATE,
0581: VOID,
0582: "readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException");
0583: begin();
0584: gen("try");
0585: begin();
0586:
0587: gen("in.defaultReadObject()");
0588: eol();
0589: gen("init(comparators, runtimeVersion)");
0590: eol();
0591: comment("init(comparators, new GenBeans.Version(1, 0, 8))");
0592: gen("final int numStrings = in.readInt()");
0593: eol();
0594: gen("final int max_size = in.readInt()");
0595: eol();
0596: gen("final StringBuffer sb = new StringBuffer(numStrings * max_size)");
0597: eol();
0598: gen("for (int i = 0; i < numStrings; i++)");
0599: begin();
0600: gen("sb.append(in.readUTF())");
0601: eol();
0602: end();
0603: gen("ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes())");
0604: eol();
0605: gen("Document doc = GraphManager.createXmlDocument(bais, false)");
0606: eol();
0607: if (config.isSetDefaults()) {
0608: gen("initOptions(Common.USE_DEFAULT_VALUES)");
0609: eol();
0610: gen("initFromNode(doc, Common.USE_DEFAULT_VALUES)");
0611: eol();
0612: } else {
0613: gen("initOptions(Common.NO_DEFAULT_VALUES)");
0614: eol();
0615: gen("initFromNode(doc, Common.NO_DEFAULT_VALUES)");
0616: eol();
0617: }
0618: end();
0619: gen("catch (Schema2BeansException e)");
0620: begin();
0621: gen("throw new RuntimeException(e)");
0622: eol();
0623: end();
0624: end();
0625: cr();
0626:
0627: jw.beginMethod("_setSchemaLocation", "String location",
0628: null, "void", jw.PUBLIC);
0629: jw
0630: .beginIf("beanProp().getAttrProp(\"xsi:schemaLocation\", true) == null");
0631: jw
0632: .writeEol("createAttribute(\"xmlns:xsi\", \"xmlns:xsi\", AttrProp.CDATA | AttrProp.IMPLIED, null, \"http://www.w3.org/2001/XMLSchema-instance\")");
0633: jw
0634: .writeEol("setAttributeValue(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\")");
0635: jw
0636: .writeEol("createAttribute(\"xsi:schemaLocation\", \"xsi:schemaLocation\", AttrProp.CDATA | AttrProp.IMPLIED, null, location)");
0637: jw.end();
0638: jw
0639: .writeEol("setAttributeValue(\"xsi:schemaLocation\", location)");
0640: jw.end();
0641: jw.cr();
0642:
0643: jw.beginMethod("_getSchemaLocation", "", null, "String",
0644: jw.PUBLIC);
0645: jw
0646: .beginIf("beanProp().getAttrProp(\"xsi:schemaLocation\", true) == null");
0647: jw
0648: .writeEol("createAttribute(\"xmlns:xsi\", \"xmlns:xsi\", AttrProp.CDATA | AttrProp.IMPLIED, null, \"http://www.w3.org/2001/XMLSchema-instance\")");
0649: jw
0650: .writeEol("setAttributeValue(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\")");
0651: jw
0652: .writeEol("createAttribute(\"xsi:schemaLocation\", \"xsi:schemaLocation\", AttrProp.CDATA | AttrProp.IMPLIED, null, null)");
0653: jw.end();
0654: jw
0655: .writeEol("return getAttributeValue(\"xsi:schemaLocation\")");
0656: jw.end();
0657: jw.cr();
0658: }
0659:
0660: select(TRAILER_SECTION);
0661: comment("Dump the content of this bean returning it as a String");
0662: gen(PUBLIC, VOID, "dump(StringBuffer str, String indent)");
0663: begin();
0664: gen("String s");
0665: eol();
0666: gen("Object o");
0667: eol();
0668: gen("org.netbeans.modules.schema2beans.BaseBean n");
0669: eol();
0670:
0671: select(CONSTRUCTOR_SECTION);
0672: comment("Properties (see root bean comments for the bean graph)");
0673:
0674: int propertyCount = 0;
0675: for (int i = 0; i < size; i++) {
0676: Property a = (Property) attrList.get(i);
0677: if (!a.isAttribute()) {
0678: ++propertyCount;
0679: }
0680: }
0681: jw.writeEol("initPropertyTables(" + propertyCount, ")");
0682:
0683: select(DECL_SECTION);
0684: cr();
0685:
0686: //
0687: // Assume that there is no property interested in veto events.
0688: // We'll adjust later on in the above properties loop.
0689: //
0690: this .genVetoListeners = false;
0691:
0692: // Find out if any element has a key
0693: if (this .metaElement != null) {
0694: MetaProperty[] mp = this .metaElement.getMetaProperty();
0695: for (int i = 0; i < mp.length; i++) {
0696: if (mp[i].isKey()) {
0697: // At least one key is defined - default to false
0698: defaultKey = false;
0699: break;
0700: }
0701: }
0702: }
0703:
0704: for (int i = 0; i < size; i++) {
0705: boolean indexed = false;
0706: Property a = (Property) this .attrList.get(i);
0707: String constName = a.constName;
0708: boolean keyedElement = defaultKey;
0709: boolean isWrapper = false;
0710: MetaElement me;
0711: String scalarType = a.getScalarType();
0712: boolean genVetoable = this .isVetoable;
0713: Signatures sigs = getSignatures(a);
0714: boolean isScalar = a.isScalar();
0715:
0716: //
0717: // Information about this property defined in the mdd file
0718: // (extra information provided on top of the dtd informations)
0719: //
0720: MetaProperty mp = getMetaProperty(a);
0721:
0722: if (mp != null) {
0723: keyedElement = (mp.isKey()) ? true : defaultKey;
0724: genVetoable = (mp.isVetoable()) ? true : genVetoable;
0725: }
0726:
0727: // Change the class type with the Wrapper info
0728: me = getMetaElement(a);
0729: if (me != null) {
0730: /*
0731: String tmp = me.getWrapperClass();
0732: if (tmp != null && !tmp.equals("") &&
0733: !tmp.equals("String") && !tmp.equals("java.lang.String")) {
0734: int t = Common.wrapperToType(tmp);
0735: if (t != Common.NONE)
0736: a.type = t;
0737: else {
0738: a.classType = tmp;
0739: isWrapper = true;
0740: }
0741: }
0742: */
0743: /*
0744: if (me.getBeanName() != null) {
0745: if (config.isTraceGen() && !a.name.equals(me.getBeanName()))
0746: config.messageOut.println("Reseting a.name from "+a.name+" to "+me.getBeanName());
0747: a.name = me.getBeanName();
0748: }
0749: */
0750:
0751: genVetoable = (me.isVetoable()) ? true : genVetoable;
0752: }
0753: if (!isScalar && !a.isBean) {
0754: if (!JavaUtil.isPrimitiveType(a.classType))
0755: isWrapper = true;
0756: }
0757:
0758: //System.out.println("a.name="+a.name+" a.type="+a.type+" a.dtdName="+a.dtdName+" isWrapper="+isWrapper+" a.classType="+a.classType+" me="+me+" constName="+constName);
0759:
0760: if (genVetoable) {
0761: // At least prop is handling veto events - we'll need to
0762: // generate the listener methods.
0763: this .genVetoListeners = true;
0764: }
0765:
0766: // Only final prop can be vetoable
0767: if (a.isBean)
0768: genVetoable = false;
0769:
0770: // Generate the constants
0771: select(DECL_SECTION);
0772: gen(STATIC, PUBLIC, FINAL, STRING, (String) constName);
0773: gen(" = \"");
0774: gen(a.name);
0775: gen("\"");
0776: eolNoI18N();
0777:
0778: // Keep track of the constant name for this property
0779: select(CONSTRUCTOR_SECTION);
0780: if (!a.isAttribute()) {
0781: gen("this.createProperty(\"", a.dtdName, "\", ");
0782: noI18N();
0783: tabIn();
0784: gen(constName, ", ");
0785: if (a.ored)
0786: gen("Common.SEQUENCE_OR | ");
0787:
0788: if (genVetoable)
0789: gen("Common.TYPE_VETOABLE |");
0790:
0791: cr();
0792: tabIn();
0793:
0794: select(BODY_SECTION);
0795: if (a.elementInstance == Common.TYPE_1) {
0796: //
0797: // The property is mandatory. Check it is properly set.
0798: //
0799: if (!Common.isBoolean(a.type)) {
0800: select(CONSTRUCTOR_SECTION);
0801: gen("Common.TYPE_1");
0802: } else {
0803: //
0804: // A boolean type cannot be mandatory because the
0805: // value of the tag depends on the existance of the
0806: // empty tag in the document (a mandatory DTD empty
0807: // element doesn't make sense since it would always
0808: // be true).
0809: //
0810: select(CONSTRUCTOR_SECTION);
0811: gen("Common.TYPE_0_1");
0812: }
0813: } else if (a.elementInstance == Common.TYPE_0_1) {
0814: select(CONSTRUCTOR_SECTION);
0815: gen("Common.TYPE_0_1");
0816: } else if (a.elementInstance == Common.TYPE_1_N) {
0817: //
0818: // Check out that the first element is set and check
0819: // that every element set is valid.
0820: //
0821: indexed = true;
0822: select(CONSTRUCTOR_SECTION);
0823: gen("Common.TYPE_1_N");
0824: } else if (a.elementInstance == Common.TYPE_0_N) {
0825: //
0826: // The property is optional. However if
0827: // the property is set and is a bean,
0828: // its content has to be valid.
0829: //
0830: indexed = true;
0831: select(CONSTRUCTOR_SECTION);
0832: gen("Common.TYPE_0_N");
0833: }
0834: }
0835: select(out);
0836:
0837: /*
0838: * Set method
0839: */
0840: Property attributeOwner = null;
0841: if (a.isAttribute()) {
0842: attributeOwner = a.getAttributeOwner();
0843: indexed = attributeOwner.isIndexed();
0844: }
0845: comment("This attribute is ", Common
0846: .instanceToString(a.elementInstance));
0847: String type;
0848: if (isScalar)
0849: type = scalarType;
0850: else
0851: type = a.classType;
0852: // Set - signature
0853: String setParameters = "";
0854: if (indexed)
0855: setParameters = "int index, ";
0856: if (a.getPropertyInterface() == null) {
0857: setParameters += type + " value";
0858: } else {
0859: setParameters += a.getPropertyInterface()
0860: + " valueInterface";
0861: }
0862: String thrownExceptions = null;
0863: if (genVetoable) {
0864: thrownExceptions = "java.beans.PropertyVetoException";
0865: }
0866:
0867: jw.beginMethod(a.getWriteMethod(), setParameters,
0868: thrownExceptions, "void", jw.PUBLIC | jw.BEANINFO);
0869: if (a.getPropertyInterface() != null) {
0870: jw.writeEol(type + " value = (" + type
0871: + ") valueInterface");
0872: }
0873: if (genVetoable)
0874: genVetoBegin();
0875:
0876: SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a
0877: .searchExtraData(SchemaRep.WhiteSpace.class);
0878: if (ws != null)
0879: genWhiteSpaceRestriction(ws, "value", type);
0880: if (a.isAttribute()) {
0881: AttrProp attrProp = a.getAttrProp();
0882: if (attributeOwner != a) {
0883: jw
0884: .comment("Make sure we've got a place to put this attribute.");
0885: jw.beginIf("size(" + attributeOwner.constName
0886: + ") == 0");
0887: String valueToSetTo = "\"\""; // The empty string.
0888: if (Common.isBoolean(attributeOwner.type)
0889: && attributeOwner.getCanBeEmpty())
0890: valueToSetTo = "java.lang.Boolean.TRUE";
0891: if (attributeOwner.isIndexed()) {
0892: jw.writeEol("addValue("
0893: + attributeOwner.constName + ", ",
0894: valueToSetTo, ")");
0895: } else {
0896: jw.writeEol("setValue("
0897: + attributeOwner.constName + ", ",
0898: valueToSetTo, ")");
0899: }
0900: jw.end();
0901: if (Common.isBoolean(attributeOwner.type)
0902: && attributeOwner.getCanBeEmpty()
0903: && attributeOwner.isIndexed()) {
0904: jw.write("setValue(", attributeOwner.constName);
0905: jw.write(", index");
0906: jw.writeEol(", java.lang.Boolean.TRUE)");
0907: }
0908: jw.write("setAttributeValue(");
0909: jw.write(attributeOwner.constName + ", ");
0910: if (attributeOwner.isIndexed())
0911: gen("index, ");
0912: jw.write("\"" + attrProp.getName() + "\"");
0913: } else {
0914: gen("setAttributeValue(");
0915: gen(a.constName);
0916: }
0917: gen(", ");
0918: if (isScalar)
0919: gen("\"\"+value");
0920: else
0921: gen(JavaUtil.typeToString(a.getType(), "value"));
0922: geneol(")");
0923: } else {
0924: gen("this.setValue(", constName, ", ");
0925:
0926: if (indexed)
0927: gen("index, ");
0928:
0929: genSetValue(isScalar, a.getType());
0930:
0931: eol();
0932: }
0933:
0934: if (genVetoable)
0935: genVetoEnd();
0936:
0937: if (!indexed)
0938: genResetMutuallyExclusive(a, true);
0939: end();
0940: cr();
0941:
0942: /*
0943: * Get method
0944: */
0945: comment();
0946: // Signature
0947: String getParameters = "";
0948: if (indexed)
0949: getParameters = "int index";
0950: jw.beginMethod(a.getReadMethod(indexed), getParameters,
0951: null, a.getPropertyInterface() == null ? type : a
0952: .getPropertyInterface(), jw.PUBLIC
0953: | jw.BEANINFO);
0954:
0955: // Body
0956: if (a.isAttribute()) {
0957: List exceps = JavaUtil.exceptionsFromParsingText(a
0958: .getType());
0959: if (!exceps.isEmpty()) {
0960: gen("try ");
0961: begin();
0962: }
0963: if (attributeOwner != a) {
0964: AttrProp attrProp = a.getAttrProp();
0965: jw
0966: .comment("If our element does not exist, then the attribute does not exist.");
0967: jw.beginIf("size(" + attributeOwner.constName
0968: + ") == 0");
0969: jw.writeEol("return null");
0970: jw.endElseBegin();
0971: jw.write("return ");
0972: jw
0973: .writeEol(JavaUtil
0974: .genParseText(
0975: a.getType(),
0976: "getAttributeValue("
0977: + attributeOwner.constName
0978: + ((attributeOwner
0979: .isIndexed()) ? ", index"
0980: : "")
0981: + ", \""
0982: + attrProp
0983: .getName()
0984: + "\")", config
0985: .isForME()));
0986: jw.end();
0987: } else {
0988: String nullValue = JavaUtil.nullValueForType(a
0989: .getType());
0990: gen("return ");
0991: if (!"null".equals(nullValue))
0992: gen("(getAttributeValue(" + a.constName
0993: + ") == null) ? " + nullValue + " : ");
0994: gen(JavaUtil.genParseText(a.getType(),
0995: "getAttributeValue(" + a.constName + ")",
0996: config.isForME()));
0997: eol();
0998: }
0999: if (!exceps.isEmpty()) {
1000: end();
1001: genRethrowExceptions(exceps);
1002: }
1003: } else if (isScalar) {
1004: gen(a.classType, " ret = ");
1005: gen("(", a.classType, ")");
1006: gen("this.getValue(", constName);
1007: if (indexed)
1008: gen(", index");
1009: PC();
1010: eol();
1011: gencr("if (ret == null)");
1012: if (this .config.isScalarException()
1013: && !Common.isBoolean(a.type)) {
1014: if (this .config.isStandalone()) {
1015: tabIn();
1016: gen(
1017: "throw new NoSuchElementException(\"The element ",
1018: a.name, " of type ");
1019: gen(scalarType, " has no value.\")");
1020: eol();
1021: } else {
1022: tabIn();
1023: gencr("throw new RuntimeException(Common.getMessage(");
1024: tabIn();
1025: tabIn();
1026: gencr("\"NoValueForElt_msg\",");
1027: tabIn();
1028: tabIn();
1029: gen("new Object[] {\"");
1030: gen(constName);
1031: gen("\", ");
1032: gen("\"", scalarType);
1033: gen("\"}));");
1034: cr();
1035: }
1036: } else {
1037: tabIn();
1038: gen("ret = ");
1039: gen("(", a.classType, ")");
1040: gen("Common.defaultScalarValue(Common.");
1041: gen(Common.typeToString(a.type), ")");
1042: eol();
1043: }
1044: gen("return " + JavaUtil.fromObject(a.getType(), "ret"));
1045: eol();
1046: } else {
1047: gen("return (", a.classType, ")");
1048: gen("this.getValue(", constName);
1049: if (indexed)
1050: gen(", index");
1051: PC();
1052: eol();
1053: }
1054: end();
1055: cr();
1056:
1057: //
1058: // If we are an indexed property, we have to generate another
1059: // kind of setter and getter method for the whole array.
1060: //
1061: if (indexed) {
1062: jw.comment("Return the number of properties");
1063: // Signature
1064: gen(sigs.findSignature(SIZE));
1065: sp();
1066: begin();
1067: // Body
1068: if (!a.isAttribute())
1069: gen("return this.size(", constName, ")");
1070: else
1071: gen("return this.size(", attributeOwner.constName,
1072: ")");
1073: eol();
1074: end();
1075: cr();
1076:
1077: if (!a.isAttribute()) {
1078: // Setter method
1079: comment("This attribute is ", Common
1080: .instanceToString(a.elementInstance));
1081: // Set - signature
1082: if (a.getPropertyInterface() == null) {
1083: setParameters = type + "[] value";
1084: } else {
1085: setParameters = a.getPropertyInterface()
1086: + "[] value";
1087: }
1088: jw.beginMethod(a.getWriteMethod(), setParameters,
1089: thrownExceptions, "void", jw.PUBLIC
1090: | jw.BEANINFO);
1091:
1092: // Body
1093: if (isScalar) {
1094: gen(a.classType, "[] values = null");
1095: eol();
1096: gencr("if (value != null)");
1097: begin();
1098: gen("values = new ", a.classType,
1099: "[value.length]");
1100: eol();
1101: gencr("for (int i=0; i<value.length; i++)");
1102: gentab(1);
1103: if (!config.isForME()
1104: && Common.isBoolean(a.type))
1105: gen("values[i] = (value[i] ? Boolean.TRUE : Boolean.FALSE)");
1106: else
1107: gen("values[i] = new ", a.classType,
1108: "(value[i])");
1109: eol();
1110: end();
1111: }
1112:
1113: if (genVetoable)
1114: genVetoBegin();
1115:
1116: gen("this.setValue(", constName, ", ");
1117:
1118: if (isScalar)
1119: gen("values)");
1120: else
1121: gen("value)");
1122:
1123: eol();
1124: if (genVetoable)
1125: genVetoEnd();
1126:
1127: genResetMutuallyExclusive(a, true);
1128: end();
1129: cr();
1130:
1131: // Getter method
1132: comment();
1133: // Signature
1134: jw.beginMethod(a.getReadMethod(false), "", null, (a
1135: .getPropertyInterface() == null ? type : a
1136: .getPropertyInterface())
1137: + "[]", jw.PUBLIC | jw.BEANINFO);
1138:
1139: // Body
1140: if (isScalar) {
1141: gen(scalarType, "[] ret = null");
1142: eol();
1143: gen(a.classType, "[] values = (", a.classType);
1144: gen("[])this.getValues(", constName, ")");
1145: eol();
1146: gencr("if (values != null)");
1147: begin();
1148: gen("ret = new ", scalarType, "[values.length]");
1149: eol();
1150: gencr("for (int i=0; i<values.length; i++)");
1151: gentab(1);
1152: gen("ret[i] = values[i].", Common
1153: .wrapperGetMethod(a.type), "()");
1154: eol();
1155: end();
1156: geneol("return ret");
1157: } else {
1158: gen("return (", a.classType, "[])");
1159: gen("this.getValues(", constName, ")");
1160: eol();
1161: }
1162: end();
1163: cr();
1164:
1165: /*
1166: * Remove and Add methods
1167: */
1168: comment("Add a new element returning its index in the list");
1169: // Signature
1170: gen(sigs.findSignature(ADD));
1171: sp();
1172: if (genVetoable) {
1173: gencr("throws PropertyVetoException ");
1174: }
1175: begin();
1176: // Body
1177: if (a.getPropertyInterface() != null) {
1178: jw.writeEol(a.getType() + " value = ("
1179: + a.getType() + ") valueInterface");
1180: }
1181: if (genVetoable)
1182: genVetoBegin();
1183: jw.write("int positionOfNewItem = this.addValue(",
1184: constName, ", ");
1185: genSetValue(isScalar, a.getType());
1186: jw.eol();
1187: if (isMutuallyExclusive(a)) {
1188: jw.beginIf("positionOfNewItem == 0");
1189: genResetMutuallyExclusive(a, false);
1190: jw.end();
1191: }
1192: jw.writeEol("return positionOfNewItem");
1193: if (genVetoable)
1194: genVetoEnd();
1195: end();
1196: cr();
1197:
1198: comment();
1199: comment("Remove an element using its reference");
1200: comment("Returns the index the element had in the list");
1201: comment();
1202: // Signature
1203: gen(sigs.findSignature(REMOVE));
1204: sp();
1205: if (genVetoable) {
1206: gencr("throws PropertyVetoException ");
1207: }
1208: begin();
1209: // Body
1210: if (a.getPropertyInterface() != null) {
1211: jw.writeEol(a.getType() + " value = ("
1212: + a.getType() + ") valueInterface");
1213: }
1214: if (genVetoable)
1215: genVetoBegin();
1216: gen("return this.removeValue(", constName, ", ");
1217: genSetValue(isScalar, a.getType());
1218: eol();
1219: if (genVetoable)
1220: genVetoEnd();
1221: end();
1222: cr();
1223:
1224: if (isScalar && !"int".equals(a.getType())) {
1225: // Don't do "int" because we already have a method
1226: // with that signature.
1227: comment();
1228: comment("Remove an element using its index");
1229: comment();
1230: // Signature
1231: jw.beginMethod(a.getRemoveMethod(),
1232: "int index", thrownExceptions, "void",
1233: jw.PUBLIC | jw.BEANINFO);
1234: // Body
1235: if (genVetoable)
1236: genVetoBegin();
1237: gen("this.removeValue(", constName, ", index)");
1238: eol();
1239: if (genVetoable)
1240: genVetoEnd();
1241: end();
1242: cr();
1243: }
1244: }
1245: }
1246:
1247: //
1248: //-----------------
1249:
1250: {
1251: // The metaDD might define the class name of the bean
1252: MetaElement e = getMetaElement(a);
1253: String cls = null;
1254: if (e != null)
1255: cls = e.getBeanClass();
1256: if (cls == null)
1257: cls = a.classType;
1258: //System.out.println("cls="+cls);
1259:
1260: if (!a.isAttribute()) {
1261: select(CONSTRUCTOR_SECTION);
1262: gen(" | Common.");
1263: if (isScalar && (a.type != Common.TYPE_BOOLEAN)) {
1264: gen(Common.typeToString(Common.TYPE_STRING));
1265: } else {
1266: gen(Common.typeToString(a.type));
1267: if (a.type == Common.TYPE_BOOLEAN
1268: && !a.getCanBeEmpty()) {
1269: gen(" | Common.TYPE_SHOULD_NOT_BE_EMPTY");
1270: }
1271: }
1272: if (keyedElement)
1273: gen(" | Common.TYPE_KEY");
1274: gen(", ");
1275: cr();
1276: tabIn();
1277: gen(cls, ".class)");
1278: eol();
1279: }
1280: }
1281:
1282: // dump()
1283: if (!a.isAttribute()) {
1284: select(TRAILER_SECTION);
1285: if (isScalar && config.isScalarException()
1286: && !Common.isBoolean(a.type)) {
1287: gen("if (this.getValue(");
1288: gen(constName);
1289: gen(") != null) ");
1290: begin();
1291: }
1292: gen("str.append(indent)");
1293: eol();
1294: gen("str.append(\"", a.name);
1295: if (indexed) {
1296: gen("[\"+this.size", a.name, "()+\"]");
1297: }
1298: gen("\")");
1299: eolNoI18N();
1300: if (indexed) {
1301: gen("for(int i=0; i<this.size", a.name, "(); i++)");
1302: cr();
1303: begin();
1304: gen("str.append(indent+\"\\t\")");
1305: eol();
1306: gen("str.append(\"#\"+i+\":\")");
1307: eol();
1308: }
1309:
1310: boolean isCharArray = a.classType.equals("char[]");
1311: if (Common.isBoolean(a.type)) {
1312: gen("str.append(indent+\"\\t\")");
1313: eolNoI18N();
1314: gen("str.append((");
1315: gen("this.is", a.name, "(");
1316: if (indexed)
1317: gen("i");
1318: gen(")?\"true\":\"false\"))");
1319: eol();
1320: } else if (Common.isString(a.type) || isScalar
1321: || isCharArray) {
1322: gen("str.append(indent+\"\\t\")");
1323: eolNoI18N();
1324: gen("str.append(\"<\")");
1325: eolNoI18N();
1326:
1327: if (isWrapper && !isCharArray)
1328: gen("o = ");
1329: else
1330: gen("s = ");
1331:
1332: if (isScalar || isCharArray)
1333: gen("String.valueOf(");
1334: gen("this.get", a.name, "(");
1335: if (indexed)
1336: gen("i");
1337: gen(")");
1338: if (isScalar || isCharArray)
1339: gen(")");
1340: eol();
1341:
1342: if (isWrapper && !isCharArray)
1343: gen("str.append((o==null?\"null\":o.toString()");
1344: else
1345: gen("str.append((s==null?\"null\":s");
1346:
1347: gen(".trim()))");
1348: eolNoI18N();
1349: gen("str.append(\">\\n\")");
1350: eolNoI18N();
1351: } else if (a.isBean) {
1352: gen(
1353: "n = (org.netbeans.modules.schema2beans.BaseBean) this.get",
1354: a.name, "(");
1355: if (indexed)
1356: gen("i");
1357: gencr(");");
1358: gencr("if (n != null)");
1359: gen("\tn.dump(str, indent + \"\\t\")");
1360: eolNoI18N();
1361: gencr("else");
1362: gen("\tstr.append(indent+\"\\tnull\")");
1363: eolNoI18N();
1364: }
1365: if (!a.isAttribute()) {
1366: gen("this.dumpAttributes(", constName, ", ");
1367: if (indexed)
1368: gen("i");
1369: else
1370: gen("0");
1371: gen(", str, indent)");
1372: eol();
1373: }
1374:
1375: if (isScalar && config.isScalarException()
1376: && !Common.isBoolean(a.type)) {
1377: end();
1378: }
1379:
1380: if (indexed) {
1381: //gen("str.append(indent+\"\\t\")"); eol();
1382: //gen("str.append(\"---\")"); eol();
1383: end();
1384: }
1385: cr();
1386: }
1387:
1388: genAttributes(CONSTRUCTOR_SECTION, a.attributes, constName);
1389: select(ACCESS_SECTION);
1390: genDefaultsAccessable(a);
1391:
1392: if (a.isBean) {
1393: genNewMethod(a.getPropertyInterface(), a.getType());
1394: }
1395: }
1396:
1397: // The root might have attributes
1398: if (this .beanElement.isRoot)
1399: genAttributes(CONSTRUCTOR_SECTION, this .beanElement.node
1400: .getAttributes(), null);
1401:
1402: select(DECL_SECTION);
1403: cr();
1404:
1405: select(TRAILER_SECTION);
1406: end(false);
1407: cr();
1408:
1409: }
1410:
1411: // Generate the createAttribute() code
1412: void genAttributes(int out, AttrProp[] attributes, String name) {
1413: select(out);
1414: if (attributes == null)
1415: return;
1416: for (int j = 0; j < attributes.length; j++) {
1417: AttrProp attr = attributes[j];
1418: genAttribute(name, attr);
1419: }
1420: }
1421:
1422: void genAttribute(String name, AttrProp attr) {
1423: String constName = Common.constName(attr.getDtdName());
1424: gen("this.createAttribute(");
1425: if (name != null)
1426: gen(name, ", ");
1427: gen("\"", attr.getDtdName(), "\", ");
1428: gen("\"", attr.getName(), "\", ");
1429: cr();
1430: gentab(4);
1431: gencr(attr.typeAsString(), ",");
1432: gentab(4);
1433: String[] values = attr.getValues();
1434: if (values.length > 0) {
1435: gencr("new String[] {");
1436: for (int k = 0; k < values.length; k++) {
1437: if (k > 0)
1438: gencr(",");
1439: gentab(5);
1440: gen("\"", values[k], "\"");
1441: }
1442: cr();
1443: gentab(4);
1444: gen("}, ");
1445: } else
1446: gen("null, ");
1447:
1448: String value = attr.getDefaultValue();
1449:
1450: if (value != null)
1451: gen("\"", value, "\")");
1452: else
1453: gen("null)");
1454: eol();
1455: }
1456:
1457: void genBody(int out) throws IOException {
1458: select(out);
1459:
1460: //
1461: // add/remove comparators
1462: //
1463: comment();
1464: jw.beginMethod("addComparator",
1465: "org.netbeans.modules.schema2beans.BeanComparator c",
1466: null, "void", jw.PUBLIC | jw.STATIC);
1467: gen("comparators.add(c)");
1468: eol();
1469: end();
1470: cr();
1471:
1472: comment();
1473: jw.beginMethod("removeComparator",
1474: "org.netbeans.modules.schema2beans.BeanComparator c",
1475: null, "void", jw.PUBLIC | jw.STATIC);
1476: gen("comparators.remove(c)");
1477: eol();
1478: end();
1479:
1480: //
1481: // Event registration methods
1482: //
1483:
1484: //
1485: // the generation of addPropertyChange are in BaseBean.
1486: //
1487:
1488: if (this .genGenericVetoListeners) {
1489: comment();
1490: jw.beginMethod("add" + VCL, VCL_FULL_CLASS_NAME + " l",
1491: null, "void", jw.PUBLIC);
1492: gengetprop();
1493: gen("\tp.addVCListener(l)");
1494: eol();
1495: end();
1496: cr();
1497:
1498: comment();
1499: jw.beginMethod("remove" + VCL, VCL_FULL_CLASS_NAME + " l",
1500: null, "void", jw.PUBLIC);
1501: gengetprop();
1502: gen("\tp.removeVCListener(l)");
1503: eol();
1504: end();
1505: cr();
1506: }
1507:
1508: //
1509: // Per property event registration method
1510: //
1511:
1512: //
1513: // the generation of addPropertyChange are now in BaseBean.
1514: //
1515: if (this .genVetoListeners) {
1516: comment();
1517: jw.beginMethod("add" + VCL, "String n, "
1518: + VCL_FULL_CLASS_NAME + " l", null, "void",
1519: jw.PUBLIC);
1520: gengetpropbyname();
1521: gen("\tp.addVCListener(l)");
1522: eol();
1523: end();
1524: cr();
1525:
1526: comment();
1527: jw.beginMethod("remove" + VCL, "String n, "
1528: + VCL_FULL_CLASS_NAME + " l", null, "void",
1529: jw.PUBLIC);
1530: gengetpropbyname();
1531: gen("\tp.removeVCListener(l)");
1532: eol();
1533: end();
1534: cr();
1535: }
1536:
1537: if (this .beanElement.isRoot) {
1538: // public void createBean(Node doc)
1539: comment();
1540: comment("This method returns the root of the bean graph");
1541: comment("Each call creates a new bean graph from the specified DOM graph");
1542: comment();
1543:
1544: String thrownExceptions = null;
1545: if (shouldThrowException()) {
1546: thrownExceptions = "org.netbeans.modules.schema2beans.Schema2BeansException";
1547: }
1548: jw.beginMethod("createGraph", "org.w3c.dom.Node doc",
1549: thrownExceptions, className, jw.PUBLIC | jw.STATIC);
1550: if (config.isSetDefaults())
1551: gen("return new ", this .className,
1552: "(doc, Common.USE_DEFAULT_VALUES)");
1553: else
1554: gen("return new ", this .className,
1555: "(doc, Common.NO_DEFAULT_VALUES)");
1556: eol();
1557: end();
1558: cr();
1559:
1560: jw.beginMethod("createGraph", "java.io.File f",
1561: (thrownExceptions == null) ? "java.io.IOException"
1562: : thrownExceptions + ", "
1563: + "java.io.IOException", className,
1564: jw.PUBLIC | jw.STATIC);
1565: jw
1566: .writeEol("java.io.InputStream in = new java.io.FileInputStream(f)");
1567: jw.beginTry();
1568: gen("return createGraph(in, false)");
1569: eol();
1570: jw.endFinallyBegin();
1571: jw.writeEol("in.close()");
1572: jw.end();
1573: jw.endMethod();
1574:
1575: // public void createGraph(InputStream in)
1576: jw.beginMethod("createGraph", "java.io.InputStream in",
1577: thrownExceptions, className, jw.PUBLIC | jw.STATIC);
1578: gen("return createGraph(in, false)");
1579: eol();
1580: jw.endMethod();
1581:
1582: // public void createGraph(InputStream in)
1583: jw.beginMethod("createGraph",
1584: "java.io.InputStream in, boolean validate",
1585: thrownExceptions, className, jw.PUBLIC | jw.STATIC);
1586: if (!shouldThrowException()) {
1587: gen("try ");
1588: begin();
1589: }
1590: gen("Document doc = GraphManager.createXmlDocument(in, validate)");
1591: eol();
1592: gen("return createGraph(doc)");
1593: eol();
1594: if (!shouldThrowException()) {
1595: end();
1596: gen("catch (Exception t) ");
1597: begin();
1598: if (this .config.isStandalone()) {
1599: gencrNoI18N("throw new RuntimeException(\"DOM graph creation failed\", t);");
1600: } else {
1601: gencr("throw new RuntimeException(Common.getMessage(");
1602: tabIn();
1603: gencr("\"DOMGraphCreateFailed_msg\",");
1604: tabIn();
1605: gen("t))");
1606: eol();
1607: }
1608: end();
1609: }
1610: end();
1611: cr();
1612:
1613: // public void createBean()
1614: comment();
1615: comment("This method returns the root for a new empty bean graph");
1616: comment();
1617:
1618: jw.beginMethod("createGraph", "", null, className,
1619: jw.PUBLIC | jw.STATIC);
1620: if (shouldThrowException()) {
1621: gen("try ");
1622: begin();
1623: }
1624: gen("return new ", this .className, "()");
1625: eol();
1626: if (shouldThrowException()) {
1627: end();
1628: gen("catch (Schema2BeansException e) ");
1629: begin();
1630: gen("throw new RuntimeException(e)");
1631: eol();
1632: end();
1633: }
1634: end();
1635: cr();
1636: }
1637:
1638: MetaElement me = getMetaElement(beanElement);
1639: if (me != null && me.getUserCode() != null) {
1640: String userCode = me.getUserCode();
1641: cr();
1642: gencr(userCode);
1643: }
1644: }
1645:
1646: boolean shouldThrowException() {
1647: return (config.isThrowErrors() || (mdd != null && mdd
1648: .isThrowExceptions()));
1649: }
1650:
1651: void genValidate() throws IOException {
1652: select(BODY_SECTION);
1653: jw.beginMethod("validate", "",
1654: "org.netbeans.modules.schema2beans.ValidateException",
1655: "void", jw.PUBLIC);
1656: if (config.isGenerateValidate()) {
1657: genValidateProperties();
1658: }
1659: end();
1660: cr();
1661: }
1662:
1663: protected void genValidateFail(String detail, String name,
1664: boolean quoteDetail, ValidateException.FailureType ft,
1665: JavaWriter out) throws IOException {
1666: out
1667: .write("throw new org.netbeans.modules.schema2beans.ValidateException(");
1668: if (quoteDetail)
1669: out.write('"');
1670: out.write(detail);
1671: if (quoteDetail)
1672: out.write('"');
1673: out
1674: .write(
1675: ", org.netbeans.modules.schema2beans.ValidateException.FailureType.",
1676: ft.toString());
1677: out.writeEolNoI18N(", \"" + name + "\", this)");
1678: }
1679:
1680: void genTrailer(int out) {
1681: select(out);
1682: gen(PUBLIC, STRING, "dumpBeanNode()");
1683: begin();
1684: gen("StringBuffer str = new StringBuffer()");
1685: eol();
1686: gen("str.append(\"", this .className, "\\n\")");
1687: eolNoI18N();
1688: gen("this.dump(str, \"\\n \")");
1689: eolNoI18N();
1690: gen("return str.toString()");
1691: eol();
1692: end(false);
1693:
1694: if (this .config.isDumpToString()) {
1695: cr();
1696: gencr(PUBLIC, STRING, "toString()");
1697: begin();
1698: gen("return this.dumpBeanNode()");
1699: eol();
1700: end();
1701: }
1702: }
1703:
1704: /**
1705: * Generate the java code in the out stream, using the optional
1706: * metaDD bean graph.
1707: */
1708: public void generate(OutputStream out, MetaDD mdd)
1709: throws IOException {
1710: this .mdd = mdd;
1711: this .metaElement = getMetaElement(beanElement);
1712:
1713: if (this .metaElement != null
1714: && this .metaElement.isSkipGeneration()) {
1715: config.messageOut.println("Skipping generation of class "
1716: + " (as specified in the mdd file)"); // NOI18N
1717: return;
1718: }
1719:
1720: findAttributeOwners();
1721:
1722: this .isVetoable = this .config.isVetoable();
1723:
1724: // Find out if there is any veto specified in the mdd
1725: if (this .mdd != null) {
1726: //if (this.mdd.findPropertyValue("vetoable", Boolean.TRUE).length != 0) {
1727: org.netbeans.modules.schema2beansdev.metadd.CommonBean[] beans = mdd
1728: .childBeans(true);
1729: for (int beanPos = 0; beanPos < beans.length; ++beanPos) {
1730: try {
1731: if (((Boolean) beans[beanPos]
1732: .fetchPropertyByName("vetoable"))
1733: .booleanValue()) {
1734: // Override information from the mdd file
1735: this .isVetoable = this .mdd.isVetoable();
1736: this .genGenericVetoListeners = true;
1737: break;
1738: }
1739: } catch (IllegalArgumentException e) {
1740: // This bean does not have that property
1741: }
1742: }
1743: }
1744:
1745: this .genHeader(HEADER_SECTION);
1746: this .genPackage(HEADER_SECTION);
1747: this .genImports(HEADER_SECTION);
1748: select(HEADER_SECTION);
1749: cr();
1750: beginNoI18N();
1751: cr();
1752: this .genClassName(HEADER_SECTION);
1753:
1754: this .genDeclarations(DECL_SECTION);
1755: this .genConstructor(CONSTRUCTOR_SECTION);
1756:
1757: this .genAccessors(ACCESS_SECTION);
1758:
1759: this .genBody(BODY_SECTION);
1760:
1761: this .genInitializer();
1762:
1763: genValidate();
1764:
1765: this .genTrailer(TRAILER_SECTION);
1766:
1767: select(CONSTRUCTOR_SECTION);
1768: gen("this.initialize(options)");
1769: eol();
1770: end();
1771: cr();
1772:
1773: select(INITIALIZE_SECTION);
1774: cr();
1775: end();
1776: cr();
1777:
1778: select(TRAILER_SECTION);
1779: end();
1780: cr();
1781: endNoI18N();
1782: cr();
1783:
1784: try {
1785: printSchema();
1786: } catch (IOException ioe) {
1787: // Do not generate the schema comment
1788: }
1789: printGenBuffers(out);
1790: }
1791:
1792: protected void addExtraMethods() {
1793: // Most of these are taken from the public methods of BaseBean
1794: jw.addToMethodStore("addComparator",
1795: "org.netbeans.modules.schema2beans.BeanComparator c",
1796: null, "void", jw.PUBLIC | jw.STATIC);
1797: jw.addToMethodStore("removeComparator",
1798: "org.netbeans.modules.schema2beans.BeanComparator c",
1799: null, "void", jw.PUBLIC | jw.STATIC);
1800: if (beanElement.isRoot) {
1801: }
1802: jw.addToMethodStore("getValue", "String name", null, "Object");
1803: jw.addToMethodStore("getValue", "String name, int index", null,
1804: "Object");
1805: jw.addToMethodStore("isNull", "String name", null, "boolean");
1806: jw.addToMethodStore("isNull", "String name, int index", null,
1807: "boolean");
1808: jw.addToMethodStore("getValues", "String name", null,
1809: "Object[]");
1810: jw.addToMethodStore("setValue", "String name, Object value",
1811: null, "void");
1812: jw.addToMethodStore("setValue",
1813: "String name, int index, Object value", null, "void");
1814: jw.addToMethodStore("setValue", "String name, Object[] value",
1815: null, "void");
1816: jw.addToMethodStore("addValue", "String name, Object value",
1817: null, "int");
1818: jw.addToMethodStore("removeValue", "String name, Object value",
1819: null, "int");
1820: jw.addToMethodStore("removeValue", "String name, int index",
1821: null, "void");
1822: jw.addToMethodStore("indexOf", "String name, Object value",
1823: null, "int");
1824: jw.addToMethodStore("size", "String name", null, "int");
1825: jw.addToMethodStore("isChoiceProperty", "String name", null,
1826: "boolean");
1827: jw.addToMethodStore("isChoiceProperty", "", null, "boolean");
1828: jw.addToMethodStore("getAttributeValue", "String name", null,
1829: "String");
1830: jw.addToMethodStore("getAttributeValue",
1831: "String propName, String name", null, "String");
1832: jw
1833: .addToMethodStore(
1834: "setAttributeValue",
1835: "String propName, int index, String name, String value",
1836: null, "void");
1837: jw.addToMethodStore("getAttributeValue",
1838: "String propName, int index, String name", null,
1839: "String");
1840: jw.addToMethodStore("getAttributeNames", "String propName",
1841: null, "String[]");
1842: jw.addToMethodStore("getAttributeNames", "", null, "String[]");
1843: jw
1844: .addToMethodStore(
1845: "write",
1846: "java.io.OutputStream out",
1847: "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansRuntimeException",
1848: "void");
1849: jw
1850: .addToMethodStore(
1851: "write",
1852: "java.io.OutputStream out, String encoding",
1853: "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException",
1854: "void");
1855: jw
1856: .addToMethodStore(
1857: "write",
1858: "java.io.Writer w",
1859: "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException",
1860: "void");
1861: jw
1862: .addToMethodStore(
1863: "write",
1864: "java.io.Writer w, String encoding",
1865: "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException",
1866: "void");
1867: jw
1868: .addToMethodStore(
1869: "writeNoReindent",
1870: "java.io.OutputStream out",
1871: "java.io.IOException, org.netbeans.modules.schema2beans.Schema2BeansException",
1872: "void");
1873: jw.addToMethodStore("reindent", "", null, "void");
1874: jw.addToMethodStore("clone", "", null, "Object");
1875: jw
1876: .addToMethodStore(
1877: "merge",
1878: "org.netbeans.modules.schema2beans.BaseBean bean, int mode",
1879: null, "void");
1880: jw.addToMethodStore("merge",
1881: "org.netbeans.modules.schema2beans.BaseBean bean",
1882: null, "void");
1883: jw.addToMethodStore("equals", "Object obj", null, "boolean");
1884: jw.addToMethodStore("parent", "", null,
1885: "org.netbeans.modules.schema2beans.BaseBean");
1886: jw.addToMethodStore("fullName", "", null, "String");
1887: jw.addToMethodStore("isRoot", "", null, "boolean");
1888: jw.addToMethodStore("name", "", null, "String");
1889: jw.addToMethodStore("dtdName", "", null, "String");
1890: jw.addToMethodStore("dump", "StringBuffer str, String indent",
1891: null, "void");
1892: jw
1893: .addToMethodStore(
1894: "createGraph",
1895: "Class clazz, java.io.InputStream in",
1896: "org.netbeans.modules.schema2beans.Schema2BeansException",
1897: "org.netbeans.modules.schema2beans.BaseBean",
1898: jw.PUBLIC | jw.STATIC);
1899: jw
1900: .addToMethodStore(
1901: "createGraph",
1902: "Class clazz, java.io.InputStream in, boolean validate",
1903: "org.netbeans.modules.schema2beans.Schema2BeansException",
1904: "org.netbeans.modules.schema2beans.BaseBean",
1905: jw.PUBLIC | jw.STATIC);
1906: jw
1907: .addToMethodStore(
1908: "createGraph",
1909: "Class clazz, java.io.InputStream in, boolean validate, org.xml.sax.EntityResolver er",
1910: "org.netbeans.modules.schema2beans.Schema2BeansException",
1911: "org.netbeans.modules.schema2beans.BaseBean",
1912: jw.PUBLIC | jw.STATIC);
1913: jw
1914: .addToMethodStore(
1915: "createGraph",
1916: "Class clazz, java.io.InputStream in, boolean validate, org.xml.sax.EntityResolver er, org.xml.sax.ErrorHandler eh",
1917: "org.netbeans.modules.schema2beans.Schema2BeansException",
1918: "org.netbeans.modules.schema2beans.BaseBean",
1919: jw.PUBLIC | jw.STATIC);
1920: jw.addToMethodStore("addPropertyChangeListener",
1921: "java.beans.PropertyChangeListener l", null, "void");
1922: jw.addToMethodStore("removePropertyChangeListener",
1923: "java.beans.PropertyChangeListener l", null, "void");
1924: jw.addToMethodStore("addPropertyChangeListener",
1925: "String n, java.beans.PropertyChangeListener l", null,
1926: "void");
1927: jw.addToMethodStore("removePropertyChangeListener",
1928: "String n, java.beans.PropertyChangeListener l", null,
1929: "void");
1930: jw.addToMethodStore("comments", "", null,
1931: "org.w3c.dom.Comment[]");
1932: jw.addToMethodStore("addComment", "String comment", null,
1933: "org.w3c.dom.Comment");
1934: jw.addToMethodStore("removeComment",
1935: "org.w3c.dom.Comment comment", null, "void");
1936: jw.addToMethodStore("childBeans", "boolean recursive", null,
1937: "org.netbeans.modules.schema2beans.BaseBean[]");
1938: jw
1939: .addToMethodStore("childBeans",
1940: "boolean recursive, java.util.List beans",
1941: null, "void");
1942: jw.addToMethodStore("setDefaultNamespace", "String namespace",
1943: null, "void");
1944: jw.addToMethodStore("getDefaultNamespace", "", null, "String");
1945: jw.addToMethodStore("toString", "", null, "String");
1946: //jw.addToMethodStore("copyProperties", "org.netbeans.modules.schema2beans.BaseBean destBean, java.util.Map nameMapping", null, "void");
1947: }
1948:
1949: /*
1950: protected Signatures getSystemSignatures() {
1951: Signatures result = new Signatures();
1952: Signature sig;
1953: if (config.isGenerateValidate()) {
1954: result.add(new Signature("void", "validate"));
1955: }
1956: sig = new Signature("void", "addComparator");
1957: sig.addParameter("org.netbeans.modules.schema2beans.BeanComparator", "c");
1958: sig.setStatic();
1959: result.add(sig);
1960: sig = new Signature("void", "removeComparator");
1961: sig.addParameter("org.netbeans.modules.schema2beans.BeanComparator", "c");
1962: sig.setStatic();
1963: result.add(sig);
1964: if (beanElement.isRoot) {
1965: sig = new Signature(className, "createGraph");
1966: sig.addParameter("org.w3c.dom.Node", "doc");
1967: sig.setStatic();
1968: result.add(sig);
1969: sig = new Signature(className, "createGraph");
1970: sig.addParameter("java.io.InputStream", "in");
1971: sig.setStatic();
1972: result.add(sig);
1973: sig = new Signature(className, "createGraph");
1974: sig.addParameter("java.io.InputStream", "in");
1975: sig.addParameter("boolean", "validate");
1976: sig.setStatic();
1977: result.add(sig);
1978: sig = new Signature(className, "createGraph");
1979: sig.setStatic();
1980: result.add(sig);
1981: }
1982: sig = new Signature("void", "write");
1983: sig.addParameter("java.io.OutputStream", "out");
1984: sig.addThrows("java.io.IOException");
1985: sig.addThrows("org.netbeans.modules.schema2beans.Schema2BeansRuntimeException");
1986: result.add(sig);
1987: sig = new Signature("void", "write");
1988: sig.addParameter("java.io.OutputStream", "out");
1989: sig.addParameter("String", "encoding");
1990: sig.addThrows("java.io.IOException");
1991: sig.addThrows("org.netbeans.modules.schema2beans.Schema2BeansException");
1992: result.add(sig);
1993: sig = new Signature("void", "write");
1994: sig.addParameter("java.io.Writer", "w");
1995: sig.addThrows("java.io.IOException");
1996: sig.addThrows("org.netbeans.modules.schema2beans.Schema2BeansException");
1997: result.add(sig);
1998: sig = new Signature("void", "write");
1999: sig.addParameter("java.io.Writer", "w");
2000: sig.addParameter("String", "encoding");
2001: sig.addThrows("java.io.IOException");
2002: sig.addThrows("org.netbeans.modules.schema2beans.Schema2BeansException");
2003: result.add(sig);
2004: sig = new Signature("void", "merge");
2005: sig.addParameter("org.netbeans.modules.schema2beans.BaseBean", "bean");
2006: result.add(sig);
2007: sig = new Signature("boolean", "equals", OTHER);
2008: sig.addParameter("Object", "obj");
2009: result.add(sig);
2010: result.add(new Signature("String", "toString"));
2011: return result;
2012: }
2013: */
2014:
2015: public void setInvalidPropertyNames(Map invalidNames) {
2016: invalidNames.put("Class", null);
2017: invalidNames.put("Property", null);
2018: invalidNames.put("AttributeNames", null);
2019: }
2020:
2021: protected String testIfPropertySet(Property prop) {
2022: if (!prop.isAttribute() && prop.isScalar()
2023: && !Common.isBoolean(prop.type))
2024: return "getValue(" + prop.constName + ") != null";
2025: else
2026: return super .testIfPropertySet(prop);
2027: }
2028:
2029: protected String testIfPropertyNotSet(Property prop) {
2030: if (!prop.isAttribute() && prop.isScalar()
2031: && !Common.isBoolean(prop.type))
2032: return "getValue(" + prop.constName + ") == null";
2033: else
2034: return super .testIfPropertyNotSet(prop);
2035: }
2036: }
2037:
2038: //******************************************************************************
2039: // END_NOI18N
2040: // This class does not (and will not) cantain strings that need to be localized.
2041: //******************************************************************************
|