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.w3c.dom.*;
0048: import org.xml.sax.*;
0049: import javax.xml.parsers.*;
0050:
0051: import org.netbeans.modules.schema2beans.*;
0052: import org.netbeans.modules.schema2beansdev.gen.XMLWriter;
0053: import org.netbeans.modules.schema2beansdev.metadd.*;
0054:
0055: public class XMLSchemaParser extends GeneralParser implements
0056: SchemaParser {
0057: public final static String JAVA_TYPE_NS = "http://schema2beans.netbeans.org/javaTypes";
0058:
0059: // Handler to callback with the tokens found in the Schema.
0060: private DocDefHandler handler;
0061:
0062: private boolean debug;
0063: private GenBeans.Config config = null;
0064: private Stack parentTypes = new Stack();
0065: private Stack parentUniqueNames = new Stack();
0066: private String lastDefinedType = null;
0067: private boolean lastDefinedExternalType = true;
0068: private List perAttributeExtraData = new LinkedList();
0069: private String targetNamespace;
0070: private Map elementsAlreadyDefined = new IdentityHashMap();
0071:
0072: SchemaRep schema;
0073:
0074: public XMLSchemaParser(GenBeans.Config config, DocDefHandler handler) {
0075: this .config = config;
0076: this .filename = config.getFilename();
0077: this .schemaIn = config.getFileIn();
0078: this .handler = handler;
0079: this .debug = config.isTraceParse();
0080:
0081: schema = new SchemaRep();
0082: schema.debug = debug;
0083: handler.setPrefixGuesser(schema);
0084: }
0085:
0086: public void process() throws java.io.IOException,
0087: Schema2BeansException {
0088: startupReader();
0089: try {
0090: MetaDD mdd = config.getMetaDD();
0091: DocumentBuilderFactory dbf = DocumentBuilderFactory
0092: .newInstance();
0093: dbf.setNamespaceAware(true);
0094: dbf.setIgnoringComments(true);
0095: dbf.setIgnoringElementContentWhitespace(true);
0096: DocumentBuilder db = dbf.newDocumentBuilder();
0097: Document xmlSchema = db.parse(new InputSource(reader));
0098: schema.setCurrentParsedURI(getReaderURI());
0099: if (config.isForME())
0100: schema.setSchemaTypesForME(true);
0101: overrideSchemaTypes();
0102: schema.readDocument(xmlSchema);
0103: } catch (javax.xml.parsers.ParserConfigurationException e) {
0104: throw new Schema2BeansNestedException(Common.getMessage(
0105: "MSG_FailedToParse", filename), e);
0106: } catch (org.xml.sax.SAXException e) {
0107: throw new Schema2BeansNestedException(Common.getMessage(
0108: "MSG_FailedToParse", filename), e);
0109: } finally {
0110: shutdownReader();
0111: }
0112: if (debug) {
0113: PrintWriter pw = new PrintWriter(config.messageOut);
0114: schema.writeXMLSchemaStandalone(pw);
0115: pw.flush();
0116: }
0117: schema.optimize();
0118: handler.startDocument(config.getDocRoot());
0119: process(schema.getRootElement());
0120: handler.endDocument();
0121: }
0122:
0123: /**
0124: * Search thru the input beangraphs for anything that might affect
0125: * the definition of types from xsd: or xml:
0126: */
0127: protected void overrideSchemaTypes() {
0128: String xsdNS = schema.getNamespaceURI("xsd");
0129: String xmlNS = schema.getNamespaceURI("xml");
0130: for (Iterator it = config.readBeanGraphs(); it.hasNext();) {
0131: org.netbeans.modules.schema2beansdev.beangraph.BeanGraph bg = (org.netbeans.modules.schema2beansdev.beangraph.BeanGraph) it
0132: .next();
0133: for (int i = 0; i < bg.sizeSchemaTypeMapping(); ++i) {
0134: org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType stm = bg
0135: .getSchemaTypeMapping(i);
0136: if (xsdNS.equals(stm.getSchemaTypeNamespace())
0137: || xmlNS.equals(stm.getSchemaTypeNamespace())) {
0138: setSchemaType(stm);
0139: }
0140: }
0141: }
0142: }
0143:
0144: public void setSchemaType(
0145: org.netbeans.modules.schema2beansdev.beangraph.BeanGraph bg) {
0146: for (int i = 0; i < bg.sizeSchemaTypeMapping(); ++i) {
0147: setSchemaType(bg.getSchemaTypeMapping(i));
0148: }
0149: }
0150:
0151: public void setSchemaType(
0152: org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType stm) {
0153: schema.setSchemaTypeMapping(stm.getSchemaTypeNamespace(), stm
0154: .getSchemaTypeName(), stm.getJavaType());
0155: }
0156:
0157: protected void process(SchemaRep.ElementExpr ee)
0158: throws Schema2BeansException {
0159: if (ee instanceof SchemaRep.Element) {
0160: processElement((SchemaRep.Element) ee);
0161: } else if (ee instanceof SchemaRep.ComplexType) {
0162: // named ComplexType
0163: processComplexType((SchemaRep.ComplexType) ee);
0164: } else if (ee instanceof SchemaRep.UnionType) {
0165: // named SimpleType
0166: processUnionType((SchemaRep.UnionType) ee);
0167: } else if (ee instanceof SchemaRep.SimpleType) {
0168: // named SimpleType
0169: processSimpleType((SchemaRep.SimpleType) ee);
0170: } else if (ee instanceof SchemaRep.Restriction) {
0171: processRestriction((SchemaRep.Restriction) ee);
0172: } else if (ee instanceof SchemaRep.SchemaNode) {
0173: processSchemaNode((SchemaRep.SchemaNode) ee);
0174: } else if (ee instanceof SchemaRep.ElementInformationItem) {
0175: if (parentTypes.empty()) {
0176: //config.messageOut.println("parentTypes1 is empty! ee="+ee);
0177: } else {
0178: handler.addExtraDataNode((String) parentUniqueNames
0179: .peek(), (String) parentTypes.peek(), ee);
0180: }
0181: } else if (ee instanceof SchemaRep.RestrictionType) {
0182: // Do nothing here as RestrictionType's are handled elsewhere.
0183: } else if (ee instanceof SchemaRep.ModelGroup) {
0184: processModelGroup((SchemaRep.ModelGroup) ee);
0185: } else if (ee instanceof SchemaRep.Annotation) {
0186: processAnnotation((SchemaRep.Annotation) ee);
0187: } else if (ee instanceof SchemaRep.Extension) {
0188: processExtension((SchemaRep.Extension) ee);
0189: } else if (ee instanceof SchemaRep.ContainsSubElements) {
0190: processContainsSubElements((SchemaRep.ContainsSubElements) ee);
0191: } else {
0192: config.messageOut
0193: .println("XMLSchemaPraser.process: Hit unknown ElementExpr: "
0194: + ee);
0195: }
0196:
0197: }
0198:
0199: protected void processContainsSubElements(
0200: SchemaRep.ContainsSubElements cse)
0201: throws Schema2BeansException {
0202: Iterator it = cse.subElementsIterator();
0203: while (it.hasNext()) {
0204: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0205: .next();
0206: process(childee);
0207: }
0208: }
0209:
0210: protected void processContainsSubElementsAndAttributes(
0211: SchemaRep.ContainsSubElements cse, String elementName)
0212: throws Schema2BeansException {
0213: Iterator it = cse.subElementsIterator();
0214: while (it.hasNext()) {
0215: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0216: .next();
0217: if (childee instanceof SchemaRep.Attribute) {
0218: processAttribute(elementName,
0219: (SchemaRep.Attribute) childee);
0220: } else {
0221: process(childee);
0222: }
0223: }
0224: }
0225:
0226: protected void processElement(SchemaRep.Element el)
0227: throws Schema2BeansException {
0228: boolean alreadyDefined = false;
0229: if (elementsAlreadyDefined.containsKey(el))
0230: alreadyDefined = true;
0231: else
0232: elementsAlreadyDefined.put(el, el);
0233: setLastDefined(config.getDefaultElementType(), true);
0234: perAttributeExtraData.clear();
0235:
0236: String name = el.getElementName();
0237: SchemaRep.Restriction[] restrict = null;
0238: boolean externalType = false;
0239: String schemaType;
0240: String namespace = targetNamespace;
0241: if (el.getRef() == null) {
0242: schemaType = el.getXMLSchemaType();
0243: namespace = el.getElementNamespace();
0244: } else {
0245: SchemaRep.Element referredElement = el.getRefElement();
0246: if (referredElement == null) {
0247: config.messageOut
0248: .println("referredElement is null for " + el);
0249: throw new IllegalStateException(
0250: "referredElement is null for " + el);
0251: }
0252: if (elementsAlreadyDefined.containsKey(referredElement))
0253: alreadyDefined = true;
0254: else
0255: elementsAlreadyDefined.put(referredElement, el);
0256: name = referredElement.getElementName();
0257: schemaType = referredElement.getXMLSchemaType();
0258: if (schemaType == null) {
0259: // The referred to element either does not have a type
0260: // associated with it, or it's type is defined in it's
0261: // subelements.
0262: schemaType = name;
0263: }
0264: String ns = schema.prefixOf(name);
0265: if (ns != null) {
0266: name = schema.removePrefix(name);
0267: //namespace = schema.getNamespaceURI(ns);
0268: }
0269: namespace = referredElement.getElementNamespace();
0270: }
0271: if (debug)
0272: config.messageOut
0273: .println("processElement (start: elementName="
0274: + name + " namespace=" + namespace);
0275: if (name == null) {
0276: config.messageOut.println("WARNING: elementName is null.");
0277: }
0278: boolean definedInSubElements;
0279: if (schemaType == null) {
0280: definedInSubElements = true;
0281: schemaType = name;
0282: } else {
0283: definedInSubElements = false;
0284: }
0285: String fullSchemaType = schema.resolveNamespaceDefault(
0286: schemaType, namespace);
0287: /*
0288: if (schemaType.equals(fullSchemaType) && namespace != null) {
0289: // No namespace prefix, so add the one we're defined with.
0290: fullSchemaType = schema.canonicalQName(namespace, schemaType);
0291: System.out.println("HAD to add namespace to fullSchemaType: "+fullSchemaType);
0292: }*/
0293: String defaultValue = el.getDefault();
0294: if (debug)
0295: config.messageOut.println("processElement: name=" + name
0296: + " schemaType=" + schemaType + " fullSchemaType="
0297: + fullSchemaType + " definedInSubElements="
0298: + definedInSubElements);
0299: if (!definedInSubElements) {
0300: SchemaRep.ElementExpr schemaTypeDef = schema
0301: .getSchemaTypeDef(schemaType);
0302: if (schemaTypeDef instanceof SchemaRep.ContainsSubElements)
0303: if (hasUnionType((SchemaRep.ContainsSubElements) schemaTypeDef))
0304: handler.setUnion(el.getFullContentName(),
0305: fullSchemaType, true);
0306: //config.messageOut.println("schemaType="+schemaType+" schemaTypeDef="+schemaTypeDef);
0307: // Look for restriction
0308: if (schemaTypeDef instanceof SchemaRep.ContainsSubElements) {
0309: SchemaRep.ContainsSubElements cse = (SchemaRep.ContainsSubElements) schemaTypeDef;
0310: restrict = lookForRestriction(cse);
0311: String foundDefault = lookForDefault(restrict);
0312: if (foundDefault != null)
0313: defaultValue = foundDefault;
0314: }
0315: String javaType = null;
0316: if (schemaTypeDef instanceof SchemaRep.HasJavaTypeName) {
0317: javaType = ((SchemaRep.HasJavaTypeName) schemaTypeDef)
0318: .getJavaTypeName();
0319: } else {
0320: String ns = schema.prefixOf(schemaType);
0321: String nsURI = schema.getNamespaceURI(ns);
0322: if (JAVA_TYPE_NS.equals(nsURI)) {
0323: javaType = schema.removePrefix(schemaType);
0324: }
0325: }
0326: handler.element(el.getFullContentName(), fullSchemaType,
0327: name, namespace, getInstanceValue(
0328: el.getMinOccurs(), el.getMaxOccurs()),
0329: externalType, defaultValue);
0330: addExtraDataForType(el.getFullContentName(),
0331: fullSchemaType, schemaTypeDef);
0332: if (javaType != null) {
0333: //config.messageOut.println("It has a java type: "+javaType);
0334: handler.javaType(el.getFullContentName(),
0335: fullSchemaType, javaType);
0336: if (parentTypes.isEmpty()) {
0337: // Top level element def
0338: String mySchemaType = schema
0339: .resolveNamespaceDefault(name, namespace);
0340: //config.messageOut.println("mySchemaType="+mySchemaType);
0341: handler.javaType(el.getFullContentName(),
0342: mySchemaType, javaType);
0343: }
0344: }
0345: handler.nillable(el.isNillable());
0346: } else {
0347: restrict = lookForRestriction(el);
0348: boolean existsAlready;
0349: while (!alreadyDefined) {
0350: existsAlready = handler
0351: .doesElementExist(fullSchemaType);
0352: if (!existsAlready)
0353: break;
0354: if (debug)
0355: config.messageOut.println("existsAlready: "
0356: + existsAlready + ", " + el);
0357: String contextName = null;
0358: if (!parentTypes.isEmpty()) {
0359: contextName = (String) parentTypes.peek();
0360: int curlyBracePos = contextName.lastIndexOf('}');
0361: if (curlyBracePos >= 0)
0362: contextName = contextName
0363: .substring(curlyBracePos + 1);
0364: }
0365: if (contextName == null)
0366: contextName = "other";
0367: fullSchemaType += '-' + contextName;
0368: if (debug)
0369: config.messageOut.println("New name: "
0370: + fullSchemaType);
0371: }
0372: }
0373: parentTypes.push(fullSchemaType);
0374: parentUniqueNames.push(el.getFullContentName());
0375: processContainsSubElements(el);
0376:
0377: parentUniqueNames.pop();
0378: parentTypes.pop();
0379:
0380: if (definedInSubElements) {
0381: //config.messageOut.println("lastDefinedType="+lastDefinedType+" name="+name);
0382: // If we're a top level element and they hadn't defined the type,
0383: // then the sub elements defined it for us. If they hadn't
0384: // defined a type, and we're not a top level element, then we now
0385: // know what type we are (in lastDefinedType), and we should
0386: // add ourselves to our enclosing element.
0387: if (!parentTypes.isEmpty()) {
0388: if (restrict != null) {
0389: String foundDefault = lookForDefault(restrict);
0390: if (foundDefault != null)
0391: defaultValue = foundDefault;
0392: }
0393: handler.element(el.getFullContentName(),
0394: lastDefinedType, name, namespace,
0395: getInstanceValue(el.getMinOccurs(), el
0396: .getMaxOccurs()),
0397: lastDefinedExternalType, defaultValue);
0398: handler.nillable(el.isNillable());
0399: }
0400: SchemaRep.ElementExpr schemaTypeDef = schema
0401: .getSchemaTypeDefResolvedNamespace(lastDefinedType);
0402: if (schemaTypeDef instanceof SchemaRep.HasJavaTypeName) {
0403: String javaType = ((SchemaRep.HasJavaTypeName) schemaTypeDef)
0404: .getJavaTypeName();
0405: //config.messageOut.println("javaType="+javaType);
0406: if (javaType != null) {
0407: handler.javaType(el.getFullContentName(),
0408: lastDefinedType, javaType);
0409: }
0410: }
0411: }
0412:
0413: // Load in all of the restrictions into this element
0414: if (restrict != null) {
0415: addExtraDataCurLink(restrict);
0416: }
0417: if (!("1".equals(el.getMaxOccurs()) || "unbounded".equals(el
0418: .getMaxOccurs()))) {
0419: // Unusual maxOccurs amount, add a restriction
0420: handler.addExtraDataCurLink(new MaxOccursRestriction(el
0421: .getMaxOccurs()));
0422: }
0423: if (!("1".equals(el.getMinOccurs()) || "0".equals(el
0424: .getMinOccurs()))) {
0425: // Unusual minOccurs amount, add a restriction
0426: handler.addExtraDataCurLink(new MinOccursRestriction(el
0427: .getMinOccurs()));
0428: }
0429: if (perAttributeExtraData.size() > 0) {
0430: for (Iterator it = perAttributeExtraData.iterator(); it
0431: .hasNext();) {
0432: handler.addExtraDataCurLink(it.next());
0433: }
0434: }
0435:
0436: if (debug)
0437: config.messageOut
0438: .println("processElement finish): elementName="
0439: + name);
0440: }
0441:
0442: protected boolean hasUnionType(
0443: SchemaRep.ContainsSubElements schemaTypeDef) {
0444: if (schemaTypeDef instanceof SchemaRep.UnionType)
0445: return true;
0446: Iterator itr = schemaTypeDef.subElementsIterator();
0447: while (itr.hasNext()) {
0448: SchemaRep.ElementExpr ee = (SchemaRep.ElementExpr) itr
0449: .next();
0450: if (ee instanceof SchemaRep.ContainsSubElements) {
0451: if (hasUnionType((SchemaRep.ContainsSubElements) ee))
0452: return true;
0453: }
0454: }
0455: return false;
0456: }
0457:
0458: protected SchemaRep.Restriction[] lookForRestriction(
0459: SchemaRep.ContainsSubElements schemaTypeDef) {
0460: if (schemaTypeDef instanceof SchemaRep.UnionType) {
0461: ArrayList restrictions = new ArrayList();
0462: SchemaRep.Restriction restricts[] = null;
0463: /*
0464: String memberTypes = ((SchemaRep.UnionType)schemaTypeDef).getMemberTypes();
0465: if (memberTypes != null && memberTypes.trim().length() > 0) {
0466: String[] members = memberTypes.trim().split(" ");
0467: for (int i=0; i < members.length; i++) {
0468: restricts = null;
0469: if (members[i].length() == 0)
0470: continue;
0471: SchemaRep.ElementExpr ee = schema.getSchemaTypeDef(members[i]);
0472: if (ee instanceof SchemaRep.ContainsSubElements)
0473: restricts = lookForRestriction((SchemaRep.ContainsSubElements)ee);
0474: if (restricts != null) {
0475: for (int j=0; j < restricts.length; j++)
0476: restrictions.add(restricts[j]);
0477: }
0478: }
0479: }
0480: */
0481: SchemaRep.ElementExpr[] eeList = ((SchemaRep.UnionType) schemaTypeDef)
0482: .getMemberTypeElements();
0483: if (eeList != null) {
0484: for (int i = 0; i < eeList.length; i++) {
0485: restricts = null;
0486: if (eeList[i] instanceof SchemaRep.ContainsSubElements)
0487: restricts = lookForRestriction((SchemaRep.ContainsSubElements) eeList[i]);
0488: if (restricts != null) {
0489: for (int j = 0; j < restricts.length; j++)
0490: restrictions.add(restricts[j]);
0491: }
0492: }
0493: }
0494: Iterator itr = schemaTypeDef.subElementsIterator();
0495: while (itr.hasNext()) {
0496: restricts = null;
0497: SchemaRep.ElementExpr ee = (SchemaRep.ElementExpr) itr
0498: .next();
0499: if (ee instanceof SchemaRep.ContainsSubElements)
0500: restricts = lookForRestriction((SchemaRep.ContainsSubElements) ee);
0501: else {
0502: continue;
0503: }
0504: if (restricts != null) {
0505: for (int i = 0; i < restricts.length; i++)
0506: restrictions.add(restricts[i]);
0507: }
0508: }
0509: if (restrictions.size() == 0)
0510: return null;
0511: restricts = new SchemaRep.Restriction[restrictions.size()];
0512: return (SchemaRep.Restriction[]) restrictions
0513: .toArray(restricts);
0514: } else if (schemaTypeDef instanceof SchemaRep.SimpleType) {
0515: SchemaRep.ContainsSubElements sube = (SchemaRep.ContainsSubElements) schemaTypeDef
0516: .findSubElement(SchemaRep.UnionType.class);
0517: if (sube != null)
0518: return lookForRestriction(sube);
0519: else {
0520: SchemaRep.Restriction restrict = (SchemaRep.Restriction) schemaTypeDef
0521: .findSubElement(SchemaRep.Restriction.class);
0522: if (restrict == null)
0523: return null;
0524: ArrayList restrictions = new ArrayList();
0525: // FIXME:
0526: // If restrict.subElementsIterator().hasNext() is false, should it be
0527: // added to the list?
0528: restrictions.add(restrict);
0529: if (restrict.getBase() != null) {
0530: String unprefixedTypeName = schema
0531: .removePrefix(restrict.getBase());
0532: if (!schema.isPredefinedType(unprefixedTypeName)) {
0533: SchemaRep.ElementExpr baseee = schema
0534: .getSchemaTypeDef(restrict.getBase());
0535: SchemaRep.Restriction[] baseRestrictions = lookForRestriction((SchemaRep.ContainsSubElements) baseee);
0536: if (baseRestrictions != null) {
0537: List rList = Arrays
0538: .asList(baseRestrictions);
0539: restrictions.addAll(rList);
0540: }
0541: }
0542: }
0543: if (restrictions.size() == 0)
0544: return null;
0545: SchemaRep.Restriction[] restrictArray = new SchemaRep.Restriction[restrictions
0546: .size()];
0547: return (SchemaRep.Restriction[]) restrictions
0548: .toArray(restrictArray);
0549: }
0550: } else if (schemaTypeDef instanceof SchemaRep.SimpleContent) {
0551: SchemaRep.Restriction restrict = (SchemaRep.Restriction) schemaTypeDef
0552: .findSubElement(SchemaRep.Restriction.class);
0553: if (restrict == null)
0554: return null;
0555: return (new SchemaRep.Restriction[] { restrict });
0556: } else if (schemaTypeDef instanceof SchemaRep.Element) {
0557: return lookForRestriction((SchemaRep.ContainsSubElements) schemaTypeDef
0558: .findSubElement(SchemaRep.SimpleType.class));
0559: } else if (schemaTypeDef instanceof SchemaRep.ComplexType) {
0560: return lookForRestriction((SchemaRep.ContainsSubElements) schemaTypeDef
0561: .findSubElement(SchemaRep.SimpleContent.class));
0562: }
0563: return null;
0564: }
0565:
0566: protected void processComplexType(SchemaRep.ComplexType el)
0567: throws Schema2BeansException {
0568: setLastDefined(null, true);
0569: String name = el.getTypeName();
0570: if (debug)
0571: config.messageOut.println("processComplexType: el=" + el);
0572: if (name == null) {
0573: if (debug)
0574: config.messageOut.println("Found unnamed complexType.");
0575: if (!parentTypes.isEmpty())
0576: name = (String) parentTypes.peek();
0577: if (name == null)
0578: name = el.getFullContentName();
0579: } else {
0580: name = schema.resolveNamespace(name);
0581: }
0582: parentTypes.push(name);
0583: parentUniqueNames.push(el.getFullContentName());
0584: handler.startElement(el.getFullContentName(), name,
0585: Common.ELEMENT);
0586: handler.setAbstract(el.getFullContentName(), name, el
0587: .isAbstract());
0588: for (Iterator it = el.subElementsIterator(); it.hasNext();) {
0589: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0590: .next();
0591: if (childee instanceof SchemaRep.ModelGroup) {
0592: processModelGroup((SchemaRep.ModelGroup) childee);
0593: } else if (childee instanceof SchemaRep.Attribute) {
0594: processAttribute(name, (SchemaRep.Attribute) childee);
0595: } else if (childee instanceof SchemaRep.AttributeGroup) {
0596: processAttributeGroup(name,
0597: (SchemaRep.AttributeGroup) childee);
0598: } else if (childee instanceof SchemaRep.SimpleContent) {
0599: processSimpleContent((SchemaRep.SimpleContent) childee);
0600: } else if (childee instanceof SchemaRep.Annotation) {
0601: processAnnotation((SchemaRep.Annotation) childee);
0602: } else if (childee instanceof SchemaRep.ComplexContent) {
0603: processComplexContent((SchemaRep.ComplexContent) childee);
0604: } else {
0605: config.messageOut
0606: .println("processComplexType: Unfamiliar subelement: "
0607: + childee);
0608: }
0609: }
0610: handler.endElement();
0611: parentUniqueNames.pop();
0612: parentTypes.pop();
0613: setLastDefined(name, false);
0614: }
0615:
0616: protected void processComplexContent(SchemaRep.ComplexContent el)
0617: throws Schema2BeansException {
0618: //config.messageOut.println("el="+el);
0619: for (Iterator it = el.subElementsIterator(); it.hasNext();) {
0620: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0621: .next();
0622: if (childee instanceof SchemaRep.Extension) {
0623: processExtension((SchemaRep.Extension) childee);
0624: } else if (childee instanceof SchemaRep.Restriction) {
0625: processRestriction((SchemaRep.Restriction) childee);
0626: } else if (childee instanceof SchemaRep.Annotation) {
0627: processAnnotation((SchemaRep.Annotation) childee);
0628: } else {
0629: config.messageOut
0630: .println("processComplexContent: Unfamiliar subelement: "
0631: + childee);
0632: }
0633: }
0634: }
0635:
0636: protected void processSimpleContent(SchemaRep.SimpleContent el)
0637: throws Schema2BeansException {
0638: processContainsSubElements(el);
0639: //System.out.println("lastDefinedType="+lastDefinedType);
0640: if (lastDefinedType == null)
0641: return;
0642: SchemaRep.ElementExpr schemaTypeDef = schema
0643: .getSchemaTypeDefResolvedNamespace(lastDefinedType);
0644: if (schemaTypeDef == null)
0645: return;
0646: //System.out.println("processSimpleContent: schemaTypeDef="+schemaTypeDef);
0647: String javaType = null;
0648: if (schemaTypeDef instanceof SchemaRep.HasJavaTypeName) {
0649: javaType = ((SchemaRep.HasJavaTypeName) schemaTypeDef)
0650: .getJavaTypeName();
0651: }
0652: addExtraDataForType((String) parentUniqueNames.peek(),
0653: (String) parentTypes.peek(), schemaTypeDef);
0654: if (javaType != null) {
0655: handler.javaType((String) parentUniqueNames.peek(),
0656: (String) parentTypes.peek(), javaType);
0657: }
0658: }
0659:
0660: protected void processExtension(SchemaRep.Extension el)
0661: throws Schema2BeansException {
0662: if (debug)
0663: config.messageOut.println("extension el=" + el);
0664: String uniqueName = (String) parentUniqueNames.peek();
0665: String name = (String) parentTypes.peek();
0666: String base = el.getBase();
0667: SchemaRep.ElementExpr baseDef = schema.getSchemaTypeDef(base);
0668: //config.messageOut.println("baseDef="+baseDef);
0669: SchemaRep.Restriction[] restrict = null;
0670: if (baseDef instanceof SchemaRep.ContainsSubElements) {
0671: restrict = lookForRestriction((SchemaRep.ContainsSubElements) baseDef);
0672: // We're extending something defined internally.
0673: if (!config.isRespectExtension())
0674: processContainsSubElementsAndAttributes(
0675: (SchemaRep.ContainsSubElements) baseDef, name);
0676: }
0677: addExtraDataForType(uniqueName, name, baseDef);
0678: if (baseDef instanceof SchemaRep.ComplexType) {
0679: SchemaRep.ComplexType complexType = (SchemaRep.ComplexType) baseDef;
0680: String resolvedExtendsName = schema
0681: .resolveNamespace(complexType.getTypeName());
0682: //config.messageOut.println("resolvedExtendsName="+resolvedExtendsName);
0683: handler.setExtension(uniqueName, name, resolvedExtendsName);
0684: }
0685: String javaType = el.getJavaTypeName();
0686: if (javaType != null) {
0687: if (debug)
0688: config.messageOut.println("Setting javatype of " + name
0689: + " to " + javaType);
0690: handler.javaType(uniqueName, name, javaType);
0691: if (restrict != null) {
0692: addExtraDataNode(uniqueName, name, restrict);
0693: }
0694: }
0695: processContainsSubElementsAndAttributes(el, name);
0696: }
0697:
0698: protected void addExtraDataForType(String uniqueName, String name,
0699: SchemaRep.ElementExpr schemaTypeDef)
0700: throws Schema2BeansException {
0701: if (schemaTypeDef instanceof SchemaRep.Base64Binary
0702: || schemaTypeDef instanceof SchemaRep.HexBinary) {
0703: handler.addExtraDataNode(uniqueName, name, schemaTypeDef);
0704: //System.out.println("Adding extradata to "+name+" of "+schemaTypeDef);
0705: } else if (schemaTypeDef instanceof SchemaRep.ContainsSubElements) {
0706: SchemaRep.Restriction[] restrict = lookForRestriction((SchemaRep.ContainsSubElements) schemaTypeDef);
0707: //System.out.println("restrict="+restrict);
0708: if (restrict != null)
0709: for (int i = 0; i < restrict.length; i++)
0710: addExtraDataForType(uniqueName, name, schema
0711: .getSchemaTypeDef(restrict[i].getBase()));
0712: } else {
0713: //System.out.println("name="+name+" schemaTypeDef="+schemaTypeDef);
0714: }
0715: }
0716:
0717: protected void processModelGroup(SchemaRep.ModelGroup group)
0718: throws Schema2BeansException {
0719: if (debug)
0720: config.messageOut.println("processModelGroup: group="
0721: + group);
0722: if (group instanceof SchemaRep.Group) {
0723: SchemaRep.Group grp = (SchemaRep.Group) group;
0724: if (grp.getRef() == null) {
0725: // This is a group definition which only used thru
0726: // a reference.
0727: return;
0728: } else {
0729: SchemaRep.Group referredGroup = grp.getRefGroup();
0730: if (referredGroup == null) {
0731: config.messageOut.println(Common.getMessage(
0732: "MSG_UnableToFind", "group", grp.getRef()));
0733: } else {
0734: processContainsSubElements(referredGroup);
0735: }
0736: return;
0737: }
0738: }
0739: char separator = ' ';
0740: if (group instanceof SchemaRep.Sequence)
0741: separator = ',';
0742: else if (group instanceof SchemaRep.Choice)
0743: separator = '|';
0744: int groupInstance = getInstanceValue(group.getMinOccurs(),
0745: group.getMaxOccurs());
0746: handler.startGroupElements();
0747:
0748: boolean first = true;
0749: Iterator it = group.subElementsIterator();
0750: while (it.hasNext()) {
0751: if (first)
0752: first = false;
0753: else
0754: handler.character(separator);
0755:
0756: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0757: .next();
0758: if (childee instanceof SchemaRep.Element) {
0759: processElement((SchemaRep.Element) childee);
0760: } else if (childee instanceof SchemaRep.ModelGroup) {
0761: processModelGroup((SchemaRep.ModelGroup) childee);
0762: } else if (childee instanceof SchemaRep.Annotation) {
0763: } else if (childee instanceof SchemaRep.Any) {
0764: processAny((SchemaRep.Any) childee);
0765: } else {
0766: config.messageOut
0767: .println("processModelGroup: Unfamiliar subelement: "
0768: + childee);
0769: }
0770: }
0771:
0772: handler.endGroupElements(groupInstance);
0773: }
0774:
0775: protected void processSimpleType(SchemaRep.SimpleType el)
0776: throws Schema2BeansException {
0777: if (debug)
0778: config.messageOut.println("processSimpleType: el=" + el);
0779: /*
0780: String name = el.getTypeName();
0781: if (name != null) {
0782: parentTypes.push(name);
0783: handler.startElement(el.getFullContentName(), name, Common.ELEMENT);
0784: }
0785: */
0786: processContainsSubElements(el);
0787: //addExtraDataForType(schema.resolveNamespace(el.getTypeName()), el);
0788: /*
0789: if (name != null) {
0790: handler.endElement();
0791: parentTypes.pop();
0792: setLastDefined(name, true);
0793: }
0794: */
0795: }
0796:
0797: protected void processUnionType(SchemaRep.UnionType el)
0798: throws Schema2BeansException {
0799: if (debug)
0800: config.messageOut.println("processUnionType: el=" + el);
0801: /*
0802: String name = el.getTypeName();
0803: if (name != null) {
0804: parentTypes.push(name);
0805: handler.startElement(el.getFullContentName(), name, Common.ELEMENT);
0806: }
0807: */
0808: /*
0809: String memberTypes = el.getMemberTypes();
0810: if (memberTypes != null && memberTypes.trim().length() > 0) {
0811: String[] members = memberTypes.trim().split(" ");
0812: for (int i=0; i < members.length; i++) {
0813: if (members[i].length() == 0)
0814: continue;
0815: SchemaRep.ElementExpr schemaTypeDef = schema.getSchemaTypeDef(members[i]);
0816: process(schemaTypeDef);
0817: }
0818: }
0819: */
0820: processContainsSubElements(el);
0821: //addExtraDataForType(schema.resolveNamespace(el.getTypeName()), el);
0822: /*
0823: if (name != null) {
0824: handler.endElement();
0825: parentTypes.pop();
0826: setLastDefined(name, true);
0827: }
0828: */
0829: }
0830:
0831: protected void processRestriction(SchemaRep.Restriction el)
0832: throws Schema2BeansException {
0833: /*
0834: boolean externalType = true;
0835: String typeName = el.getJavaTypeName();
0836: if (typeName == null) {
0837: externalType = false;
0838: typeName = el.getBase();
0839: } else {
0840: setLastDefined(typeName, externalType);
0841: }
0842: */
0843: //System.out.println("processRestriction: el.base="+el.getBase()+" el.javaType="+el.getJavaTypeName());
0844: setLastDefined(schema.resolveNamespace(el.getBase()), false);
0845: //setLastDefined(el.getBase(), false);
0846: //handler.element(el.getFullContentName(), typeName, typeName, Common.TYPE_1, externalType);
0847: processContainsSubElements(el);
0848: }
0849:
0850: protected void processAny(SchemaRep.Any el)
0851: throws Schema2BeansException {
0852: if (debug)
0853: config.messageOut.println("Found " + el);
0854: String namespace = el.getNamespace();
0855: if (namespace != null && namespace.startsWith("##"))
0856: namespace = null;
0857: handler.element(el.getFullContentName(), "any", "any",
0858: namespace, getInstanceValue(el.getMinOccurs(), el
0859: .getMaxOccurs()), true, null);
0860: handler.javaType("any", "any", "org.w3c.dom.Element");
0861: handler.addExtraDataCurLink(el);
0862: }
0863:
0864: protected void processSchemaNode(SchemaRep.SchemaNode sn)
0865: throws Schema2BeansException {
0866: targetNamespace = sn.getTargetNamespace();
0867: if (targetNamespace != null && !"".equals(targetNamespace))
0868: handler.setDefaultNamespace(targetNamespace);
0869: processContainsSubElements(sn);
0870: }
0871:
0872: protected void setLastDefined(String typeName) {
0873: //config.messageOut.println("setLastDefined: typeName="+typeName);
0874: this .lastDefinedType = typeName;
0875: this .lastDefinedExternalType = false;
0876: }
0877:
0878: protected void setLastDefined(String typeName, boolean externalType) {
0879: //config.messageOut.println("setLastDefined: typeName="+typeName);
0880: this .lastDefinedType = typeName;
0881: this .lastDefinedExternalType = externalType;
0882: }
0883:
0884: protected void addTopAttributes(SchemaRep.Element parentElement,
0885: SchemaRep.Element el) {
0886: //config.messageOut.println("fullContentName="+el.getFullContentName());
0887: Iterator it = el.subElementsIterator();
0888: while (it.hasNext()) {
0889: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0890: .next();
0891: if (childee instanceof SchemaRep.Attribute) {
0892: //addTopAttributes(parentElement, (SchemaRep.Attribute) childee);
0893: } else if (childee instanceof SchemaRep.AttributeGroup) {
0894: //addTopAttributes(parentElement, (SchemaRep.AttributeGroup) childee);
0895: } else if (childee instanceof SchemaRep.ComplexType) {
0896: addTopAttributes(parentElement,
0897: (SchemaRep.ComplexType) childee);
0898: }
0899: }
0900: }
0901:
0902: protected void addTopAttributes(SchemaRep.Element parentElement,
0903: SchemaRep.ComplexType el) {
0904: Iterator it = el.subElementsIterator();
0905: while (it.hasNext()) {
0906: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
0907: .next();
0908: if (childee instanceof SchemaRep.Attribute) {
0909: //addTopAttributes(parentElement, (SchemaRep.Attribute) childee);
0910: } else if (childee instanceof SchemaRep.AttributeGroup) {
0911: //addTopAttributes(parentElement, (SchemaRep.AttributeGroup) childee);
0912: }
0913: }
0914: }
0915:
0916: protected void processAttribute(String parentElement,
0917: SchemaRep.Attribute attr) throws Schema2BeansException {
0918: if (debug)
0919: config.messageOut.println("processAttribute to "
0920: + parentElement + " attr=" + attr);
0921: if (attr.getRef() != null) {
0922: SchemaRep.Attribute referredAttr = attr.getRefAttribute();
0923: if (referredAttr == null) {
0924: config.messageOut
0925: .println(Common.getMessage("MSG_UnableToFind",
0926: "attribute", attr.getRef()));
0927: } else {
0928: processAttribute(parentElement, referredAttr);
0929: }
0930: return;
0931: }
0932:
0933: //config.messageOut.println("fullContentName="+attr.getFullContentName());
0934: String attributeName = attr.getAttributeName();
0935: boolean externalType = true;
0936: String attrType = attr.getJavaType();
0937: String schemaType = attr.getType();
0938: String defaultValue = attr.getDefaultValue();
0939: SchemaRep.Restriction[] restrict = null;
0940: SchemaRep.ElementExpr ee = null;
0941: if (schemaType != null && defaultValue == null) {
0942: ee = schema.getSchemaTypeDef(schemaType);
0943: if (ee instanceof SchemaRep.SimpleType) {
0944: SchemaRep.SimpleType st = (SchemaRep.SimpleType) ee;
0945: SchemaRep.Restriction r = (SchemaRep.Restriction) st
0946: .findSubElement("restriction");
0947: if (r != null)
0948: restrict = new SchemaRep.Restriction[] { r };
0949: //config.messageOut.println("restrict="+restrict);
0950: String foundDefault = lookForDefault(restrict);
0951: if (foundDefault != null)
0952: defaultValue = foundDefault;
0953: } else {
0954: config.messageOut.println("Type for attribute "
0955: + attributeName + " is not simple enough: "
0956: + ee);
0957: }
0958: //config.messageOut.println("defaultValue="+defaultValue);
0959: }
0960: int instance;
0961: if (defaultValue != null || attr.getFixed() != null
0962: || attr.isRequired())
0963: instance = Common.TYPE_1;
0964: else {
0965: instance = Common.TYPE_0_1;
0966: }
0967: handler.startElement(attr.getFullContentName(), parentElement,
0968: Common.ATTLIST);
0969: handler.element(attr.getFullContentName(), attrType,
0970: attributeName, attr.getAttributeNamespace(), instance,
0971: externalType, defaultValue);
0972: handler.element("CDATA", "CDATA", instance);
0973: if (attr.getFixed() != null) {
0974: handler.element("#FIXED", "#FIXED", instance); // NOI18N
0975: handler.element(attr.getFullContentName(), attrType, attr
0976: .getFixed(), null, instance, externalType,
0977: defaultValue);
0978: } else if (attr.isRequired()) {
0979: handler.element("#REQUIRED", "#REQUIRED", instance); // NOI18N
0980: } else {
0981: handler.element("#IMPLIED", "#IMPLIED", instance); // NOI18N
0982: }
0983: handler.javaType(attr.getFullContentName(), attr
0984: .getAttributeName(), attrType);
0985: if (ee != null) {
0986: addExtraDataForType(attr.getFullContentName(), attr
0987: .getAttributeName(), ee);
0988: }
0989: if (restrict != null) {
0990: addExtraDataCurLink(restrict);
0991: }
0992: handler.endElement();
0993: }
0994:
0995: protected void addExtraDataCurLink(SchemaRep.Restriction[] restrict) {
0996: //config.messageOut.println("restrict="+restrict);
0997: for (int i = 0; i < restrict.length; i++)
0998: if (restrict[i].subElementsIterator().hasNext())
0999: handler.addExtraDataCurLink(restrict[i]);
1000: /*
1001: for (Iterator it = restrict[i].subElementsIterator(); it.hasNext(); ) {
1002: Object o = it.next();
1003: if (o instanceof SchemaRep.RestrictionType) {
1004: //config.messageOut.println("Adding RestrictionType: "+o);
1005: handler.addExtraDataCurLink(o);
1006: }
1007: }
1008: */
1009: }
1010:
1011: protected void addExtraDataNode(String uniqueName, String name,
1012: SchemaRep.Restriction[] restrict)
1013: throws org.netbeans.modules.schema2beans.Schema2BeansException {
1014: for (int i = 0; i < restrict.length; i++)
1015: if (restrict[i].subElementsIterator().hasNext())
1016: handler.addExtraDataNode(uniqueName, name, restrict[i]);
1017: /*
1018: for (Iterator it = restrict[i].subElementsIterator(); it.hasNext(); ) {
1019: Object o = it.next();
1020: if (o instanceof SchemaRep.RestrictionType) {
1021: handler.addExtraDataNode(name, o);
1022: }
1023: }
1024: */
1025: }
1026:
1027: /**
1028: * Given the restrictions, can we pick a decent default.
1029: */
1030: protected String lookForDefault(SchemaRep.Restriction[] restrict) {
1031: if (config.isMakeDefaults() && restrict != null) {
1032: for (int i = 0; i < restrict.length; i++)
1033: for (Iterator subelements = restrict[i]
1034: .subElementsIterator(); subelements.hasNext();) {
1035: Object rt = subelements.next();
1036: //config.messageOut.println("rt="+rt);
1037: // If we find an Enumeration, then let's pick the
1038: // first value as the default, since it's gotta be
1039: // one of the values.
1040: if (rt instanceof SchemaRep.Enumeration) {
1041: String defaultValue = ((SchemaRep.Enumeration) rt)
1042: .getValue();
1043: //config.messageOut.println("Found defaultValue="+defaultValue);
1044: return defaultValue;
1045: }
1046: }
1047: }
1048: return null;
1049: }
1050:
1051: protected void processAttributeGroup(String parentElement,
1052: SchemaRep.AttributeGroup attrGroup)
1053: throws Schema2BeansException {
1054: SchemaRep.AttributeGroup schemaTypeDef = (SchemaRep.AttributeGroup) schema
1055: .getSchemaTypeDef(attrGroup.getRef());
1056: if (debug)
1057: config.messageOut
1058: .println("processAttributeGroup schemaTypeDef="
1059: + schemaTypeDef);
1060: if (schemaTypeDef == null)
1061: throw new IllegalStateException(
1062: "attributeGroup ref has reference to unknown name: "
1063: + attrGroup.getRef());
1064:
1065: Iterator it = schemaTypeDef.subElementsIterator();
1066: while (it.hasNext()) {
1067: SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it
1068: .next();
1069: if (childee instanceof SchemaRep.Attribute) {
1070: processAttribute(parentElement,
1071: (SchemaRep.Attribute) childee);
1072: } else if (childee instanceof SchemaRep.AttributeGroup) {
1073: processAttributeGroup(parentElement,
1074: (SchemaRep.AttributeGroup) childee);
1075: }
1076: }
1077: }
1078:
1079: protected void processAnnotation(SchemaRep.Annotation ann)
1080: throws Schema2BeansException {
1081: SchemaRep.Documentation doc = (SchemaRep.Documentation) ann
1082: .findSubElement(SchemaRep.Documentation.class);
1083: String name = null;
1084: if (!parentTypes.isEmpty())
1085: name = (String) parentTypes.peek();
1086: if (name == null)
1087: return;
1088: String uniqueName = (String) parentUniqueNames.peek();
1089: if (doc != null) {
1090: StringBuffer comment = new StringBuffer();
1091: for (Iterator subelements = doc.subElementsIterator(); subelements
1092: .hasNext();) {
1093: SchemaRep.ElementExpr el = (SchemaRep.ElementExpr) subelements
1094: .next();
1095: if (el instanceof SchemaRep.TextNode) {
1096: comment.append(((SchemaRep.TextNode) el).getText());
1097: } else if (el instanceof SchemaRep.AnyNode) {
1098: try {
1099: XMLWriter xw = new XMLWriter(false);
1100: ((SchemaRep.AnyNode) el).writeXMLSchema(xw);
1101: xw.writeTo(comment);
1102: } catch (IOException e) {
1103: // Should not occur
1104: throw new RuntimeException(e);
1105: }
1106: }
1107: }
1108: handler.setExtendedProperty(uniqueName, name, "comment",
1109: comment.toString());
1110: }
1111: SchemaRep.AppInfo appInfo = (SchemaRep.AppInfo) ann
1112: .findSubElement(SchemaRep.AppInfo.class);
1113: if (appInfo != null) {
1114: String switchName = null;
1115: String switchHelp = null;
1116: boolean switchMandatory = false;
1117: for (Iterator subelements = appInfo.subElementsIterator(); subelements
1118: .hasNext();) {
1119: SchemaRep.ElementExpr el = (SchemaRep.ElementExpr) subelements
1120: .next();
1121: if (el instanceof SchemaRep.AnyNode) {
1122: SchemaRep.AnyNode anyNode = (SchemaRep.AnyNode) el;
1123: String anyNodeName = anyNode.getContentName();
1124: if (anyNodeName == null)
1125: continue;
1126: anyNodeName = anyNodeName.intern();
1127: if ("extends" == anyNodeName) {
1128: SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode
1129: .findSubElement(SchemaRep.TextNode.class);
1130: if (value != null) {
1131: handler.setExtendedProperty(uniqueName,
1132: name, "extends", value.getText());
1133: }
1134: } else if ("implements" == anyNodeName) {
1135: SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode
1136: .findSubElement(SchemaRep.TextNode.class);
1137: if (value != null) {
1138: handler
1139: .setExtendedProperty(uniqueName,
1140: name, "implements", value
1141: .getText());
1142: }
1143: } else if ("switch" == anyNodeName) {
1144: SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode
1145: .findSubElement(SchemaRep.TextNode.class);
1146: if (value != null)
1147: switchName = value.getText();
1148: } else if ("switchHelp" == anyNodeName) {
1149: SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode
1150: .findSubElement(SchemaRep.TextNode.class);
1151: if (value != null)
1152: switchHelp = value.getText();
1153: } else if ("switchMandatory" == anyNodeName) {
1154: SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode
1155: .findSubElement(SchemaRep.TextNode.class);
1156: if (value != null) {
1157: switchMandatory = "true"
1158: .equalsIgnoreCase(value.getText());
1159: }
1160: }
1161: }
1162: }
1163: if (switchName != null) {
1164: perAttributeExtraData.add(new SwitchData(switchName,
1165: switchHelp, switchMandatory));
1166: }
1167: }
1168: }
1169:
1170: static protected int getInstanceValue(String minOccurs,
1171: String maxOccurs) {
1172: if (minOccurs == null)
1173: minOccurs = "1";
1174: if (maxOccurs == null)
1175: maxOccurs = "1";
1176:
1177: if (minOccurs.equals("0")) {
1178: if (maxOccurs.equals("1"))
1179: return Common.TYPE_0_1;
1180: //if (maxOccurs.equals("unbounded"))
1181: return Common.TYPE_0_N;
1182: }
1183: if (maxOccurs.equals("1"))
1184: return Common.TYPE_1;
1185: return Common.TYPE_1_N;
1186: }
1187:
1188: public static class MaxOccursRestriction implements
1189: DataListRestriction/*, HasAnnotation*/{
1190: private String maxOccurs;
1191:
1192: public MaxOccursRestriction(String maxOccurs) {
1193: //assert "unbounded".equalsIgnoreCase(maxOccurs) || Integer.parseInt(maxOccurs) >= 0;
1194: this .maxOccurs = maxOccurs;
1195: }
1196:
1197: public void genRestriction(Writer out, String sizeExpr,
1198: String readMethod, String type, String failVar,
1199: boolean passCheck) throws IOException {
1200: if (!passCheck) {
1201: out.write("if (" + sizeExpr + " > " + maxOccurs
1202: + ") {\n");
1203: out.write(failVar + " = true;\n");
1204: out.write("}\n");
1205: } else {
1206: out.write("if (" + sizeExpr + " <= " + maxOccurs
1207: + ") {\n");
1208: out.write(failVar + " = true;\n");
1209: out.write("}\n");
1210: }
1211: }
1212:
1213: public int getMaxOccurs() {
1214: if ("unbounded".equalsIgnoreCase(maxOccurs))
1215: return Integer.MAX_VALUE;
1216: return Integer.parseInt(maxOccurs);
1217: }
1218:
1219: public String toString() {
1220: return "maxOccurs (" + maxOccurs + ")";
1221: }
1222:
1223: public String genAnnotation() {
1224: return "MaxOccurs(" + maxOccurs + ")";
1225: }
1226:
1227: }
1228:
1229: public static class MinOccursRestriction implements
1230: DataListRestriction/*, HasAnnotation*/{
1231: private String minOccurs;
1232:
1233: public MinOccursRestriction(String minOccurs) {
1234: //assert Integer.parseInt(minOccurs) >= 0;
1235: this .minOccurs = minOccurs;
1236: }
1237:
1238: public void genRestriction(Writer out, String sizeExpr,
1239: String readMethod, String type, String failVar,
1240: boolean passCheck) throws IOException {
1241: if (!passCheck) {
1242: out.write("if (" + sizeExpr + " < " + minOccurs
1243: + ") {\n");
1244: out.write(failVar + " = true;\n");
1245: out.write("}\n");
1246: } else {
1247: out.write("if (" + sizeExpr + " >= " + minOccurs
1248: + ") {\n");
1249: out.write(failVar + " = true;\n");
1250: out.write("}\n");
1251: }
1252: }
1253:
1254: public int getMinOccurs() {
1255: return Integer.parseInt(minOccurs);
1256: }
1257:
1258: public String toString() {
1259: return "minOccurs (" + minOccurs + ")";
1260: }
1261:
1262: public String genAnnotation() {
1263: return "MinOccurs(" + minOccurs + ")";
1264: }
1265:
1266: }
1267:
1268: public static class GeneralAnnotation implements HasAnnotation {
1269: private String annotation;
1270:
1271: public GeneralAnnotation(String annotation) {
1272: this .annotation = annotation;
1273: }
1274:
1275: public String genAnnotation() {
1276: return annotation;
1277: }
1278: }
1279:
1280: public static class SwitchData {
1281: private String switchName;
1282: private String switchHelp;
1283: private boolean mandatory;
1284:
1285: public SwitchData(String switchName) {
1286: this .switchName = switchName;
1287: }
1288:
1289: public SwitchData(String switchName, String switchHelp) {
1290: this .switchName = switchName;
1291: this .switchHelp = switchHelp;
1292: }
1293:
1294: public SwitchData(String switchName, String switchHelp,
1295: boolean mandatory) {
1296: this .switchName = switchName;
1297: this .switchHelp = switchHelp;
1298: this .mandatory = mandatory;
1299: }
1300:
1301: public String getName() {
1302: return switchName;
1303: }
1304:
1305: public String getHelp() {
1306: return switchHelp;
1307: }
1308:
1309: public boolean isMandatory() {
1310: return mandatory;
1311: }
1312:
1313: public String toString() {
1314: return "Switch";
1315: }
1316: }
1317: }
|