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: public class JavaBeanClass extends AbstractCodeGeneratorClass implements
0052: CodeGeneratorClass {
0053: // No element type number can be this one.
0054: final protected int elementTypeSetnull = -1;
0055: private Map/*QName, String*/declaredQNames = new HashMap();
0056: private boolean loggerDeclared = false;
0057:
0058: JavaBeanClass(BeanBuilder.BeanElement be, GenBeans.Config config) {
0059: init(be, config);
0060: }
0061:
0062: /**
0063: * Generate the java code in the out stream, using the optional
0064: * metaDD bean graph.
0065: */
0066: public void generate(OutputStream out, MetaDD mdd)
0067: throws IOException {
0068: this .mdd = mdd;
0069: metaElement = getMetaElement(beanElement);
0070:
0071: if (metaElement != null && metaElement.isSkipGeneration()) {
0072: config.messageOut.println(Common
0073: .getMessage("MSG_SkippingGeneration"));
0074: return;
0075: }
0076:
0077: findAttributeOwners();
0078:
0079: genAllParts();
0080:
0081: select(DECL_SECTION);
0082: cr();
0083:
0084: printGenBuffers(out);
0085: }
0086:
0087: protected void genAllParts() throws IOException {
0088: genHeader(HEADER_SECTION);
0089: genPackage(HEADER_SECTION);
0090: genImports(HEADER_SECTION);
0091: genClassName(HEADER_SECTION);
0092:
0093: genConstructor();
0094: genAccessors();
0095: if (!config.isMinFeatures())
0096: genDeepCopy();
0097:
0098: if (config.isGenerateXMLIO() || config.isDumpToString()) {
0099: genXMLIO();
0100: }
0101: if (config.isStaxProduceXMLEventReader())
0102: genFetchXMLEventReader();
0103:
0104: if (config.isGenerateValidate()) {
0105: genValidate();
0106: }
0107: if (config.isGeneratePropertyEvents()) {
0108: genPropertyEvents();
0109: }
0110: if (config.isGenerateStoreEvents()) {
0111: genStoreEvents();
0112: }
0113: if (config.isVetoable()) {
0114: genVetoable();
0115: }
0116: if (config.isGenerateTransactions()) {
0117: genTransactions();
0118: }
0119: if (config.isGenerateHasChanged()) {
0120: genHasChanged();
0121: }
0122: if (config.isGenerateSwitches()) {
0123: generateSwitches();
0124: }
0125: if (!config.isMinFeatures())
0126: genPropertiesByName();
0127: if (config.isKeepElementPositions()) {
0128: genElementPositions();
0129: }
0130: if (beanElement.isRoot && config.isProcessDocType()) {
0131: genProcessDocType();
0132: }
0133:
0134: if (!config.isMinFeatures()) {
0135: genName();
0136:
0137: genChildBeans();
0138: }
0139: genEqualsHashCode();
0140: if (config.isDumpToString()) {
0141: genToString();
0142: }
0143: if (config.isExtendBaseBean())
0144: genExtendBaseBean();
0145:
0146: genTrailer(TRAILER_SECTION);
0147: genFinishClass(TRAILER_SECTION);
0148:
0149: try {
0150: select(TRAILER_SECTION);
0151: printSchema();
0152: } catch (IOException ioe) {
0153: // Do not generate the schema comment
0154: }
0155: }
0156:
0157: protected void genMadeChange() throws IOException {
0158: if (config.isGenerateHasChanged()) {
0159: jw.writeEol("_setChanged(true)");
0160: }
0161: }
0162:
0163: /**
0164: * Whenever we create a constructor, call this method first.
0165: */
0166: protected void genExtendBaseBeanConstructor() throws IOException {
0167: if (config.isExtendBaseBean()) {
0168: jw.writeEol("super(null, baseBeanRuntimeVersion)");
0169: //jw.writeEol("System.out.println(\"Created class \"+getClass())");
0170: }
0171: }
0172:
0173: public void genHeader(int out) throws IOException {
0174: select(out);
0175: gencr("/**");
0176: gencr(" * This generated bean class " + className);
0177: gencr(" * matches the schema element '"
0178: + beanElement.node.getName() + "'.");
0179: if (!beanElement.isRoot) {
0180: jw.writecr(" * The root bean class is "
0181: + rootBeanElement.getClassType());
0182: }
0183: gencr(" *");
0184: printComment(" * ");
0185: if (config.isGenerateTimeStamp())
0186: gencr(" * Generated on " + new Date());
0187: if (beanElement.isRoot) {
0188: gencr(" *");
0189: gencr(" * This class matches the root element of the "
0190: + (config.getSchemaTypeNum() == GenBeans.Config.DTD ? "DTD"
0191: : "XML Schema") + ",");
0192: gencr(" * and is the root of the bean graph.");
0193: gencr(" *");
0194:
0195: dumpBeanTree(jw, " * " + jw.getIndent(), jw.getIndent());
0196: gencr(" *");
0197: }
0198: gencr(" * @" + Common.GENERATED_TAG);
0199: gencr(" */");
0200: cr();
0201: }
0202:
0203: public void genPackage(int out) {
0204: select(out);
0205: if (packageName != null) {
0206: gen(PACKAGE, packageName);
0207: eol();
0208: cr();
0209: }
0210: }
0211:
0212: public void genImports(int out) {
0213: select(out);
0214: }
0215:
0216: public void genClassName(int out) throws IOException {
0217: String name = null;
0218: String impName = null;
0219:
0220: select(out);
0221: jw.write("public ");
0222: // disable this for now: things are setup to new this class.
0223: if (false && beanElement.isAbstract)
0224: jw.write("abstract ");
0225: jw.write("class ", className);
0226: //gen(PUBLIC, CLASS, className);
0227:
0228: if (mdd != null) {
0229: if (metaElement != null) {
0230: name = metaElement.getExtends();
0231: impName = metaElement.getImplements();
0232: }
0233: if (name == null) {
0234: name = mdd.getExtends();
0235: }
0236: if (impName == null) {
0237: impName = mdd.getImplements();
0238: }
0239: }
0240:
0241: if (name != null) {
0242: gen(" extends ");
0243: gencr(name);
0244: }
0245: if (impName != null) {
0246: gen(" implements ", impName);
0247: }
0248:
0249: sp();
0250: begin();
0251: }
0252:
0253: public void genConstructor() throws IOException {
0254: int size = attrList.size();
0255: if (!config.isMinFeatures()) {
0256: jw.select(DECL_SECTION);
0257: for (int i = 0; i < size; i++) {
0258: Property a = (Property) attrList.get(i);
0259: jw.write("public static final String ", a.constName,
0260: " = \"", a.name);
0261: jw.writeEolNoI18N("\"");
0262: }
0263: jw.cr();
0264: }
0265:
0266: jw.select(CONSTRUCTOR_SECTION);
0267: jw.bigComment("Normal starting point constructor.");
0268: jw.beginConstructor(className);
0269: if (config.isExtendBaseBean()) {
0270: jw.select(DECL_SECTION);
0271: jw
0272: .write(
0273: "private static final org.netbeans.modules.schema2beans.Version baseBeanRuntimeVersion = new org.netbeans.modules.schema2beans.Version("
0274: + Version.MAJVER, ", ");
0275: jw.write(Version.MINVER + ", ");
0276: jw.writeEol(Version.PTCVER + ")");
0277: jw.select(CONSTRUCTOR_SECTION);
0278: jw.writeEol("this(null, baseBeanRuntimeVersion)");
0279: jw.end();
0280: jw.cr();
0281: jw
0282: .bigComment("This constructor is here for BaseBean compatibility.");
0283: jw
0284: .beginConstructor(
0285: className,
0286: "java.util.Vector comps, org.netbeans.modules.schema2beans.Version baseBeanRuntimeVersion");
0287: }
0288: genExtendBaseBeanConstructor();
0289:
0290: if (config.isMakeDefaults() || config.isSetDefaults()) {
0291: for (int i = 0; i < size; i++) {
0292: Property a = (Property) attrList.get(i);
0293: boolean indexed = a.isIndexed();
0294: boolean isScalar = a.isScalar();
0295: if (indexed || isScalar || a.ored)
0296: continue;
0297: if (a.getDefaultValue() != null)
0298: continue;
0299: String type = a.getType();
0300: String attr = "_" + a.name;
0301: if (a.elementInstance == Common.TYPE_1) {
0302: // There has to be at least 1 of them.
0303: List exceps = JavaUtil.exceptionsFromParsingText(
0304: type, false);
0305: if (!exceps.isEmpty()) {
0306: jw.beginTry();
0307: }
0308: gen(attr, " = ");
0309: genNewDefault(a, true);
0310: eol();
0311: if (a.isBean && config.isGenerateParentRefs()) {
0312: jw.writeEol(attr, "._setParent(this)");
0313: }
0314: if (!exceps.isEmpty()) {
0315: end();
0316: genRethrowExceptions(exceps);
0317: }
0318: }
0319: }
0320: }
0321: jw.end();
0322: jw.cr();
0323:
0324: // Create a constructor that has all of the required parameters.
0325: List requiredParameters = new LinkedList();
0326: for (int i = 0; i < size; i++) {
0327: Property a = (Property) attrList.get(i);
0328: // Is the property optional?
0329: if (!a.ored
0330: && (a.elementInstance == Common.TYPE_1 || a.elementInstance == Common.TYPE_1_N))
0331: requiredParameters.add(a);
0332: }
0333: if (!config.isMinFeatures() && requiredParameters.size() > 0) {
0334: String parameters = null;
0335: for (Iterator it = requiredParameters.iterator(); it
0336: .hasNext();) {
0337: Property a = (Property) it.next();
0338: if (parameters != null)
0339: parameters += ", ";
0340: else
0341: parameters = "";
0342: String type = a.getType();
0343: String baseType = type;
0344: if (a.isIndexed())
0345: type = baseType + "[]";
0346: String fullClassType = getTypeFullClassName(a, type);
0347: parameters += fullClassType + " " + a.instanceOf();
0348: }
0349: jw.bigComment("Required parameters constructor");
0350: jw.beginConstructor(className, parameters);
0351: genExtendBaseBeanConstructor();
0352: for (Iterator it = requiredParameters.iterator(); it
0353: .hasNext();) {
0354: Property a = (Property) it.next();
0355: boolean indexed = a.isIndexed();
0356: String type = a.getType();
0357: String baseType = type;
0358: if (indexed)
0359: type = baseType + "[]";
0360: String attr = "_" + a.name;
0361: SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a
0362: .searchExtraData(SchemaRep.WhiteSpace.class);
0363: if (!indexed) {
0364: if (ws != null)
0365: genWhiteSpaceRestriction(ws, a.instanceOf(),
0366: baseType);
0367: jw.write(attr, " = ");
0368: jw.writeEol(a.instanceOf());
0369: if (a.isBean && config.isGenerateParentRefs()) {
0370: jw.beginIf(attr + " != null");
0371: jw.writeEol(attr, "._setParent(this)");
0372: jw.end();
0373: } else if (config.isOptionalScalars()
0374: && a.isScalar()) {
0375: jw.writeEol(a.getScalarIsSet(), " = true");
0376: }
0377: } else {
0378: jw.beginIf(a.instanceOf() + "!= null");
0379: if ("java.util.ArrayList".equals(config
0380: .getIndexedPropertyType())) {
0381: jw.write("((", config.getIndexedPropertyType(),
0382: ") ", attr);
0383: jw.writeEol(").ensureCapacity(",
0384: a.instanceOf(), ".length)");
0385: }
0386: jw.beginFor("int i = 0", "i < " + a.instanceOf()
0387: + ".length", "++i");
0388: if (ws != null)
0389: genWhiteSpaceRestriction(ws, a.instanceOf()
0390: + "[i]", baseType);
0391: if (a.isBean && config.isGenerateParentRefs()) {
0392: jw.beginIf(a.instanceOf() + "[i] != null");
0393: jw.writeEol(a.instanceOf(),
0394: "[i]._setParent(this)");
0395: jw.end();
0396: }
0397: jw.write(attr, ".add(", JavaUtil.toObject(a
0398: .instanceOf()
0399: + "[i]", baseType, config.isForME()));
0400: jw.writeEol(")");
0401: jw.end();
0402: jw.end();
0403: }
0404: }
0405: jw.end();
0406: jw.cr();
0407: }
0408: }
0409:
0410: public void genAccessors() throws IOException {
0411: int size = attrList.size();
0412: for (int i = 0; i < size; i++) {
0413: Property a = (Property) attrList.get(i);
0414: boolean indexed = a.isIndexed();
0415: boolean isArrayStyle = (config.getIndexedPropertyType() == null);
0416: boolean isWrapper = false;
0417: boolean isScalar = a.isScalar();
0418: String scalarType = a.getScalarType();
0419: String attr = "_" + a.name;
0420: String propertyName = a.beanIntrospectorName();
0421: MetaElement me = getMetaElement(a);
0422:
0423: String type = a.getType();
0424: String baseType = type;
0425: if (indexed)
0426: type = baseType + "[]";
0427: Signatures sigs = getSignatures(a);
0428: Signature sig;
0429:
0430: //System.out.println("a.name="+a.name+" a.type="+a.type+" a.dtdName="+a.dtdName+" isWrapper="+isWrapper+" a.classType="+a.classType+" me="+me);
0431: select(DECL_SECTION);
0432: if (indexed) {
0433: if (isArrayStyle) {
0434: gen(PRIVATE, baseType + "[]", attr);
0435: gen(" = new ", baseType, "[0]");
0436: eol();
0437: } else {
0438: String paramType = (config.jdkTarget >= 150 ? "<"
0439: + JavaUtil.toObjectType(baseType) + ">"
0440: : "");
0441: gen(PRIVATE, "java.util.List" + paramType, attr);
0442: gen(" = new " + config.getIndexedPropertyType()
0443: + paramType + "();");
0444: tabIn();
0445: if (config.jdkTarget < 150)
0446: comment("List<" + baseType + ">");
0447: else
0448: cr();
0449: }
0450: } else {
0451: gen(PRIVATE, type, attr);
0452: if (a.getDefaultValue() != null
0453: && (a.elementInstance == Common.TYPE_1 || a.elementInstance == Common.TYPE_1_N)) {
0454: gen(" = ");
0455: if (!JavaUtil.checkValueToType(type, a
0456: .getDefaultValue())) {
0457: config.messageOut.println(Common.getMessage(
0458: "MSG_NotAGoodValue", a
0459: .getDefaultValue(), type));
0460: }
0461: gen(JavaUtil
0462: .instanceFrom(type, a.getDefaultValue()));
0463: eol();
0464: if (config.isOptionalScalars() && a.isScalar()) {
0465: gen(PRIVATE, "boolean", a.getScalarIsSet());
0466: gen(" = true");
0467: eol();
0468: }
0469: } else {
0470: eol();
0471: if (config.isOptionalScalars() && a.isScalar()) {
0472: gen(PRIVATE, "boolean", a.getScalarIsSet());
0473: gen(" = false");
0474: eol();
0475: }
0476: }
0477: }
0478:
0479: select(ACCESS_SECTION);
0480: comment("This attribute is ", Common
0481: .instanceToString(a.elementInstance));
0482: // Generate setter
0483: if (config.jdkTarget >= 150) {
0484: boolean first = true;
0485: for (Iterator extraDataIt = a.extraDataIterator(); extraDataIt
0486: .hasNext();) {
0487: Object extraData = extraDataIt.next();
0488: if (extraData instanceof HasAnnotation) {
0489: HasAnnotation annotator = (HasAnnotation) extraData;
0490: if (first)
0491: first = false;
0492: else
0493: jw.write(' ');
0494: jw.write('@');
0495: jw.write(annotator.genAnnotation());
0496: }
0497: }
0498: if (!first)
0499: jw.cr();
0500: }
0501: gen(sigs.findSignature(SETTER));
0502: sp();
0503: begin();
0504: if (a.getPropertyInterface() != null) {
0505: jw.writeEol(type + " value = (" + type
0506: + ") valueInterface");
0507: }
0508: if (indexed) {
0509: gen("if (value == null)");
0510: cr();
0511: tabIn();
0512: if (baseType.equals("byte[]"))
0513: jw.writeEol("value = new byte[0][0]");
0514: else
0515: jw.writeEol("value = new ", baseType, "[0]");
0516: }
0517: if (!indexed) {
0518: SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a
0519: .searchExtraData(SchemaRep.WhiteSpace.class);
0520: if (ws != null)
0521: genWhiteSpaceRestriction(ws, "value", baseType);
0522: }
0523: if (config.isGeneratePropertyEvents()
0524: || config.isVetoable()) {
0525: if (indexed) {
0526: jw.beginIf("value.length == " + a.getSizeMethod()
0527: + "()");
0528: jw.writeEol("boolean same = true");
0529: jw.beginFor("int i = 0", "i < value.length", "++i");
0530: jw.beginIf("!("
0531: + JavaUtil.genEquals(type, "value[i]", a
0532: .getReadMethod(true)
0533: + "(i)") + ")");
0534: jw.writeEol("same = false");
0535: jw.writeEol("break");
0536: jw.end();
0537: jw.end();
0538: jw.beginIf("same");
0539: } else {
0540: jw.beginIf(JavaUtil.genEquals(type, "value", attr));
0541: }
0542: jw.comment("No change.");
0543: jw.writeEol("return");
0544: jw.end();
0545: if (indexed)
0546: jw.end();
0547:
0548: if (a.isBean) {
0549: if (config.isGeneratePropertyEvents())
0550: comment("Make the foreign beans take on our property change event listeners.");
0551: if (config.isGenerateParentRefs())
0552: comment("Maintain the parent reference.");
0553: String iterVar = "value";
0554: if (indexed) {
0555: jw.beginFor("int i = 0", "i < value.length",
0556: "++i");
0557: iterVar = "value[i]";
0558: }
0559: jw.beginIf(iterVar + " != null");
0560: if (config.isGeneratePropertyEvents())
0561: geneol(iterVar
0562: + "._setPropertyChangeSupport(eventListeners)");
0563: if (config.isVetoable())
0564: geneol(iterVar
0565: + "._setVetoableChangeSupport(vetos)");
0566: if (config.isGenerateParentRefs())
0567: jw.writeEol(iterVar + "._setParent(this)");
0568: jw.end();
0569: if (indexed)
0570: jw.end();
0571: }
0572:
0573: jw
0574: .writeEol("java.beans.PropertyChangeEvent event = null");
0575: gen("if (");
0576: jw.setFirst(" || ");
0577: if (config.isGeneratePropertyEvents()) {
0578: jw.writeNext("eventListeners != null");
0579: }
0580: if (config.isGenerateStoreEvents()) {
0581: jw.writeNext("storeEvents");
0582: }
0583: if (config.isVetoable()) {
0584: jw.writeNext("vetos != null");
0585: }
0586: gen(") ");
0587: begin();
0588: if (indexed) {
0589: jw.comment("See if only 1 thing changed.");
0590: jw.writeEol("int addIndex = -1");
0591: jw.writeEol("int removeIndex = -1");
0592: jw.writeEol("int oldSize = size", a.name, "()");
0593: jw.writeEol("int newSize = value.length");
0594: jw
0595: .beginIf("oldSize + 1 == newSize || oldSize == newSize + 1");
0596: jw.writeEol("boolean checkAddOrRemoveOne = true");
0597: jw.writeEol("int oldIndex = 0, newIndex = 0");
0598: jw.beginFor("",
0599: "oldIndex < oldSize && newIndex < newSize",
0600: "++newIndex, ++oldIndex");
0601: //jw.writeEol("System.out.println(\"oldIndex=\"+oldIndex+\" newIndex=\"+newIndex)");
0602: //jw.writeEol("System.out.println(\"addIndex=\"+addIndex+\" removeIndex=\"+removeIndex)");
0603: jw.beginIf(JavaUtil.genEquals(baseType,
0604: "value[newIndex]", a.getReadMethod(true)
0605: + "(oldIndex)"));
0606: jw.comment("Same, so just continue.");
0607: jw
0608: .endElseBeginIf("addIndex != -1 || removeIndex != -1");
0609: jw.comment("More than 1 difference detected.");
0610: jw.writeEol("addIndex = removeIndex = -1");
0611: jw.writeEol("checkAddOrRemoveOne = false");
0612: jw.writeEol("break");
0613: jw.endElseBeginIf("oldIndex + 1 < oldSize && ("
0614: + JavaUtil.genEquals(baseType,
0615: "value[newIndex]", a
0616: .getReadMethod(true)
0617: + "(oldIndex+1)") + ")");
0618: jw.writeEol("removeIndex = oldIndex");
0619: jw.writeEol("++oldIndex");
0620: jw.endElseBeginIf("newIndex + 1 < newSize && ("
0621: + JavaUtil.genEquals(baseType,
0622: "value[newIndex+1]", a
0623: .getReadMethod(true)
0624: + "(oldIndex)") + ")");
0625: jw.writeEol("addIndex = newIndex");
0626: jw.writeEol("++newIndex");
0627: jw.endElseBegin();
0628: jw.comment("More than 1 difference.");
0629: jw.writeEol("addIndex = removeIndex = -1");
0630: jw.writeEol("checkAddOrRemoveOne = false");
0631: jw.writeEol("break");
0632: jw.end();
0633: jw.end(); // for
0634: // Only do this check if we fell off the end (oldIndex == newIndex)
0635: jw
0636: .beginIf("checkAddOrRemoveOne && addIndex == -1 && removeIndex == -1");
0637: jw.beginIf("oldSize + 1 == newSize");
0638: jw.comment("Added last one");
0639: jw.writeEol("addIndex = oldSize");
0640: jw.endElseBeginIf("oldSize == newSize + 1");
0641: jw.comment("Removed last one");
0642: jw.writeEol("removeIndex = newSize");
0643: jw.end();
0644: jw.end();
0645: jw.end();
0646:
0647: //jw.writeEol("System.out.println(\"addIndex=\"+addIndex+\" removeIndex=\"+removeIndex)");
0648: jw.beginIf("addIndex >= 0");
0649: jw.write("event = ");
0650: genNewEvent(a, "addIndex", "null",
0651: "value[addIndex]", baseType);
0652: jw.eol();
0653: if (!isArrayStyle && !config.isVetoable()
0654: && !config.isKeepElementPositions()
0655: && !config.isGenerateStoreEvents()) {
0656: SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a
0657: .searchExtraData(SchemaRep.WhiteSpace.class);
0658: if (ws != null)
0659: genWhiteSpaceRestriction(ws,
0660: "value[addIndex]", baseType);
0661: jw.writeEol(attr
0662: + ".add(addIndex, value[addIndex])");
0663: genMadeChange();
0664: jw
0665: .writeEol("eventListeners.firePropertyChange(event)");
0666: jw.writeEol("return");
0667: }
0668: jw.endElseBeginIf("removeIndex >= 0");
0669: jw.write("event = ");
0670: genNewEvent(a, "removeIndex", a.getReadMethod(true)
0671: + "(removeIndex)", "null", baseType);
0672: jw.eol();
0673: if (!isArrayStyle && !config.isVetoable()
0674: && !config.isKeepElementPositions()
0675: && !config.isGenerateStoreEvents()) {
0676: jw.writeEol(attr + ".remove(removeIndex)");
0677: genMadeChange();
0678: jw
0679: .writeEol("eventListeners.firePropertyChange(event)");
0680: jw.writeEol("return");
0681: }
0682: jw.endElseBegin();
0683: }
0684: jw.write("event = ");
0685: genNewEvent(a, indexed ? "-1" : "", a
0686: .getReadMethod(false)
0687: + "()", "value", type);
0688: jw.eol();
0689: end();
0690: if (indexed) {
0691: jw.end();
0692: }
0693: }
0694: if (config.isVetoable()) {
0695: gencr("if (vetos != null)");
0696: tabIn();
0697: geneol("vetos.fireVetoableChange(event)");
0698: }
0699: if (indexed) {
0700: if (config.isKeepElementPositions()) {
0701: jw.comment("Figure out where this type belongs.");
0702: jw.writeEol("int elementCount = fetchChildCount()");
0703: jw.writeEol("int destPos = 0");
0704: jw.writeEol("int srcPos = 0");
0705: // Remove all of the old entries
0706: jw
0707: .beginFor(
0708: "",
0709: "destPos < elementCount && srcPos < value.length",
0710: "++destPos");
0711: jw.beginIf("elementTypesByPosition[destPos] == "
0712: + i);
0713: jw.comment("replace it");
0714: jw
0715: .writeEol("elementsByPosition[destPos] = value[srcPos++]");
0716: jw.end();
0717: jw.end();
0718: jw
0719: .comment("Handle when the replacement array is smaller.");
0720: jw.beginWhile("destPos < elementCount");
0721: jw.beginIf("elementTypesByPosition[destPos] == "
0722: + i);
0723: jw.writeEol("deleteElement(destPos)");
0724: jw.writeEol("--elementCount");
0725: jw.endElseBegin();
0726: jw.writeEol("++destPos");
0727: jw.end();
0728: jw.end();
0729: jw
0730: .comment("Handle when replacement array is larger.");
0731: jw
0732: .beginFor("", "srcPos < value.length",
0733: "++srcPos");
0734: jw.writeEol("insertElementByPosition(destPos++, ",
0735: JavaUtil.toObject("value[srcPos]",
0736: baseType, config.isForME()), ", "
0737: + i + ")");
0738: jw.end();
0739: }
0740: if (isArrayStyle) {
0741: jw.writeEol(attr, " = value");
0742: } else {
0743: jw.writeEol(attr, ".clear()");
0744: if ("java.util.ArrayList".equals(config
0745: .getIndexedPropertyType())) {
0746: jw.writeEol("((", config
0747: .getIndexedPropertyType(), ") " + attr,
0748: ").ensureCapacity(value.length)");
0749: }
0750: jw.beginFor("int i = 0", "i < value.length", "++i");
0751: SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a
0752: .searchExtraData(SchemaRep.WhiteSpace.class);
0753: if (ws != null)
0754: genWhiteSpaceRestriction(ws, "value[i]",
0755: baseType);
0756: jw.write(attr, ".add(");
0757: String objectValue = JavaUtil.toObject("value[i]",
0758: baseType, config.isForME());
0759: jw.writeEol(objectValue, ")");
0760: jw.end();
0761: }
0762: } else {
0763: geneol(attr + " = value");
0764: if (config.isOptionalScalars() && a.isScalar()) {
0765: jw.writeEol(a.getScalarIsSet(), " = true");
0766: }
0767: if (config.isKeepElementPositions()) {
0768: jw.comment("Figure out where this type belongs.");
0769: jw.writeEol("int elementCount = fetchChildCount()");
0770: jw.writeEol("int pos = findFirstOfElementType(" + i
0771: + ")");
0772: if (!isScalar) {
0773: jw.beginIf("value != null");
0774: }
0775: jw.beginIf("pos >= elementCount");
0776: jw.comment("It's the last one to be added");
0777: jw
0778: .writeEol("expandElementsByPosition(elementCount+1)");
0779: jw.writeEol("elementTypesByPosition[pos] = " + i);
0780: jw.end();
0781: jw.writeEol("elementsByPosition[pos] = ", JavaUtil
0782: .toObject("value", type, config.isForME()));
0783: if (!isScalar) {
0784: jw.endElseBegin();
0785: jw.beginIf("pos < elementCount");
0786: jw.writeEol("deleteElement(pos)");
0787: jw.end();
0788: jw.end();
0789: }
0790: }
0791: }
0792: genMadeChange();
0793: if (config.isGeneratePropertyEvents()) {
0794: if (config.isGenerateStoreEvents()) {
0795: gencr("if (storeEvents)");
0796: tabIn();
0797: geneol("storedEvents.add(event)");
0798: gen("else ");
0799: }
0800: gencr("if (event != null)");
0801: tabIn();
0802: geneol("eventListeners.firePropertyChange(event)");
0803: }
0804:
0805: genResetMutuallyExclusive(a, true);
0806: end();
0807: cr();
0808:
0809: if (indexed) {
0810: gen(sigs.findSignature(SETTERINDEXED));
0811: sp();
0812: begin();
0813: if (a.getPropertyInterface() != null) {
0814: jw.writeEol(baseType + " value = (" + baseType
0815: + ") valueInterface");
0816: }
0817: if (config.isGeneratePropertyEvents()) {
0818: jw.beginIf(JavaUtil.genEquals(type, "value", a
0819: .getReadMethod(true)
0820: + "(index)", true));
0821: jw.comment("No change.");
0822: jw.writeEol("return");
0823: jw.end();
0824: }
0825: if (a.isBean && config.isGenerateParentRefs()) {
0826: jw.beginIf("value != null");
0827: jw.writeEol("value._setParent(this)");
0828: jw.end();
0829: }
0830: if (config.isGeneratePropertyEvents()) {
0831: if (a.isBean) {
0832: jw.beginIf("value != null");
0833: comment("Make the foreign beans take on our property change event listeners.");
0834: geneol("value._setPropertyChangeSupport(eventListeners)");
0835: if (config.isVetoable())
0836: geneol("value._setVetoableChangeSupport(vetos)");
0837: jw.end();
0838: }
0839:
0840: gen("if (");
0841: if (config.isGenerateStoreEvents()) {
0842: gen("storeEvents || ");
0843: }
0844: gen("eventListeners != null) ");
0845: begin();
0846: jw.write("java.beans.PropertyChangeEvent event = ");
0847: genNewEvent(a, "index", a.getReadMethod(true)
0848: + "(index)", "value", baseType);
0849: jw.eol();
0850: if (config.isVetoable()) {
0851: gencr("if (vetos != null)");
0852: tabIn();
0853: geneol("vetos.fireVetoableChange(event)");
0854: }
0855: if (config.isGenerateStoreEvents()) {
0856: gencr("if (storeEvents)");
0857: tabIn();
0858: geneol("storedEvents.add(event)");
0859: gencr("else");
0860: tabIn();
0861: }
0862: geneol("eventListeners.firePropertyChange(event)");
0863: end();
0864: }
0865: if (isArrayStyle) {
0866: jw.writeEol(attr, "[index] = value");
0867: } else {
0868: if (!a.isDirectChild()) {
0869: // Check to see if we need to grow before we set.
0870: jw.beginFor("int size = " + attr + ".size()",
0871: "index >= size", "++size");
0872: jw.writeEol(attr, ".add(null)");
0873: jw.end();
0874: }
0875: jw.write(attr, ".set(index, ");
0876: jw.write(JavaUtil.toObject("value", baseType,
0877: config.isForME()));
0878: jw.writeEol(")");
0879: }
0880: if (config.isKeepElementPositions()) {
0881: jw.writeEol("int pos = findElementType(" + i
0882: + ", index)");
0883: jw.writeEol("elementsByPosition[pos] = ", JavaUtil
0884: .toObject("value", baseType, config
0885: .isForME()));
0886: }
0887: genMadeChange();
0888: end();
0889: cr();
0890: }
0891:
0892: // Generate getter
0893: gen(sigs.findSignature(GETTER));
0894: sp();
0895: begin();
0896: if (indexed && !isArrayStyle) {
0897: if (baseType.equals("byte[]"))
0898: jw.writeEol(type, " arr = new byte[", attr,
0899: ".size()][]");
0900: else
0901: jw.writeEol(type, " arr = new ", baseType + "["
0902: + attr, ".size()]");
0903: if (isScalar) {
0904: jw.beginFor("int i = 0", "i < arr.length", "++i");
0905: jw.write("arr[i] = ");
0906: jw.write(JavaUtil.fromObject(baseType, attr
0907: + ".get(i)"));
0908: jw.eol();
0909: jw.end();
0910: jw.write("return arr");
0911: } else {
0912: gen("return (" + type + ") " + attr
0913: + ".toArray(arr)");
0914: }
0915: } else
0916: gen("return " + attr);
0917: eol();
0918: end();
0919: cr();
0920:
0921: if (indexed) {
0922: if (!config.isMinFeatures() && !isArrayStyle) {
0923: gen(sigs.findSignature(GETTERLIST));
0924: sp();
0925: begin();
0926: gen("return " + attr);
0927: eol();
0928: end();
0929: cr();
0930: }
0931:
0932: gen(sigs.findSignature(GETTERINDEXED));
0933: sp();
0934: begin();
0935: jw.write("return ");
0936: if (isArrayStyle) {
0937: jw.writeEol(attr, "[index]");
0938: } else {
0939: jw.writeEol(JavaUtil.fromObject(baseType, attr
0940: + ".get(index)"));
0941: }
0942: end();
0943: cr();
0944:
0945: comment("Return the number of " + propertyName);
0946: gen(sigs.findSignature(SIZE));
0947: sp();
0948: begin();
0949: if (isArrayStyle) {
0950: jw.writeEol("return ", attr, ".length");
0951: } else {
0952: jw.writeEol("return ", attr, ".size()");
0953: }
0954: end();
0955: cr();
0956:
0957: if (!isArrayStyle) {
0958: gen(sigs.findSignature(ADD));
0959: sp();
0960: begin();
0961: if (a.getPropertyInterface() != null) {
0962: jw.writeEol(baseType + " value = (" + baseType
0963: + ") valueInterface");
0964: }
0965: if (a.isBean && config.isGenerateParentRefs()) {
0966: jw.beginIf("value != null");
0967: jw.writeEol("value._setParent(this)");
0968: jw.end();
0969: }
0970: if (a.isBean && config.isGeneratePropertyEvents()) {
0971: jw.beginIf("value != null");
0972: comment("Make the foreign beans take on our property change event listeners.");
0973: geneol("value._setPropertyChangeSupport(eventListeners)");
0974: if (config.isVetoable())
0975: geneol("value._setVetoableChangeSupport(vetos)");
0976: jw.end();
0977: }
0978: jw.writeEol(attr, ".add(", JavaUtil.toObject(
0979: "value", baseType, config.isForME()), ")");
0980: if (config.isKeepElementPositions()) {
0981: GraphLink gl = a.getGraphLink();
0982: //jw.comment("gl="+gl);
0983: //jw.comment("gl.lastInGroup="+gl.getLastInGroup());
0984: //jw.comment("gl.lastInGroup.object="+gl.getLastInGroup().getObject());
0985: int lastPropNum;
0986: if (gl == null) {
0987: lastPropNum = i;
0988: } else {
0989: Property lastProp = (Property) gl
0990: .getLastSibling().getObject();
0991: if (lastProp == a) {
0992: lastPropNum = i;
0993: } else {
0994: for (lastPropNum = 0; lastPropNum < size; ++lastPropNum)
0995: if (attrList.get(lastPropNum) == lastProp)
0996: break;
0997: if (lastPropNum == size) {
0998: jw
0999: .comment("Did not find lastPropNum");
1000: lastPropNum = i;
1001: }
1002: }
1003: }
1004: jw.writeEol("int pos = findLastOfElementType("
1005: + lastPropNum + ")+1");
1006: jw.writeEol("insertElementByPosition(pos, ",
1007: JavaUtil.toObject("value", baseType,
1008: config.isForME()), ", " + i
1009: + ")");
1010: }
1011: if (config.isGeneratePropertyEvents()) {
1012: gen("if (");
1013: if (config.isGenerateStoreEvents()) {
1014: gen("storeEvents || ");
1015: }
1016: gen("eventListeners != null) ");
1017: begin();
1018: jw
1019: .write("java.beans.PropertyChangeEvent event = ");
1020: genNewEvent(a, attr + ".size()-1", "null",
1021: "value", baseType);
1022: jw.eol();
1023: if (config.isVetoable()) {
1024: gencr("if (vetos != null)");
1025: tabIn();
1026: geneol("vetos.fireVetoableChange(event)");
1027: }
1028: if (config.isGenerateStoreEvents()) {
1029: gencr("if (storeEvents)");
1030: tabIn();
1031: geneol("storedEvents.add(event)");
1032: gencr("else");
1033: tabIn();
1034: }
1035: geneol("eventListeners.firePropertyChange(event)");
1036: end();
1037: }
1038: jw.writeEol("int positionOfNewItem = " + attr
1039: + ".size()-1");
1040: if (isMutuallyExclusive(a)) {
1041: jw.beginIf("positionOfNewItem == 0");
1042: genResetMutuallyExclusive(a, false);
1043: jw.end();
1044: }
1045: genMadeChange();
1046: geneol("return positionOfNewItem");
1047: end();
1048: cr();
1049:
1050: jw
1051: .bigComment("Search from the end looking for @param value, and then remove it.");
1052: gen(sigs.findSignature(REMOVE));
1053: sp();
1054: begin();
1055: if (a.getPropertyInterface() != null) {
1056: jw.writeEol(baseType + " value = (" + baseType
1057: + ") valueInterface");
1058: }
1059: jw.writeEol("int pos = ", attr, ".indexOf(",
1060: JavaUtil.toObject("value", baseType, config
1061: .isForME())
1062: + ")");
1063: gen("if (pos >= 0) ");
1064: begin();
1065: geneol(attr + ".remove(pos)");
1066: if (config.isKeepElementPositions()) {
1067: jw.writeEol("int elementPos = findElementType("
1068: + i + ", pos)");
1069: jw.writeEol("deleteElement(elementPos)");
1070: }
1071: if (config.isGeneratePropertyEvents()) {
1072: gen("if (");
1073: if (config.isGenerateStoreEvents()) {
1074: gen("storeEvents || ");
1075: }
1076: gen("eventListeners != null) ");
1077: begin();
1078: gen("java.beans.PropertyChangeEvent event = ");
1079: genNewEvent(a, "pos", "value", "null", baseType);
1080: jw.eol();
1081: if (config.isVetoable()) {
1082: gencr("if (vetos != null)");
1083: tabIn();
1084: geneol("vetos.fireVetoableChange(event)");
1085: }
1086: if (config.isGenerateStoreEvents()) {
1087: gencr("if (storeEvents)");
1088: tabIn();
1089: geneol("storedEvents.add(event)");
1090: gencr("else");
1091: tabIn();
1092: }
1093: geneol("eventListeners.firePropertyChange(event)");
1094: end();
1095: }
1096: end();
1097: genMadeChange();
1098: geneol("return pos");
1099: end();
1100: cr();
1101: }
1102:
1103: MetaProperty mp = getMetaProperty(a.name);
1104: GraphNode graphNode = a.getGraphNode();
1105: SchemaRep.Key key = null;
1106: if (graphNode != null)
1107: key = (SchemaRep.Key) graphNode
1108: .searchExtraData(SchemaRep.Key.class);
1109: if (a.isBean
1110: && ((key != null) || (mp != null && mp.isKey()))) {
1111: //System.out.println("mp for "+a.name);
1112: //System.out.println(mp.dumpBeanNode());
1113: config.messageOut.println("Found key: " + key);
1114: SchemaRep.Selector selector = (SchemaRep.Selector) key
1115: .findSubElement(SchemaRep.Selector.class);
1116: SchemaRep.Field field = (SchemaRep.Field) key
1117: .findSubElement(SchemaRep.Field.class);
1118: genFinder(a, graphNode, attr, key.getElementName(),
1119: baseType, a.name, selector.getXPath(),
1120: field.getXPath(), false);
1121: }
1122: } // end if (indexed)
1123: genDefaultsAccessable(a);
1124:
1125: if (a.isBean && !a.getBeanElement().isAbstract) {
1126: genNewMethod(a.getPropertyInterface(), baseType,
1127: getTypeFullClassName(a));
1128: }
1129: }
1130: if (config.isKeepElementPositions()) {
1131: select(DECL_SECTION);
1132: jw
1133: .writeEol("private java.lang.Object[] elementsByPosition = new java.lang.Object[0]"); // we might be storing String, int's, and beans in there.
1134: jw
1135: .writeEol("private int[] elementTypesByPosition = new int[0]");
1136: }
1137: if (beanElement.isRoot && !config.isMinFeatures()) {
1138: select(DECL_SECTION);
1139: jw.write("private java.lang.String schemaLocation");
1140: if (mdd.getSchemaLocation() != null) {
1141: jw.write(" = ");
1142: jw.write(JavaUtil.instanceFrom("java.lang.String", mdd
1143: .getSchemaLocation()));
1144: }
1145: jw.eol();
1146: select(ACCESS_SECTION);
1147: jw.beginMethod("_setSchemaLocation", "String location",
1148: null, "void", jw.PUBLIC);
1149: jw.writeEol("schemaLocation = location");
1150: genMadeChange();
1151: jw.end();
1152: jw.cr();
1153:
1154: jw.beginMethod("_getSchemaLocation", "", null, "String",
1155: jw.PUBLIC);
1156: jw.writeEol("return schemaLocation");
1157: jw.endMethod();
1158: }
1159: if (config.isGenerateParentRefs()) {
1160: select(DECL_SECTION);
1161: String parentType = parentBeanType();
1162: jw.writeEol("private ", parentType, " parent");
1163: select(BODY_SECTION);
1164: jw.beginMethod("_setParent", parentType + " parent");
1165: jw.writeEol("this.parent = parent");
1166: jw.endMethod();
1167:
1168: jw.beginMethod("_getXPathExpr", "", null, "String",
1169: jw.PUBLIC);
1170: jw.beginIf("parent == null");
1171: jw.writeEol("return \"/", beanElement.node.getName(), "\"");
1172: jw.endElseBegin();
1173: jw
1174: .writeEol("String parentXPathExpr = parent._getXPathExpr()");
1175: jw
1176: .writeEol("String myExpr = parent.nameChild(this, false, false, true)");
1177: jw.writeEol("return parentXPathExpr + \"/\" + myExpr");
1178: jw.end();
1179: jw.endMethod();
1180:
1181: jw.beginMethod("_getXPathExpr", "Object childObj", null,
1182: "String", jw.PUBLIC);
1183: jw
1184: .writeEol("String childName = nameChild(childObj, false, false, true)");
1185: jw.beginIf("childName == null");
1186: jw
1187: .writeEol("throw new IllegalArgumentException(\"childObj (\"+childObj.toString()+\") is not a child of this bean ("
1188: + className + ").\")");
1189: jw.end();
1190: jw.writeEol("return _getXPathExpr() + \"/\" + childName");
1191: jw.endMethod();
1192: }
1193:
1194: // Look for finders
1195: for (Iterator it = beanElement.getGraphNode()
1196: .extraDataIterator(); it.hasNext();) {
1197: Object data = it.next();
1198: if (data instanceof BeanBuilder.Finder) {
1199: BeanBuilder.Finder finder = (BeanBuilder.Finder) data;
1200: //System.out.println("Found finder: "+finder);
1201: genFinder(beanElement.getGraphNode(), finder);
1202: }
1203: }
1204: }
1205:
1206: protected void genFinder(GraphNode startingGraphNode,
1207: BeanBuilder.Finder finder) throws IOException {
1208: genFinder(startingGraphNode, finder.getFindExpr(), finder
1209: .getByExpr(), finder.isListFindExpr());
1210: }
1211:
1212: protected void genFinder(GraphNode startingGraphNode,
1213: String selectorXPath, String fieldXPath,
1214: boolean isListFindExpr) throws IOException {
1215: genFinder(null, startingGraphNode, null, null, null, null,
1216: selectorXPath, fieldXPath, isListFindExpr);
1217: }
1218:
1219: protected void genFinder(Property a, GraphNode startingGraphNode,
1220: String attr, String keyName, String returnType,
1221: String nameToFind, String selectorXPath, String fieldXPath,
1222: boolean isListFindExpr) throws IOException {
1223: String dtdName = null;
1224: String keyVar = null;
1225: String keyType = null;
1226: String keyTypeInterface = null;
1227: List xpathChain = null;
1228:
1229: GraphNode graphNode = startingGraphNode;
1230: Property selectorProp = null;
1231: if (graphNode != null && graphNode.getGraphLink() != null) {
1232: xpathChain = new LinkedList();
1233: GraphLink gl = null;
1234: for (Iterator it = graphNode.getGraphLink().xPathIterator(
1235: selectorXPath); it.hasNext();) {
1236: gl = (GraphLink) it.next();
1237: //System.out.println("selector gl="+gl);
1238: if (gl == null)
1239: break;
1240: if (gl.getObject() != null)
1241: xpathChain.add(gl);
1242: }
1243: if (gl != null) {
1244: GraphLink nextStartingLink;
1245: if (gl.element != null) {
1246: nextStartingLink = gl.element.getGraphLink();
1247: } else {
1248: nextStartingLink = gl;
1249: }
1250: selectorProp = (Property) gl.getObject();
1251: if (!fieldXPath.equals(".")) {
1252: //System.out.println("nextStartingLink="+nextStartingLink);
1253: for (Iterator it = nextStartingLink
1254: .xPathIterator(fieldXPath); it.hasNext();) {
1255: gl = (GraphLink) it.next();
1256: //System.out.println("field gl="+gl);
1257: if (gl == null) {
1258: break;
1259: }
1260: if (gl.getObject() != null)
1261: xpathChain.add(gl);
1262: }
1263: }
1264: if (gl != null) {
1265: //
1266: // Found the final one.
1267: //
1268: //System.out.println("gl="+gl+" gl.name="+gl.name);
1269: Property finalProp = (Property) gl.getObject();
1270: if (finalProp != null) {
1271: dtdName = finalProp.dtdName;
1272: keyVar = finalProp.instanceOf() + "Key";
1273: keyType = finalProp.getType();
1274: keyTypeInterface = finalProp
1275: .getPropertyInterface();
1276: } else {
1277: config.messageOut
1278: .println("Warning: finalProp=null while generating finder.");
1279: }
1280: }
1281: }
1282: if (gl == null)
1283: xpathChain = null;
1284: }
1285: //System.out.println("dtdName="+dtdName);
1286:
1287: if (dtdName == null) {
1288: dtdName = fieldXPath;
1289: keyVar = Common.convertNameInstance(dtdName);
1290: keyType = "String";
1291: jw
1292: .comment("Did not figure out proper expression to find the key. This method may not work.");
1293: jw.comment("selectorXPath=" + selectorXPath);
1294: if (selectorProp == null) {
1295: jw.comment(" Failed to find selector.");
1296: } else {
1297: jw.comment(" found selector property name="
1298: + selectorProp.dtdName);
1299: }
1300: jw.comment("fieldXPath=" + fieldXPath);
1301: xpathChain = null;
1302: }
1303:
1304: if (selectorProp != null) {
1305: if (nameToFind == null)
1306: nameToFind = Common.convertName(selectorProp.dtdName);
1307: if (returnType == null) {
1308: if (selectorProp.getPropertyInterface() == null)
1309: returnType = selectorProp.getType();
1310: else
1311: returnType = selectorProp.getPropertyInterface();
1312: }
1313: }
1314: if (keyName == null)
1315: keyName = fieldXPath;
1316: if (returnType == null)
1317: returnType = "String";
1318: if (isListFindExpr) {
1319: returnType = "java.util.List/*<" + returnType + ">*/";
1320: jw
1321: .bigComment("Search for the key.\n@return all elements found that match.");
1322: } else {
1323: jw
1324: .bigComment("Search for the key.\n@return null if the key is not found.");
1325: }
1326: String methodName;
1327: if (isListFindExpr)
1328: methodName = "findAll" + nameToFind;
1329: else
1330: methodName = "find" + nameToFind;
1331: if (!keyName.equals("."))
1332: methodName = methodName + "By"
1333: + Common.convertName(keyName);
1334: String arguments;
1335: if (keyTypeInterface == null)
1336: arguments = keyType + " " + keyVar;
1337: else
1338: arguments = keyTypeInterface + " " + keyVar + "Interface";
1339: jw.beginMethod(methodName, arguments, null, returnType,
1340: jw.PUBLIC | jw.BEANINFO);
1341: if (keyTypeInterface != null)
1342: jw.writeEol(keyVar, " = (" + keyType, ") ", keyVar
1343: + "Interface");
1344: if (isListFindExpr)
1345: jw
1346: .writeEol("java.util.List _result = new java.util.ArrayList()");
1347: String lastVar, topVar;
1348: if (attr != null) {
1349: beginAttrIterator(attr, a, "_el");
1350: jw.beginIf("_el == null");
1351: jw.writeEol("continue");
1352: jw.end();
1353: lastVar = "_el";
1354: topVar = "_el";
1355: } else {
1356: lastVar = "this";
1357: topVar = "null";
1358: }
1359:
1360: if (xpathChain != null) {
1361: GraphLink gl = null;
1362: int foundIndices = 0;
1363: for (Iterator it = xpathChain.iterator(); it.hasNext();) {
1364: gl = (GraphLink) it.next();
1365: Property prop = (Property) gl.getObject();
1366: String var = prop.instanceOf() + foundIndices;
1367: if (attr == null && selectorProp == prop)
1368: topVar = var;
1369: if (prop.isIndexed()) {
1370: ++foundIndices;
1371: jw.beginFor("int " + var + "Index = 0", var
1372: + "Index < " + lastVar + ".size"
1373: + prop.name + "()", var + "Index++");
1374: }
1375: jw.write(prop.getType() + " " + var);
1376: jw.write(" = (", prop.getType(), ") ");
1377: jw.write(lastVar, ".");
1378: jw.write(prop.getReadMethod(prop.isIndexed()));
1379: if (prop.isIndexed())
1380: jw.write("(" + var + "Index)");
1381: else
1382: jw.write("()");
1383: jw.eol();
1384: jw.beginIf(var + " == null");
1385: if (!it.hasNext()) {
1386: jw.beginIf(keyVar, " == null");
1387: if (isListFindExpr)
1388: jw.writeEol("_result.add(", topVar, ")");
1389: else
1390: jw.writeEol("return ", topVar);
1391: jw.end();
1392: }
1393: jw.writeEol("continue");
1394: jw.end();
1395: lastVar = var;
1396: }
1397: jw.beginIf(lastVar + ".equals(" + keyVar + ")");
1398: if (isListFindExpr)
1399: jw.writeEol("_result.add(", topVar, ")");
1400: else
1401: jw.writeEol("return ", topVar);
1402: jw.end();
1403: for (; foundIndices > 0; --foundIndices)
1404: jw.end();
1405: }
1406:
1407: if (attr != null)
1408: jw.end(); // endAttrIterator
1409: if (isListFindExpr)
1410: jw.writeEol("return _result");
1411: else
1412: jw.writeEol("return ", JavaUtil
1413: .nullValueForType(returnType));
1414: jw.endMethod();
1415: }
1416:
1417: public void genXMLIO() throws IOException {
1418: select(BODY_SECTION);
1419: String beanName = beanElement.node.getName();
1420: if (beanElement.isRoot) {
1421: if (!config.isStandalone()) {
1422: jw.beginMethod("write",
1423: "org.openide.filesystems.FileObject fo",
1424: "java.io.IOException", "void", jw.PUBLIC
1425: | jw.IO);
1426: jw
1427: .writeEol("org.openide.filesystems.FileLock lock = fo.lock()");
1428: jw.beginTry();
1429: jw
1430: .writeEol("java.io.OutputStream out = fo.getOutputStream(lock)");
1431: jw.writeEol("write(out)");
1432: jw.writeEol("out.close()");
1433: jw.endFinallyBegin();
1434: jw.writeEol("lock.releaseLock()");
1435: jw.end();
1436: jw.endMethod();
1437:
1438: jw
1439: .beginMethod(
1440: "write",
1441: "final org.openide.filesystems.FileObject dir, final String filename",
1442: "java.io.IOException", "void",
1443: jw.PUBLIC | jw.IO);
1444: jw
1445: .writeEol("org.openide.filesystems.FileSystem fs = dir.getFileSystem()");
1446: jw
1447: .write("fs.runAtomicAction(new org.openide.filesystems.FileSystem.AtomicAction()\n");
1448: jw.begin();
1449: jw
1450: .write("public void run() throws java.io.IOException {\n");
1451: jw
1452: .writeEol("org.openide.filesystems.FileObject file = dir.getFileObject(filename)");
1453: jw.beginIf("file == null");
1454: jw.writeEol("file = dir.createData(filename)");
1455: jw.end();
1456: jw.writeEol("write(file)");
1457: jw.end();
1458: jw.end();
1459: jw.writeEol(")");
1460: jw.endMethod();
1461: }
1462:
1463: jw.beginMethod("write", "java.io.File f",
1464: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
1465: jw
1466: .writeEol("java.io.OutputStream out = new java.io.FileOutputStream(f)");
1467: jw.beginTry();
1468: jw.writeEol("write(out)");
1469: jw.endFinallyBegin();
1470: jw.writeEol("out.close()");
1471: jw.end();
1472: jw.endMethod();
1473:
1474: jw.beginMethod("write", "java.io.OutputStream out",
1475: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
1476: geneol("write(out, null)");
1477: jw.endMethod();
1478:
1479: jw.beginMethod("write",
1480: "java.io.OutputStream out, String encoding",
1481: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
1482: geneol("java.io.Writer w");
1483: jw.beginIf("encoding == null");
1484: gen("encoding = \"UTF-8\"");
1485: eolNoI18N();
1486: end();
1487: gen("w = new java.io.BufferedWriter(new java.io.OutputStreamWriter(out, encoding))");
1488: eol();
1489: geneol("write(w, encoding)");
1490: geneol("w.flush()");
1491: jw.endMethod();
1492:
1493: jw
1494: .bigComment("Print this Java Bean to @param out including an XML header.\n@param encoding is the encoding style that @param out was opened with.");
1495: jw.beginMethod("write",
1496: "java.io.Writer out, String encoding",
1497: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
1498: gen("out.write(\"<?xml version='1.0'\")");
1499: eolNoI18N();
1500: gen("if (encoding != null)");
1501: cr();
1502: tabIn();
1503: gen("out.write(\" encoding='\"+encoding+\"'\")");
1504: eolNoI18N();
1505: gen("out.write(\" ?>\\n\")");
1506: eolNoI18N();
1507: if (config.isProcessDocType()) {
1508: jw.beginIf("docType != null");
1509: jw.writeEol("out.write(docType.toString())");
1510: jw.writeEol("out.write(\"\\n\")");
1511: jw.end();
1512: }
1513: jw.write("writeNode(out, \"", beanName, "\", \"\")");
1514: eolNoI18N();
1515: jw.endMethod();
1516: }
1517:
1518: jw.beginMethod("writeNode", "java.io.Writer out",
1519: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
1520: jw.writeEol("String myName");
1521: if (config.isGenerateParentRefs())
1522: jw.beginIf("parent == null");
1523: jw.writeEol("myName = \"", beanName, "\"");
1524: if (config.isGenerateParentRefs()) {
1525: jw.endElseBegin();
1526: jw.writeEol("myName = parent.nameChild(this, false, true)");
1527: jw.beginIf("myName == null");
1528: jw.writeEol("myName = \"", beanName, "\"");
1529: jw.end();
1530: jw.end();
1531: }
1532: jw.write("writeNode(out, myName, \"\")");
1533: eolNoI18N();
1534: jw.endMethod();
1535:
1536: jw.beginMethod("writeNode",
1537: "java.io.Writer out, String nodeName, String indent",
1538: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
1539: jw
1540: .write("writeNode(out, nodeName, null, indent, new java.util.HashMap");
1541: if (config.jdkTarget >= 150)
1542: jw.write("<String, String>");
1543: jw.writeEol("())");
1544: jw.endMethod();
1545:
1546: jw
1547: .bigComment("It's not recommended to call this method directly.");
1548: String writeNodeArgs = "java.io.Writer out, String nodeName, String namespace, String indent, java.util.Map";
1549: if (config.jdkTarget >= 150)
1550: writeNodeArgs += "<String, String>";
1551: writeNodeArgs += " namespaceMap";
1552: jw.beginMethod("writeNode", writeNodeArgs,
1553: "java.io.IOException", "void", jw.PUBLIC | jw.IO
1554: | jw.UNSUPPORTED);
1555: int size = attrList.size();
1556: Map namespaceToPrefixTable = new HashMap();
1557: geneol("out.write(indent)");
1558: outWrite("<");
1559: jw.beginIf("namespace != null");
1560: jw.writeEol("out.write((String)namespaceMap.get(namespace))");
1561: outWrite(":");
1562: jw.end();
1563: geneol("out.write(nodeName)");
1564: if (beanElement.isRoot && getDefaultNamespace() != null) {
1565: jw.writeEolNoI18N("out.write(\" xmlns='\")");
1566: jw.writeEolNoI18N("out.write("
1567: + JavaUtil.instanceFrom("java.lang.String",
1568: getDefaultNamespace()) + ")");
1569: jw.writeEolNoI18N("out.write(\"'\")");
1570: }
1571: if (beanElement.isRoot) {
1572: jw.beginIf("schemaLocation != null");
1573: jw
1574: .writeEol("namespaceMap.put(\"http://www.w3.org/2001/XMLSchema-instance\", \"xsi\")");
1575: namespaceToPrefixTable.put(
1576: "http://www.w3.org/2001/XMLSchema-instance", "xsi");
1577: jw
1578: .writeEol("out.write(\" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='\")");
1579: jw.writeEol("out.write(schemaLocation)");
1580: jw.writeEolNoI18N("out.write(\"'\")");
1581: jw.end();
1582: }
1583:
1584: // Deal with namespaces
1585: boolean firstNS = true;
1586: for (int i = 0; i < size; i++) {
1587: Property a = (Property) attrList.get(i);
1588: boolean indexed = a.isIndexed();
1589: String attr = "_" + a.name;
1590: String type = a.getType().intern();
1591: String baseType = type;
1592: if (indexed)
1593: type = (baseType + "[]").intern();
1594: if (isTypeQName(baseType)) {
1595: if (firstNS) {
1596: firstNS = false;
1597: jw.comment("Work out any namespaces.");
1598: jw.writeEol("boolean firstNSAddition = true");
1599: }
1600: if (indexed) {
1601: beginAttrIterator(attr, a, "element");
1602: attr = "element";
1603: }
1604: jw
1605: .beginIf(
1606: attr
1607: + " != null && "
1608: + attr
1609: + ".getNamespaceURI() != null && !\"\".equals(",
1610: attr, ".getNamespaceURI())");
1611: jw.writeEol(
1612: "String prefix = (String) namespaceMap.get(",
1613: attr, ".getNamespaceURI())");
1614: jw.beginIf("prefix == null || \"\".equals(prefix)");
1615: jw.writeEol("prefix = ", attr, ".getPrefix()");
1616: jw.beginIf("prefix == null || \"\".equals(prefix)");
1617: jw.writeEol("prefix = \"", a.dtdName, "_ns__\"");
1618: jw.end();
1619: jw
1620: .comment("Need to make sure it's a unique prefix too.");
1621: jw.writeEol("boolean changed");
1622: jw.write("do ");
1623: jw.begin();
1624: jw.writeEol("changed = false");
1625: jw
1626: .beginFor(
1627: "java.util.Iterator valueIt = namespaceMap.values().iterator()",
1628: "valueIt.hasNext()", "");
1629: jw
1630: .writeEol("String otherPrefix = (String) valueIt.next()");
1631: jw.beginIf("prefix.equals(otherPrefix)");
1632: jw.writeEol("prefix += \"_\"");
1633: jw.writeEol("changed = true");
1634: jw.end();
1635: jw.end();
1636: jw.end(false);
1637: jw.writeEol(" while (changed)");
1638: jw.beginIf("firstNSAddition");
1639: jw.writeEol("firstNSAddition = false");
1640: jw.comment("Copy on write");
1641: jw
1642: .writeEol("namespaceMap = new java.util.HashMap(namespaceMap)");
1643: jw.end();
1644: jw.writeEol("namespaceMap.put(", attr,
1645: ".getNamespaceURI(), prefix)");
1646: jw.writeEol("out.write(\" xmlns:\")");
1647: jw.writeEol("out.write(prefix)");
1648: jw.writeEol("out.write(\"='\")");
1649: jw.writeEol("out.write(", attr, ".getNamespaceURI())");
1650: jw.writeEol("out.write(\"'\")");
1651: jw.end();
1652: jw.end();
1653: if (indexed)
1654: jw.end();
1655: } else if (a.getNamespace() != null
1656: && !a.getNamespace().equals(getDefaultNamespace())
1657: && !a.getNamespace().equals(
1658: "http://www.w3.org/XML/1998/namespace")) {
1659: if (namespaceToPrefixTable
1660: .containsKey(a.getNamespace()))
1661: continue;
1662: if (firstNS) {
1663: firstNS = false;
1664: jw.comment("Work out any namespaces.");
1665: jw.writeEol("boolean firstNSAddition = true");
1666: }
1667: String prefix;
1668: prefix = SchemaRep.prefixOf(a.dtdName);
1669: if (prefix == null) {
1670: prefix = prefixGuesser.guessPrefixFromURI(a
1671: .getNamespace());
1672: //config.messageOut.println("Guessing prefix for "+a.getNamespace()+" is "+prefix);
1673: }
1674: jw.beginIf("namespaceMap.get(", JavaUtil.instanceFrom(
1675: "String", a.getNamespace()), ") == null");
1676: jw.beginIf("firstNSAddition");
1677: jw.writeEol("firstNSAddition = false");
1678: jw.comment("Copy on write");
1679: jw.write("namespaceMap = new java.util.HashMap");
1680: if (config.jdkTarget >= 150)
1681: jw.write("<String, String>");
1682: jw.writeEol("(namespaceMap)");
1683: jw.end();
1684: jw.writeEol("namespaceMap.put(", JavaUtil.instanceFrom(
1685: "String", a.getNamespace()), ", \"", prefix
1686: + "\")");
1687: outWrite(" xmlns:" + prefix + "='");
1688: outWrite(a.getNamespace());
1689: outWrite("'");
1690: jw.end();
1691: namespaceToPrefixTable.put(a.getNamespace(), prefix);
1692: }
1693: }
1694: List directAttributes = new LinkedList(); // List<Property>
1695: List nonDirectAttributes = new LinkedList(); // List<Property>
1696: boolean hasSubElements = false;
1697: if (size > 0) {
1698: // Go over the attributes
1699: for (int i = 0; i < size; i++) {
1700: Property a = (Property) attrList.get(i);
1701: if (!a.isAttribute()) {
1702: hasSubElements = true;
1703: continue;
1704: }
1705: if (!a.isDirectChild()) {
1706: nonDirectAttributes.add(a);
1707: continue;
1708: }
1709: directAttributes.add(a);
1710: }
1711: }
1712: jw
1713: .writeEol("writeNodeAttributes(out, nodeName, namespace, indent, namespaceMap)");
1714:
1715: if (hasSubElements) {
1716: geneol("out.write(\">\\n\")");
1717: } else {
1718: }
1719:
1720: jw
1721: .writeEol("writeNodeChildren(out, nodeName, namespace, indent, namespaceMap)");
1722: if (hasSubElements) {
1723: geneol("out.write(indent)");
1724: outWrite("</");
1725: jw.beginIf("namespace != null");
1726: jw
1727: .writeEol("out.write((String)namespaceMap.get(namespace))");
1728: outWrite(":");
1729: jw.end();
1730: jw.writeEol("out.write(nodeName)");
1731: outWrite(">\n");
1732: } else {
1733: geneol("out.write(\"/>\\n\")");
1734: }
1735: jw.endMethod(); // writeNode
1736:
1737: jw.beginMethod("writeNodeAttributes", writeNodeArgs,
1738: "java.io.IOException", "void", jw.PROTECTED | jw.IO
1739: | jw.UNSUPPORTED);
1740: for (Iterator it = directAttributes.iterator(); it.hasNext();) {
1741: Property prop = (Property) it.next();
1742: genWriteAttr(prop);
1743: }
1744: if (config.isRespectExtension()
1745: && beanElement.getExtension() != null) {
1746: comment("extension is: " + beanElement.getExtension());
1747: jw
1748: .writeEol("super.writeNodeAttributes(out, nodeName, namespace, indent, namespaceMap)");
1749: }
1750: jw.endMethod();
1751:
1752: /*
1753: String writeNodeArgs = "java.io.Writer out, String nodeName, String namespace, String indent, java.util.Map";
1754: if (config.jdkTarget >= 150)
1755: writeNodeArgs += "<String, String>";
1756: writeNodeArgs += " namespaceMap";
1757: */
1758: jw.beginMethod("writeNodeChildren", writeNodeArgs,
1759: "java.io.IOException", "void", jw.PROTECTED | jw.IO
1760: | jw.UNSUPPORTED);
1761: if (config.isRespectExtension()
1762: && beanElement.getExtension() != null) {
1763: jw
1764: .writeEol("super.writeNodeChildren(out, nodeName, namespace, indent, namespaceMap)");
1765: }
1766: if (hasSubElements) {
1767: geneol("String nextIndent = indent + \"" + jw.getIndent()
1768: + "\"");
1769: if (config.isKeepElementPositions()) {
1770: jw.beginFor(
1771: "int position = 0, count = fetchChildCount()",
1772: "position < count", "++position");
1773: jw
1774: .writeEol("java.lang.Object child = elementsByPosition[position]");
1775: jw
1776: .writeEol("int elementType = elementTypesByPosition[position]");
1777: jw.write("switch (elementType) ");
1778: jw.begin();
1779: }
1780: boolean firstUseOfIndex = true;
1781: for (int i = 0; i < size; i++) {
1782: Property a = (Property) attrList.get(i);
1783: if (a.isAttribute())
1784: continue;
1785: boolean indexed = a.isIndexed();
1786: String attr = "_" + a.name;
1787: String baseAttr = attr;
1788:
1789: boolean isScalar = a.isScalar();
1790: boolean isNamespaceSignificant = (a.getNamespace() != null
1791: && !a.getNamespace().equals(
1792: getDefaultNamespace()) && !a
1793: .getNamespace().equals(
1794: "http://www.w3.org/XML/1998/namespace"));
1795:
1796: String type = a.getType().intern();
1797: String baseType = type;
1798: if (indexed)
1799: type = (baseType + "[]").intern();
1800:
1801: if (config.isKeepElementPositions()) {
1802: jw.writecr("case " + i + ":");
1803: jw.indentRight();
1804: jw.writeEol(baseType + " a" + a.name + " = "
1805: + JavaUtil.fromObject(baseType, "child"));
1806: attr = "a" + a.name;
1807: } else {
1808: if (indexed) {
1809: if (!a.isBean && a.attributes != null
1810: && a.attributes.length > 0) {
1811: if (firstUseOfIndex) {
1812: firstUseOfIndex = false;
1813: jw.writeEol("int index = 0");
1814: } else {
1815: jw.writeEol("index = 0");
1816: }
1817: }
1818: beginAttrIterator(attr, a, "element");
1819: attr = "element";
1820: }
1821: if (!isScalar) {
1822: jw.beginIf(attr, " != null");
1823: } else if (!indexed && config.isOptionalScalars()
1824: && isScalar) {
1825: jw.beginIf(a.getScalarIsSet());
1826: }
1827: }
1828: if (a.isBean) {
1829: jw.write(attr, ".writeNode(out, \"", a.dtdName);
1830: jw.write("\", ");
1831: if (isNamespaceSignificant)
1832: jw.write(JavaUtil.instanceFrom("String", a
1833: .getNamespace()));
1834: else
1835: jw.write("null");
1836: jw.writeEol(", nextIndent, namespaceMap)");
1837: } else if (a.type == Common.TYPE_COMMENT) {
1838: jw.writeEol("out.write(nextIndent)");
1839: jw.writeEol("out.write(\"<!--\")");
1840: jw.writeEol("out.write(", attr, ")");
1841: jw.writeEol("out.write(\"-->\\n\")");
1842: } else if ("org.w3c.dom.Element".equals(type)) {
1843: jw.writeEol("out.write(nextIndent)");
1844: if (config.isUseRuntime()) {
1845: jw
1846: .writeEol("org.netbeans.modules.schema2beans.XMLUtil.DOMWriter domWriter = new org.netbeans.modules.schema2beans.XMLUtil.DOMWriter()");
1847: jw.writeEol("domWriter.setWriter(out)");
1848: jw.writeEol("domWriter.write(", attr, ")");
1849: } else {
1850: jw.writeEol("out.write(", attr, ".toString())");
1851: }
1852: outWrite("\n");
1853: } else {
1854: boolean needEndTag = true;
1855: if (baseType == "boolean" && a.getCanBeEmpty()) {
1856: jw.beginIf(attr);
1857: needEndTag = false;
1858: }
1859: if (!a.dtdName.equals("#PCDATA")) {
1860: jw.writeEol("out.write(nextIndent)");
1861: if (isNamespaceSignificant) {
1862: outWrite("<");
1863: jw
1864: .writeEol(
1865: "out.write((String)namespaceMap.get(",
1866: JavaUtil.instanceFrom(
1867: "String",
1868: a.getNamespace()),
1869: "))");
1870: jw.write("out.write(\":", a.dtdName, "\")");
1871: } else {
1872: jw.write("out.write(\"<", a.dtdName, "\")");
1873: }
1874: eolNoI18N();
1875: if (isTypeQName(baseType)) {
1876: jw.writeEol("String nsPrefix", attr,
1877: " = null");
1878: jw
1879: .beginIf(
1880: attr
1881: + ".getNamespaceURI() != null && !\"\".equals(",
1882: attr, ".getNamespaceURI())");
1883: jw.write("nsPrefix", attr,
1884: " = (String) namespaceMap.get(");
1885: jw.writeEol(attr, ".getNamespaceURI())");
1886: jw.end();
1887: }
1888: for (int attrNum = 0; attrNum < a.attributes.length; ++attrNum) {
1889: AttrProp myAttrProp = a.attributes[attrNum];
1890: //jw.comment("myAttrProp="+myAttrProp);
1891: for (Iterator it = nonDirectAttributes
1892: .iterator(); it.hasNext();) {
1893: Property attrProperty = (Property) it
1894: .next();
1895: AttrProp attrProp = attrProperty
1896: .getAttrProp();
1897: if (myAttrProp == attrProp) {
1898: String varName;
1899: if (attrProperty.isIndexed()) {
1900: jw.beginIf("index < size"
1901: + attrProperty.name
1902: + "()");
1903: varName = attrProperty
1904: .getReadMethod(true)
1905: + "(index)";
1906: } else
1907: varName = "_"
1908: + attrProperty.name;
1909: genWriteAttr(attrProperty, varName);
1910: if (attrProperty.isIndexed()) {
1911: jw.end();
1912: }
1913: }
1914: }
1915: }
1916: if (needEndTag) {
1917: gen("out.write(\">\")");
1918: eolNoI18N();
1919: }
1920: }
1921: if (!needEndTag) {
1922: // no end tag, then no contents
1923: } else {
1924: genWriteType(a, attr, false);
1925: }
1926: if (!needEndTag) {
1927: gen("out.write(\"/>\\n\")");
1928: eolNoI18N();
1929: end();
1930: } else if (!a.dtdName.equals("#PCDATA")) {
1931: if (isNamespaceSignificant) {
1932: outWrite("</");
1933: jw
1934: .writeEol(
1935: "out.write((String)namespaceMap.get(",
1936: JavaUtil.instanceFrom(
1937: "String",
1938: a.getNamespace()),
1939: "))");
1940: jw.write("out.write(\":", a.dtdName,
1941: ">\\n\")");
1942: } else {
1943: jw.write("out.write(\"</" + a.dtdName,
1944: ">\\n\")");
1945: }
1946: eolNoI18N();
1947: }
1948: }
1949: if (config.isKeepElementPositions()) {
1950: jw.writeEol("break");
1951: jw.indentLeft();
1952: } else {
1953: if (a.isNillable()) {
1954: if (!isScalar) {
1955: jw.endElseBegin();
1956: jw.writeEol("out.write(nextIndent)");
1957: jw
1958: .writeEol("out.write(\"<",
1959: a.dtdName,
1960: " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true'/>\\n\")");
1961: }
1962: }
1963: if (!isScalar
1964: || (!indexed && config.isOptionalScalars() && isScalar))
1965: end();
1966: if (indexed) {
1967: if (!a.isBean && a.attributes != null
1968: && a.attributes.length > 0) {
1969: jw.writeEol("++index");
1970: }
1971: end();
1972: }
1973: }
1974: }
1975: if (config.isKeepElementPositions()) {
1976: jw.end();
1977: jw.end();
1978: }
1979: }
1980: jw.endMethod(); // writeNodeChildren
1981:
1982: // Generate for reading
1983: if (beanElement.isRoot) {
1984: String exceps = "javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException";
1985: if (config.isVetoable())
1986: exceps += ", java.beans.PropertyVetoException";
1987:
1988: if (!config.isStandalone()) {
1989: jw.beginMethod("read",
1990: "org.openide.filesystems.FileObject fo",
1991: exceps, className, jw.PUBLIC | jw.STATIC
1992: | jw.IO);
1993: jw
1994: .writeEol("java.io.InputStream in = fo.getInputStream()");
1995: jw.beginTry();
1996: jw.writeEol("return read(in)");
1997: jw.endFinallyBegin();
1998: jw.writeEol("in.close()");
1999: jw.end();
2000: jw.endMethod();
2001: }
2002:
2003: jw.beginMethod("read", "java.io.File f", exceps, className,
2004: jw.PUBLIC | jw.STATIC | jw.IO);
2005: jw
2006: .writeEol("java.io.InputStream in = new java.io.FileInputStream(f)");
2007: jw.beginTry();
2008: jw.writeEol("return read(in)");
2009: jw.endFinallyBegin();
2010: jw.writeEol("in.close()");
2011: jw.end();
2012: jw.endMethod();
2013:
2014: jw.beginMethod("read", "java.io.InputStream in", exceps,
2015: className, jw.PUBLIC | jw.STATIC | jw.IO);
2016: geneol("return read(new org.xml.sax.InputSource(in), false, null, null)");
2017: end();
2018: cr();
2019:
2020: jw
2021: .bigComment("Warning: in readNoEntityResolver character and entity references will\nnot be read from any DTD in the XML source.\nHowever, this way is faster since no DTDs are looked up\n(possibly skipping network access) or parsed.");
2022: jw.beginMethod("readNoEntityResolver",
2023: "java.io.InputStream in", exceps, className,
2024: jw.PUBLIC | jw.STATIC | jw.IO);
2025: gencr("return read(new org.xml.sax.InputSource(in), false,");
2026: tabIn();
2027: gen("new org.xml.sax.EntityResolver() ");
2028: begin();
2029: gen("public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) ");
2030: begin();
2031: geneol("java.io.ByteArrayInputStream bin = new java.io.ByteArrayInputStream(new byte[0])");
2032: geneol("return new org.xml.sax.InputSource(bin)");
2033: end();
2034: end();
2035: tabIn();
2036: geneol(", null)");
2037: end();
2038: cr();
2039:
2040: jw
2041: .beginMethod(
2042: "read",
2043: "org.xml.sax.InputSource in, boolean validate, org.xml.sax.EntityResolver er, org.xml.sax.ErrorHandler eh",
2044: exceps, className, jw.PUBLIC | jw.STATIC
2045: | jw.IO);
2046: geneol("javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance()");
2047: geneol("dbf.setValidating(validate)");
2048: geneol("dbf.setNamespaceAware(true)");
2049: geneol("javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder()");
2050: gen("if (er != null)");
2051: tabIn();
2052: geneol("db.setEntityResolver(er)");
2053: gen("if (eh != null)");
2054: tabIn();
2055: geneol("db.setErrorHandler(eh)");
2056: geneol("org.w3c.dom.Document doc = db.parse(in)");
2057: geneol("return read(doc)");
2058: end();
2059: cr();
2060:
2061: exceps = null;
2062: if (config.isVetoable())
2063: exceps = "java.beans.PropertyVetoException";
2064: jw.beginMethod("read", "org.w3c.dom.Document document",
2065: exceps, className, jw.PUBLIC | jw.STATIC | jw.IO);
2066: jw.writeEol(className, " a", className, " = new "
2067: + className + "()");
2068: jw.writeEol("a", className, ".readFromDocument(document)");
2069: jw.writeEol("return a", className);
2070: jw.endMethod();
2071:
2072: jw.beginMethod("readFromDocument",
2073: "org.w3c.dom.Document document", exceps, "void",
2074: jw.PROTECTED);
2075: if (config.isProcessDocType()) {
2076: String fullDocTypeName;
2077: if (packageName == null)
2078: fullDocTypeName = className + ".DocType";
2079: else
2080: fullDocTypeName = packageName + "." + className
2081: + ".DocType";
2082: jw
2083: .writeEol("org.w3c.dom.NodeList children = document.getChildNodes()");
2084: jw.writeEol("int length = children.getLength()");
2085: jw.beginFor("int i = 0", "i < length", "++i");
2086: jw
2087: .beginIf("children.item(i) instanceof org.w3c.dom.DocumentType");
2088: jw
2089: .writeEol("docType = new "
2090: + fullDocTypeName
2091: + "((org.w3c.dom.DocumentType)children.item(i))");
2092: jw.writeEol("break");
2093: jw.end();
2094: jw.end();
2095: }
2096: geneol("readNode(document.getDocumentElement())");
2097: end();
2098: cr();
2099: }
2100:
2101: if (beanElement.isRoot) {
2102: jw.write("protected static class ReadState ");
2103: jw.begin();
2104: jw.writeEol("int lastElementType");
2105: jw.writeEol("int elementPosition");
2106: jw.end();
2107: jw.cr();
2108: }
2109:
2110: jw.beginMethod("readNode", "org.w3c.dom.Node node", config
2111: .isVetoable() ? "java.beans.PropertyVetoException"
2112: : null, "void", jw.PUBLIC | jw.IO);
2113: jw.write("readNode(node, new java.util.HashMap");
2114: if (config.jdkTarget >= 150)
2115: jw.write("<String, String>");
2116: jw.writeEol("())");
2117: jw.endMethod();
2118:
2119: String namespacePrefixesFormalParameter;
2120: if (config.jdkTarget >= 150)
2121: namespacePrefixesFormalParameter = "java.util.Map<String, String> namespacePrefixes";
2122: else
2123: namespacePrefixesFormalParameter = "java.util.Map namespacePrefixes";
2124:
2125: String readNodeArgs = "org.w3c.dom.Node node, "
2126: + namespacePrefixesFormalParameter;
2127: jw
2128: .beginMethod(
2129: "readNode",
2130: readNodeArgs,
2131: config.isVetoable() ? "java.beans.PropertyVetoException"
2132: : null, "void", jw.PUBLIC | jw.IO);
2133: // Go over attributes
2134: beginAttrProcessing("node");
2135: jw.writeEol("boolean firstNamespaceDef = true");
2136: genUpdateNamespaces("namespacePrefixes", "firstNamespaceDef");
2137:
2138: int attrCount = 0;
2139: if (beanElement.isRoot) {
2140: ++attrCount;
2141: jw.writeEol("String xsiPrefix = \"xsi\"");
2142: jw
2143: .beginFor(
2144: "java.util.Iterator it = namespacePrefixes.keySet().iterator()",
2145: "it.hasNext()", "");
2146: jw.writeEol("String prefix = (String) it.next()");
2147: jw
2148: .writeEol("String ns = (String) namespacePrefixes.get(prefix)");
2149: jw
2150: .beginIf("\"http://www.w3.org/2001/XMLSchema-instance\".equals(ns)");
2151: jw.writeEol("xsiPrefix = prefix");
2152: jw.writeEol("break");
2153: jw.end();
2154: jw.end();
2155: genReadAttr("schemaLocation", "String",
2156: "\"+xsiPrefix+\":schemaLocation", "node", null,
2157: false, null, false, null);
2158: }
2159: jw
2160: .writeEol("readNodeAttributes(node, namespacePrefixes, attrs)");
2161: jw.end();
2162: jw.writeEol("readNodeChildren(node, namespacePrefixes)");
2163: jw.endMethod();
2164:
2165: jw.beginMethod("readNodeAttributes", readNodeArgs
2166: + ", org.w3c.dom.NamedNodeMap attrs", config
2167: .isVetoable() ? "java.beans.PropertyVetoException"
2168: : null, "void", jw.PROTECTED | jw.IO);
2169: if (config.isRespectExtension()
2170: && beanElement.getExtension() != null)
2171: jw
2172: .writeEol("super.readNodeAttributes(node, namespacePrefixes, attrs)");
2173: jw.writeEol("org.w3c.dom.Attr attr");
2174: jw.writeEol("java.lang.String attrValue");
2175: boolean hasNillableElement = false;
2176: for (int i = 0; i < size; i++) {
2177: Property a = (Property) attrList.get(i);
2178: if (a.isNillable())
2179: hasNillableElement = true;
2180: if (!a.isAttribute())
2181: continue;
2182: if (!a.isDirectChild())
2183: continue;
2184: ++attrCount;
2185: genReadAttr(a, "node");
2186: }
2187: jw.endMethod();
2188:
2189: jw.beginMethod("readNodeChildren", readNodeArgs, config
2190: .isVetoable() ? "java.beans.PropertyVetoException"
2191: : null, "void", jw.PROTECTED | jw.IO);
2192: if (hasSubElements) {
2193: // FIXME: Should deal with the situation where we get 2 "foo" elements
2194: // and the schema only mentioned 1.
2195: Map dtdNames = new HashMap();
2196: boolean hasDuplicateDtdNames = config
2197: .isKeepElementPositions();
2198: boolean hasQNameType = false;
2199: boolean hasNonElementTypes = false;
2200: for (int i = 0; i < size; i++) {
2201: Property a = (Property) attrList.get(i);
2202: if (isTypeQName(a.getType()))
2203: hasQNameType = true;
2204: if (a.type == Common.TYPE_COMMENT
2205: || a.dtdName.equals("#PCDATA")) {
2206: hasNonElementTypes = true;
2207: continue;
2208: }
2209: if (a.isAttribute())
2210: continue;
2211: if (dtdNames.containsKey(a.dtdName)) {
2212: //jw.comment(a.dtdName+" has duplicates");
2213: hasDuplicateDtdNames = true;
2214: }
2215: dtdNames.put(a.dtdName, a);
2216: }
2217: geneol("org.w3c.dom.NodeList children = node.getChildNodes()");
2218: boolean hasReadState = false;
2219: if (hasDuplicateDtdNames || config.isKeepElementPositions()) {
2220: hasReadState = true;
2221: jw.writeEol(getRootClassName(),
2222: ".ReadState readState = new ",
2223: getRootClassName(), ".ReadState()");
2224: }
2225: if (hasDuplicateDtdNames)
2226: jw.writeEol("readState.lastElementType = -1");
2227: if (config.isKeepElementPositions()) {
2228: jw.writeEol("readState.elementPosition = 0");
2229: jw
2230: .writeEol("elementsByPosition = new java.lang.Object[children.getLength()]");
2231: jw
2232: .writeEol("elementTypesByPosition = new int[children.getLength()]");
2233: }
2234: jw.beginFor("int i = 0, size = children.getLength()",
2235: "i < size", "++i");
2236: geneol("org.w3c.dom.Node childNode = children.item(i)");
2237: if (!hasNonElementTypes
2238: && !(config.isRespectExtension() && beanElement
2239: .getExtension() != null)) {
2240: jw
2241: .beginIf("!(childNode instanceof org.w3c.dom.Element)");
2242: jw.writeEol("continue");
2243: jw.end();
2244: }
2245: geneol("String childNodeName = (childNode.getLocalName() == null ? childNode.getNodeName().intern() : childNode.getLocalName().intern())");
2246: geneol("String childNodeValue = \"\"");
2247: jw.beginIf("childNode.getFirstChild() != null");
2248: geneol("childNodeValue = childNode.getFirstChild().getNodeValue()");
2249: jw.end();
2250: jw
2251: .write("boolean recognized = readNodeChild(childNode, childNodeName, childNodeValue, namespacePrefixes");
2252: if (hasReadState)
2253: jw.write(", readState");
2254: jw.writeEol(")");
2255: jw.beginIf("!recognized");
2256: if (!config.isLogSuspicious()) {
2257: comment("Found extra unrecognized childNode");
2258: } else {
2259: jw.beginIf("childNode instanceof org.w3c.dom.Element");
2260: declareLogger();
2261: jw
2262: .writeEol("_logger.info(\"Found extra unrecognized childNode '\"+childNodeName+\"'\")");
2263: jw.end();
2264: }
2265: jw.end();
2266: jw.end();
2267: if (config.isKeepElementPositions()) {
2268: jw
2269: .beginFor(
2270: "",
2271: "readState.elementPosition < elementTypesByPosition.length",
2272: "++readState.elementPosition");
2273: jw
2274: .writeEol("elementTypesByPosition[readState.elementPosition] = "
2275: + elementTypeSetnull);
2276: jw.end();
2277: }
2278: jw.endMethod();
2279:
2280: jw
2281: .beginMethod(
2282: "readNodeChild",
2283: "org.w3c.dom.Node childNode, String childNodeName, String childNodeValue, "
2284: + namespacePrefixesFormalParameter
2285: + (hasReadState ? ", "
2286: + getRootClassName()
2287: + ".ReadState readState"
2288: : ""),
2289: config.isVetoable() ? "java.beans.PropertyVetoException"
2290: : null, "boolean", jw.PROTECTED
2291: | jw.IO);
2292: jw
2293: .comment("assert childNodeName == childNodeName.intern()");
2294: if (hasNillableElement || hasQNameType
2295: || nonDirectAttributes.size() > 0)
2296: declareAttrsForRead("childNode");
2297: boolean first = true;
2298: Property anyProp = null; // Is this an any property?
2299: for (int i = 0; i < size; i++) {
2300: Property a = (Property) attrList.get(i);
2301: if (a.isAttribute())
2302: continue;
2303: boolean indexed = a.isIndexed();
2304: String attr = "_" + a.name;
2305: String baseAttr = attr;
2306: boolean isScalar = a.isScalar();
2307:
2308: String type = a.getType().intern();
2309: String baseType = type;
2310: if (indexed)
2311: type = (baseType + "[]").intern();
2312:
2313: if (baseType == "org.w3c.dom.Element") {
2314: anyProp = a;
2315: continue;
2316: }
2317: if (first)
2318: first = false;
2319: else
2320: gen("else ");
2321: jw.write("if (");
2322: if (hasDuplicateDtdNames
2323: && dtdNames.get(a.dtdName) != a) {
2324: int alwaysBeforeElementType;
2325: if (indexed) {
2326: // Keep reading in elements into this slot, until
2327: // there's another element that says to go on.
2328: alwaysBeforeElementType = i + 1;
2329: } else {
2330: // Force it to goto the next slot.
2331: alwaysBeforeElementType = i;
2332: }
2333: jw.write("readState.lastElementType < "
2334: + alwaysBeforeElementType, " && ");
2335: }
2336: if (a.dtdName.equals("#PCDATA"))
2337: jw
2338: .write("childNode instanceof org.w3c.dom.CharacterData");
2339: else if (a.type == Common.TYPE_COMMENT)
2340: jw
2341: .write("childNode instanceof org.w3c.dom.Comment");
2342: else
2343: jw.write("childNodeName == \"" + a.dtdName + "\"");
2344: jw.write(") ");
2345: begin();
2346: String var;
2347: if (a.isNillable()) {
2348: jw
2349: .writeEol("org.w3c.dom.Attr nilAttr = (org.w3c.dom.Attr) attrs.getNamedItem(\"xsi:nil\")");
2350: jw
2351: .beginIf("nilAttr == null || !\"true\".equals(nilAttr.getValue())");
2352: }
2353: if (indexed) {
2354: var = "a" + a.name;
2355: if (a.isBean) {
2356: jw.write(baseType, " ");
2357: } else {
2358: geneol(baseType + " " + var);
2359: }
2360: } else {
2361: var = attr;
2362: }
2363: if (a.isBean) {
2364: jw.write(var, " = ");
2365: genNewDefault(a, true);
2366: jw.eol();
2367: if (config.isGeneratePropertyEvents()) {
2368: geneol(var
2369: + "._setPropertyChangeSupport(eventListeners)");
2370: if (config.isVetoable())
2371: geneol(var
2372: + "._setVetoableChangeSupport(vetos)");
2373: }
2374: if (config.isGenerateParentRefs())
2375: jw.writeEol(var + "._setParent(this)");
2376: }
2377: boolean generatedSet = true;
2378: if (a.isBean)
2379: geneol(var
2380: + ".readNode(childNode, namespacePrefixes)");
2381: else if (a.dtdName.equals("#PCDATA")) {
2382: geneol(var
2383: + " = ((org.w3c.dom.CharacterData)childNode).getData()");
2384: } else if (a.type == Common.TYPE_COMMENT) {
2385: geneol(var
2386: + " = ((org.w3c.dom.CharacterData)childNode).getData()");
2387: } else {
2388: if (config.isTrimNonStrings()
2389: && baseType != "String"
2390: && baseType != "java.lang.String") {
2391: jw
2392: .writeEol("childNodeValue = childNodeValue.trim()");
2393: }
2394: List exceps = JavaUtil
2395: .exceptionsFromParsingText(baseType);
2396: if (!exceps.isEmpty()) {
2397: jw.beginTry();
2398: }
2399: if (baseType == "boolean"
2400: || baseType == "java.lang.Boolean") {
2401: gencr("if (childNode.getFirstChild() == null)");
2402: tabIn();
2403: if (baseType == "boolean")
2404: geneol(var + " = true");
2405: else
2406: geneol(var + " = Boolean.TRUE");
2407: gencr("else");
2408: tabIn();
2409: }
2410: generatedSet = genReadType(
2411: baseType,
2412: var,
2413: "childNodeValue",
2414: false,
2415: null,
2416: isScalar,
2417: (SchemaRep.EncodingStyle) a
2418: .searchExtraData(SchemaRep.EncodingStyle.class));
2419: SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a
2420: .searchExtraData(SchemaRep.WhiteSpace.class);
2421: if (ws != null)
2422: genWhiteSpaceRestriction(ws, var, baseType);
2423: //}
2424: if (!indexed && config.isOptionalScalars()
2425: && isScalar) {
2426: jw.writeEol(a.getScalarIsSet(), " = true");
2427: }
2428: int directAttrCount = 0;
2429: for (int attrNum = 0; attrNum < a.attributes.length; ++attrNum) {
2430: AttrProp myAttrProp = a.attributes[attrNum];
2431: //jw.comment("myAttrProp="+myAttrProp);
2432: for (Iterator it = nonDirectAttributes
2433: .iterator(); it.hasNext();) {
2434: Property attrProperty = (Property) it
2435: .next();
2436: AttrProp attrProp = attrProperty
2437: .getAttrProp();
2438: if (myAttrProp == attrProp) {
2439: ++directAttrCount;
2440: genReadAttr(attrProperty, "childNode");
2441: }
2442: }
2443: }
2444:
2445: if (!exceps.isEmpty()) {
2446: end();
2447: genRethrowExceptions(exceps);
2448: }
2449: }
2450: if (indexed && generatedSet)
2451: jw.writeEol(attr, ".add(", JavaUtil.toObject(var,
2452: baseType, config.isForME()), ")");
2453: if (a.isNillable()) {
2454: jw.endElseBegin();
2455: if (indexed)
2456: jw.writeEol(attr, ".add(null)");
2457: else
2458: jw.writeEol(var, " = ", JavaUtil
2459: .nullValueForType(baseType));
2460: jw.end();
2461: }
2462: if (hasDuplicateDtdNames)
2463: jw.writeEol("readState.lastElementType = " + i);
2464: if (config.isKeepElementPositions()) {
2465: jw
2466: .writeEol("elementsByPosition[readState.elementPosition] = "
2467: + JavaUtil.toObject(var, type,
2468: config.isForME()));
2469: jw
2470: .writeEol("elementTypesByPosition[readState.elementPosition++] = "
2471: + i);
2472: }
2473: end();
2474: }
2475: if (anyProp != null) {
2476: String attr = "_" + anyProp.name;
2477: boolean indexed = anyProp.isIndexed();
2478: if (first)
2479: first = false;
2480: else
2481: gen("else ");
2482: jw.beginIf("childNode instanceof org.w3c.dom.Element");
2483: if (indexed)
2484: jw.writeEol(attr,
2485: ".add((org.w3c.dom.Element)childNode)");
2486: else
2487: jw.writeEol(attr,
2488: " = (org.w3c.dom.Element) childNode");
2489: jw.end();
2490: }
2491: if (!first) {
2492: gen("else ");
2493: begin();
2494: if (config.isRespectExtension()
2495: && beanElement.getExtension() != null) {
2496: jw
2497: .write("return super.readNodeChild(childNode, childNodeName, childNodeValue, namespacePrefixes");
2498: if (hasReadState)
2499: jw.write(", readState");
2500: jw.writeEol(")");
2501: } else
2502: jw.writeEol("return false");
2503: /*
2504: jw.writeEol("System.out.println(\"Found extra unrecognized childNode: \"+childNodeName)");
2505: jw.writeEol("System.out.println(\"namespaceURI=\"+childNode.getNamespaceURI())");
2506: jw.writeEol("System.out.println(\"prefix=\"+childNode.getPrefix())");
2507: jw.writeEol("System.out.println(\"localName=\"+childNode.getLocalName())");
2508: jw.writeEol("System.out.println(\"nodeName=\"+childNode.getNodeName())");
2509: */
2510: end();
2511: }
2512: jw.writeEol("return true");
2513: } else {
2514: if (config.isRespectExtension()
2515: && beanElement.getExtension() != null) {
2516: jw
2517: .writeEol("super.readNodeChildren(node, namespacePrefixes)");
2518: }
2519: }
2520: jw.endMethod();
2521:
2522: if (beanElement.isRoot) {
2523: if (!config.isUseRuntime())
2524: genPrintXML();
2525: genSpecialTypes();
2526: }
2527: }
2528:
2529: protected void genFetchXMLEventReader() throws IOException {
2530: jw.select(BODY_SECTION);
2531: String commonBean = commonBeanType();
2532: String beanName = beanElement.node.getName();
2533: if (beanElement.isRoot) {
2534: String beanQNameName = declareQName(beanElement
2535: .getNamespace(), beanName, null);
2536:
2537: jw.beginMethod("fetchXMLEventReader", "", null,
2538: "javax.xml.stream.XMLEventReader");
2539: jw.writeEol("return new ", getRootClassName()
2540: + ".XMLEventStateManager(this, ", beanQNameName,
2541: ")");
2542: jw.endMethod();
2543:
2544: jw
2545: .write("static class XMLEventStateManager implements javax.xml.stream.XMLEventReader");
2546: jw.begin();
2547: jw.writeEol("private java.util.Stack<" + commonBean
2548: + "> beans = new java.util.Stack<" + commonBean
2549: + ">()");
2550: jw
2551: .writeEol("private java.util.Stack<javax.xml.namespace.QName> nodeNames = new java.util.Stack<javax.xml.namespace.QName>()");
2552: jw
2553: .writeEol("private java.util.Stack<Integer> eventNumbers = new java.util.Stack<Integer>()");
2554: jw
2555: .writeEol("private java.util.Stack<Integer> arrayIndices = new java.util.Stack<Integer>()");
2556: jw
2557: .writeEol("private java.util.Stack<Integer> elementNumbers = new java.util.Stack<Integer>()");
2558: jw.writeEol("private " + commonBean + " currentBean");
2559: jw
2560: .writeEol("private javax.xml.namespace.QName currentNodeName");
2561: jw.writeEol("private int currentEventNumber");
2562: jw.writeEol("private int currentArrayIndex");
2563: jw.writeEol("private int currentElementNumber");
2564: jw
2565: .writeEol("private javax.xml.stream.events.XMLEvent next");
2566: jw.cr();
2567: jw.write("public XMLEventStateManager(" + commonBean
2568: + " bean, javax.xml.namespace.QName nodeName)");
2569: jw.begin();
2570: jw.writeEol("currentBean = bean");
2571: jw.writeEol("this.currentNodeName = nodeName");
2572: jw.writeEol("currentEventNumber = 0");
2573: jw.writeEol("currentArrayIndex = 0");
2574: jw.writeEol("currentElementNumber = 0");
2575: jw.end();
2576: jw.cr();
2577: jw.write("public int getEventNumber()");
2578: jw.begin();
2579: jw.writeEol("return currentEventNumber");
2580: jw.end();
2581: jw.cr();
2582: jw.write("public void nextEventNumber()");
2583: jw.begin();
2584: jw.writeEol("++currentEventNumber");
2585: jw.writeEol("currentArrayIndex = 0");
2586: jw.writeEol("currentElementNumber = 0");
2587: jw.end();
2588: jw.cr();
2589: jw.write("public int getArrayIndex()");
2590: jw.begin();
2591: jw.writeEol("return currentArrayIndex");
2592: jw.end();
2593: jw.cr();
2594: jw.write("public void nextArrayIndex()");
2595: jw.begin();
2596: jw.writeEol("++currentArrayIndex");
2597: jw.writeEol("currentElementNumber = 0");
2598: jw.end();
2599: jw.cr();
2600: jw.write("public int getElementNumber()");
2601: jw.begin();
2602: jw.writeEol("return currentElementNumber");
2603: jw.end();
2604: jw.cr();
2605: jw.write("public void nextElementNumber()");
2606: jw.begin();
2607: jw.writeEol("++currentElementNumber");
2608: jw.end();
2609: jw.cr();
2610: jw.write("public javax.xml.namespace.QName getNodeName()");
2611: jw.begin();
2612: jw.writeEol("return currentNodeName");
2613: jw.end();
2614: jw.cr();
2615: jw.write("public void enterChildBean(" + commonBean
2616: + " bean, javax.xml.namespace.QName nodeName)");
2617: jw.begin();
2618: jw.writeEol("beans.push(currentBean)");
2619: jw.writeEol("eventNumbers.push(currentEventNumber)");
2620: jw.writeEol("arrayIndices.push(currentArrayIndex)");
2621: jw.writeEol("elementNumbers.push(currentElementNumber)");
2622: jw.writeEol("nodeNames.push(currentNodeName)");
2623: jw.writeEol("currentBean = bean");
2624: jw.writeEol("currentEventNumber = 0");
2625: jw.writeEol("currentArrayIndex = 0");
2626: jw.writeEol("currentElementNumber = 0");
2627: jw.writeEol("currentNodeName = nodeName");
2628: jw.end();
2629: jw.cr();
2630: jw.write("public void doneWithBean()");
2631: jw.begin();
2632: jw.write("if (beans.isEmpty())");
2633: jw.begin();
2634: jw.writeEol("currentBean = null");
2635: jw.writeEol("currentEventNumber = -1");
2636: jw.end();
2637: jw.write("else");
2638: jw.begin();
2639: jw.writeEol("currentBean = beans.pop()");
2640: jw.writeEol("currentEventNumber = eventNumbers.pop()");
2641: jw.writeEol("currentArrayIndex = arrayIndices.pop()");
2642: jw.writeEol("currentElementNumber = elementNumbers.pop()");
2643: jw.writeEol("currentNodeName = nodeNames.pop()");
2644: jw.end();
2645: jw.end();
2646: jw.cr();
2647: jw
2648: .write("public Object getProperty(String name) throws IllegalArgumentException");
2649: jw.begin();
2650: jw
2651: .writeEol("throw new IllegalArgumentException(\"Got no properties\")");
2652: jw.end();
2653: jw.cr();
2654: jw.write("public void remove()");
2655: jw.begin();
2656: jw.writeEol("throw new UnsupportedOperationException()");
2657: jw.end();
2658: jw.cr();
2659: jw
2660: .write("public javax.xml.stream.events.XMLEvent peek() throws javax.xml.stream.XMLStreamException");
2661: jw.begin();
2662: jw.beginIf("next != null");
2663: jw.writeEol("return next");
2664: jw.end();
2665: jw.writeEol("setNextEvent()");
2666: jw.writeEol("return next");
2667: jw.end();
2668: jw.cr();
2669: jw
2670: .write("public javax.xml.stream.events.XMLEvent nextTag() throws javax.xml.stream.XMLStreamException");
2671: jw.begin();
2672: jw.writeEol("throw new IllegalArgumentException()");
2673: jw.end();
2674: jw.cr();
2675: jw
2676: .write("public javax.xml.stream.events.XMLEvent nextEvent() throws javax.xml.stream.XMLStreamException");
2677: jw.begin();
2678: jw
2679: .writeEol("javax.xml.stream.events.XMLEvent result = null");
2680: jw.write("if (next != null)");
2681: jw.begin();
2682: jw.writeEol("result = next");
2683: jw.writeEol("next = null");
2684: jw.writeEol("return result");
2685: jw.end();
2686: jw.writeEol("setNextEvent()");
2687: jw.beginIf("next == null");
2688: jw.writeEol("throw new java.util.NoSuchElementException()");
2689: jw.end();
2690: jw.writeEol("result = next");
2691: jw.writeEol("next = null");
2692: jw.writeEol("return result");
2693: jw.end();
2694: jw.cr();
2695: jw
2696: .write("private void setNextEvent() throws javax.xml.stream.XMLStreamException");
2697: jw.begin();
2698: jw.write("while (next == null && currentEventNumber >= 0)");
2699: jw.begin();
2700: jw.writeEol("next = currentBean.getXMLEvent(this)");
2701: jw.end();
2702: jw.end();
2703: jw.cr();
2704: jw.write("public Object next()");
2705: jw.begin();
2706: jw.beginTry();
2707: jw.writeEol("return nextEvent()");
2708: jw.write("} catch (javax.xml.stream.XMLStreamException e)");
2709: jw.begin();
2710: jw.writeEol("throw new RuntimeException(e)");
2711: jw.end();
2712: jw.end();
2713: jw.cr();
2714: jw.write("public boolean hasNext()");
2715: jw.begin();
2716: jw.beginIf("next != null");
2717: jw.writeEol("return true");
2718: jw.end();
2719: jw.beginTry();
2720: jw.writeEol("setNextEvent()");
2721: jw.endCatch("javax.xml.stream.XMLStreamException e");
2722: jw.writeEol("throw new RuntimeException(e)");
2723: jw.end();
2724: jw.writeEol("return (next != null)");
2725: jw.end();
2726: jw.cr();
2727: jw
2728: .write("public String getElementText() throws javax.xml.stream.XMLStreamException");
2729: jw.begin();
2730: jw.writeEol("throw new IllegalArgumentException()");
2731: jw.end();
2732: jw.cr();
2733: jw
2734: .write("public void close() throws javax.xml.stream.XMLStreamException");
2735: jw.begin();
2736: jw.writeEol("currentEventNumber = -1");
2737: jw.writeEol("beans.clear()");
2738: jw.writeEol("eventNumbers.clear()");
2739: jw.writeEol("arrayIndices.clear()");
2740: jw.writeEol("nodeNames.clear()");
2741: jw.end();
2742: jw.end();
2743:
2744: jw
2745: .write("static abstract class BaseXMLEvent implements javax.xml.stream.events.XMLEvent ");
2746: jw.begin();
2747: jw
2748: .write("public void writeAsEncodedUnicode(java.io.Writer writer) throws javax.xml.stream.XMLStreamException ");
2749: jw.begin();
2750: jw.writeEol("throw new UnsupportedOperationException()");
2751: jw.end();
2752: jw.cr();
2753: jw.write("public boolean isStartElement() ");
2754: jw.begin();
2755: jw.writeEol("return false");
2756: jw.end();
2757: jw.cr();
2758: jw.write("public boolean isStartDocument() ");
2759: jw.begin();
2760: jw.writeEol("return false");
2761: jw.end();
2762: jw.cr();
2763: jw
2764: .write("public javax.xml.stream.events.Characters asCharacters() ");
2765: jw.begin();
2766: jw
2767: .writeEol("return (javax.xml.stream.events.Characters) this");
2768: jw.end();
2769: jw.cr();
2770: jw
2771: .write("public javax.xml.stream.events.EndElement asEndElement() ");
2772: jw.begin();
2773: jw
2774: .writeEol("return (javax.xml.stream.events.EndElement) this");
2775: jw.end();
2776: jw.cr();
2777: jw
2778: .write("public javax.xml.stream.events.StartElement asStartElement() ");
2779: jw.begin();
2780: jw
2781: .writeEol("return (javax.xml.stream.events.StartElement) this");
2782: jw.end();
2783: jw.cr();
2784: jw.write("public javax.xml.stream.Location getLocation() ");
2785: jw.begin();
2786: jw.writeEol("throw new UnsupportedOperationException()");
2787: jw.end();
2788: jw.cr();
2789: jw
2790: .write("public javax.xml.namespace.QName getSchemaType() ");
2791: jw.begin();
2792: jw.writeEol("return null");
2793: jw.end();
2794: jw.cr();
2795: jw.write("public boolean isAttribute() ");
2796: jw.begin();
2797: jw.writeEol("return false");
2798: jw.end();
2799: jw.cr();
2800: jw.write("public boolean isCharacters() ");
2801: jw.begin();
2802: jw.writeEol("return false");
2803: jw.end();
2804: jw.cr();
2805: jw.write("public boolean isEndDocument() ");
2806: jw.begin();
2807: jw.writeEol("return false");
2808: jw.end();
2809: jw.cr();
2810: jw.write("public boolean isEndElement() ");
2811: jw.begin();
2812: jw.writeEol("return false");
2813: jw.end();
2814: jw.cr();
2815: jw.write("public boolean isEntityReference() ");
2816: jw.begin();
2817: jw.writeEol("return false");
2818: jw.end();
2819: jw.cr();
2820: jw.write("public boolean isNamespace() ");
2821: jw.begin();
2822: jw.writeEol("return false");
2823: jw.end();
2824: jw.cr();
2825: jw.write("public boolean isProcessingInstruction() ");
2826: jw.begin();
2827: jw.writeEol("return false");
2828: jw.end();
2829: jw.cr();
2830: jw.end();
2831: jw.cr();
2832: jw
2833: .write("static class ThisStartDocument extends BaseXMLEvent implements javax.xml.stream.events.StartDocument ");
2834: jw.begin();
2835: jw.write("public int getEventType() ");
2836: jw.begin();
2837: jw
2838: .writeEol("return javax.xml.stream.XMLStreamConstants.START_DOCUMENT");
2839: jw.end();
2840: jw.cr();
2841: jw.write("public boolean isStartDocument() ");
2842: jw.begin();
2843: jw.writeEol("return true");
2844: jw.end();
2845: jw.cr();
2846: jw.write("public boolean standaloneSet() ");
2847: jw.begin();
2848: jw.writeEol("return false");
2849: jw.end();
2850: jw.cr();
2851: jw.write("public boolean isStandalone() ");
2852: jw.begin();
2853: jw.writeEol("return false");
2854: jw.end();
2855: jw.cr();
2856: jw.write("public String getVersion() ");
2857: jw.begin();
2858: jw.writeEol("return \"1.0\"");
2859: jw.end();
2860: jw.cr();
2861: jw.write("public String getSystemId() ");
2862: jw.begin();
2863: jw.writeEol("return null");
2864: jw.end();
2865: jw.cr();
2866: jw.write("public String getCharacterEncodingScheme() ");
2867: jw.begin();
2868: jw.writeEol("return \"UTF-8\"");
2869: jw.end();
2870: jw.cr();
2871: jw.write("public boolean encodingSet() ");
2872: jw.begin();
2873: jw.writeEol("return true");
2874: jw.end();
2875: jw.end();
2876: jw.cr();
2877: jw
2878: .write("static class ThisAttribute extends BaseXMLEvent implements javax.xml.stream.events.Attribute ");
2879: jw.begin();
2880: jw.writeEol("private javax.xml.namespace.QName name");
2881: jw.writeEol("private String value");
2882: jw.writeEol("private String dtdType");
2883: jw.writeEol("private javax.xml.namespace.QName schemaType");
2884: jw.cr();
2885: jw
2886: .write("public ThisAttribute(javax.xml.namespace.QName name, String value, String dtdType, javax.xml.namespace.QName schemaType) ");
2887: jw.begin();
2888: jw.writeEol("this.name = name");
2889: jw.writeEol("this.value = value");
2890: jw.writeEol("this.dtdType = dtdType");
2891: jw.writeEol("this.schemaType = schemaType");
2892: jw.end();
2893: jw.cr();
2894: jw.write("public int getEventType() ");
2895: jw.begin();
2896: jw
2897: .writeEol("return javax.xml.stream.XMLStreamConstants.ATTRIBUTE");
2898: jw.end();
2899: jw.cr();
2900: jw.write("public boolean isSpecified() ");
2901: jw.begin();
2902: jw.writeEol("return true");
2903: jw.end();
2904: jw.cr();
2905: jw.write("public String getValue() ");
2906: jw.begin();
2907: jw.writeEol("return value");
2908: jw.end();
2909: jw.cr();
2910: jw.write("public javax.xml.namespace.QName getName() ");
2911: jw.begin();
2912: jw.writeEol("return name");
2913: jw.end();
2914: jw.cr();
2915: jw.write("public String getDTDType() ");
2916: jw.begin();
2917: jw.writeEol("return dtdType");
2918: jw.end();
2919: jw.cr();
2920: jw
2921: .write("public javax.xml.namespace.QName getSchemaType() ");
2922: jw.begin();
2923: jw.writeEol("return schemaType");
2924: jw.end();
2925: jw.end();
2926: jw.cr();
2927: jw
2928: .write("static class ThisNamespace extends ThisAttribute implements javax.xml.stream.events.Namespace ");
2929: jw.begin();
2930: jw.writeEol("private String prefix");
2931: jw.cr();
2932: jw
2933: .write("public ThisNamespace(String prefix, String uri) ");
2934: jw.begin();
2935: jw
2936: .writeEol("super(new javax.xml.namespace.QName(\"http://FIXME/xmlns\", prefix, \"xmlns\"), uri, null, null)");
2937: jw.writeEol("this.prefix = prefix");
2938: jw.end();
2939: jw.cr();
2940: jw.write("public boolean isDefaultNamespaceDeclaration() ");
2941: jw.begin();
2942: jw.writeEol("return \"\".equals(prefix)");
2943: jw.end();
2944: jw.cr();
2945: jw.write("public String getPrefix() ");
2946: jw.begin();
2947: jw.writeEol("return prefix");
2948: jw.end();
2949: jw.cr();
2950: jw.write("public String getNamespaceURI() ");
2951: jw.begin();
2952: jw.writeEol("return getValue()");
2953: jw.end();
2954: jw.end();
2955: jw.cr();
2956: jw
2957: .write("static class ThisStartElement extends BaseXMLEvent implements javax.xml.stream.events.StartElement ");
2958: jw.begin();
2959: jw.writeEol("private javax.xml.namespace.QName name");
2960: jw
2961: .writeEol("private java.util.Map<javax.xml.namespace.QName, ThisAttribute> attributes = new java.util.HashMap<javax.xml.namespace.QName, ThisAttribute>()");
2962: jw
2963: .writeEol("private java.util.Map<String, ThisNamespace> namespaces = new java.util.HashMap<String, ThisNamespace>()");
2964: jw.cr();
2965: jw
2966: .write("public ThisStartElement(javax.xml.namespace.QName name) ");
2967: jw.begin();
2968: jw.writeEol("this.name = name");
2969: jw.end();
2970: jw.cr();
2971: jw
2972: .write("public void addAttribute(javax.xml.namespace.QName attrName, String attrValue, String dtdType, javax.xml.namespace.QName schemaType) ");
2973: jw.begin();
2974: jw
2975: .writeEol("attributes.put(attrName, new ThisAttribute(attrName, attrValue, dtdType, schemaType))");
2976: jw.end();
2977: jw.cr();
2978: jw
2979: .write("public void addNamespace(String prefix, String uri) ");
2980: jw.begin();
2981: jw
2982: .writeEol("namespaces.put(prefix, new ThisNamespace(prefix, uri))");
2983: jw.end();
2984: jw.cr();
2985: jw.write("public int getEventType() ");
2986: jw.begin();
2987: jw
2988: .writeEol("return javax.xml.stream.XMLStreamConstants.START_ELEMENT");
2989: jw.end();
2990: jw.cr();
2991: jw.write("public boolean isStartElement() ");
2992: jw.begin();
2993: jw.writeEol("return true");
2994: jw.end();
2995: jw.cr();
2996: jw.write("public String getNamespaceURI(String prefix) ");
2997: jw.begin();
2998: jw
2999: .writeEol("return namespaces.get(prefix).getNamespaceURI()");
3000: jw.end();
3001: jw.cr();
3002: jw
3003: .write("public javax.xml.stream.events.Attribute getAttributeByName(javax.xml.namespace.QName name) ");
3004: jw.begin();
3005: jw.writeEol("return attributes.get(name)");
3006: jw.end();
3007: jw.cr();
3008: jw.write("public java.util.Iterator getNamespaces() ");
3009: jw.begin();
3010: jw.writeEol("return namespaces.values().iterator()");
3011: jw.end();
3012: jw.cr();
3013: jw
3014: .write("public javax.xml.namespace.NamespaceContext getNamespaceContext() ");
3015: jw.begin();
3016: jw.writeEol("throw new UnsupportedOperationException()");
3017: jw.end();
3018: jw.cr();
3019: jw.write("public javax.xml.namespace.QName getName() ");
3020: jw.begin();
3021: jw.writeEol("return name");
3022: jw.end();
3023: jw.cr();
3024: jw.write("public java.util.Iterator getAttributes() ");
3025: jw.begin();
3026: jw.writeEol("return attributes.values().iterator()");
3027: jw.end();
3028: jw.write("public String toString() ");
3029: jw.begin();
3030: jw.writeEol("return \"StartElement: \"+name.toString()");
3031: jw.end();
3032: jw.end();
3033: jw.cr();
3034: jw
3035: .write("static class ThisEndElement extends BaseXMLEvent implements javax.xml.stream.events.EndElement ");
3036: jw.begin();
3037: jw.writeEol("private javax.xml.namespace.QName name");
3038: jw.cr();
3039: jw
3040: .write("public ThisEndElement(javax.xml.namespace.QName name) ");
3041: jw.begin();
3042: jw.writeEol("this.name = name");
3043: jw.end();
3044: jw.cr();
3045: jw.write("public int getEventType() ");
3046: jw.begin();
3047: jw
3048: .writeEol("return javax.xml.stream.XMLStreamConstants.END_ELEMENT");
3049: jw.end();
3050: jw.cr();
3051: jw.write("public boolean isEndElement() ");
3052: jw.begin();
3053: jw.writeEol("return true");
3054: jw.end();
3055: jw.cr();
3056: jw.write("public java.util.Iterator getNamespaces() ");
3057: jw.begin();
3058: jw.writeEol("throw new UnsupportedOperationException()");
3059: jw.end();
3060: jw.cr();
3061: jw.write("public javax.xml.namespace.QName getName() ");
3062: jw.begin();
3063: jw.writeEol("return name");
3064: jw.end();
3065: jw.write("public String toString() ");
3066: jw.begin();
3067: jw.writeEol("return \"EndElement: \"+name.toString()");
3068: jw.end();
3069: jw.end();
3070: jw.cr();
3071: jw
3072: .write("static class ThisCharacters extends BaseXMLEvent implements javax.xml.stream.events.Characters ");
3073: jw.begin();
3074: jw.writeEol("private String data");
3075: jw.cr();
3076: jw.write("public ThisCharacters(String data) ");
3077: jw.begin();
3078: jw.writeEol("this.data = data");
3079: jw.end();
3080: jw.cr();
3081: jw.write("public int getEventType() ");
3082: jw.begin();
3083: jw
3084: .writeEol("return javax.xml.stream.XMLStreamConstants.CHARACTERS");
3085: jw.end();
3086: jw.cr();
3087: jw.write("public boolean isCharacters() ");
3088: jw.begin();
3089: jw.writeEol("return true");
3090: jw.end();
3091: jw.cr();
3092: jw.write("public boolean isWhiteSpace() ");
3093: jw.begin();
3094: jw.write("for (int i = 0; i < data.length(); ++i) ");
3095: jw.begin();
3096: jw.writeEol("char c = data.charAt(i)");
3097: jw.writeEol("if (!Character.isWhitespace(c))");
3098: jw.writeEol("return false");
3099: jw.end();
3100: jw.writeEol("return true");
3101: jw.end();
3102: jw.cr();
3103: jw.write("public boolean isIgnorableWhiteSpace() ");
3104: jw.begin();
3105: jw.writeEol("return false");
3106: jw.end();
3107: jw.cr();
3108: jw.write("public boolean isCData() ");
3109: jw.begin();
3110: jw.writeEol("return false");
3111: jw.end();
3112: jw.cr();
3113: jw.write("public String getData() ");
3114: jw.begin();
3115: jw.writeEol("return data");
3116: jw.end();
3117: jw.write("public String toString() ");
3118: jw.begin();
3119: jw.writeEol("return \"Characters: \"+data");
3120: jw.end();
3121: jw.end();
3122: jw.cr();
3123: jw
3124: .write("static class ThisEndDocument extends BaseXMLEvent implements javax.xml.stream.events.EndDocument ");
3125: jw.begin();
3126: jw.write("public int getEventType() ");
3127: jw.begin();
3128: jw
3129: .writeEol("return javax.xml.stream.XMLStreamConstants.END_DOCUMENT");
3130: jw.end();
3131: jw.cr();
3132: jw.write("public boolean isEndDocument() ");
3133: jw.begin();
3134: jw.writeEol("return true");
3135: jw.end();
3136: jw.end();
3137: jw.cr();
3138: }
3139: jw.beginMethod("getXMLEvent", getRootClassName()
3140: + ".XMLEventStateManager state", null,
3141: "javax.xml.stream.events.XMLEvent");
3142: jw.write("switch (state.getEventNumber()) ");
3143: jw.begin();
3144: int caseNum = 0;
3145: if (beanElement.isRoot) {
3146: jw.writecr("case " + caseNum++, ":");
3147: jw.indentRight();
3148: jw.writeEol("state.nextEventNumber()");
3149: jw.writeEol("return new ThisStartDocument()");
3150: jw.indentLeft();
3151: }
3152:
3153: jw.writecr("case " + caseNum++, ":");
3154: jw.begin();
3155: jw.writeEol("state.nextEventNumber()");
3156: jw.writeEol(getRootClassName()
3157: + ".ThisStartElement startElement = new ",
3158: getRootClassName(),
3159: ".ThisStartElement(state.getNodeName())");
3160: int size = attrList.size();
3161: for (int i = 0; i < size; i++) {
3162: Property a = (Property) attrList.get(i);
3163: if (!a.isAttribute())
3164: continue;
3165: if (!a.isDirectChild())
3166: continue;
3167: String qNameName = declareQName(a.getNamespace(),
3168: a.dtdName, null);
3169: String typeQNameName;
3170: //System.out.println("graphNode null for "+a.getType());
3171: SchemaRep schemaRep = new SchemaRep();
3172: String xmlSchemaType = schemaRep
3173: .javaType2XMLSchemaTypeComplex(a.getType());
3174: //System.out.println("xmlSchemaType="+xmlSchemaType);
3175: typeQNameName = declareQName(
3176: schemaRep.getNamespaceURI(schemaRep
3177: .prefixOf(xmlSchemaType)), schemaRep
3178: .removePrefix(xmlSchemaType), null);
3179:
3180: String varName = "_" + a.name;
3181: if (!a.isScalar()) {
3182: jw.beginIf(varName + " != null");
3183: } else if (config.isOptionalScalars() && a.isScalar()) {
3184: jw.beginIf(a.getScalarIsSet());
3185: }
3186: jw.write("startElement.addAttribute(", qNameName, ", ");
3187: jw.write(JavaUtil.typeToString(a.getType(), varName));
3188: jw.writeEol(", \"CDATA\", ", typeQNameName, ")");
3189: if (!a.isScalar()
3190: || (config.isOptionalScalars() && a.isScalar()))
3191: jw.end();
3192: }
3193:
3194: if (beanElement.isRoot) {
3195: jw.beginIf("schemaLocation != null");
3196: String slQNameName = declareQName(
3197: "http://www.w3.org/2001/XMLSchema-instance",
3198: "schemaLocation", "xsi");
3199: String typeQNameName = declareQName(
3200: "http://www.w3.org/2001/XMLSchema", "string", "xsd");
3201: jw.write("startElement.addAttribute(", slQNameName, ", ");
3202: jw.writeEol("schemaLocation, \"CDATA\", ", typeQNameName,
3203: ")");
3204: jw.end();
3205: }
3206: jw.writeEol("return startElement");
3207: jw.end();
3208:
3209: int numberOfNonAttributeProperties = countNumberOfNonAttributeProperties();
3210: comment("Number of non attribute properties: "
3211: + numberOfNonAttributeProperties);
3212: int childStartCaseNum = caseNum;
3213: if (caseNum < childStartCaseNum
3214: + numberOfNonAttributeProperties) {
3215: while (caseNum < childStartCaseNum
3216: + numberOfNonAttributeProperties) {
3217: jw.writecr("case " + caseNum++, ":");
3218: }
3219: jw.indentRight();
3220: // numberOfNonAttributeProperties
3221: jw
3222: .writeEol("javax.xml.stream.events.XMLEvent event = getXMLEventChildren(state, state.getEventNumber() - "
3223: + (childStartCaseNum) + ")");
3224: jw.writeEol("return event");
3225: jw.indentLeft();
3226: }
3227:
3228: jw.writecr("case " + caseNum++, ":");
3229: jw.indentRight();
3230: jw.writeEol("state.nextEventNumber()");
3231: jw.writeEol("return new ", getRootClassName(),
3232: ".ThisEndElement(state.getNodeName())");
3233: jw.indentLeft();
3234:
3235: if (beanElement.isRoot) {
3236: jw.writecr("case " + caseNum++, ":");
3237: jw.indentRight();
3238: jw.writeEol("state.nextEventNumber()");
3239: jw.writeEol("return new ThisEndDocument()");
3240: jw.indentLeft();
3241: }
3242: jw.writecr("default:");
3243: jw.indentRight();
3244: jw.writeEol("state.doneWithBean()");
3245: jw.writeEol("break");
3246: jw.indentLeft();
3247: jw.end();
3248: jw.writeEol("return null");
3249: jw.endMethod();
3250:
3251: jw.beginMethod("getXMLEventChildren", getRootClassName()
3252: + ".XMLEventStateManager state, int eventNumber", null,
3253: "javax.xml.stream.events.XMLEvent", jw.PROTECTED);
3254: caseNum = 0;
3255: jw.write("switch (eventNumber) ");
3256: jw.begin();
3257:
3258: if (config.isRespectExtension()
3259: && beanElement.getExtension() != null) {
3260: jw.writecr("default:");
3261: jw.indentRight();
3262: jw
3263: .writeEol("javax.xml.stream.events.XMLEvent event = super.getXMLEventChildren(state, eventNumber)");
3264: jw.beginIf("true"); // workaround "unreachable statement" error from javac which can happen if default is the only thing in the switch
3265: jw.writeEol("return event");
3266: jw.end();
3267: jw.indentLeft();
3268: caseNum = countNumberOfNonAttributePropertiesRecurse(beanElement
3269: .getExtension());
3270: }
3271: for (int i = 0; i < size; i++) {
3272: Property a = (Property) attrList.get(i);
3273: if (a.isAttribute())
3274: continue;
3275: boolean indexed = a.isIndexed();
3276: String varName = "_" + a.name;
3277:
3278: String qNameName = declareQName(a.getNamespace(),
3279: a.dtdName, null);
3280: jw.writecr("case " + caseNum++, ":");
3281: jw.begin();
3282: if (indexed) {
3283: varName = "element";
3284: jw.writeEol(a.getTypeFullClassName(packageName),
3285: " element = null");
3286: if (!a.isBean) {
3287: jw.beginIf("state.getElementNumber() == 0");
3288: }
3289: jw.beginWhile("state.getArrayIndex() < size" + a.name
3290: + "()");
3291: jw.writeEol("element = ", a.getReadMethod(true),
3292: "(state.getArrayIndex())");
3293: jw.beginIf("element != null");
3294: jw.writeEol("break");
3295: jw.end();
3296: jw.writeEol("state.nextArrayIndex()");
3297: jw.end();
3298: if (!a.isBean) {
3299: jw.endElseBegin();
3300: jw.writeEol("element = ", a.getReadMethod(true),
3301: "(state.getArrayIndex())");
3302: jw.writeEol("assert element != null");
3303: jw.end();
3304: }
3305: } else {
3306: }
3307: if (a.isBean) {
3308: jw.beginIf(varName + " != null");
3309: if (indexed)
3310: jw.writeEol("state.nextArrayIndex()");
3311: else
3312: jw.writeEol("state.nextEventNumber()");
3313: jw.write("state.enterChildBean(", varName, ", ");
3314: jw.writeEol(qNameName, ")");
3315: jw.writeEol("return ", varName, ".getXMLEvent(state)");
3316: jw.endElseBegin();
3317: jw.writeEol("state.nextEventNumber()");
3318: jw.end();
3319: jw.writeEol("break");
3320: jw.end();
3321: } else {
3322: if (!a.isScalar()) {
3323: jw.beginIf(varName + " != null");
3324: } else if (config.isOptionalScalars() && a.isScalar()) {
3325: jw.beginIf(a.getScalarIsSet());
3326: }
3327: jw.writeEol("state.nextElementNumber()");
3328: jw.write("switch (state.getElementNumber()) ");
3329: jw.begin();
3330: jw.writecr("case 1:");
3331: jw.indentRight();
3332: jw.write(getRootClassName(),
3333: ".ThisStartElement startElement = new ",
3334: getRootClassName());
3335: jw.writeEol(".ThisStartElement(", qNameName, ")");
3336: for (int attrNum = 0; attrNum < a.attributes.length; ++attrNum) {
3337: AttrProp attr = a.attributes[attrNum];
3338: //jw.comment("attr "+attr);
3339: Property prop = findProperty(attr);
3340:
3341: String qNameNameAttr = declareQName(prop
3342: .getNamespace(), prop.dtdName, null);
3343: String typeQNameNameAttr;
3344: //System.out.println("graphNode null for "+a.getType());
3345: SchemaRep schemaRep = new SchemaRep();
3346: String xmlSchemaType = schemaRep
3347: .javaType2XMLSchemaTypeComplex(a.getType());
3348: //System.out.println("xmlSchemaType="+xmlSchemaType);
3349: typeQNameNameAttr = declareQName(schemaRep
3350: .getNamespaceURI(schemaRep
3351: .prefixOf(xmlSchemaType)),
3352: schemaRep.removePrefix(xmlSchemaType), null);
3353:
3354: String varNameAttr = "_" + prop.name;
3355: if (!a.isScalar()) {
3356: jw.beginIf(varNameAttr + " != null");
3357: } else if (config.isOptionalScalars()
3358: && a.isScalar()) {
3359: jw.beginIf(a.getScalarIsSet());
3360: }
3361: jw.write("startElement.addAttribute("
3362: + qNameNameAttr + ", ");
3363: jw.write(JavaUtil.typeToString(a.getType(),
3364: varNameAttr));
3365: jw
3366: .writeEol(", \"CDATA\", ",
3367: typeQNameNameAttr, ")");
3368: if (!a.isScalar()
3369: || (config.isOptionalScalars() && a
3370: .isScalar()))
3371: jw.end();
3372: }
3373: jw.writeEol("return startElement");
3374: jw.indentLeft();
3375: jw.writecr("case 2:");
3376: jw.indentRight();
3377: jw.write("return new ", getRootClassName(),
3378: ".ThisCharacters(");
3379: if (isTypeQName(a.getType())) {
3380: // see line 3118 or so
3381: jw.write(a.getType());
3382: } else if ("byte[]".equals(a.getType())) {
3383: // Is this hexBinary or base64Binary?
3384: SchemaRep.EncodingStyle style = (SchemaRep.EncodingStyle) a
3385: .searchExtraData(SchemaRep.EncodingStyle.class);
3386: if (style instanceof SchemaRep.HexBinary) {
3387: } else if (style instanceof SchemaRep.Base64Binary) {
3388: jw.write(getRootClassName(),
3389: ".encodeBase64BinaryString(");
3390: jw.writeEol(varName, "))");
3391: } else {
3392: config.messageOut
3393: .println("Unknown encoding style for "
3394: + a.getType()
3395: + " for property " + a.name);
3396: }
3397: } else {
3398: jw.write(JavaUtil
3399: .typeToString(a.getType(), varName));
3400: jw.writeEol(")");
3401: }
3402: jw.indentLeft();
3403: jw.writecr("case 3:");
3404: jw.indentRight();
3405: jw.write("return new ", getRootClassName(),
3406: ".ThisEndElement(", qNameName);
3407: jw.writeEol(")");
3408: jw.indentLeft();
3409: jw.writecr("default:");
3410: jw.indentRight();
3411: if (indexed) {
3412: jw.writeEol("state.nextArrayIndex()");
3413: jw.writeEol("return getXMLEvent(state)");
3414: } else {
3415: jw.writeEol("break");
3416: }
3417: jw.indentLeft();
3418: jw.end();
3419: if (!a.isScalar()
3420: || (config.isOptionalScalars() && a.isScalar())) {
3421: if (indexed) {
3422: jw.endElse();
3423: jw.cr();
3424: jw.indentOneLevel();
3425: } else {
3426: jw.end();
3427: }
3428: }
3429: jw.writeEol("state.nextEventNumber()");
3430: jw.writeEol("break");
3431: jw.end();
3432: }
3433: /*
3434: if (indexed) {
3435: jw.writeEol("state.nextEventNumber()");
3436: jw.writeEol("return getXMLEvent(state)");
3437: jw.end();
3438: }
3439: */
3440: }
3441:
3442: jw.end();
3443: jw.writeEol("return null");
3444: jw.endMethod();
3445:
3446: }
3447:
3448: /**
3449: * Declare the QNAME as a constant.
3450: * @return the name of the identifier choosen.
3451: */
3452: protected String declareQName(String namespaceUri,
3453: String localPart, String prefix) throws IOException {
3454: if ("http://www.w3.org/XML/1998/namespace".equals(namespaceUri)) {
3455: if (prefix == null)
3456: prefix = "xml";
3457: if (localPart.startsWith("xml:"))
3458: localPart = localPart.substring(4);
3459: }
3460: QName qname = new QName(namespaceUri, localPart, prefix);
3461: if (declaredQNames.containsKey(qname))
3462: return (String) declaredQNames.get(qname);
3463: jw.pushSelect(DECL_SECTION);
3464: if (declaredQNames.size() == 0)
3465: jw.cr();
3466: String qNameName = "QNAME_" + Common.constName(localPart);
3467: while (declaredQNames.containsKey(qNameName))
3468: qNameName = qNameName + "2";
3469: declaredQNames.put(qname, qNameName);
3470: //System.out.println("Declaring "+qNameName+" "+qname);
3471: jw.write("public static final javax.xml.namespace.QName ");
3472: jw.write(qNameName);
3473: jw.write(" = new javax.xml.namespace.QName(");
3474: if (namespaceUri == null)
3475: jw.write("null");
3476: else
3477: jw.write(JavaUtil.instanceFrom("String", namespaceUri));
3478: jw.write(", \"");
3479: jw.write(localPart);
3480: jw.write("\"");
3481: if (prefix != null)
3482: jw.write(", ", JavaUtil.instanceFrom("String", prefix));
3483: jw.writeEolNoI18N(")");
3484: jw.popSelect();
3485: return qNameName;
3486: }
3487:
3488: protected void declareLogger() throws IOException {
3489: if (loggerDeclared)
3490: return;
3491: jw.pushSelect(DECL_SECTION);
3492: jw
3493: .writeEol("private static final java.util.logging.Logger _logger = java.util.logging.Logger.getLogger(\""
3494: + fullClassName + "\")");
3495: jw.popSelect();
3496: loggerDeclared = true;
3497: }
3498:
3499: protected void genUpdateNamespaces(String namespaceMapName,
3500: String firstNamespaceVarName) throws IOException {
3501: jw.beginFor("int attrNum = 0", "attrNum < attrs.getLength()",
3502: "++attrNum");
3503: jw.writeEol("attr = (org.w3c.dom.Attr) attrs.item(attrNum)");
3504: jw.writeEol("String attrName = attr.getName()");
3505: //jw.writeEol("System.out.println(\"attrName=\"+attrName)
3506: jw.beginIf("attrName.startsWith(\"xmlns:\")");
3507: if (firstNamespaceVarName != null) {
3508: jw.beginIf(firstNamespaceVarName);
3509: jw.writeEol(firstNamespaceVarName, " = false");
3510: jw
3511: .comment("Dup prefix map, so as to not write over previous values, and to make it easy to clear out our entries.");
3512: jw.write(namespaceMapName, " = new java.util.HashMap");
3513: if (config.jdkTarget >= 150)
3514: jw.write("<String, String>");
3515: jw.writeEol("(", namespaceMapName, ")");
3516: jw.end();
3517: }
3518: jw
3519: .writeEol("String attrNSPrefix = attrName.substring(6, attrName.length())");
3520: jw.writeEol(namespaceMapName,
3521: ".put(attrNSPrefix, attr.getValue())");
3522: jw.end();
3523: jw.end();
3524: }
3525:
3526: protected void genWriteAttr(Property a) throws IOException {
3527: genWriteAttr(a, "_" + a.name);
3528: }
3529:
3530: protected void genWriteAttr(Property a, String varName)
3531: throws IOException {
3532: boolean isScalar = a.isScalar();
3533: String dtdName = a.dtdName;
3534: String namespace = a.getNamespace();
3535:
3536: comment(dtdName + " is an attribute with namespace "
3537: + namespace);
3538: if (!isScalar) {
3539: gen("if (" + varName + " != null) ");
3540: begin();
3541: }
3542: if (namespace != null
3543: && !namespace.equals(getDefaultNamespace())
3544: && !namespace
3545: .equals("http://www.w3.org/XML/1998/namespace")) {
3546: dtdName = SchemaRep.removePrefix(dtdName);
3547: outWrite(" ");
3548: jw.writeEol("out.write((String)namespaceMap.get(", JavaUtil
3549: .instanceFrom("String", namespace), "))");
3550: jw.writeEol("out.write(\":", dtdName, "='\")");
3551: } else {
3552: jw.writeEol("out.write(\" ", dtdName, "='\")");
3553: }
3554: genWriteType(a, varName, true);
3555: gen("out.write(\"'\")");
3556: eolNoI18N();
3557: if (!isScalar) {
3558: end();
3559: }
3560: }
3561:
3562: protected void genWriteType(Property a, String varName,
3563: boolean isAttribute) throws IOException {
3564: String type = a.getType();
3565: boolean isScalar = a.isScalar();
3566:
3567: if (isScalar || JavaUtil.canProduceNoXMLMetaChars(type)) {
3568: jw.write("out.write("
3569: + JavaUtil.typeToString(type, varName));
3570: jw.writeEol(")");
3571: } else if (isTypeQName(type)) {
3572: jw.beginIf(varName
3573: + ".getNamespaceURI() != null && !\"\".equals(",
3574: varName, ".getNamespaceURI())");
3575: jw.writeEol("out.write((String) namespaceMap.get(",
3576: varName, ".getNamespaceURI()))");
3577: jw.writeEol("out.write(\":\")");
3578: jw.end();
3579: if (config.isUseRuntime())
3580: jw.write("org.netbeans.modules.schema2beans.XMLUtil");
3581: else
3582: jw.write(getRootClassName());
3583: jw.write(".writeXML(out, ");
3584: jw.write(varName, ".getLocalPart(), " + isAttribute);
3585: jw.writeEol(")");
3586: } else if ("byte[]".equals(type)) {
3587: // Is this hexBinary or base64Binary?
3588: SchemaRep.EncodingStyle style = (SchemaRep.EncodingStyle) a
3589: .searchExtraData(SchemaRep.EncodingStyle.class);
3590: if (style instanceof SchemaRep.HexBinary) {
3591: jw.beginFor("int byteIndex = 0", "byteIndex < "
3592: + varName + ".length", "++byteIndex");
3593: jw.writeEol("int belement = (int) ", varName,
3594: "[byteIndex]");
3595: jw.beginIf("belement < 0");
3596: jw.writeEol("belement += 256");
3597: jw.end();
3598: jw.beginIf("belement < 16");
3599: jw.writeEol("out.write(\"0\")");
3600: jw.end();
3601: jw
3602: .writeEol("out.write(Integer.toHexString(belement).toUpperCase())");
3603: jw.end();
3604: } else if (style instanceof SchemaRep.Base64Binary) {
3605: jw.write("out.write(");
3606: jw.write(getRootClassName());
3607: jw.write(".encodeBase64BinaryString(");
3608: jw.write(varName);
3609: jw.write(")");
3610: jw.writeEol(")");
3611: } else {
3612: config.messageOut.println("Unknown encoding style for "
3613: + type + " for property " + a.name);
3614: }
3615: } else {
3616: if (config.isUseRuntime())
3617: jw.write("org.netbeans.modules.schema2beans.XMLUtil");
3618: else
3619: jw.write(getRootClassName());
3620: jw.write(".writeXML(out, ");
3621: if ("java.util.Calendar".equals(type)) {
3622: jw.write(getRootClassName());
3623: jw.write(".calendarToString(");
3624: jw.write(varName);
3625: jw.write(")");
3626: } else {
3627: jw.write(JavaUtil.typeToString(type, varName));
3628: }
3629: jw.write(", " + isAttribute);
3630: jw.writeEol(")");
3631: }
3632: }
3633:
3634: protected void beginAttrProcessing(String nodeName)
3635: throws IOException {
3636: jw.beginIf(nodeName, ".hasAttributes()");
3637: declareAttrsForRead(nodeName);
3638: }
3639:
3640: protected void genReadAttr(Property a, String nodeName)
3641: throws IOException {
3642: genReadAttr("_" + a.name, a.getType().intern(), a.dtdName,
3643: nodeName, (SchemaRep.WhiteSpace) a
3644: .searchExtraData(SchemaRep.WhiteSpace.class), a
3645: .isIndexed(), a.getAddMethod(), a.isScalar(),
3646: (SchemaRep.EncodingStyle) a
3647: .searchExtraData(SchemaRep.EncodingStyle.class));
3648: }
3649:
3650: protected void genReadAttr(String attr, String type,
3651: String dtdName, String nodeName, SchemaRep.WhiteSpace ws,
3652: boolean isIndexed, String addMethod, boolean isScalar,
3653: SchemaRep.EncodingStyle style) throws IOException {
3654: String baseType = type;
3655: jw.writeEol("attr = (org.w3c.dom.Attr) attrs.getNamedItem(\"",
3656: dtdName, "\")");
3657: List exceps = JavaUtil.exceptionsFromParsingText(baseType);
3658: String var = "attrValue";
3659: jw.beginIf("attr != null");
3660: jw.writeEol("attrValue = attr.getValue()");
3661: if (ws != null) {
3662: genWhiteSpaceRestriction(ws, var, "java.lang.String");
3663: }
3664: if (isIndexed) {
3665: jw.endElseBegin();
3666: jw.writeEol("attrValue = null");
3667: jw.end();
3668: }
3669: if (!exceps.isEmpty()) {
3670: jw.beginTry();
3671: }
3672: genReadType(baseType, attr, var, isIndexed, addMethod,
3673: isScalar, style);
3674: if (!exceps.isEmpty()) {
3675: jw.end();
3676: genRethrowExceptions(exceps);
3677: }
3678: if (!isIndexed)
3679: jw.end();
3680: }
3681:
3682: /**
3683: * @return whether or not a setter was generated.
3684: */
3685: protected boolean genReadType(String type, String var, String expr,
3686: boolean isIndexed, String addMethod, boolean isScalar,
3687: SchemaRep.EncodingStyle style) throws IOException {
3688: type = type.intern();
3689: if (isIndexed) {
3690: jw.writeEol(type, " processedValueFor", var);
3691: if (genReadType(type, "processedValueFor" + var, expr,
3692: false, null, isScalar, style)) {
3693: jw.writeEol(addMethod, "(processedValueFor", var, ")");
3694: }
3695: } else if (isTypeQName(type)) {
3696: jw.writeEol("int colonPos = ", expr, ".indexOf(':')");
3697: //jw.writeEol("System.out.println(\"colonPos=\"+colonPos+\" childNodeValue=\"+childNodeValue)");
3698: jw.beginIf("colonPos < 0");
3699: jw.write(var, " = new ", type);
3700: jw.writeEol("(", expr, ")");
3701: jw.endElseBegin();
3702: jw
3703: .writeEol("java.util.Map nsPrefixes = new java.util.HashMap(namespacePrefixes)");
3704: genUpdateNamespaces("nsPrefixes", null);
3705: jw.writeEol("String prefix = ", expr,
3706: ".substring(0, colonPos)");
3707: jw.writeEol("String ns = (String) nsPrefixes.get(prefix)");
3708: //jw.writeEol("System.out.println(\"prefix=\"+prefix+\" ns=\"+ns)");
3709: jw.write("String localPart = ", expr);
3710: jw.writeEol(".substring(colonPos+1, ", expr, ".length())");
3711: jw
3712: .writeEol(var, " = new ", type,
3713: "(ns, localPart, prefix)");
3714: jw.end();
3715: } else if (type == "byte[]") {
3716: // Is this hexBinary or base64Binary?
3717: if (style instanceof SchemaRep.HexBinary) {
3718: // example: 07FB
3719: jw.writeEol(var,
3720: " = new byte[(childNodeValue.length()+1) / 2]");
3721: jw.beginFor("int byteIndex = 0", "byteIndex < " + var
3722: + ".length", "++byteIndex");
3723: jw
3724: .writeEol("String octet = childNodeValue.substring(byteIndex * 2, byteIndex * 2 + 2)");
3725: jw
3726: .writeEol(var,
3727: "[byteIndex] = Integer.valueOf(octet, 16).byteValue()");
3728: jw.end();
3729: } else if (style instanceof SchemaRep.Base64Binary) {
3730: jw.write(var, " = ");
3731: jw.write(getRootClassName());
3732: jw
3733: .writeEol(".decodeBase64BinaryString(childNodeValue)");
3734: } else {
3735: config.messageOut.println("Unknown encoding style for "
3736: + type + " for " + var);
3737: }
3738: } else if ((!isScalar && !JavaUtil.isInstantiable(type) && type != "java.util.Calendar")
3739: || type == "org.netbeans.modules.schema2beansdev.beangraph.BeanGraph"
3740: || type == "org.netbeans.modules.schema2beansdev.metadd.MetaDD"
3741: || type == "java.io.PrintStream") {
3742: config.messageOut
3743: .println("Warning: Don't know how to create a "
3744: + type);
3745: jw.comment("Don't know how to create a " + type);
3746: return false;
3747: } else {
3748: if (type == "java.util.Calendar") {
3749: jw.write(var, " = ");
3750: jw.write(getRootClassName());
3751: jw.writeEol(".stringToCalendar(", expr, ")");
3752: } else {
3753: jw.writecr(JavaUtil.genParseText(type, expr, var,
3754: config.isForME()));
3755: }
3756: }
3757: return true;
3758: }
3759:
3760: protected void declareAttrsForRead(String nodeName)
3761: throws IOException {
3762: jw.writeEol("org.w3c.dom.NamedNodeMap attrs = ", nodeName,
3763: ".getAttributes()");
3764: jw.writeEol("org.w3c.dom.Attr attr");
3765: jw.writeEol("java.lang.String attrValue");
3766: }
3767:
3768: public void genPrintXML() throws IOException {
3769: jw
3770: .bigComment("Takes some text to be printed into an XML stream and escapes any\ncharacters that might make it invalid XML (like '<').");
3771: jw.beginMethod("writeXML", "java.io.Writer out, String msg",
3772: "java.io.IOException", "void", jw.PUBLIC | jw.STATIC
3773: | jw.IO);
3774: geneol("writeXML(out, msg, true)");
3775: end();
3776: cr();
3777: jw.beginMethod("writeXML",
3778: "java.io.Writer out, String msg, boolean attribute",
3779: "java.io.IOException", "void", jw.PUBLIC | jw.STATIC
3780: | jw.IO);
3781: gencr("if (msg == null)");
3782: tabIn();
3783: geneol("return");
3784: geneol("int msgLength = msg.length()");
3785: jw.beginFor("int i = 0", "i < msgLength", "++i");
3786: geneol("char c = msg.charAt(i)");
3787: geneol("writeXML(out, c, attribute)");
3788: end();
3789: end();
3790: cr();
3791: jw.beginMethod("writeXML",
3792: "java.io.Writer out, char msg, boolean attribute",
3793: "java.io.IOException", "void", jw.PUBLIC | jw.STATIC
3794: | jw.IO);
3795: gencr("if (msg == '&')");
3796: tabIn();
3797: geneol("out.write(\"&\")");
3798: gencr("else if (msg == '<')");
3799: tabIn();
3800: geneol("out.write(\"<\")");
3801: gencr("else if (msg == '>')");
3802: tabIn();
3803: geneol("out.write(\">\")");
3804: gen("else ");
3805: jw.beginIf("attribute");
3806: gencr("if (msg == '\"')");
3807: tabIn();
3808: geneol("out.write(\""\")");
3809: gencr("else if (msg == '\\'')");
3810: tabIn();
3811: geneol("out.write(\"'\")");
3812: gencr("else if (msg == '\\n')");
3813: tabIn();
3814: geneol("out.write(\"
\")");
3815: gencr("else if (msg == '\\t')");
3816: tabIn();
3817: geneol("out.write(\"	\")");
3818: gencr("else");
3819: tabIn();
3820: geneol("out.write(msg)");
3821: jw.end();
3822: gencr("else");
3823: tabIn();
3824: geneol("out.write(msg)");
3825: jw.endMethod();
3826: }
3827:
3828: protected void outWrite(String text) throws IOException {
3829: jw.writeEol("out.write(",
3830: JavaUtil.instanceFrom("String", text), ")");
3831: }
3832:
3833: /**
3834: * Generate stuff for special types as needed.
3835: */
3836: public void genSpecialTypes() throws IOException {
3837: if (rootBeanElement.isUsedType("java.util.Calendar")) {
3838: JavaBeansUtil.genReadType(jw, "java.util.Calendar");
3839: JavaBeansUtil.genWriteType(jw, "java.util.Calendar");
3840: jw.cr();
3841: }
3842: if (rootBeanElement.isUsedType("byte[]")) {
3843: JavaBeansUtil.genReadType(jw, "base64Binary");
3844: JavaBeansUtil.genWriteType(jw, "base64Binary");
3845: jw.cr();
3846: }
3847: }
3848:
3849: public void genValidate() throws IOException {
3850: select(BODY_SECTION);
3851: if (beanElement.isRoot && !config.isUseRuntime()) {
3852: String commonBeanType = commonBeanType();
3853: jw
3854: .write("public static class ValidateException extends Exception ");
3855: jw.begin();
3856: jw.write("private ", commonBeanType);
3857: jw.writeEol(" failedBean");
3858: jw.writeEol("private String failedPropertyName");
3859: jw.writeEol("private FailureType failureType");
3860: jw
3861: .write(
3862: "public ValidateException(String msg, String failedPropertyName, ",
3863: commonBeanType, " failedBean) ");
3864: jw.begin();
3865: jw.writeEol("super(msg)");
3866: jw.writeEol("this.failedBean = failedBean");
3867: jw.writeEol("this.failedPropertyName = failedPropertyName");
3868: jw.end();
3869: jw
3870: .write(
3871: "public ValidateException(String msg, FailureType ft, String failedPropertyName, ",
3872: commonBeanType, " failedBean) ");
3873: jw.begin();
3874: jw.writeEol("super(msg)");
3875: jw.writeEol("this.failureType = ft");
3876: jw.writeEol("this.failedBean = failedBean");
3877: jw.writeEol("this.failedPropertyName = failedPropertyName");
3878: jw.end();
3879: jw
3880: .writecr("public String getFailedPropertyName() {return failedPropertyName;}");
3881: jw
3882: .writecr("public FailureType getFailureType() {return failureType;}");
3883: jw.write("public ", commonBeanType);
3884: jw.writecr(" getFailedBean() {return failedBean;}");
3885: jw.write("public static class FailureType ");
3886: jw.begin();
3887: jw.writeEol("private final String name");
3888: jw
3889: .writecr("private FailureType(String name) {this.name = name;}");
3890: jw.writecr("public String toString() { return name;}");
3891: jw
3892: .writeEol("public static final FailureType NULL_VALUE = new FailureType(\"NULL_VALUE\")");
3893: jw
3894: .writeEol("public static final FailureType DATA_RESTRICTION = new FailureType(\"DATA_RESTRICTION\")");
3895: jw
3896: .writeEol("public static final FailureType ENUM_RESTRICTION = new FailureType(\"ENUM_RESTRICTION\")");
3897: jw
3898: .writeEol("public static final FailureType ALL_RESTRICTIONS = new FailureType(\"ALL_RESTRICTIONS\")");
3899: jw
3900: .writeEol("public static final FailureType MUTUALLY_EXCLUSIVE = new FailureType(\"MUTUALLY_EXCLUSIVE\")");
3901: jw.end();
3902: jw.end();
3903: jw.cr();
3904: }
3905: jw
3906: .beginMethod(
3907: "validate",
3908: "",
3909: (config.isUseRuntime() ? "org.netbeans.modules.schema2beans"
3910: : getRootClassName())
3911: + ".ValidateException", "void",
3912: jw.PUBLIC);
3913: genValidateProperties();
3914: if (config.isRespectExtension()
3915: && beanElement.getExtension() != null) {
3916: jw.writeEol("super.validate()");
3917: }
3918: jw.endMethod();
3919: }
3920:
3921: protected void genValidateFail(String detail, String name,
3922: boolean quoteDetail, ValidateException.FailureType ft,
3923: JavaWriter out) throws IOException {
3924: out.write("throw new ");
3925: String validateException;
3926: if (config.isUseRuntime())
3927: validateException = "org.netbeans.modules.schema2beans.ValidateException";
3928: else
3929: validateException = getRootClassName()
3930: + ".ValidateException";
3931: out.write(validateException);
3932: out.write("(");
3933: if (quoteDetail)
3934: out.write('"');
3935: out.write(detail);
3936: if (quoteDetail)
3937: out.write('"');
3938: out.write(", ", validateException + ".FailureType.", ft
3939: .toString());
3940: out.writeEolNoI18N(", \"" + name + "\", this)");
3941: }
3942:
3943: public void genPropertyEvents() throws IOException {
3944: select(DECL_SECTION);
3945: gen(PRIVATE, "java.beans.PropertyChangeSupport",
3946: "eventListeners");
3947: eol();
3948: select(BODY_SECTION);
3949: jw.beginMethod("addPropertyChangeListener",
3950: "java.beans.PropertyChangeListener listener", null,
3951: "void", jw.PUBLIC | jw.BEANINFO);
3952: jw.beginIf("eventListeners == null");
3953: geneol("eventListeners = new java.beans.PropertyChangeSupport(this)");
3954: end();
3955: geneol("eventListeners.addPropertyChangeListener(listener)");
3956: genCallMethodOnBeans(".addPropertyChangeListener(listener)");
3957: jw.endMethod();
3958:
3959: jw.beginMethod("removePropertyChangeListener",
3960: "java.beans.PropertyChangeListener listener", null,
3961: "void", jw.PUBLIC | jw.BEANINFO);
3962: genCallMethodOnBeans(".removePropertyChangeListener(listener)");
3963: jw.beginIf("eventListeners == null");
3964: geneol("return");
3965: end();
3966: geneol("eventListeners.removePropertyChangeListener(listener)");
3967: jw.beginIf("!eventListeners.hasListeners(null)");
3968: geneol("eventListeners = null");
3969: jw.end();
3970: jw.endMethod();
3971:
3972: jw.beginMethod("_setPropertyChangeSupport",
3973: "java.beans.PropertyChangeSupport listeners", null,
3974: "void", jw.PUBLIC);
3975: jw.writeEol("eventListeners = listeners");
3976: genCallMethodOnBeans("._setPropertyChangeSupport(listeners)");
3977: jw.endMethod();
3978: }
3979:
3980: protected void genCallMethodOnBeans(String methodParams)
3981: throws IOException {
3982: int size = attrList.size();
3983: for (int i = 0; i < size; i++) {
3984: Property a = (Property) attrList.get(i);
3985: if (!a.isBean)
3986: continue;
3987: boolean indexed = a.isIndexed();
3988: String attr = "_" + a.name;
3989: String type = a.getType().intern();
3990: String baseType = type;
3991: if (indexed) {
3992: beginAttrIterator(attr, a, "element");
3993: attr = "element";
3994: }
3995: jw.beginIf(attr, " != null");
3996: jw.writeEol(attr, methodParams);
3997: jw.end();
3998: if (indexed)
3999: jw.end();
4000: }
4001: }
4002:
4003: public void genStoreEvents() throws IOException {
4004: select(DECL_SECTION);
4005: gen(PROTECTED, "boolean", "storeEvents = false");
4006: eol();
4007: gen(PRIVATE, "java.util.List",
4008: "storedEvents = new java.util.LinkedList()");
4009: eol();
4010: select(BODY_SECTION);
4011: jw.beginMethod("fireStoredEvents");
4012: jw.beginIf("eventListeners == null");
4013: geneol("storedEvents.clear()");
4014: geneol("return");
4015: end();
4016: /*
4017: comment("Compress the events");
4018: geneol("java.util.Map uniqueEvents = new java.util.HashMap(storedEvents.size()*4)");
4019: gen("for (java.util.Iterator it = storedEvents.iterator(); it.hasNext(); ) ");
4020: begin();
4021: geneol("java.beans.PropertyChangeEvent event = (java.beans.PropertyChangeEvent) it.next()");
4022: comment("The last event by the property name is stored in uniqueEvents");
4023: gencr("if (uniqueEvents.containsKey(event.getPropertyName())");
4024: tabIn();
4025: geneol("uniqueEvents.put(event.getPropertyName(), event)");
4026: end();
4027: */
4028: jw.beginFor("java.util.Iterator it = storedEvents.iterator()",
4029: "it.hasNext()", "");
4030: geneol("java.beans.PropertyChangeEvent event = (java.beans.PropertyChangeEvent) it.next()");
4031: /*gencr("if (uniqueEvents.get(event.getPropertyName()) == event)");
4032: tabIn();*/
4033: geneol("eventListeners.firePropertyChange(event)");
4034: end();
4035: geneol("storedEvents.clear()");
4036: end();
4037: cr();
4038: }
4039:
4040: public void genVetoable() throws IOException {
4041: select(DECL_SECTION);
4042: gen(PRIVATE, "java.beans.VetoableChangeSupport", "vetos");
4043: eol();
4044: select(BODY_SECTION);
4045: jw.beginMethod("addVetoableChangeListener",
4046: "java.beans.VetoableChangeListener listener", null,
4047: "void", jw.PUBLIC | jw.BEANINFO);
4048: jw.beginIf("vetos == null");
4049: geneol("vetos = new java.beans.VetoableChangeSupport(this)");
4050: end();
4051: geneol("vetos.addVetoableChangeListener(listener)");
4052: end();
4053: cr();
4054: jw.beginMethod("removePropertyChangeListener",
4055: "java.beans.VetoableChangeListener listener", null,
4056: "void", jw.PUBLIC | jw.BEANINFO);
4057: jw.beginIf("vetos == null");
4058: geneol("return");
4059: end();
4060: geneol("vetos.removeVetoableChangeListener(listener)");
4061: jw.beginIf("!vetos.hasListeners(null)");
4062: geneol("vetos = null");
4063: end();
4064: end();
4065: cr();
4066: jw.beginMethod("_setVetoableChangeSupport",
4067: "java.beans.VetoableChangeSupport vs", "void", null,
4068: jw.PACKAGE_LEVEL);
4069: geneol("vetos = vs");
4070: end();
4071: cr();
4072: }
4073:
4074: public void genElementPositions() throws IOException {
4075: select(BODY_SECTION);
4076: jw.beginMethod("fetchChildByPosition", "int position", null,
4077: "java.lang.Object", jw.PUBLIC);
4078: jw.beginIf("elementTypesByPosition[position] == "
4079: + elementTypeSetnull);
4080: jw
4081: .writeEol("throw new IndexOutOfBoundsException(\"position \"+position+\" out of bounds\")");
4082: jw.end();
4083: jw.writeEol("return elementsByPosition[position]");
4084: jw.endMethod();
4085: jw.beginMethod("fetchChildCount", "", null, "int", jw.PUBLIC);
4086: jw.writeEol("int position = elementTypesByPosition.length");
4087: jw.beginFor("",
4088: "position > 0 && elementTypesByPosition[position-1] == "
4089: + elementTypeSetnull, "--position");
4090: jw.writeEol("");
4091: jw.end();
4092: jw.writeEol("return position");
4093: jw.endMethod();
4094: jw.beginMethod("expandElementsByPosition", "int size", null,
4095: "void", jw.PROTECTED);
4096: jw.beginIf("elementTypesByPosition.length >= size");
4097: jw.writeEol("return");
4098: jw.end();
4099: jw.writeEol("int newSize = elementTypesByPosition.length+8");
4100: jw
4101: .writeEol("java.lang.Object[] newElementsByPosition = new java.lang.Object[newSize]");
4102: jw
4103: .writeEol("int[] newElementTypesByPosition = new int[newSize]");
4104: jw
4105: .writeEol("System.arraycopy(elementsByPosition, 0, newElementsByPosition, 0, elementTypesByPosition.length)");
4106: jw
4107: .writeEol("System.arraycopy(elementTypesByPosition, 0, newElementTypesByPosition, 0, elementTypesByPosition.length)");
4108: jw.beginFor("int i = elementTypesByPosition.length",
4109: "i < newSize", "++i");
4110: jw.writeEol("newElementTypesByPosition[i] = "
4111: + elementTypeSetnull);
4112: jw.end();
4113: jw
4114: .writeEol("elementTypesByPosition = newElementTypesByPosition");
4115: jw.writeEol("elementsByPosition = newElementsByPosition");
4116: jw.endMethod();
4117:
4118: jw.beginMethod("insertElementByPosition",
4119: "int position, Object element, int elementType", null,
4120: "void", jw.PROTECTED);
4121: jw.writeEol("int childCount = fetchChildCount()");
4122: jw.writeEol("expandElementsByPosition(childCount+1)");
4123: jw.writeEol("int i = childCount - 1");
4124: jw.beginFor("", "i >= position", "--i");
4125: jw
4126: .writeEol("elementsByPosition[i + 1] = elementsByPosition[i]");
4127: jw
4128: .writeEol("elementTypesByPosition[i + 1] = elementTypesByPosition[i]");
4129: jw.end();
4130: jw.writeEol("elementTypesByPosition[position] = elementType");
4131: jw.writeEol("elementsByPosition[position] = element");
4132: jw.comment("assert childCount == fetchChildCount() + 1;");
4133: jw.endMethod();
4134:
4135: jw.beginMethod("deleteElement", "int position", null, "void",
4136: jw.PROTECTED);
4137: jw.writeEol("int i = position+1");
4138: jw.writeEol("int size = elementTypesByPosition.length");
4139: jw.beginFor("", "i < size && elementTypesByPosition[i] != "
4140: + elementTypeSetnull, "++i");
4141: jw
4142: .writeEol("elementsByPosition[i - 1] = elementsByPosition[i]");
4143: jw
4144: .writeEol("elementTypesByPosition[i - 1] = elementTypesByPosition[i]");
4145: //jw.writeEol("System.out.println(\"elementTypesByPosition[\"+(i-1)+\"]=\"+elementTypesByPosition[i-1])");
4146: jw.end();
4147: jw.writeEol("elementTypesByPosition[i-1] = "
4148: + elementTypeSetnull);
4149: jw.writeEol("elementsByPosition[i - 1] = null");
4150: jw.comment("assert size == fetchChildCount() - 1;");
4151: jw.endMethod();
4152:
4153: jw.beginMethod("findFirstOfElementType", "int elementType",
4154: null, "int", jw.PROTECTED);
4155: jw.writeEol("int maxPos = elementTypesByPosition.length");
4156: jw.writeEol("int pos = 0");
4157: jw
4158: .beginFor(
4159: "",
4160: "pos < maxPos && elementTypesByPosition[pos] < elementType",
4161: "++pos");
4162: jw.end();
4163: jw.writeEol("return pos");
4164: jw.endMethod();
4165:
4166: jw.beginMethod("findElementType", "int elementType, int index",
4167: null, "int", jw.PROTECTED);
4168: jw.writeEol("int maxPos = elementTypesByPosition.length");
4169: jw.writeEol("int pos = 0");
4170: jw.beginFor("", "pos < maxPos", "++pos");
4171: jw.beginIf("elementTypesByPosition[pos] == elementType");
4172: jw.beginIf("index <= 0");
4173: jw.writeEol("return pos");
4174: jw.end();
4175: jw.writeEol("--index");
4176: jw.end();
4177: jw.end();
4178: jw.writeEol("return pos");
4179: jw.endMethod();
4180:
4181: jw.beginMethod("findLastOfElementType", "int elementType",
4182: null, "int", jw.PROTECTED);
4183: jw.writeEol("int childCount = elementTypesByPosition.length");
4184: jw.writeEol("int pos = childCount - 1");
4185: // We can't do a binary search here, since the element types are
4186: // not always sorted.
4187: jw.beginFor("", "pos >=0", "--pos");
4188: jw.beginIf("elementTypesByPosition[pos] == "
4189: + elementTypeSetnull);
4190: jw.writeEol("--childCount");
4191: jw.writeEol("continue");
4192: jw.end();
4193: jw.beginIf("elementTypesByPosition[pos] <= elementType");
4194: jw.writeEol("return pos");
4195: jw.end();
4196: jw.end();
4197: //jw.beginIf("childCount < 0");
4198: //jw.writeEol("return 0");
4199: //jw.end();
4200: jw.writeEol("return childCount");
4201: jw.endMethod();
4202: }
4203:
4204: protected void genProcessDocType() throws IOException {
4205: String fullDocTypeName;
4206: fullDocTypeName = fullClassName + ".DocType";
4207: select(DECL_SECTION);
4208: jw.writeEol("private " + fullDocTypeName + " docType = null");
4209: select(ACCESS_SECTION);
4210: jw.beginMethod("fetchDocType", "", null, fullDocTypeName,
4211: jw.PUBLIC);
4212: jw.writeEol("return docType");
4213: jw.endMethod();
4214: jw.beginMethod("changeDocType", fullDocTypeName + " dt", null,
4215: "void", jw.PUBLIC);
4216: jw.writeEol("docType = dt");
4217: jw.endMethod();
4218: jw.beginMethod("changeDocType",
4219: "String publicId, String systemId", null, "void",
4220: jw.PUBLIC);
4221: jw.writeEol("docType = new ", fullDocTypeName,
4222: "(publicId, systemId)");
4223: jw.endMethod();
4224: select(BODY_SECTION);
4225: jw.write("public static class DocType ");
4226: jw.begin();
4227: jw.writeEol("private org.w3c.dom.NamedNodeMap entities");
4228: jw.writeEol("private String internalSubset");
4229: jw.writeEol("private String name");
4230: jw.writeEol("private org.w3c.dom.NamedNodeMap notations");
4231: jw.writeEol("private String publicId");
4232: jw.writeEol("private String systemId");
4233: jw.cr();
4234: jw.write("public DocType(" + fullDocTypeName + " docType) ");
4235: jw.begin();
4236: jw.writeEol("entities = docType.getEntities()");
4237: jw.writeEol("internalSubset = docType.getInternalSubset()");
4238: jw.writeEol("name = docType.getName()");
4239: jw.writeEol("notations = docType.getNotations()");
4240: jw.writeEol("publicId = docType.getPublicId()");
4241: jw.writeEol("systemId = docType.getSystemId()");
4242: jw.endMethod();
4243: jw.write("public DocType(org.w3c.dom.DocumentType docType) ");
4244: jw.begin();
4245: jw.writeEol("entities = docType.getEntities()");
4246: jw.writeEol("internalSubset = docType.getInternalSubset()");
4247: jw.writeEol("name = docType.getName()");
4248: jw.writeEol("notations = docType.getNotations()");
4249: jw.writeEol("publicId = docType.getPublicId()");
4250: jw.writeEol("systemId = docType.getSystemId()");
4251: jw.endMethod();
4252: jw.write("public DocType(String publicId, String systemId) ");
4253: jw.begin();
4254: jw.writeEol("this(\"", beanElement.node.getName(),
4255: "\", publicId, systemId)");
4256: jw.end();
4257: jw.cr();
4258: jw
4259: .write("public DocType(String name, String publicId, String systemId) ");
4260: jw.begin();
4261: jw.writeEol("this.name = name");
4262: jw.writeEol("this.publicId = publicId");
4263: jw.writeEol("this.systemId = systemId");
4264: jw.endMethod();
4265: jw.write("public org.w3c.dom.NamedNodeMap getEntities() ");
4266: jw.begin();
4267: jw.writeEol("return entities");
4268: jw.endMethod();
4269: jw.write("public String getInternalSubset() ");
4270: jw.begin();
4271: jw.writeEol("return internalSubset");
4272: jw.endMethod();
4273: jw.write("public String getName() ");
4274: jw.begin();
4275: jw.writeEol("return name");
4276: jw.endMethod();
4277: jw.write("public org.w3c.dom.NamedNodeMap getNotations() ");
4278: jw.begin();
4279: jw.writeEol("return notations");
4280: jw.endMethod();
4281: jw.write("public String getPublicId() ");
4282: jw.begin();
4283: jw.writeEol("return publicId");
4284: jw.endMethod();
4285: jw.write("public String getSystemId() ");
4286: jw.begin();
4287: jw.writeEol("return systemId");
4288: jw.endMethod();
4289: jw.write("public String toString() ");
4290: jw.begin();
4291: jw.writeEol("String result = \"<!DOCTYPE \"");
4292: jw.writeEol("result += name");
4293: jw.beginIf("publicId != null");
4294: jw.writeEol("result += \" PUBLIC \\\"\"");
4295: jw.writeEol("result += publicId"); // should be printXML
4296: jw.writeEol("result += \"\\\"\"");
4297: jw.beginIf("systemId == null");
4298: jw.writeEol("systemId = \"SYSTEM\"");
4299: jw.end();
4300: jw.end();
4301: jw.beginIf("systemId != null");
4302: jw.writeEol("result += \" \\\"\"");
4303: jw.writeEol("result += systemId");
4304: jw.writeEol("result += \"\\\"\"");
4305: jw.end();
4306: jw.beginIf("entities != null");
4307: jw.writeEol("int length = entities.getLength()");
4308: jw.beginIf("length > 0");
4309: jw.writeEol("result += \" [\"");
4310: jw.beginFor("int i = 0", "i < length", "++i");
4311: jw.writeEol("org.w3c.dom.Node node = entities.item(i)");
4312: jw.writeEol("result += \"<\"+node.getNodeName()+\">\"");
4313: jw.writeEol("result += node.getNodeValue()");
4314: jw.writeEol("result += \"</\"+node.getNodeName()+\">\"");
4315: jw.end();
4316: jw.writeEol("result += \"]\"");
4317: jw.end();
4318: jw.end();
4319: jw.writeEol("result += \">\"");
4320: jw.writeEol("return result");
4321: jw.end();
4322:
4323: jw.endMethod();
4324: }
4325:
4326: public void genTransactions() throws IOException {
4327: select(BODY_SECTION);
4328: jw.beginMethod("beginTransaction");
4329: //geneol("inTransaction = true");
4330: geneol("storeEvents = true");
4331: comment("Tell each child bean to also beginTransaction");
4332: genCallMethodOnSubBeans("beginTransaction()");
4333: end();
4334: cr();
4335:
4336: jw.beginMethod("commit");
4337: geneol("storeEvents = false");
4338: comment("Tell each child bean to also commit");
4339: genCallMethodOnSubBeans("commit()");
4340: geneol("fireStoredEvents()");
4341: end();
4342: cr();
4343:
4344: jw.beginMethod("rollback");
4345: comment("Go over all of the events and put them back");
4346: geneol("java.beans.PropertyChangeEvent[] events = new java.beans.PropertyChangeEvent[storedEvents.size()]");
4347: jw.beginFor("int i = events.length-1", "i >= 0", "--i");
4348: geneol("java.beans.PropertyChangeEvent event = events[i]");
4349: //jw.beginIf("event.getOldValue() == null");
4350: geneol("changePropertyByName(event.getPropertyName(), event.getOldValue())");
4351: end();
4352: // we might be able to speed things up by setting eventListeners = null
4353: geneol("storeEvents = false");
4354: geneol("storedEvents.clear()");
4355: comment("Tell each child bean to also rollback");
4356: genCallMethodOnSubBeans("rollback()");
4357: end();
4358: cr();
4359: }
4360:
4361: protected void genCallMethodOnSubBeans(String methodName)
4362: throws IOException {
4363: int size = attrList.size();
4364: for (int i = 0; i < size; i++) {
4365: Property a = (Property) attrList.get(i);
4366: boolean indexed = a.isIndexed();
4367: String attr = "_" + a.name;
4368: String type = a.getType();
4369: if (a.isBean) {
4370: String value = attr;
4371: if (indexed) {
4372: beginAttrIterator(attr, a, "element");
4373: value = "element";
4374: }
4375: gencr("if (" + value + " != null)");
4376: tabIn();
4377: gen(value);
4378: gen(".");
4379: geneol(methodName);
4380: if (indexed)
4381: end();
4382: }
4383: }
4384: }
4385:
4386: public void genPropertiesByName() throws IOException {
4387: jw.beginMethod("changePropertyByName",
4388: "String name, Object value", null, "void", jw.PUBLIC);
4389: geneol("if (name == null) return");
4390: geneol("name = name.intern()");
4391: int size = attrList.size();
4392: for (int i = 0; i < size; i++) {
4393: Property a = (Property) attrList.get(i);
4394: boolean indexed = a.isIndexed();
4395: boolean isScalar = a.isScalar();
4396: String attr = "_" + a.name;
4397: String type = a.getType();
4398: if (i > 0)
4399: gen("else ");
4400: gencr("if (name == \"" + a.beanIntrospectorName() + "\")");
4401: tabIn();
4402: if (indexed) {
4403: gen("add" + a.name);
4404: geneol("(" + JavaUtil.fromObject(type, "value") + ")");
4405: gen("else ");
4406: gencr("if (name == \"" + a.beanIntrospectorName()
4407: + "[]\")");
4408: tabIn();
4409: gen(a.getWriteMethod() + "(");
4410: geneol("(" + type + "[]) value)");
4411: } else {
4412: gen(a.getWriteMethod() + "(");
4413: gen(JavaUtil.fromObject(type, "value"));
4414: geneol(")");
4415: }
4416: }
4417: if (size > 0) {
4418: gencr("else");
4419: tabIn();
4420: if (config.isRespectExtension()
4421: && beanElement.getExtension() != null) {
4422: jw.writeEol("super.changePropertyByName(name, value)");
4423: } else {
4424: genInvalidName("name");
4425: }
4426: }
4427: genMadeChange();
4428: end();
4429: cr();
4430:
4431: jw.beginMethod("fetchPropertyByName", "String name", null,
4432: "Object", jw.PUBLIC);
4433: for (int i = 0; i < size; i++) {
4434: Property a = (Property) attrList.get(i);
4435: boolean indexed = a.isIndexed();
4436: String type = a.getType();
4437: gen("if (name == \"" + a.beanIntrospectorName());
4438: if (indexed)
4439: gen("[]");
4440: gencr("\")");
4441: tabIn();
4442: jw.write("return ");
4443: if (indexed)
4444: jw.writeEol(a.getReadMethod(false) + "()");
4445: else
4446: jw.writeEol(JavaUtil.toObject(a.getReadMethod(false)
4447: + "()", type, config.isForME()));
4448: }
4449: if (config.isRespectExtension()
4450: && beanElement.getExtension() != null) {
4451: jw.writeEol("return super.fetchPropertyByName(name)");
4452: } else {
4453: genInvalidName("name");
4454: }
4455: jw.endMethod();
4456: }
4457:
4458: public void genEqualsHashCode() throws IOException {
4459: select(EQUALS_SECTION);
4460: jw
4461: .beginMethod("equals", "Object o", null, "boolean",
4462: jw.PUBLIC);
4463: jw.write("return o instanceof ", fullClassName);
4464: jw.writeEol(" && equals((", fullClassName, ") o)");
4465: jw.endMethod();
4466:
4467: jw.beginMethod("equals", fullClassName + " inst", null,
4468: "boolean", jw.PUBLIC);
4469: jw.beginIf("inst == this");
4470: jw.writeEol("return true");
4471: jw.end();
4472: jw.beginIf("inst == null");
4473: jw.writeEol("return false");
4474: jw.end();
4475:
4476: select(HASHCODE_SECTION);
4477: jw.beginMethod("hashCode", "", null, "int", jw.PUBLIC);
4478: geneol("int result = 17");
4479:
4480: boolean isArrayStyle = (config.getIndexedPropertyType() == null);
4481: int size = attrList.size();
4482: for (int i = 0; i < size; i++) {
4483: Property a = (Property) attrList.get(i);
4484: boolean indexed = a.isIndexed();
4485: boolean isWrapper = false;
4486: MetaElement me = getMetaElement(a);
4487: boolean isScalar = a.isScalar();
4488:
4489: String type = a.getType().intern();
4490: String baseType = type;
4491: if (indexed)
4492: type = (baseType + "[]").intern();
4493:
4494: String attr = "_" + a.name;
4495: String baseAttr = attr;
4496: String otherAttr = "inst." + attr;
4497: select(EQUALS_SECTION);
4498: if (indexed) {
4499: gen("if (size" + a.name + "() != inst.size" + a.name
4500: + "())");
4501: cr();
4502: tabIn();
4503: gen("return false");
4504: eol();
4505: comment("Compare every element.");
4506: String fullType = getTypeFullClassName(a);
4507: if (isArrayStyle) {
4508: jw.beginFor("int pos = 0, size = size" + a.name
4509: + "()", "pos < size", "++pos");
4510: attr = attr + "[pos]";
4511: otherAttr = otherAttr + "[pos]";
4512: } else {
4513: jw.beginFor("java.util.Iterator it = " + attr
4514: + ".iterator(), it2 = inst." + attr
4515: + ".iterator()",
4516: "it.hasNext() && it2.hasNext()", "");
4517: gen(fullType);
4518: gen(" element = ");
4519: geneol(JavaUtil.fromObject(fullType, "it.next()"));
4520: gen(fullType);
4521: gen(" element2 = ");
4522: geneol(JavaUtil.fromObject(fullType, "it2.next()"));
4523: attr = "element";
4524: otherAttr = "element2";
4525: }
4526: }
4527: if (!indexed && config.isOptionalScalars() && a.isScalar()) {
4528: jw.beginIf(a.getScalarIsSet() + " != inst."
4529: + a.getScalarIsSet());
4530: jw.writeEol("return false");
4531: jw.end();
4532: jw.beginIf(a.getScalarIsSet());
4533: }
4534: jw.beginIf("!("
4535: + JavaUtil.genEquals(baseType, attr, otherAttr)
4536: + ")");
4537: jw.writeEol("return false");
4538: jw.end();
4539: if (!indexed && config.isOptionalScalars() && a.isScalar()) {
4540: jw.end();
4541: }
4542: if (indexed) {
4543: end();
4544: }
4545:
4546: select(HASHCODE_SECTION);
4547: gen("result = 37*result + (");
4548: if (isScalar) {
4549: if (!indexed && config.isOptionalScalars()
4550: && a.isScalar())
4551: gen(a.getScalarIsSet(), " ? 0 : (");
4552: gen(JavaUtil.exprToInt(type, baseAttr));
4553: if (!indexed && config.isOptionalScalars()
4554: && a.isScalar())
4555: gen(")");
4556: } else
4557: gen(baseAttr + " == null ? 0 : " + baseAttr
4558: + ".hashCode()");
4559: gen(")");
4560: eol();
4561: }
4562: select(EQUALS_SECTION);
4563: if (config.isRespectExtension()
4564: && beanElement.getExtension() != null) {
4565: jw.writeEol("return super.equals(inst)");
4566: } else {
4567: jw.writeEol("return true");
4568: }
4569: jw.endMethod();
4570:
4571: select(HASHCODE_SECTION);
4572: if (config.isRespectExtension()
4573: && beanElement.getExtension() != null) {
4574: jw.writeEol("result = 37*result + super.hashCode()");
4575: }
4576: jw.writeEol("return result");
4577: jw.endMethod();
4578: }
4579:
4580: void genDeepCopy() throws IOException {
4581: select(CONSTRUCTOR_SECTION);
4582: jw.bigComment("Deep copy");
4583: jw.beginConstructor(className, fullClassName + " source");
4584: jw.writeEol("this(source, false)");
4585: jw.end();
4586: jw.cr();
4587: jw
4588: .bigComment("Deep copy\n@param justData just copy the XML relevant data");
4589: jw.beginConstructor(className, fullClassName
4590: + " source, boolean justData");
4591: if (config.isGenerateParentRefs()) {
4592: jw.writeEol("this(source, null, justData)");
4593: jw.end();
4594: jw.cr();
4595: jw.bigComment("Deep copy");
4596: jw.beginConstructor(className, fullClassName + " source, "
4597: + parentBeanType() + " parent, boolean justData");
4598: }
4599: if (config.isRespectExtension()
4600: && beanElement.getExtension() != null) {
4601: if (config.isGenerateParentRefs()) {
4602: jw.writeEol("super(source, parent, justData)");
4603: } else {
4604: jw.writeEol("super(source, justData)");
4605: }
4606: }
4607: genExtendBaseBeanConstructor();
4608: //jw.writeEol("System.out.println(\"Deep copy for "+className+"\")");
4609: if (config.isGenerateParentRefs()) {
4610: jw.writeEol("this.parent = parent");
4611: }
4612: for (int i = 0, size = attrList.size(); i < size; i++) {
4613: Property a = (Property) attrList.get(i);
4614: boolean indexed = a.isIndexed();
4615: boolean isScalar = a.isScalar();
4616: String attr = "_" + a.name;
4617: String type = a.getType().intern();
4618: String fullClassType = getTypeFullClassName(a);
4619: boolean isArrayStyle = (config.getIndexedPropertyType() == null);
4620:
4621: String nextElement = "it.next()";
4622: if (indexed) {
4623: if (isArrayStyle) {
4624: jw.writeEol(attr, " = new ", fullClassType,
4625: "[source." + attr + ".length]");
4626: jw.beginFor("int index = 0", "index < source."
4627: + attr + ".length", "++index");
4628: nextElement = "source." + attr + "[index]";
4629: } else {
4630: jw.beginFor("java.util.Iterator it = source."
4631: + attr + ".iterator()", "it.hasNext()", "");
4632: }
4633: }
4634: //System.out.println("type="+type+" needToCallClone="+needToCallClone+" mutable="+mutable);
4635: String getter;
4636: if (indexed) {
4637: getter = "srcElement";
4638: if (isScalar) {
4639: if (config.jdkTarget < 150) {
4640: // It's already in object format, it would be silly to
4641: // convert back and forth.
4642: jw.write("Object");
4643: } else {
4644: jw.write(JavaUtil.toObjectType(fullClassType));
4645: }
4646: jw.write(" ", getter, " = ");
4647: if (config.jdkTarget >= 150)
4648: jw.write("("
4649: + JavaUtil.toObjectType(fullClassType)
4650: + ") ");
4651: jw.writeEol(nextElement);
4652: } else {
4653: jw.writeEol(fullClassType + " ", getter, " = ",
4654: isArrayStyle ? nextElement : JavaUtil
4655: .fromObject(fullClassType,
4656: nextElement));
4657: }
4658: } else
4659: getter = "source." + attr;
4660:
4661: if (type == "byte[]") {
4662: jw.writeEol("byte[] destElement" + attr,
4663: " = new byte[", getter, ".length]");
4664: jw.writeEol("System.arraycopy(", getter
4665: + ", 0, destElement" + attr + ", 0, ", getter,
4666: ".length)");
4667: }
4668: gen(attr);
4669: if (indexed) {
4670: if (isArrayStyle) {
4671: jw.write("[index] = ");
4672: } else {
4673: gen(".add(");
4674: }
4675: } else {
4676: gen(" = ");
4677: }
4678: if (type == "byte[]") {
4679: jw.write("destElement", attr);
4680: } else {
4681: genCopy(getter, a, "justData");
4682: }
4683: if (indexed && !isArrayStyle) {
4684: jw.write(")");
4685: }
4686: jw.eol();
4687: if (!indexed && config.isOptionalScalars() && a.isScalar()) {
4688: jw.writeEol(a.getScalarIsSet(), " = source.", a
4689: .getScalarIsSet());
4690: }
4691: if (indexed) {
4692: jw.end();
4693: }
4694: }
4695: if (beanElement.isRoot && config.isProcessDocType()) {
4696: String fullDocTypeName;
4697: if (packageName == null)
4698: fullDocTypeName = className + ".DocType";
4699: else
4700: fullDocTypeName = packageName + "." + className
4701: + ".DocType";
4702: jw.beginIf("source.docType != null");
4703: jw.writeEol("docType = new " + fullDocTypeName
4704: + "(source.docType)");
4705: jw.end();
4706: }
4707: if (beanElement.isRoot) {
4708: jw.writeEol("schemaLocation = source.schemaLocation");
4709: }
4710: boolean firstJustData = true;
4711: if (config.isGenerateStoreEvents()) {
4712: if (firstJustData) {
4713: firstJustData = false;
4714: jw.beginIf("!justData");
4715: }
4716: geneol("storeEvents = source.storeEvents");
4717: }
4718: if (config.isVetoable()) {
4719: if (firstJustData) {
4720: firstJustData = false;
4721: jw.beginIf("!justData");
4722: }
4723: geneol("vetos = source.vetos");
4724: }
4725: if (config.isGeneratePropertyEvents()) {
4726: if (firstJustData) {
4727: firstJustData = false;
4728: jw.beginIf("!justData");
4729: }
4730: jw.beginIf("source.eventListeners != null");
4731: jw
4732: .writeEol("eventListeners = new java.beans.PropertyChangeSupport(this)");
4733: jw
4734: .writeEol("java.beans.PropertyChangeListener[] theListeners = source.eventListeners.getPropertyChangeListeners()");
4735: jw.beginFor("int i = 0", "i < theListeners.length", "++i");
4736: jw
4737: .writeEol("eventListeners.addPropertyChangeListener(theListeners[i])");
4738: jw.end();
4739: jw.end();
4740: }
4741: if (!firstJustData)
4742: jw.end();
4743: end();
4744: cr();
4745: }
4746:
4747: public void genHasChanged() throws IOException {
4748: if (beanElement.isRoot) {
4749: select(DECL_SECTION);
4750: jw.writeEol("private boolean changed");
4751: }
4752: select(BODY_SECTION);
4753: jw
4754: .bigComment("Change the result of _hasChanged().\nThis is done automatically when a value in this bean graph gets changed.");
4755: jw.beginMethod("_setChanged", "boolean changed", null, "void",
4756: jw.PUBLIC);
4757: if (beanElement.isRoot) {
4758: jw.writeEol("this.changed = changed");
4759: } else {
4760: jw.beginIf("parent != null");
4761: jw.writeEol("parent._setChanged(changed)");
4762: jw.end();
4763: }
4764: jw.endMethod();
4765: if (beanElement.isRoot) {
4766: jw
4767: .bigComment("@return true if something has changed in this bean graph.");
4768: jw.beginMethod("_hasChanged", "", null, "boolean",
4769: jw.PUBLIC);
4770: jw.writeEol("return changed");
4771: jw.endMethod();
4772: }
4773: }
4774:
4775: public void genChildBeans() throws IOException {
4776: select(BODY_SECTION);
4777: jw
4778: .bigComment("Return an array of all of the properties that are beans and are set.");
4779: String cb = config.isExtendBaseBean() ? "org.netbeans.modules.schema2beans.BaseBean"
4780: : commonBeanType();
4781: jw.beginMethod("childBeans", "boolean recursive", null, cb
4782: + "[]", jw.PUBLIC);
4783: jw.write("java.util.List");
4784: if (config.jdkTarget >= 150)
4785: jw.write("<", cb, ">");
4786: jw.write(" children = new java.util.LinkedList");
4787: if (config.jdkTarget >= 150)
4788: jw.write("<", cb, ">");
4789: jw.writeEol("()");
4790: jw.writeEol("childBeans(recursive, children)");
4791: jw.writeEol(cb + "[] result = new " + cb + "[children.size()]");
4792: jw.writeEol("return (" + cb + "[]) children.toArray(result)");
4793: jw.endMethod();
4794: jw.bigComment("Put all child beans into the beans list.");
4795: String childBeansArgs = "boolean recursive, java.util.List";
4796: if (config.jdkTarget >= 150)
4797: childBeansArgs += "<" + cb + ">";
4798: childBeansArgs += " beans";
4799: jw.beginMethod("childBeans", childBeansArgs, null, "void",
4800: jw.PUBLIC);
4801: for (int i = 0, size = attrList.size(); i < size; i++) {
4802: Property a = (Property) attrList.get(i);
4803: if (!a.isBean)
4804: continue;
4805: boolean indexed = a.isIndexed();
4806: String type = a.getType().intern();
4807: String attr = "_" + a.name;
4808: if (indexed) {
4809: beginAttrIterator(attr, a, "element");
4810: attr = "element";
4811: }
4812: jw.beginIf(attr + " != null");
4813: jw.beginIf("recursive");
4814: jw.writeEol(attr + ".childBeans(true, beans)");
4815: jw.end();
4816: jw.writeEol("beans.add(" + attr + ")");
4817: jw.end();
4818: if (indexed) {
4819: jw.end();
4820: }
4821: }
4822: if (config.isRespectExtension()
4823: && beanElement.getExtension() != null) {
4824: jw.writeEol("super.childBeans(recursive, beans)");
4825: }
4826: jw.endMethod();
4827: }
4828:
4829: public void genName() throws IOException {
4830: select(BODY_SECTION);
4831: jw.beginMethod("nameSelf", "", null, "String", jw.PUBLIC);
4832: if (config.isGenerateParentRefs()) {
4833: jw.beginIf("parent != null");
4834: jw.writeEol("String parentName = parent.nameSelf()");
4835: jw
4836: .writeEol("String myName = parent.nameChild(this, false, false)");
4837: jw.writeEol("return parentName + \"/\" + myName");
4838: jw.end();
4839: }
4840: String beanName = Common
4841: .convertName(beanElement.node.getName());
4842: if (beanElement.isRoot)
4843: jw.writeEol("return \"/" + beanName + "\"");
4844: else
4845: jw.writeEol("return \"" + beanName + "\"");
4846: jw.endMethod();
4847:
4848: jw.beginMethod("nameChild", "Object childObj", null, "String",
4849: jw.PUBLIC);
4850: jw.writeEol("return nameChild(childObj, false, false)");
4851: jw.endMethod();
4852:
4853: jw
4854: .bigComment("@param childObj The child object to search for\n@param returnSchemaName Whether or not the schema name should be returned or the property name\n@return null if not found");
4855: jw
4856: .beginMethod(
4857: "nameChild",
4858: "Object childObj, boolean returnConstName, boolean returnSchemaName",
4859: null, "String", jw.PUBLIC);
4860: jw
4861: .writeEol("return nameChild(childObj, returnConstName, returnSchemaName, false)");
4862: jw.endMethod();
4863:
4864: jw
4865: .bigComment("@param childObj The child object to search for\n@param returnSchemaName Whether or not the schema name should be returned or the property name\n@return null if not found");
4866: jw
4867: .beginMethod(
4868: "nameChild",
4869: "Object childObj, boolean returnConstName, boolean returnSchemaName, boolean returnXPathName",
4870: null, "String", jw.PUBLIC);
4871: // Sort by type
4872: Map typeMap = new HashMap(); // Map<String, List<Property>>
4873: for (int i = 0, size = attrList.size(); i < size; i++) {
4874: Property prop = (Property) attrList.get(i);
4875: String type = JavaUtil.toObjectType(prop.getType());
4876: if (type.equals("String"))
4877: type = "java.lang.String"; // normalize
4878: List lst = (List) typeMap.get(type);
4879: if (lst == null) {
4880: lst = new LinkedList();
4881: typeMap.put(type, lst);
4882: }
4883: lst.add(prop);
4884: }
4885: for (Iterator types = typeMap.keySet().iterator(); types
4886: .hasNext();) {
4887: String type = (String) types.next();
4888: jw.beginIf("childObj instanceof " + type);
4889: jw.writeEol(type, " child = (", type, ") childObj");
4890: boolean firstUseOfIndex = true;
4891: for (Iterator props = ((List) typeMap.get(type)).iterator(); props
4892: .hasNext();) {
4893: Property prop = (Property) props.next();
4894: String attr = "_" + prop.name;
4895: String childExpr = "child";
4896: if (JavaUtil.isPrimitiveType(prop.getType()))
4897: childExpr = JavaUtil.fromObject(prop.getType(),
4898: "child");
4899: if (prop.isIndexed()) {
4900: if (firstUseOfIndex) {
4901: firstUseOfIndex = false;
4902: jw.writeEol("int index = 0");
4903: } else {
4904: jw.writeEol("index = 0");
4905: }
4906: beginAttrIterator(attr, prop, "element");
4907: jw.beginIf(childExpr + " == element");
4908: jw.beginIf("returnConstName");
4909: jw.writeEol("return ", prop.constName);
4910: if (prop.type != Common.TYPE_COMMENT) {
4911: jw.endElseBeginIf("returnSchemaName");
4912: jw.writeEol("return \"", prop.dtdName, "\"");
4913: jw.endElseBeginIf("returnXPathName");
4914: jw.write("return \"");
4915: if (prop.isAttribute())
4916: jw.write("@");
4917: jw.writeEol(prop.dtdName,
4918: "[position()=\"+index+\"]\"");
4919: }
4920: jw.endElseBegin();
4921: jw.writeEol("return \"" + prop.getEventName()
4922: + ".\"+Integer.toHexString(index)");
4923: jw.end();
4924: jw.end();
4925: jw.writeEol("++index");
4926: jw.end();
4927: } else {
4928: jw.beginIf(childExpr + " == " + attr);
4929: jw.beginIf("returnConstName");
4930: jw.writeEol("return ", prop.constName);
4931: if (prop.type != Common.TYPE_COMMENT) {
4932: jw.endElseBeginIf("returnSchemaName");
4933: jw.writeEol("return \"", prop.dtdName, "\"");
4934: jw.endElseBeginIf("returnXPathName");
4935: jw.write("return \"");
4936: if (prop.isAttribute())
4937: jw.write("@");
4938: jw.writeEol(prop.dtdName, "\"");
4939: }
4940: jw.endElseBegin();
4941: jw.writeEol("return \"" + prop.getEventName()
4942: + "\"");
4943: jw.end();
4944: jw.end();
4945: }
4946: }
4947: jw.end();
4948: }
4949: if (config.isRespectExtension()
4950: && beanElement.getExtension() != null) {
4951: jw
4952: .writeEol("return super.nameChild(childObj, returnConstName, returnSchemaName, returnXPathName)");
4953: } else {
4954: jw.writeEol("return null");
4955: }
4956: jw.endMethod();
4957: }
4958:
4959: public void genToString() throws IOException {
4960: jw.beginMethod("toString", "", null, "String", jw.PUBLIC);
4961: jw
4962: .writeEol("java.io.StringWriter sw = new java.io.StringWriter()");
4963: jw.beginTry();
4964: jw.writeEol("writeNode(sw)");
4965: jw.endCatch("java.io.IOException e");
4966: jw
4967: .comment("How can we actually get an IOException on a StringWriter?");
4968: jw.writeEol("throw new RuntimeException(e)");
4969: jw.end();
4970: jw.writeEol("return sw.toString()");
4971: jw.endMethod();
4972: }
4973:
4974: public void genExtendBaseBean() throws IOException {
4975: int size = attrList.size();
4976: jw.beginMethod("dump", "StringBuffer str, String indent", null,
4977: "void", jw.PUBLIC);
4978: jw.writeEol("str.append(toString())");
4979: jw.endMethod();
4980:
4981: jw.pushSelect(jw.DECL_SECTION);
4982: String[] propByNameKeys = new String[size + 1];
4983: for (int i = 0; i < size; i++) {
4984: Property a = (Property) attrList.get(i);
4985: propByNameKeys[i] = a.name;
4986: }
4987: propByNameKeys[size] = "";
4988: jw
4989: .writeEol(
4990: "private java.util.Map propByName = new java.util.HashMap("
4991: + (JavaUtil
4992: .getOptimialHashMapSize(propByNameKeys)),
4993: ", 1.0f)");
4994: jw.popSelect();
4995: jw
4996: .beginMethod("beanProp", "String name", null,
4997: "org.netbeans.modules.schema2beans.BeanProp",
4998: jw.PUBLIC);
4999: jw.writeEol("if (name == null) return null");
5000: jw
5001: .writeEol("org.netbeans.modules.schema2beans.BeanProp prop = (org.netbeans.modules.schema2beans.BeanProp) propByName.get(name)");
5002: jw.beginIf("prop == null");
5003: jw.writeEol("name = name.intern()");
5004: jw.writeEol("boolean indexed");
5005: jw.writeEol("int options");
5006: jw.writeEol("String constName");
5007: jw.writeEol("String schemaName");
5008: jw.writeEol("java.lang.reflect.Method writer = null");
5009: jw.writeEol("java.lang.reflect.Method arrayWriter = null");
5010: jw.writeEol("java.lang.reflect.Method reader = null");
5011: jw.writeEol("java.lang.reflect.Method arrayReader = null");
5012: jw.writeEol("java.lang.reflect.Method adder = null");
5013: jw.writeEol("java.lang.reflect.Method remover = null");
5014: boolean defaultKey = true;
5015: if (metaElement != null) {
5016: MetaProperty[] mp = metaElement.getMetaProperty();
5017: for (int i = 0; i < mp.length; i++) {
5018: if (mp[i].isKey()) {
5019: // At least one key is defined - default to false
5020: defaultKey = false;
5021: break;
5022: }
5023: }
5024: }
5025:
5026: jw.beginTry();
5027: for (int i = 0; i < size; i++) {
5028: Property a = (Property) attrList.get(i);
5029: boolean indexed = a.isIndexed();
5030: String type = a.getType();
5031: MetaProperty mp = getMetaProperty(a.name);
5032: boolean keyedElement = defaultKey;
5033: boolean genVetoable = config.isVetoable();
5034: if (mp != null) {
5035: keyedElement = (mp.isKey()) ? true : defaultKey;
5036: genVetoable = (mp.isVetoable()) ? true : genVetoable;
5037: }
5038: MetaElement me = getMetaElement(a);
5039: if (me != null) {
5040: genVetoable = (me.isVetoable()) ? true : genVetoable;
5041: }
5042: if (a.isBean)
5043: genVetoable = false;
5044: String signatureType = a.getSignatureType(packageName);
5045:
5046: /*
5047: if (a.getEventName().equals(a.beanIntrospectorName()))
5048: jw.beginIf("name == \""+a.getEventName()+"\" || name == \""+a.constName+"\"");
5049: else
5050: jw.beginIf("name == \""+a.getEventName()+"\" || name == \""+a.beanIntrospectorName()+"\" || name == \""+a.constName+"\"");
5051: */
5052: jw.beginIf("name == " + a.constName + "");
5053: jw.writeEol("indexed = " + indexed);
5054: jw.writeEol("constName = ", a.constName, "");
5055: jw.writeEol("schemaName = \"", a.dtdName, "\"");
5056: jw.write("options = ");
5057: if (a.ored)
5058: jw
5059: .write("org.netbeans.modules.schema2beans.Common.SEQUENCE_OR | ");
5060: if (genVetoable)
5061: jw
5062: .write("org.netbeans.modules.schema2beans.Common.TYPE_VETOABLE |");
5063: if (a.type == Common.TYPE_BOOLEAN && !a.getCanBeEmpty())
5064: gen("org.netbeans.modules.schema2beans.Common.TYPE_SHOULD_NOT_BE_EMPTY | ");
5065: if (keyedElement)
5066: gen("org.netbeans.modules.schema2beans.Common.TYPE_KEY | ");
5067: jw.writeEol("org.netbeans.modules.schema2beans.Common.",
5068: Common.instanceToCommonString(a.elementInstance),
5069: "|org.netbeans.modules.schema2beans.Common.",
5070: Common.typeToString(a.type));
5071: if (indexed) {
5072: jw.writeEol("reader = getClass().getMethod(\"", a
5073: .getReadMethod(true),
5074: "\", new Class[] {Integer.TYPE})");
5075: jw.writeEol("arrayReader = getClass().getMethod(\"", a
5076: .getReadMethod(false), "\", new Class[] {})");
5077: jw.writeEol("writer = getClass().getMethod(\"", a
5078: .getWriteMethod(),
5079: "\", new Class[] {Integer.TYPE, ",
5080: signatureType + ".class})");
5081: jw.writeEol("arrayWriter = getClass().getMethod(\"", a
5082: .getWriteMethod(), "\", new Class[] {",
5083: signatureType + "[].class})");
5084: jw.writeEol("adder = getClass().getMethod(\"", a
5085: .getAddMethod(), "\", new Class[] {",
5086: signatureType + ".class})");
5087: jw.writeEol("remover = getClass().getMethod(\"", a
5088: .getRemoveMethod(), "\", new Class[] {",
5089: signatureType + ".class})");
5090: } else {
5091: jw.writeEol("writer = getClass().getMethod(\"", a
5092: .getWriteMethod(), "\", new Class[] {",
5093: signatureType + ".class})");
5094: jw.writeEol("reader = getClass().getMethod(\"", a
5095: .getReadMethod(false), "\", new Class[] {})");
5096: }
5097: jw.end(false);
5098: jw.write(" else ");
5099: }
5100: jw.begin();
5101: jw.comment("Check if name is a schema name.");
5102: for (int i = 0; i < size; i++) {
5103: Property a = (Property) attrList.get(i);
5104: // Only check those names we haven't looked at before.
5105: if (a.dtdName.equals(a.name))
5106: continue;
5107: jw.beginIf("name == \"" + a.dtdName + "\"");
5108: jw.writeEol("prop = beanProp(", a.constName, ")");
5109: jw.writeEol("propByName.put(name, prop)");
5110: jw.writeEol("return prop");
5111: jw.end();
5112: }
5113: genInvalidName("name");
5114: jw.end();
5115: jw.endCatch("java.lang.NoSuchMethodException e");
5116: jw.writeEol("throw new RuntimeException(e)");
5117: jw.end();
5118:
5119: jw
5120: .write("prop = new org.netbeans.modules.schema2beans.ReflectiveBeanProp(this, schemaName, ");
5121: jw.write("constName, options, getClass(), ");
5122: jw
5123: .writeEol("" + beanElement.isRoot,
5124: ", writer, arrayWriter, reader, arrayReader, adder, remover)");
5125: jw.writeEol("propByName.put(name, prop)");
5126: jw.end(); // if
5127:
5128: jw.writeEol("return prop");
5129: jw.endMethod();
5130:
5131: jw
5132: .beginMethod("beanProp", "", null,
5133: "org.netbeans.modules.schema2beans.BeanProp",
5134: jw.PUBLIC);
5135: jw.beginIf("parent == null");
5136: jw
5137: .writeEol("org.netbeans.modules.schema2beans.BeanProp prop = (org.netbeans.modules.schema2beans.BeanProp) propByName.get(\"\")");
5138: jw.beginIf("prop == null");
5139: jw
5140: .write(
5141: "prop = new org.netbeans.modules.schema2beans.ReflectiveBeanProp(this, \"",
5142: beanElement.node.getName(), "\", \"", className);
5143: jw
5144: .writeEol(
5145: "\", org.netbeans.modules.schema2beans.Common.TYPE_1 | org.netbeans.modules.schema2beans.Common.TYPE_BEAN, ",
5146: className,
5147: ".class, isRoot(), null, null, null, null, null, null)");
5148: jw.writeEol("propByName.put(\"\", prop)");
5149: jw.end(); // if
5150: jw.writeEol("return prop");
5151: jw.end();
5152: jw
5153: .writeEol("String myConstName = parent.nameChild(this, true, false)");
5154: jw.writeEol("return parent.beanProp(myConstName)");
5155: jw.endMethod();
5156:
5157: jw.beginMethod("beanProp", "int order", null,
5158: "org.netbeans.modules.schema2beans.BeanProp", jw.PUBLIC
5159: | jw.UNSUPPORTED);
5160: jw.writeEol("prepareBeanPropList()");
5161: jw
5162: .writeEol("return (org.netbeans.modules.schema2beans.BeanProp) beanPropList.get(order)");
5163: jw.endMethod();
5164:
5165: jw
5166: .beginMethod("parent", "", null,
5167: "org.netbeans.modules.schema2beans.BaseBean",
5168: jw.PUBLIC);
5169: jw
5170: .writeEol("return (org.netbeans.modules.schema2beans.BaseBean) parent");
5171: jw.endMethod();
5172:
5173: jw.beginMethod("_getParent", "", null,
5174: "org.netbeans.modules.schema2beans.Bean", jw.PUBLIC);
5175: jw.writeEol("return parent");
5176: jw.endMethod();
5177:
5178: jw.beginMethod("newInstance", "String name", null,
5179: "org.netbeans.modules.schema2beans.BaseBean", jw.PUBLIC
5180: | jw.UNSUPPORTED);
5181: jw
5182: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5183: jw.endMethod();
5184:
5185: jw.beginMethod("dtdName", "", null, "String", jw.PUBLIC);
5186: jw.beginIf("parent == null");
5187: if (!beanElement.isRoot())
5188: jw
5189: .comment("Not necessarily the right schema name, but make a good guess.");
5190: jw.writeEol("return \"", beanElement.node.getName(), "\"");
5191: jw.end();
5192: jw.writeEol("return parent.nameChild(this, false, true)");
5193: jw.endMethod();
5194:
5195: jw.beginMethod("comments", "", null, "org.w3c.dom.Comment[]",
5196: jw.PUBLIC | jw.UNSUPPORTED);
5197: jw
5198: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5199: jw.endMethod();
5200:
5201: jw.beginMethod("addComment", "String comment", null,
5202: "org.w3c.dom.Comment", jw.PUBLIC | jw.UNSUPPORTED);
5203: jw
5204: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5205: jw.endMethod();
5206:
5207: jw.beginMethod("removeComment", "org.w3c.dom.Comment comment",
5208: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5209: jw
5210: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5211: jw.endMethod();
5212:
5213: jw.beginMethod("createProperty",
5214: "String dtdName, String beanName, Class type", null,
5215: "void", jw.PUBLIC | jw.UNSUPPORTED);
5216: jw
5217: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5218: jw.endMethod();
5219:
5220: jw
5221: .beginMethod(
5222: "createProperty",
5223: "String dtdName, String beanName, int option, Class type",
5224: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5225: jw
5226: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5227: jw.endMethod();
5228:
5229: jw
5230: .beginMethod(
5231: "createRoot",
5232: "String dtdName, String beanName, int option, Class type",
5233: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5234: jw
5235: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5236: jw.endMethod();
5237:
5238: jw.beginMethod("knownValues", "String name", null, "Object[]",
5239: jw.PUBLIC | jw.UNSUPPORTED);
5240: jw
5241: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5242: jw.endMethod();
5243:
5244: jw.beginMethod("addKnownValue", "String name, Object value",
5245: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5246: jw
5247: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5248: jw.endMethod();
5249:
5250: jw
5251: .beginMethod(
5252: "createAttribute",
5253: "String dtdName, String name, int type, String[] values, String defValue",
5254: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5255: jw
5256: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5257: jw.endMethod();
5258:
5259: jw
5260: .beginMethod(
5261: "createAttribute",
5262: "String propName, String dtdName, String name, int type, String[] values, String defValue",
5263: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5264: jw
5265: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5266: jw.endMethod();
5267:
5268: jw.beginMethod("setAttributeValue",
5269: "String propName, String name, String value", null,
5270: "void", jw.PUBLIC | jw.UNSUPPORTED);
5271: jw
5272: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5273: jw.endMethod();
5274:
5275: jw.beginMethod("setAttributeValue",
5276: "String name, String value", null, "void", jw.PUBLIC
5277: | jw.UNSUPPORTED);
5278: jw
5279: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5280: jw.endMethod();
5281:
5282: jw.beginMethod("getAttributeValue", "String name", null,
5283: "String", jw.PUBLIC | jw.UNSUPPORTED);
5284: jw
5285: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5286: jw.endMethod();
5287:
5288: jw.beginMethod("getAttributeValue",
5289: "String propName, String name", null, "String",
5290: jw.PUBLIC | jw.UNSUPPORTED);
5291: jw
5292: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5293: jw.endMethod();
5294:
5295: jw
5296: .beginMethod(
5297: "setAttributeValue",
5298: "String propName, int index, String name, String value",
5299: null, "void", jw.PUBLIC | jw.UNSUPPORTED);
5300: jw
5301: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5302: jw.endMethod();
5303:
5304: jw.beginMethod("getAttributeValue",
5305: "String propName, int index, String name", null,
5306: "String", jw.PUBLIC | jw.UNSUPPORTED);
5307: jw
5308: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5309: jw.endMethod();
5310:
5311: jw.beginMethod("getAttributeNames", "String propName", null,
5312: "String[]", jw.PUBLIC);
5313: jw.writeEol("return new String[] {}");
5314: jw.endMethod();
5315:
5316: jw.beginMethod("getAttributeNames", "", null, "String[]",
5317: jw.PUBLIC);
5318: jw.writeEol("return new String[] {}");
5319: jw.endMethod();
5320:
5321: jw.beginMethod("listAttributes", "String propName", null,
5322: "org.netbeans.modules.schema2beans.BaseAttribute[]",
5323: jw.PUBLIC);
5324: jw
5325: .writeEol("return new org.netbeans.modules.schema2beans.BaseAttribute[] {}");
5326: jw.endMethod();
5327:
5328: jw.beginMethod("listAttributes", "", null,
5329: "org.netbeans.modules.schema2beans.BaseAttribute[]",
5330: jw.PUBLIC);
5331: jw
5332: .writeEol("return new org.netbeans.modules.schema2beans.BaseAttribute[] {}");
5333: jw.endMethod();
5334:
5335: jw.beginMethod("findAttributeValue",
5336: "String attrName, String value", null, "String[]",
5337: jw.PUBLIC);
5338: jw.writeEol("return new String[] {}");
5339: jw.endMethod();
5340:
5341: jw.beginMethod("findPropertyValue",
5342: "String propName, Object value", null, "String[]",
5343: jw.PUBLIC | jw.UNSUPPORTED);
5344: jw
5345: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5346: jw.endMethod();
5347:
5348: jw.beginMethod("findValue", "Object value", null, "String[]",
5349: jw.PUBLIC | jw.UNSUPPORTED);
5350: jw
5351: .writeEol("throw new UnsupportedOperationException(\"Not implemented\")");
5352: jw.endMethod();
5353:
5354: jw.beginMethod("buildPathName", "StringBuffer str", null,
5355: "void", jw.PROTECTED);
5356: jw.writeEol("str.append(nameSelf())");
5357: jw.endMethod();
5358:
5359: jw.beginMethod("graphManager", "", null,
5360: "org.netbeans.modules.schema2beans.GraphManager",
5361: jw.PUBLIC);
5362: jw.beginIf("graphManager == null");
5363: jw.beginIf("parent == null");
5364: //jw.writeEol("System.out.println(\"Creating GraphManager for \"+getClass()+\" this=\"+this)");
5365: //jw.comment("new Exception().printStackTrace()");
5366: jw
5367: .writeEol("graphManager = new org.netbeans.modules.schema2beans.GraphManager(this)");
5368: jw.endElseBegin();
5369: jw.writeEol("graphManager = parent.graphManager()");
5370: jw.end();
5371: jw.end();
5372: jw.writeEol("return graphManager");
5373: jw.endMethod();
5374:
5375: jw.beginMethod("clone", "", null, "Object", jw.PUBLIC
5376: | jw.BEANINFO);
5377: jw.writeEol("return new ", className, "(this, null, false)");
5378: jw.endMethod();
5379:
5380: jw.beginMethod("cloneData", "", null, "Object", jw.PUBLIC
5381: | jw.BEANINFO);
5382: jw.writeEol("return new ", className, "(this, null, true)");
5383: jw.endMethod();
5384:
5385: jw.pushSelect(DECL_SECTION);
5386: jw.write("private java.util.List beanPropList = null");
5387: jw.eol(false);
5388: jw.write("\t");
5389: jw.comment("List<org.netbeans.modules.schema2beans.BeanProp>");
5390: jw.popSelect();
5391: jw.beginMethod("prepareBeanPropList", "", null, "void",
5392: jw.PRIVATE);
5393: jw.beginIf("beanPropList == null");
5394: jw.writeEol("beanPropList = new java.util.ArrayList(" + size
5395: + ")");
5396: for (int i = 0; i < size; i++) {
5397: Property a = (Property) attrList.get(i);
5398: jw
5399: .writeEol("beanPropList.add(beanProp(",
5400: a.constName, "))");
5401: }
5402: jw.end();
5403: jw.endMethod();
5404:
5405: jw.beginMethod("beanPropsIterator", "", null,
5406: "java.util.Iterator", jw.PROTECTED);
5407: jw.writeEol("prepareBeanPropList()");
5408: jw.writeEol("return beanPropList.iterator()");
5409: jw.endMethod();
5410:
5411: jw.beginMethod("beanProps", "", null,
5412: "org.netbeans.modules.schema2beans.BeanProp[]",
5413: jw.PUBLIC);
5414: jw.writeEol("prepareBeanPropList()");
5415: jw
5416: .writeEol("org.netbeans.modules.schema2beans.BeanProp[] ret = new org.netbeans.modules.schema2beans.BeanProp["
5417: + size + "]");
5418: jw
5419: .writeEol("ret = (org.netbeans.modules.schema2beans.BeanProp[]) beanPropList.toArray(ret)");
5420: jw.writeEol("return ret");
5421: jw.endMethod();
5422:
5423: jw.beginMethod("setValue", "String name, Object value", null,
5424: "void", jw.PUBLIC);
5425: new NamePropertyVisitor("name") {
5426: public void generateProp() throws IOException {
5427: if (curProp.isIndexed()) {
5428: jw.write(curProp.getWriteMethod(), "(");
5429: jw.writeEol("(", curProp.getType(), "[]) value)");
5430: } else {
5431: jw.write(curProp.getWriteMethod(), "(");
5432: jw.write(JavaUtil.fromObject(curProp.getType(),
5433: "value"));
5434: jw.writeEol(")");
5435: }
5436: }
5437: }.generate();
5438: jw.endMethod();
5439:
5440: jw.beginMethod("setValue",
5441: "String name, int index, Object value", null, "void",
5442: jw.PUBLIC);
5443: new NamePropertyVisitor("name") {
5444: public void generateProp() throws IOException {
5445: if (curProp.isIndexed()) {
5446: jw.write(curProp.getWriteMethod(), "(index, ");
5447: jw.write(JavaUtil.fromObject(curProp.getType(),
5448: "value"));
5449: jw.writeEol(")");
5450: } else {
5451: jw
5452: .writeEol(
5453: "throw new IllegalArgumentException(name+\" is not an indexed property for ",
5454: className, "\")");
5455: }
5456: }
5457: }.generate();
5458: jw.endMethod();
5459:
5460: jw.beginMethod("getValue", "String name", null, "Object",
5461: jw.PUBLIC);
5462: new NamePropertyVisitor("name") {
5463: public void generateProp() throws IOException {
5464: jw.write("return ");
5465: if (curProp.isIndexed())
5466: jw.writeEol(curProp.getReadMethod(false) + "()");
5467: else
5468: jw.writeEol(JavaUtil
5469: .toObject(curProp.getReadMethod(false)
5470: + "()", curProp.getType(), config
5471: .isForME()));
5472: }
5473: }.generate();
5474: jw.endMethod();
5475:
5476: jw.beginMethod("getValue", "String name, int index", null,
5477: "Object", jw.PUBLIC);
5478: new NamePropertyVisitor("name") {
5479: public boolean skip() {
5480: return !curProp.isIndexed();
5481: }
5482:
5483: public void generateProp() throws IOException {
5484: jw.write("return ");
5485: jw.writeEol(JavaUtil.toObject(curProp
5486: .getReadMethod(true)
5487: + "(index)", curProp.getType(), config
5488: .isForME()));
5489: }
5490:
5491: public void postGenerate() throws IOException {
5492: }
5493: }.generate();
5494: new NamePropertyVisitor("name") {
5495: public void preGenerate() throws IOException {
5496: }
5497:
5498: public boolean skip() {
5499: return curProp.isIndexed();
5500: }
5501:
5502: public void generateProp() throws IOException {
5503: jw.beginIf("index > 0");
5504: jw
5505: .writeEol("throw new IllegalArgumentException(\"index > 0\")");
5506: jw.end();
5507: jw.write("return ");
5508: jw.writeEol(JavaUtil.toObject(curProp
5509: .getReadMethod(false)
5510: + "()", curProp.getType(), config.isForME()));
5511: }
5512: }.generate();
5513: jw.endMethod();
5514:
5515: jw
5516: .beginMethod(
5517: "mergeUpdate",
5518: "org.netbeans.modules.schema2beans.BaseBean sourceBean",
5519: null, "void", jw.PUBLIC);
5520: //jw.writeEol("System.out.println(\"mergeUpdate for \"+getClass())");
5521: jw.writeEol(fullClassName, " source = (", fullClassName,
5522: ") sourceBean");
5523: boolean isArrayStyle = (config.getIndexedPropertyType() == null);
5524: for (int i = 0; i < size; i++) {
5525: Property a = (Property) attrList.get(i);
5526: boolean indexed = a.isIndexed();
5527: String type = a.getType().intern();
5528: boolean willCopy = genCopyWillCopy(a);
5529: String getter = "source." + a.getReadMethod(false) + "()";
5530: String signatureType = a.getSignatureType(packageName);
5531: String baseFullClassName = a
5532: .getTypeFullClassName(packageName);
5533: String fullClassName;
5534: if (indexed) {
5535: fullClassName = baseFullClassName + "[]";
5536: } else
5537: fullClassName = baseFullClassName;
5538: jw.begin();
5539: jw.write(fullClassName, " srcProperty = ");
5540: if (!signatureType.equals(baseFullClassName))
5541: jw.write("(", fullClassName, ") ");
5542: jw.writeEol(getter);
5543: String srcProperty = "srcProperty";
5544: if (a.isBean) {
5545: if (indexed) {
5546: jw.writeEol("int destSize = ", a.getSizeMethod(),
5547: "()");
5548: jw.beginIf("destSize == srcProperty.length");
5549: srcProperty = "srcProperty[i]";
5550: jw.beginFor("int i = 0", "i < srcProperty.length",
5551: "++i");
5552: genMergeUpdateBean(a, srcProperty, indexed,
5553: signatureType, baseFullClassName,
5554: isArrayStyle, true);
5555: jw.endElseBegin();
5556: jw.writeEol(fullClassName, " destArray = new ",
5557: baseFullClassName, "[srcProperty.length]");
5558: jw.beginFor("int i = 0", "i < srcProperty.length",
5559: "++i");
5560: genMergeUpdateBean(a, srcProperty, indexed,
5561: signatureType, baseFullClassName,
5562: isArrayStyle, false);
5563: jw.end();
5564: jw.writeEol(a.getWriteMethod(), "(destArray)");
5565: jw.end();
5566: } else {
5567: genMergeUpdateBean(a, srcProperty, indexed,
5568: signatureType, baseFullClassName,
5569: isArrayStyle, false);
5570: }
5571:
5572: } else if (indexed && (willCopy || isArrayStyle)) {
5573: jw.writeEol(fullClassName, " destArray = new ",
5574: baseFullClassName, "[srcProperty.length]");
5575: jw.beginFor("int i = 0", "i < srcProperty.length",
5576: "++i");
5577: srcProperty = "srcProperty[i]";
5578: jw.write("destArray[i] = ");
5579: genCopy(srcProperty, a, "false");
5580: jw.eol();
5581: jw.end();
5582: jw.writeEol(a.getWriteMethod(), "(destArray)");
5583: } else {
5584: jw.write(a.getWriteMethod(), "(");
5585: genCopy(srcProperty, a, "false");
5586: jw.writeEol(")");
5587: }
5588: jw.end();
5589: }
5590: jw.endMethod();
5591:
5592: if (beanElement.isRoot()) {
5593: jw.beginMethod("isRoot", "", null, "boolean", jw.PUBLIC);
5594: jw.writeEol("return true");
5595: jw.endMethod();
5596:
5597: jw
5598: .beginMethod(
5599: "createGraph",
5600: "java.io.InputStream in, boolean validate",
5601: "java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException",
5602: className, jw.PUBLIC | jw.STATIC | jw.IO);
5603: //jw.beginTry();
5604: jw
5605: .writeEol("return read(new org.xml.sax.InputSource(in), validate, null, null)");
5606: /*
5607: jw.endCatch("javax.xml.parsers.ParserConfigurationException e");
5608: jw.writeEol("throw new java.lang.RuntimeException(e)");
5609: jw.endCatch("org.xml.sax.SAXException e");
5610: jw.writeEol("throw new java.lang.RuntimeException(e)");
5611: jw.endCatch("java.io.IOException e");
5612: jw.writeEol("throw new java.lang.RuntimeException(e)");
5613: jw.end();
5614: */
5615: jw.endMethod();
5616:
5617: jw
5618: .beginMethod(
5619: "createGraph",
5620: "java.io.InputStream in",
5621: "java.io.IOException, javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException",
5622: className, jw.PUBLIC | jw.STATIC | jw.IO);
5623: jw.writeEol("return createGraph(in, false)");
5624: jw.endMethod();
5625:
5626: jw.beginMethod("createGraph", "", null, className,
5627: jw.PUBLIC | jw.STATIC | jw.IO);
5628: jw.writeEol("return new ", className, "()");
5629: jw.endMethod();
5630:
5631: jw.beginMethod("createGraph",
5632: "org.w3c.dom.Document document", null, className,
5633: jw.PUBLIC | jw.STATIC);
5634: jw.writeEol("return read(document)");
5635: jw.endMethod();
5636:
5637: jw.pushSelect(CONSTRUCTOR_SECTION);
5638: jw.beginConstructor(className,
5639: "org.w3c.dom.Node doc, int currentlyUnusedOptions");
5640: jw.writeEol("this()");
5641: jw.writeEol("readFromDocument((org.w3c.dom.Document) doc)");
5642: jw.end();
5643: jw.cr();
5644: jw.popSelect();
5645: } else {
5646: jw.bigComment("@deprecated");
5647: jw.beginMethod("write",
5648: "java.io.Writer out, String encoding",
5649: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
5650: jw.writeEol("writeNode(out)");
5651: jw.endMethod();
5652:
5653: jw.bigComment("@deprecated");
5654: jw.beginMethod("write", "java.io.OutputStream out",
5655: "java.io.IOException", "void", jw.PUBLIC | jw.IO);
5656: jw
5657: .writeEol("java.io.PrintWriter pw = new java.io.PrintWriter(out)");
5658: jw.writeEol("writeNode(pw)");
5659: jw.writeEol("pw.flush()");
5660: jw.endMethod();
5661: }
5662: }
5663:
5664: private void genMergeUpdateBean(Property a, String srcProperty,
5665: boolean indexed, String signatureType,
5666: String baseFullClassName, boolean isArrayStyle,
5667: boolean mergeEach) throws IOException {
5668: //jw.writeEol("System.out.println(\"mergeEach="+mergeEach+"\")");
5669: String attr = "_" + a.name;
5670: jw.writeEol(baseFullClassName, " dest");
5671: if (!indexed)
5672: jw.writeEol("boolean needToSet = false");
5673: jw.beginIf(srcProperty + " == null");
5674: jw.writeEol("dest = null");
5675: if (!indexed)
5676: jw.writeEol("needToSet = true");
5677: jw.endElseBegin();
5678: if (indexed) {
5679: jw.beginIf("i < destSize");
5680: }
5681: jw.write("dest = ");
5682: if (!signatureType.equals(baseFullClassName))
5683: jw.write("(", baseFullClassName, ") ");
5684: jw.write(a.getReadMethod(indexed), "(");
5685: if (indexed)
5686: jw.write("i");
5687: jw.writeEol(")");
5688: if (indexed) {
5689: if (!mergeEach) {
5690: jw.beginIf("!" + srcProperty + ".equals(dest)");
5691: jw
5692: .comment("It's different, so have it just dup the source one.");
5693: jw.writeEol("dest = null");
5694: jw.end();
5695: }
5696: jw.endElseBegin();
5697: jw.writeEol("dest = null");
5698: jw.end();
5699: }
5700: //jw.writeEol("System.out.println(\"dest=\"+dest)");
5701: jw.beginIf("dest == null");
5702: jw
5703: .comment("Use a temp variable, and store it after we've merged everything into it, so as to make it only 1 change event.");
5704: jw.writeEol("dest = new ", baseFullClassName, "(", srcProperty
5705: + ", this, false)");
5706: if (!indexed) {
5707: jw.writeEol("needToSet = true");
5708: jw.endElseBegin();
5709: jw.writeEol("dest.mergeUpdate(", srcProperty, ")");
5710: jw.end();
5711: } else {
5712: if (mergeEach) {
5713: jw.endElseBegin();
5714: jw.writeEol("dest.mergeUpdate(", srcProperty, ")");
5715: jw.end();
5716: }
5717: }
5718: jw.end();
5719: if (indexed) {
5720: if (mergeEach) {
5721: jw
5722: .comment("Merge events were generated by the above dest.mergeUpdate, so just set it directly now.");
5723: if (isArrayStyle) {
5724: jw.writeEol(attr, "[index] = value");
5725: } else {
5726: jw.writeEol(attr, ".set(i, dest)");
5727: }
5728: } else {
5729: jw.writeEol("destArray[i] = dest");
5730: }
5731: jw.end();
5732: } else {
5733: jw.beginIf("needToSet");
5734: jw.write(a.getWriteMethod(), "(");
5735: jw.writeEol("dest)");
5736: jw.end();
5737: }
5738: }
5739:
5740: public void genTrailer(int out) {
5741: select(out);
5742: if (metaElement != null && metaElement.getUserCode() != null) {
5743: String userCode = metaElement.getUserCode();
5744: cr();
5745: gencr(userCode);
5746: }
5747: }
5748:
5749: public void genFinishClass(int out) {
5750: select(out);
5751: end();
5752: cr();
5753: }
5754:
5755: public void setInvalidPropertyNames(Map invalidNames) {
5756: invalidNames.put("Class", null);
5757: if (config.isExtendBaseBean()) {
5758: invalidNames.put("Property", null);
5759: invalidNames.put("AttributeNames", null);
5760: }
5761: }
5762:
5763: protected void beginAttrIterator(String attr, Property a,
5764: String elementName) throws IOException {
5765: String fullType = getTypeFullClassName(a);
5766: boolean isArrayStyle = (config.getIndexedPropertyType() == null);
5767: if (isArrayStyle) {
5768: jw.beginFor("int elementIndex = 0", "elementIndex < "
5769: + attr + ".length", "++elementIndex");
5770: } else {
5771: jw.beginFor("java.util.Iterator it = " + attr
5772: + ".iterator()", "it.hasNext()", "");
5773: }
5774: jw.write(fullType);
5775: jw.write(" ", elementName, " = ");
5776: if (isArrayStyle) {
5777: jw.writeEol(attr, "[elementIndex]");
5778: } else {
5779: jw.writeEol(JavaUtil.fromObject(fullType, "it.next()"));
5780: }
5781: }
5782:
5783: protected void genNewEvent(Property a, String index,
5784: String oldValue, String newValue, String type)
5785: throws IOException {
5786: jw
5787: .write("new java.beans.PropertyChangeEvent(this, nameSelf()+\"/");
5788: jw.write(a.getEventName());
5789: if (index.equals(""))
5790: jw.write("\"");
5791: else if (index.equals("-1"))
5792: jw.write(".", index, "\"");
5793: else
5794: jw.write(".\"+Integer.toHexString(", index, ")");
5795: jw.write(", ");
5796: if ("null".equals(oldValue))
5797: jw.write("null");
5798: else
5799: jw.write(JavaUtil
5800: .toObject(oldValue, type, config.isForME()));
5801: jw.write(", ");
5802: if ("null".equals(newValue))
5803: jw.write("null");
5804: else
5805: jw.write(JavaUtil
5806: .toObject(newValue, type, config.isForME()));
5807: jw.write(")");
5808: }
5809:
5810: protected boolean hasDeepCopyConstructor() {
5811: return true;
5812: }
5813: }
|