0001: /*
0002: * WSDL2JavaImpl.java
0003: *
0004: * Created on October 30, 2006, 10:35 AM
0005: *
0006: * To change this template, choose Tools | Template Manager
0007: * and open the template in the editor.
0008: */
0009:
0010: package org.netbeans.modules.e2e.wsdl.wsdl2java;
0011:
0012: import java.io.File;
0013: import java.security.InvalidParameterException;
0014: import java.util.ArrayList;
0015: import java.util.Collections;
0016: import java.util.HashMap;
0017: import java.util.HashSet;
0018: import java.util.Iterator;
0019: import java.util.List;
0020: import java.util.Map;
0021: import java.util.Set;
0022: import javax.xml.namespace.QName;
0023: import org.netbeans.modules.e2e.api.schema.Element;
0024: import org.netbeans.modules.e2e.api.schema.SchemaConstruct;
0025: import org.netbeans.modules.e2e.api.schema.Type;
0026: import org.netbeans.modules.e2e.api.wsdl.Binding;
0027: import org.netbeans.modules.e2e.api.wsdl.BindingOperation;
0028: import org.netbeans.modules.e2e.api.wsdl.Definition;
0029: import org.netbeans.modules.e2e.api.wsdl.Input;
0030: import org.netbeans.modules.e2e.api.wsdl.Message;
0031: import org.netbeans.modules.e2e.api.wsdl.Operation;
0032: import org.netbeans.modules.e2e.api.wsdl.Output;
0033: import org.netbeans.modules.e2e.api.wsdl.Part;
0034: import org.netbeans.modules.e2e.api.wsdl.Port;
0035: import org.netbeans.modules.e2e.api.wsdl.PortType;
0036: import org.netbeans.modules.e2e.api.wsdl.Service;
0037: import org.netbeans.modules.e2e.api.wsdl.extensions.ExtensibilityElement;
0038: import org.netbeans.modules.e2e.api.wsdl.extensions.soap.SOAPAddress;
0039: import org.netbeans.modules.e2e.api.wsdl.extensions.soap.SOAPOperation;
0040: import org.netbeans.modules.e2e.api.wsdl.wsdl2java.WSDL2Java;
0041: import org.netbeans.modules.e2e.schema.SchemaConstants;
0042: import org.netbeans.modules.e2e.wsdl.WSDLException;
0043: import org.netbeans.modules.e2e.wsdl.WSDLParser;
0044: import org.netbeans.modules.e2e.wsdl.extensions.soap.SOAPConstants;
0045: import org.openide.filesystems.FileObject;
0046: import org.openide.filesystems.FileUtil;
0047:
0048: /**
0049: *
0050: * @author Michal Skvor
0051: */
0052: public class WSDL2JavaImpl implements WSDL2Java {
0053:
0054: private WSDLParser wsdlParser;
0055: private Definition definition;
0056:
0057: private Set<QName> usedParameterTypes;
0058: private Set<QName> usedReturnTypes;
0059:
0060: private Map<QName, Integer> uniqueTypeName;
0061:
0062: private WSDL2Java.Configuration configuration;
0063:
0064: /** Creates a new instance of WSDL2JavaImpl */
0065: public WSDL2JavaImpl(WSDL2Java.Configuration configuration) {
0066: this .configuration = configuration;
0067:
0068: wsdlParser = new WSDLParser();
0069: }
0070:
0071: public boolean generate() {
0072: uniqueTypeName = new HashMap<QName, Integer>();
0073:
0074: try {
0075:
0076: definition = wsdlParser.parse(configuration
0077: .getWSDLFileName());
0078:
0079: // Check for validity of the WSDL
0080: boolean valid = true;
0081: for (ValidationResult vr : validate()) {
0082: if (ValidationResult.ErrorLevel.FATAL.equals(vr
0083: .getErrorLevel())) {
0084: valid = false;
0085: break;
0086: }
0087: }
0088:
0089: if (valid) {
0090: generateInterfaces();
0091: generateTypes();
0092: generateStub();
0093: }
0094: } catch (WSDLException e) {
0095: e.printStackTrace();
0096: return false;
0097: } catch (Exception e) {
0098: e.printStackTrace();
0099: return false;
0100: }
0101: return true;
0102: }
0103:
0104: public List<ValidationResult> validate() {
0105: List<ValidationResult> validationResults = new ArrayList<ValidationResult>();
0106: try {
0107: definition = wsdlParser.parse(configuration
0108: .getWSDLFileName());
0109: validationResults.addAll(wsdlParser.getValidationResults());
0110:
0111: // Check of errors during the WSDL parsing. If fatal, return the validation results
0112: // from the parsing process
0113: for (ValidationResult result : validationResults) {
0114: if (ValidationResult.ErrorLevel.FATAL.equals(result
0115: .getErrorLevel())) {
0116: return validationResults;
0117: }
0118: }
0119:
0120: // Do the validation
0121: WSDLValidator validator = new WSDLValidator(wsdlParser
0122: .getValidationResults(), definition);
0123: validationResults = validator.validate();
0124: } catch (WSDLException e) {
0125: // Drop e because the error is already in validationResults
0126: // e.printStackTrace();
0127: } catch (Exception e) {
0128: e.printStackTrace();
0129: }
0130: return validationResults;
0131: }
0132:
0133: /**
0134: * @return null when void type
0135: */
0136: public Element getReturnElement(Element element) {
0137: if (element.getMaxOccurs() > 1)
0138: return element;
0139: Type type = element.getType();
0140: if (Type.FLAVOR_PRIMITIVE == type.getFlavor()) {
0141: return element;
0142: } else if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
0143: if (type.getSubconstructs().size() == 0) {
0144: return element;
0145: } else if (type.getSubconstructs().size() == 1) {
0146: return (Element) type.getSubconstructs().get(0);
0147: }
0148: }
0149: return element;
0150: }
0151:
0152: public List<Element> getParameterElements(Element element) {
0153: List<Element> params = new ArrayList<Element>();
0154: params.add(element);
0155:
0156: if (element.getMaxOccurs() > 1) {
0157: return params;
0158: }
0159: Type type = element.getType();
0160: if (Type.FLAVOR_PRIMITIVE == type.getFlavor()) {
0161: return params;
0162: } else if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
0163: params = new ArrayList<Element>();
0164: for (SchemaConstruct sc : type.getSubconstructs()) {
0165: if (SchemaConstruct.ConstructType.ELEMENT.equals(sc
0166: .getConstructType())) {
0167: Element sce = (Element) sc;
0168: params.add(sce);
0169: }
0170: }
0171: return params;
0172: }
0173: return params;
0174: }
0175:
0176: public String getJavaTypeName(Element e) {
0177: boolean isArray = e.getMaxOccurs() > 1;
0178: String javaTypeName = "";
0179: Type type = e.getType();
0180: if (Type.FLAVOR_PRIMITIVE == type.getFlavor()) {
0181: if (e.isNillable() || e.getMinOccurs() == 0) {
0182: javaTypeName = getWrapperTypeName(e.getType());
0183: } else {
0184: javaTypeName = type.getJavaTypeName();
0185: }
0186: } else if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
0187: if (type.getSubconstructs().size() == 0) {
0188: javaTypeName = "void";
0189: } else {
0190: javaTypeName = e.getType().getName() == null ? e
0191: .getName().getLocalPart() : e.getType()
0192: .getName().getLocalPart();
0193: }
0194: }
0195: return javaTypeName + (isArray ? "[]" : "");
0196: }
0197:
0198: private boolean isElementComplex(Element e) {
0199: boolean isArray = e.getMaxOccurs() > 1;
0200: if (isArray)
0201: return true;
0202:
0203: Type type = e.getType();
0204: if (Type.FLAVOR_PRIMITIVE == type.getFlavor()) {
0205: if (e.isNillable()) {
0206: return true;
0207: } else {
0208: return false;
0209: }
0210: } else if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
0211: if (type.getSubconstructs().size() == 0) {
0212: return false;
0213: } else {
0214: return true;
0215: }
0216: }
0217: return true;
0218: }
0219:
0220: private void generateInterfaces() throws Exception {
0221: Set<QName> usedTypes = new HashSet<QName>();
0222:
0223: usedReturnTypes = new HashSet<QName>();
0224: usedParameterTypes = new HashSet<QName>();
0225:
0226: Set<QName> usedReturnTypeNames = new HashSet<QName>();
0227: Set<QName> usedParameterTypeNames = new HashSet<QName>();
0228:
0229: for (String serviceName : definition.getServices().keySet()) {
0230:
0231: File outputDirectoryF = new File(configuration
0232: .getOutputDirectory());
0233: FileObject outputDirectoryFO = FileUtil
0234: .toFileObject(FileUtil
0235: .normalizeFile(outputDirectoryF));
0236: FileObject outputFileDirectoryFO = outputDirectoryFO
0237: .getFileObject(configuration.getPackageName()
0238: .replace('.', '/')); // NOI18N
0239: FileObject outputFile = outputFileDirectoryFO
0240: .getFileObject(serviceName, "java");
0241: if (outputFile == null) {
0242: outputFile = outputFileDirectoryFO.createData(
0243: serviceName, "java");
0244: }
0245:
0246: OutputFileFormatter off = new OutputFileFormatter(
0247: outputFile);
0248:
0249: Service service = definition.getService(serviceName);
0250: if (configuration.getPackageName() != null
0251: && !""
0252: .equals(configuration.getPackageName()
0253: .trim())) {
0254: off.write("package " + configuration.getPackageName()
0255: + ";\t\t");
0256: }
0257: off.write("\n");
0258: off.write("import javax.xml.namespace.QName;\n");
0259: off.write("\n");
0260:
0261: off.write("public interface " + serviceName
0262: + " extends java.rmi.Remote {\n");
0263:
0264: for (Port port : service.getPorts()) {
0265: for (ExtensibilityElement ee : port
0266: .getExtensibilityElements()) {
0267: if (SOAPConstants.ADDRESS.equals(ee
0268: .getElementType())) {
0269: PortType portType = port.getBinding()
0270: .getPortType();
0271: for (Operation operation : portType
0272: .getOperations()) {
0273:
0274: String operationDocumentation = operation
0275: .getDocumentation();
0276: if (operationDocumentation == null)
0277: operationDocumentation = "";
0278: off
0279: .write("\n/**\n * "
0280: + operationDocumentation
0281: + "\n*/\n");
0282:
0283: Output output = operation.getOutput();
0284: if (output != null) {
0285: for (Part part : output.getMessage()
0286: .getParts()) {
0287: Element re = definition
0288: .getSchemaHolder()
0289: .getSchemaElement(
0290: part
0291: .getElementName());
0292: Element e = getReturnElement(re);
0293: if (isElementComplex(e)) {
0294: usedTypes.add(e.getName());
0295: usedReturnTypeNames.add(re
0296: .getName());
0297: }
0298: String javaTypeName = getJavaTypeName(e);
0299: off.write("public " + javaTypeName
0300: + " ");
0301: break;
0302: }
0303: } else {
0304: off.write("public void ");
0305: }
0306:
0307: off.write(operation.getName() + "(");
0308:
0309: Input input = operation.getInput();
0310: if (input != null) {
0311: for (Part part : input.getMessage()
0312: .getParts()) {
0313: Element element = definition
0314: .getSchemaHolder()
0315: .getSchemaElement(
0316: part
0317: .getElementName());
0318: List<Element> params = getParameterElements(element);
0319: for (Iterator<Element> it = params
0320: .iterator(); it.hasNext();) {
0321: Element e = it.next();
0322: if (isElementComplex(e)) {
0323: usedTypes.add(e.getName());
0324: usedParameterTypeNames
0325: .add(e.getName());
0326: }
0327: Type type = e.getType();
0328: String javaTypeName = getJavaTypeName(e);
0329: off
0330: .write(javaTypeName
0331: + " "
0332: + e
0333: .getName()
0334: .getLocalPart());
0335: if (it.hasNext())
0336: off.write(", ");
0337: }
0338: }
0339: }
0340: off
0341: .write(") throws java.rmi.RemoteException;\n");
0342: }
0343: }
0344: }
0345: }
0346: off.write("\n}\n");
0347: off.close();
0348: }
0349:
0350: for (QName typeName : usedParameterTypeNames) {
0351: usedParameterTypes.addAll(traverseTypes(
0352: usedParameterTypeNames, typeName));
0353: }
0354:
0355: for (QName typeName : usedReturnTypeNames) {
0356: usedReturnTypes.addAll(traverseTypes(
0357: usedParameterTypeNames, typeName));
0358: }
0359: }
0360:
0361: private String getWrapperTypeName(Type type) {
0362: QName typeName = type.getName();
0363: if (SchemaConstants.TYPE_INT.equals(typeName)) {
0364: return "Integer";
0365: } else if (SchemaConstants.TYPE_BOOLEAN.equals(typeName)) {
0366: return "Boolean";
0367: } else if (SchemaConstants.TYPE_BYTE.equals(typeName)) {
0368: return "Byte";
0369: } else if (SchemaConstants.TYPE_DOUBLE.equals(typeName)) {
0370: return "Double";
0371: } else if (SchemaConstants.TYPE_FLOAT.equals(typeName)) {
0372: return "Float";
0373: } else if (SchemaConstants.TYPE_LONG.equals(typeName)) {
0374: return "Long";
0375: } else if (SchemaConstants.TYPE_SHORT.equals(typeName)) {
0376: return "Short";
0377: } else if (SchemaConstants.TYPE_BASE64_BINARY.equals(typeName)) {
0378: return "byte[]";
0379: } else if (SchemaConstants.TYPE_HEX_BINARY.equals(typeName)) {
0380: return "byte[]";
0381: } else if (SchemaConstants.TYPE_STRING.equals(typeName)) {
0382: return "String";
0383: }
0384: return type.getName().getLocalPart();
0385: }
0386:
0387: /**
0388: * Traverses used types by methods and creates complete list of used types
0389: */
0390: public Set<QName> traverseTypes(Set<QName> types, QName typeName) {
0391: Set<QName> result = new HashSet<QName>();
0392: result.add(typeName);
0393: Element element = definition.getSchemaHolder()
0394: .getSchemaElement(typeName);
0395: Type type = element.getType();
0396: if (type == null) {
0397: throw new IllegalArgumentException("Invalid element type.");
0398: }
0399: if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
0400: for (SchemaConstruct sc : type.getSubconstructs()) {
0401: if (SchemaConstruct.ConstructType.ELEMENT == sc
0402: .getConstructType()) {
0403: Element sce = (Element) sc;
0404: if (Type.FLAVOR_SEQUENCE == sce.getType()
0405: .getFlavor()) {
0406: result.add(sce.getName());
0407: result.addAll(traverseTypes(types, sce
0408: .getName()));
0409: }
0410: }
0411: }
0412: }
0413: return result;
0414: }
0415:
0416: private Set<QName> getUsedTypes(QName typeName) {
0417: Set<QName> result = new HashSet<QName>();
0418: Element element = definition.getSchemaHolder()
0419: .getSchemaElement(typeName);
0420: Type type = null;
0421: if (element == null) {
0422: type = definition.getSchemaHolder().getSchemaType(typeName);
0423: } else {
0424: type = element.getType();
0425: }
0426: if (Type.FLAVOR_PRIMITIVE == type.getFlavor())
0427: return result;
0428: if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
0429: result.add(type.getName());
0430:
0431: SchemaConstruct parent = type.getParent();
0432: if (parent != null)
0433: result.addAll(getUsedTypes(parent.getName()));
0434: for (SchemaConstruct sc : type.getSubconstructs()) {
0435: if (SchemaConstruct.ConstructType.ELEMENT == sc
0436: .getConstructType()) {
0437: Element sce = (Element) sc;
0438: if (Type.FLAVOR_SEQUENCE == sce.getType()
0439: .getFlavor()) {
0440: result.addAll(getUsedTypes(sce.getName()));
0441: }
0442: }
0443: }
0444: }
0445: return result;
0446: }
0447:
0448: /**
0449: * Generates all used complex types
0450: */
0451: private void generateTypes() throws Exception {
0452: Set<Element> usedArrayTypes = new HashSet<Element>();
0453:
0454: for (QName type : usedReturnTypes) {
0455: Element e = getReturnElement(definition.getSchemaHolder()
0456: .getSchemaElement(type));
0457: usedArrayTypes.addAll(generateType(e));
0458: }
0459: for (QName type : usedParameterTypes) {
0460: Element e = definition.getSchemaHolder().getSchemaElement(
0461: type);
0462: usedArrayTypes.addAll(generateType(e));
0463: }
0464: if (configuration.getGenerateDataBinding()) {
0465: generateDataBindingArrays(usedArrayTypes);
0466: }
0467:
0468: }
0469:
0470: private Set<Element> generateType(Element element) throws Exception {
0471: Set<Element> arrayTypes = new HashSet<Element>();
0472:
0473: Type type = element.getType();
0474: if (type == null) {
0475: throw new IllegalArgumentException("Invalid element type.");
0476: }
0477: if (Type.FLAVOR_PRIMITIVE == type.getFlavor())
0478: return arrayTypes;
0479: String name = type.getName() == null ? element.getName()
0480: .getLocalPart() : type.getName().getLocalPart();
0481: File outputDirectoryF = new File(configuration
0482: .getOutputDirectory());
0483: FileObject outputDirectoryFO = FileUtil.toFileObject(FileUtil
0484: .normalizeFile(outputDirectoryF));
0485: FileObject outputFileDirectoryFO = outputDirectoryFO
0486: .getFileObject(configuration.getPackageName().replace(
0487: '.', '/')); // NOI18N
0488: FileObject outputFile = outputFileDirectoryFO.getFileObject(
0489: name, "java");
0490: if (outputFile == null) {
0491: outputFile = outputFileDirectoryFO.createData(name, "java");
0492: }
0493:
0494: OutputFileFormatter off = new OutputFileFormatter(outputFile);
0495:
0496: if (configuration.getPackageName() != null
0497: && !"".equals(configuration.getPackageName().trim())) {
0498: off.write("package " + configuration.getPackageName()
0499: + ";\n");
0500: }
0501: off.write("\n");
0502: off.write("import javax.xml.namespace.QName;\n");
0503: if (configuration.getGenerateDataBinding()) {
0504: off
0505: .write("import org.netbeans.microedition.databinding.DataSet;\n");
0506: off
0507: .write("import org.netbeans.microedition.databinding.DataBindingException;\n");
0508: // off.write( "import org.netbeans.microedition.databinding.DataSource;\n" );
0509: off.write("\n");
0510: if (type.getParent() == null) {
0511: off.write("public class " + name
0512: + " implements DataSet {\n");
0513: } else {
0514: Type parentType = definition.getSchemaHolder()
0515: .getSchemaType(type.getParent().getName());
0516: String parentName = parentType.getName().getLocalPart();
0517: off.write("public class " + name + " extends "
0518: + parentName + " implements DataSet {\n");
0519: }
0520: } else {
0521: if (type.getParent() == null) {
0522: off.write("public class " + name + " {\n");
0523: } else {
0524: Type parentType = definition.getSchemaHolder()
0525: .getSchemaType(type.getParent().getName());
0526: String parentName = parentType.getName().getLocalPart();
0527: off.write("public class " + name + " extends "
0528: + parentName + " {\n");
0529: }
0530: }
0531: for (SchemaConstruct sc : type.getSubconstructs()) {
0532: if (SchemaConstruct.ConstructType.ELEMENT == sc
0533: .getConstructType()) {
0534: Element sce = (Element) sc;
0535: String propertyName = sce.getName().getLocalPart();
0536: String propertyVariableName = propertyName.substring(0,
0537: 1).toLowerCase()
0538: + propertyName.substring(1);
0539: String propertyType = sce.getType().getName()
0540: .getLocalPart();
0541: boolean isArray = sce.getMaxOccurs() > 1;
0542: if (Type.FLAVOR_PRIMITIVE == sce.getType().getFlavor()) {
0543: if (sce.getMinOccurs() == 0 || sce.isNillable()) {
0544: propertyType = getWrapperTypeName(sce.getType());
0545: } else {
0546: propertyType = sce.getType().getJavaTypeName();
0547: }
0548: //propertyType = sce.getType().getJavaTypeName();
0549: }
0550: if (WSDL2Java.Configuration.TYPE_JAVA_BEANS == configuration
0551: .getGenerateType()) {
0552: // Generate code in JavaBeans style
0553:
0554: off.write("\n");
0555: if (!configuration.getGenerateDataBinding()) {
0556: off.write("private " + propertyType
0557: + (isArray ? "[] " : " ")
0558: + propertyVariableName + ";\n");
0559: } else {
0560: if (!isArray) {
0561: off.write("private " + propertyType + " "
0562: + propertyVariableName + ";\n");
0563: } else {
0564: off.write("private " + propertyType
0565: + "ArrayItem "
0566: + propertyVariableName
0567: + "_array_item = " + "new "
0568: + propertyType + "ArrayItem();\n");
0569: }
0570: }
0571: off.write("\n");
0572:
0573: off.write("public void " + setter(propertyName)
0574: + "( " + propertyType
0575: + (isArray ? "[] " : " ")
0576: + propertyVariableName + " ) {\n");
0577: if (isArray
0578: && configuration.getGenerateDataBinding()) {
0579: off.write(propertyVariableName
0580: + "_array_item.setArray("
0581: + propertyVariableName + ");\n");
0582: } else {
0583: off.write("this." + propertyVariableName
0584: + " = " + propertyVariableName + ";\n");
0585: }
0586: off.write("}\n\n");
0587:
0588: off.write("public " + propertyType
0589: + (isArray ? "[] " : " ")
0590: + getter(propertyName) + "() {\n");
0591: if (isArray
0592: && configuration.getGenerateDataBinding()) {
0593: off.write("return " + propertyVariableName
0594: + "_array_item.getArray();\n");
0595: } else {
0596: off.write("return " + propertyVariableName
0597: + ";\n");
0598: }
0599: off.write("}\n");
0600:
0601: } else if (WSDL2Java.Configuration.TYPE_STRUCTURES == configuration
0602: .getGenerateType()) {
0603: // Generate code in struct style
0604:
0605: off.write("\n");
0606: off.write("public " + propertyType
0607: + (isArray ? "[] " : " ")
0608: + propertyVariableName + ";\n");
0609: }
0610:
0611: if (sce.getMaxOccurs() > 1) {
0612: arrayTypes.add(sce);
0613: }
0614:
0615: }
0616: }
0617: off.write("\n");
0618:
0619: if (configuration.getGenerateDataBinding()) {
0620: // getType
0621: off.write("public Class getType(String dataItemName) {\n");
0622: for (SchemaConstruct sc : type.getSubconstructs()) {
0623: if (SchemaConstruct.ConstructType.ELEMENT == sc
0624: .getConstructType()) {
0625: Element sce = (Element) sc;
0626: String propertyName = sce.getName().getLocalPart();
0627: String propertyVariableName = propertyName
0628: .substring(0, 1).toLowerCase()
0629: + propertyName.substring(1);
0630: String propertyType = sce.getType().getName()
0631: .getLocalPart();
0632: boolean isArray = sce.getMaxOccurs() > 1;
0633: if (Type.FLAVOR_PRIMITIVE == sce.getType()
0634: .getFlavor()) {
0635: propertyType = getWrapperTypeName(sce.getType());
0636: }
0637:
0638: off.write("if( \"" + propertyVariableName
0639: + "\".equals(dataItemName)) {\n");
0640: if (isArray) {
0641: off
0642: .write("return org.netbeans.microedition.databinding.IndexableDataSet.class;\n");
0643: } else {
0644: off.write("return " + propertyType
0645: + ".class;\n");
0646: }
0647: off.write("}\n");
0648: }
0649: }
0650: off
0651: .write("throw new IllegalArgumentException( \"Invalid data item name \" + dataItemName );\n");
0652: off.write("}\n");
0653: off.write("\n");
0654:
0655: // getValue
0656: off
0657: .write("public Object getValue(String dataItemName) {\n");
0658: for (SchemaConstruct sc : type.getSubconstructs()) {
0659: if (SchemaConstruct.ConstructType.ELEMENT == sc
0660: .getConstructType()) {
0661: Element sce = (Element) sc;
0662: String propertyName = sce.getName().getLocalPart();
0663: String propertyVariableName = propertyName
0664: .substring(0, 1).toLowerCase()
0665: + propertyName.substring(1);
0666: String propertyType = sce.getType().getName()
0667: .getLocalPart();
0668:
0669: off.write("if( \"" + propertyVariableName
0670: + "\".equals(dataItemName)) {\n");
0671: // if( Type.FLAVOR_PRIMITIVE == sce.getType().getFlavor()) {
0672: // off.write( "return " + wrapPrimitiveType( sce.getType(), propertyVariableName ) + ";\n" );
0673: // } else {
0674: if (sce.getMaxOccurs() > 1) {
0675: off.write("return " + propertyVariableName
0676: + "_array_item;\n");
0677: } else {
0678: off.write("return "
0679: + wrapPrimitiveType(sce.getType(),
0680: propertyVariableName) + ";\n");
0681: // off.write( "return " + propertyVariableName + ";\n" );
0682: }
0683: // }
0684: off.write("}\n");
0685: }
0686: }
0687: off
0688: .write("throw new IllegalArgumentException( \"Invalid data item name \" + dataItemName );\n");
0689: off.write("}\n");
0690: off.write("\n");
0691:
0692: // setValue
0693: off
0694: .write("public void setValue(String dataItemName, Object value) throws DataBindingException {\n");
0695: for (SchemaConstruct sc : type.getSubconstructs()) {
0696: if (SchemaConstruct.ConstructType.ELEMENT == sc
0697: .getConstructType()) {
0698: Element sce = (Element) sc;
0699: String propertyName = sce.getName().getLocalPart();
0700: String propertyVariableName = propertyName
0701: .substring(0, 1).toLowerCase()
0702: + propertyName.substring(1);
0703: String propertyType = sce.getType().getName()
0704: .getLocalPart();
0705:
0706: // Generate set only for non array fields
0707: if (sce.getMaxOccurs() <= 1) {
0708: off.write("if( \"" + propertyVariableName
0709: + "\".equals(dataItemName)) {\n");
0710: if (Type.FLAVOR_PRIMITIVE == sce.getType()
0711: .getFlavor()) {
0712: off
0713: .write(propertyVariableName
0714: + " = "
0715: + unwrapPrimitiveType(sce,
0716: " value") + ";\n");
0717: } else {
0718: off.write(propertyVariableName + " = ("
0719: + propertyType + ") value;\n");
0720: }
0721: off.write("}\n");
0722: }
0723: }
0724: }
0725: off.write("}\n");
0726: off.write("\n");
0727:
0728: // setAsString
0729: off
0730: .write("public void setAsString(String dataItemName, String value) throws DataBindingException {\n");
0731: for (SchemaConstruct sc : type.getSubconstructs()) {
0732: if (SchemaConstruct.ConstructType.ELEMENT == sc
0733: .getConstructType()) {
0734: Element sce = (Element) sc;
0735: String propertyName = sce.getName().getLocalPart();
0736: String propertyVariableName = propertyName
0737: .substring(0, 1).toLowerCase()
0738: + propertyName.substring(1);
0739: String propertyType = sce.getType().getName()
0740: .getLocalPart();
0741:
0742: // Generate set only for non array fields
0743: if (sce.getMaxOccurs() <= 1) {
0744: off.write("if( \"" + propertyVariableName
0745: + "\".equals(dataItemName)) {\n");
0746: if (Type.FLAVOR_PRIMITIVE == sce.getType()
0747: .getFlavor()) {
0748: off.write(propertyVariableName + " = "
0749: + parsePrimitiveType(sce, "value")
0750: + ";\n");
0751: } else {
0752: off
0753: .write("throw new DataBindingException( \"Illegal assigment.\");\n");
0754: }
0755: off.write("}\n");
0756: }
0757: }
0758: }
0759: off.write("}\n");
0760: off.write("\n");
0761: off
0762: .write("public boolean isReadOnly(String dataItemName) {\n");
0763: off.write("return false;\n");
0764: off.write("}\n");
0765: }
0766: off.write("}\n");
0767: off.close();
0768:
0769: return arrayTypes;
0770: }
0771:
0772: /**
0773: * Generates structures for databinding arrays
0774: */
0775: private void generateDataBindingArrays(Set<Element> types)
0776: throws Exception {
0777: for (Element element : types) {
0778: //Element element = definition.getSchemaHolder().getSchemaElement( qName );
0779: Type type = element.getType();
0780: if (type == null) {
0781: throw new IllegalArgumentException(
0782: "Invalid element type.");
0783: }
0784:
0785: String typeName = "";
0786: if (Type.FLAVOR_PRIMITIVE == type.getFlavor()) {
0787: typeName = type.getJavaTypeName();
0788: } else {
0789: typeName = type.getName() == null ? element.getName()
0790: .getLocalPart() : type.getName().getLocalPart();
0791: }
0792: String name = typeName + "ArrayItem";
0793: File outputDirectoryF = new File(configuration
0794: .getOutputDirectory());
0795: FileObject outputDirectoryFO = FileUtil
0796: .toFileObject(FileUtil
0797: .normalizeFile(outputDirectoryF));
0798: FileObject outputFileDirectoryFO = outputDirectoryFO
0799: .getFileObject(configuration.getPackageName()
0800: .replace('.', '/')); // NOI18N
0801: FileObject outputFile = outputFileDirectoryFO
0802: .getFileObject(name, "java");
0803: if (outputFile == null) {
0804: outputFile = outputFileDirectoryFO.createData(name,
0805: "java");
0806: }
0807:
0808: OutputFileFormatter off = new OutputFileFormatter(
0809: outputFile);
0810:
0811: if (configuration.getPackageName() != null
0812: && !""
0813: .equals(configuration.getPackageName()
0814: .trim())) {
0815: off.write("package " + configuration.getPackageName()
0816: + ";\n");
0817: }
0818: off.write("\n");
0819: off
0820: .write("import org.netbeans.microedition.databinding.IndexableDataSet;\n");
0821: off
0822: .write("import org.netbeans.microedition.databinding.DataBinder;\n");
0823: off
0824: .write("import org.netbeans.microedition.databinding.DataBindingException;\n");
0825: // off.write( "import org.netbeans.microedition.databinding.DataSource;\n" );
0826: off.write("\n");
0827: if (type.getParent() == null) {
0828: off.write("public class " + name
0829: + " implements IndexableDataSet {\n");
0830: } else {
0831: Type parentType = definition.getSchemaHolder()
0832: .getSchemaType(type.getParent().getName());
0833: String parentName = parentType.getName().getLocalPart();
0834: off.write("public class " + name + " extends "
0835: + parentName
0836: + " implements IndexableDataSet {\n");
0837: }
0838: off.write("\n");
0839:
0840: // field representing the array
0841: off.write("private " + typeName + "[] values = new "
0842: + typeName + "[] {};\n");
0843: off.write("\n");
0844:
0845: // default constructor
0846: off.write("/**\n");
0847: off.write(" * Public default constructor for class " + name
0848: + "\n");
0849: off.write(" */\n");
0850: off.write("public " + name + "() {\n");
0851: off.write("}\n");
0852: off.write("\n");
0853:
0854: // constructor with parameter
0855: off.write("public " + name + "( " + typeName
0856: + "[] values ) {\n");
0857: off.write("this.values = values;\n");
0858: off.write("}\n");
0859: off.write("\n");
0860:
0861: // isReadOnly
0862: off.write("public boolean isReadOnly() {\n");
0863: off.write("return false;\n");
0864: off.write("}\n");
0865: off.write("\n");
0866:
0867: // getSize
0868: off.write("public int getSize() {\n");
0869: off.write("return values.length;\n");
0870: off.write("}\n");
0871: off.write("\n");
0872:
0873: // getRow
0874: off
0875: .write("public Object getRow(int index) throws DataBindingException {\n");
0876: off.write("if(index >=0 && index < values.length) {\n");
0877: if (type.getFlavor() == Type.FLAVOR_PRIMITIVE) {
0878: off.write("return "
0879: + wrapPrimitiveType(type, "values[index]")
0880: + ";\n");
0881: } else {
0882: off.write("return values[index];\n");
0883: }
0884: off.write("}\n");
0885: off
0886: .write("throw new DataBindingException(\"Index is out of range.\");\n");
0887: off.write("}\n");
0888: off.write("\n");
0889:
0890: // setRow
0891: off
0892: .write("public void setRow( int index, Object value ) throws DataBindingException {\n");
0893: off.write("if( index >=0 && index < values.length ) {\n");
0894: if (type.getFlavor() == Type.FLAVOR_PRIMITIVE) {
0895: if (element == null) {
0896: off.write("values[index] = "
0897: + unwrapPrimitiveType(type, "value")
0898: + ";\n");
0899: } else {
0900: off.write("values[index] = "
0901: + unwrapPrimitiveType(element, "value")
0902: + ";\n");
0903: }
0904: } else {
0905: off
0906: .write("values[index] = (" + typeName
0907: + ") value;\n");
0908: }
0909: off
0910: .write("DataBinder.fireDataSetChanged( this, new Integer( index ));\n");
0911: off.write("}\n");
0912: off
0913: .write("throw new DataBindingException(\"Index is out of range.\");\n");
0914: off.write("}\n");
0915: off.write("\n");
0916:
0917: // insertRow
0918: off
0919: .write("public void insertRow(int index, Object value) throws DataBindingException {\n");
0920: off.write("if( index >= 0 && index <= values.length ) {\n");
0921: off.write(typeName + " _newArray[] = new " + typeName
0922: + "[values.length + 1];\n");
0923: off
0924: .write("if( index > 1 ) System.arraycopy(values, 0, _newArray, 0, index - 1 );\n");
0925: off
0926: .write("if( index < values.length ) System.arraycopy(values, index, _newArray, index + 1, values.length - index );\n");
0927: off.write("_newArray[index] = (" + typeName + ")value;\n");
0928: off.write("values = _newArray;\n");
0929: off
0930: .write("DataBinder.fireDataSetChanged( this, new Integer( index ));");
0931: off.write("}\n");
0932: off.write("}\n");
0933: off.write("\n");
0934:
0935: // deleteRow
0936: off
0937: .write("public void deleteRow(int index) throws DataBindingException {\n");
0938: off.write("if( index >= 0 && index < values.length ) {\n");
0939: off.write(typeName + " _newArray[] = new " + typeName
0940: + "[values.length - 1];\n");
0941: off
0942: .write("if(index > 0) System.arraycopy(values, 0, _newArray, 0, index - 1);\n");
0943: off
0944: .write("if(index == 0) System.arraycopy(values, index + 1, _newArray, index, values.length - index - 1 );\n");
0945: off.write("}\n");
0946: off
0947: .write("DataBinder.fireDataSetChanged( this, new Integer( index ));");
0948: off.write("}\n");
0949: off.write("\n");
0950:
0951: // getType
0952: off
0953: .write("public Class getType( String arg0 ) throws DataBindingException {\n");
0954: off.write("return " + typeName + ".class;\n");
0955: off.write("}\n");
0956: off.write("\n");
0957:
0958: // getValue
0959: off
0960: .write("public Object getValue(String attribute) throws DataBindingException {\n");
0961: off.write("if( \"length\".equals( attribute )) {\n");
0962: off.write("return new Integer( values.length );\n");
0963: off.write("}\n");
0964: off
0965: .write("throw new DataBindingException( \"Invalid attribute name.\" );\n");
0966: off.write("}\n");
0967: off.write("\n");
0968:
0969: // setValue
0970: off
0971: .write("public void setValue(String attribute, Object value) throws DataBindingException {\n");
0972: off
0973: .write("throw new DataBindingException(\"Invalid attribute name.\");\n");
0974: off.write("}\n");
0975: off.write("\n");
0976:
0977: // setAsString
0978: off
0979: .write("public void setAsString(String attribute, String value) throws DataBindingException {\n");
0980: off
0981: .write("throw new DataBindingException(\"Invalid attribute name.\");\n");
0982: off.write("}\n");
0983: off.write("\n");
0984:
0985: // isReadOnly
0986: off
0987: .write("public boolean isReadOnly(String attribute) throws DataBindingException {\n");
0988: off.write("return true;\n");
0989: off.write("}\n");
0990:
0991: // getArray
0992: off.write(typeName + "[] getArray() {\n");
0993: off.write("return values;\n");
0994: off.write("}\n");
0995: off.write("\n");
0996:
0997: // setArray
0998: off.write("void setArray(" + typeName + " values[] ) {\n");
0999: off.write("this.values = values;\n");
1000: off.write("}\n");
1001:
1002: off.write("}\n");
1003: off.close();
1004: }
1005: }
1006:
1007: private void generateStub() throws Exception {
1008: usedParameterTypes = new HashSet<QName>();
1009: Set<QName> operationQNames = new HashSet<QName>();
1010:
1011: for (String serviceName : definition.getServices().keySet()) {
1012:
1013: Service service = definition.getService(serviceName);
1014: for (Port port : service.getPorts()) {
1015: for (ExtensibilityElement ee : port
1016: .getExtensibilityElements()) {
1017: if (SOAPConstants.ADDRESS.equals(ee
1018: .getElementType())) {
1019:
1020: SOAPAddress soapAddress = (SOAPAddress) ee;
1021:
1022: File outputDirectoryF = new File(configuration
1023: .getOutputDirectory());
1024: FileObject outputDirectoryFO = FileUtil
1025: .toFileObject(FileUtil
1026: .normalizeFile(outputDirectoryF));
1027: FileObject outputFileDirectoryFO = outputDirectoryFO
1028: .getFileObject(configuration
1029: .getPackageName().replace('.',
1030: '/')); // NOI18N
1031: FileObject outputFile = outputFileDirectoryFO
1032: .getFileObject(serviceName + "_Stub",
1033: "java");
1034: if (outputFile == null) {
1035: outputFile = outputFileDirectoryFO
1036: .createData(serviceName + "_Stub",
1037: "java");
1038: }
1039:
1040: OutputFileFormatter off = new OutputFileFormatter(
1041: outputFile);
1042:
1043: if (configuration.getPackageName() != null
1044: && !"".equals(configuration
1045: .getPackageName().trim())) {
1046: off.write("package "
1047: + configuration.getPackageName()
1048: + ";\n");
1049: }
1050: off.write("\n");
1051: off
1052: .write("import javax.xml.rpc.JAXRPCException;\n");
1053: off
1054: .write("import javax.xml.namespace.QName;\n");
1055: off
1056: .write("import javax.microedition.xml.rpc.Operation;\n");
1057: off
1058: .write("import javax.microedition.xml.rpc.Type;\n");
1059: off
1060: .write("import javax.microedition.xml.rpc.ComplexType;\n");
1061: off
1062: .write("import javax.microedition.xml.rpc.Element;\n\n");
1063:
1064: off.write("public class " + serviceName
1065: + "_Stub implements " + serviceName
1066: + ", javax.xml.rpc.Stub {\n");
1067:
1068: off.write("\n");
1069: off.write("private String[] _propertyNames;\n");
1070: off
1071: .write("private Object[] _propertyValues;\n");
1072: off.write("\n");
1073: off.write("public " + serviceName
1074: + "_Stub() {\n");
1075: off
1076: .write("_propertyNames = new String[] { ENDPOINT_ADDRESS_PROPERTY };\n");
1077: off.write("_propertyValues = new Object[] { \""
1078: + soapAddress.getLocationURI()
1079: + "\" };\n");
1080: off.write("}\n\n");
1081:
1082: off
1083: .write("public void _setProperty( String name, Object value ) {\n");
1084: off
1085: .write("int size = _propertyNames.length;\n");
1086: off.write("for (int i = 0; i < size; ++i) {\n");
1087: off
1088: .write("if( _propertyNames[i].equals( name )) {\n");
1089: off.write("_propertyValues[i] = value;\n");
1090: off.write("return;\n");
1091: off.write("}\n");
1092: off.write("}\n");
1093:
1094: off
1095: .write("String[] newPropNames = new String[size + 1];\n");
1096: off
1097: .write("System.arraycopy(_propertyNames, 0, newPropNames, 0, size);\n");
1098: off.write("_propertyNames = newPropNames;\n");
1099: off
1100: .write("Object[] newPropValues = new Object[size + 1];\n");
1101: off
1102: .write("System.arraycopy(_propertyValues, 0, newPropValues, 0, size);\n");
1103: off.write("_propertyValues = newPropValues;\n");
1104: off.write("\n");
1105: off.write("_propertyNames[size] = name;\n");
1106: off.write("_propertyValues[size] = value;\n");
1107: off.write("}\n\n");
1108:
1109: off
1110: .write("public Object _getProperty(String name) {\n");
1111: off
1112: .write("for (int i = 0; i < _propertyNames.length; ++i) {\n");
1113: off
1114: .write("if (_propertyNames[i].equals(name)) {\n");
1115: off.write("return _propertyValues[i];\n");
1116: off.write("}\n");
1117: off.write("}\n");
1118: off
1119: .write("if (ENDPOINT_ADDRESS_PROPERTY.equals(name) || USERNAME_PROPERTY.equals(name) || PASSWORD_PROPERTY.equals(name)) {\n");
1120: off.write("return null;\n");
1121: off.write("}\n");
1122: off
1123: .write("if (SESSION_MAINTAIN_PROPERTY.equals(name)) {\n");
1124: off.write("return new Boolean(false);\n");
1125: off.write("}\n");
1126: off
1127: .write("throw new JAXRPCException(\"Stub does not recognize property: \" + name);\n");
1128: off.write("}\n\n");
1129:
1130: off
1131: .write("protected void _prepOperation(Operation op) {\n");
1132: off
1133: .write("for (int i = 0; i < _propertyNames.length; ++i) {\n");
1134: off
1135: .write("op.setProperty(_propertyNames[i], _propertyValues[i].toString());\n");
1136: off.write("}\n");
1137: off.write("}\n");
1138:
1139: Binding binding = port.getBinding();
1140: PortType portType = binding.getPortType();
1141: String returnTypeName = "", paramTypeName = "";
1142:
1143: Set<Element> fromObjects = new HashSet<Element>();
1144: Set<Element> toObjects = new HashSet<Element>();
1145:
1146: for (Operation operation : portType
1147: .getOperations()) {
1148: BindingOperation bindingOperation = binding
1149: .getBindingOperation(operation
1150: .getName());
1151:
1152: operationQNames.add(new QName(definition
1153: .getTargetNamespace(), operation
1154: .getName()));
1155:
1156: String messageName = operation.getOutput()
1157: .getMessage().getName();
1158: Message message = definition
1159: .getMessage(messageName);
1160: for (Part part : message.getParts()) {
1161: Element element = definition
1162: .getSchemaHolder()
1163: .getSchemaElement(
1164: part.getElementName());
1165: returnTypeName = element.getName()
1166: .getLocalPart();
1167: Element e = getReturnElement(element);
1168: String javaTypeName = getJavaTypeName(e);
1169:
1170: off.write("\n");
1171: off.write("public " + javaTypeName
1172: + " ");
1173: usedParameterTypes.add(element
1174: .getName());
1175: break;
1176: }
1177:
1178: off.write(operation.getName() + "(");
1179:
1180: Input input = operation.getInput();
1181: if (input != null) {
1182: for (Part part : input.getMessage()
1183: .getParts()) {
1184: Element element = definition
1185: .getSchemaHolder()
1186: .getSchemaElement(
1187: part
1188: .getElementName());
1189: paramTypeName = element.getName()
1190: .getLocalPart();
1191: usedParameterTypes.add(element
1192: .getName());
1193: List<Element> params = getParameterElements(element);
1194: for (Iterator<Element> it = params
1195: .iterator(); it.hasNext();) {
1196: Element e = it.next();
1197: Type type = e.getType();
1198: String javaTypeName = getJavaTypeName(e);
1199: off
1200: .write(javaTypeName
1201: + " "
1202: + e
1203: .getName()
1204: .getLocalPart());
1205: if (it.hasNext())
1206: off.write(", ");
1207: }
1208: }
1209: }
1210:
1211: off
1212: .write(") throws java.rmi.RemoteException {\n");
1213: // Wrap to Object[] array
1214: for (Iterator<Part> it = input.getMessage()
1215: .getParts().iterator(); it
1216: .hasNext();) {
1217: Part part = it.next();
1218: Element e = definition
1219: .getSchemaHolder()
1220: .getSchemaElement(
1221: part.getElementName());
1222:
1223: Type type = e.getType();
1224: if (Type.FLAVOR_PRIMITIVE == type
1225: .getFlavor()) {
1226: if (e.isNillable()
1227: || e.getMinOccurs() == 0) {
1228: off
1229: .write("Object inputObject = "
1230: + e
1231: .getName()
1232: .getLocalPart()
1233: + ";\n");
1234: } else {
1235: off
1236: .write("Object inputObject = "
1237: + wrapPrimitiveType(
1238: type,
1239: e
1240: .getName()
1241: .getLocalPart())
1242: + ";\n");
1243: }
1244: } else if (Type.FLAVOR_SEQUENCE == type
1245: .getFlavor()) {
1246: off
1247: .write("Object inputObject[] = new Object[] {\n");
1248: for (Iterator<SchemaConstruct> scit = type
1249: .getSubconstructs()
1250: .iterator(); scit.hasNext();) {
1251: SchemaConstruct sc = scit
1252: .next();
1253: if (SchemaConstruct.ConstructType.ELEMENT
1254: .equals(sc
1255: .getConstructType())) {
1256: Element sce = (Element) sc;
1257: boolean isArray = sce
1258: .getMaxOccurs() > 1;
1259: Type t = sce.getType();
1260: if (Type.FLAVOR_PRIMITIVE == t
1261: .getFlavor()) {
1262: if (!isArray) {
1263: if (sce
1264: .isNillable()
1265: || sce
1266: .getMinOccurs() == 0) {
1267: // Wrapper non array
1268: off
1269: .write(sce
1270: .getName()
1271: .getLocalPart());
1272: } else {
1273: // Primitive non array
1274: off
1275: .write(wrapPrimitiveType(
1276: t,
1277: sce
1278: .getName()
1279: .getLocalPart()));
1280: }
1281: } else {
1282: if (sce
1283: .isNillable()
1284: || sce
1285: .getMinOccurs() == 0) {
1286: off
1287: .write(sce
1288: .getName()
1289: .getLocalPart());
1290: } else {
1291: off
1292: .write(wrapPrimitiveType(
1293: t,
1294: sce
1295: .getName()
1296: .getLocalPart()));
1297: }
1298: }
1299: } else if (Type.FLAVOR_SEQUENCE == t
1300: .getFlavor()) {
1301: String typeName = sce
1302: .getType()
1303: .getJavaName();
1304: if (typeName == null)
1305: typeName = sce
1306: .getType()
1307: .getName() == null ? sce
1308: .getName()
1309: .getLocalPart()
1310: : sce
1311: .getType()
1312: .getName()
1313: .getLocalPart();
1314: off
1315: .write(typeName
1316: + "_"
1317: + (isArray ? "Array"
1318: : "")
1319: + "toObject( "
1320: + sce
1321: .getName()
1322: .getLocalPart()
1323: + " )");
1324: toObjects.add(sce);
1325: }
1326: if (scit.hasNext())
1327: off.write(", ");
1328: off.write("\n");
1329: }
1330: }
1331: off.write("};\n");
1332: }
1333: }
1334: off.write("\n");
1335: off
1336: .write("Operation op = Operation.newInstance( _qname_operation_"
1337: + operation.getName()
1338: + ", _type_"
1339: + paramTypeName
1340: + ", _type_"
1341: + returnTypeName + " );\n");
1342: off.write("_prepOperation( op );\n");
1343: for (ExtensibilityElement exe : bindingOperation
1344: .getExtensibilityElements()) {
1345: if (exe instanceof SOAPOperation) {
1346: SOAPOperation so = (SOAPOperation) exe;
1347: off
1348: .write("op.setProperty( Operation.SOAPACTION_URI_PROPERTY, \"");
1349: off.write(so.getSoapActionURI());
1350: off.write("\" );\n");
1351: }
1352: }
1353: off.write("Object resultObj;\n");
1354: off.write("try {\n");
1355: off
1356: .write("resultObj = op.invoke( inputObject );\n");
1357: off
1358: .write("} catch( JAXRPCException e ) {\n");
1359: off
1360: .write("Throwable cause = e.getLinkedCause();\n");
1361: off
1362: .write("if( cause instanceof java.rmi.RemoteException ) {\n");
1363: off
1364: .write("throw (java.rmi.RemoteException) cause;\n");
1365: off.write("}\n");
1366: off.write("throw e;\n");
1367: off.write("}\n");
1368: off.write("\n");
1369:
1370: Output output = operation.getOutput();
1371: if (output != null) {
1372: message = definition.getMessage(output
1373: .getMessage().getName());
1374: for (Part part : message.getParts()) {
1375: Element e = getReturnElement(definition
1376: .getSchemaHolder()
1377: .getSchemaElement(
1378: part
1379: .getElementName()));
1380: Type type = e.getType();
1381: boolean isArray = e.getMaxOccurs() > 1;
1382: if (Type.FLAVOR_PRIMITIVE == type
1383: .getFlavor()) {
1384: if (!isArray) {
1385: off
1386: .write("return "
1387: + unwrapPrimitiveType(
1388: e,
1389: "((Object[])resultObj)[0]")
1390: + ";\n");
1391: } else {
1392: if (!e.isNillable()) {
1393: off
1394: .write("return "
1395: + type
1396: .getJavaTypeName()
1397: .replace(
1398: '.',
1399: '_')
1400: + "_ArrayfromObject((Object []) resultObj);\n");
1401: } else {
1402: // Handle string object properly
1403: if (SchemaConstants.TYPE_STRING
1404: .equals(type
1405: .getName())) {
1406: off
1407: .write("String[] _res = new String[((Object [])((Object []) resultObj)[0]).length];\n");
1408: off
1409: .write("System.arraycopy(((Object []) resultObj)[0], 0, _res, 0, _res.length);\n");
1410: off
1411: .write("return _res;\n");
1412: } else {
1413: off
1414: .write("return "
1415: + unwrapPrimitiveType(
1416: e,
1417: "((Object[])resultObj)[0]")
1418: + ";\n");
1419: }
1420: }
1421: //fromObjects.add( e );
1422: }
1423: } else if (Type.FLAVOR_SEQUENCE == type
1424: .getFlavor()) {
1425: if (type.getSubconstructs()
1426: .size() == 0) {
1427: // void
1428: } else {
1429: String typeName = e
1430: .getType()
1431: .getJavaTypeName();
1432: if (typeName == null)
1433: typeName = e.getType()
1434: .getName() == null ? e
1435: .getName()
1436: .getLocalPart()
1437: : e
1438: .getType()
1439: .getName()
1440: .getLocalPart();
1441: if (!isArray) {
1442: off
1443: .write("return "
1444: + typeName
1445: .replace(
1446: '.',
1447: '_')
1448: + "_fromObject((Object[])((Object[]) resultObj)[0]);\n");
1449: } else {
1450: off
1451: .write("return "
1452: + typeName
1453: .replace(
1454: '.',
1455: '_')
1456: + "_ArrayfromObject((Object[])((Object[]) resultObj)[0]);\n");
1457: }
1458: fromObjects.add(e);
1459: }
1460: }
1461: break;
1462: }
1463:
1464: }
1465: off.write("}\n");
1466: }
1467:
1468: // Generate object methods
1469: // Traversing
1470: Set<SchemaConstruct> to = new HashSet<SchemaConstruct>();
1471: for (Element e : toObjects) {
1472: to.addAll(traverseObjectElements(e, to));
1473: }
1474: Set<SchemaConstruct> from = new HashSet<SchemaConstruct>();
1475: for (Element e : fromObjects) {
1476: from
1477: .addAll(traverseObjectElements(e,
1478: from));
1479: }
1480: // toObject methods
1481: Set<String> usedToMethods = new HashSet<String>();
1482: for (SchemaConstruct sc : to) {
1483: String typeName = sc.getName()
1484: .getLocalPart();
1485: boolean isA = false;
1486: if (SchemaConstruct.ConstructType.ELEMENT
1487: .equals(sc.getConstructType())) {
1488: Element e = (Element) sc;
1489: if (e.getMaxOccurs() > 1)
1490: isA = true;
1491: if (e.getType().getName() != null)
1492: typeName = e.getType().getName()
1493: .getLocalPart();
1494: }
1495: String methodName = typeName + "_"
1496: + (isA ? "Array" : "") + "toObject";
1497: if (usedToMethods.contains(methodName))
1498: continue;
1499: else
1500: usedToMethods.add(methodName);
1501: off.write("\n");
1502: off.write("private static Object "
1503: + methodName + "( " + typeName
1504: + (isA ? "[]" : "") + " obj ) {\n");
1505: off.write("if(obj == null) return null;\n");
1506: Type type = null;
1507: if (SchemaConstruct.ConstructType.ELEMENT
1508: .equals(sc.getConstructType())) {
1509: Element e = (Element) sc;
1510: type = e.getType();
1511: } else if (SchemaConstruct.ConstructType.TYPE
1512: .equals(sc.getConstructType())) {
1513: type = (Type) sc;
1514: } else {
1515: throw new InvalidParameterException(
1516: "Invalid SchemaConstruct type");
1517: }
1518: String resultVariableName = (isA ? "resultArray"
1519: : "result");
1520: if (isA) {
1521: off
1522: .write("Object result[] = new Object[ obj.length ];\n");
1523: off
1524: .write("for( int i = 0; i < obj.length; i++ ) {\n");
1525: off.write("Object[] "
1526: + resultVariableName
1527: + " = new Object[ "
1528: + type.getSubconstructs()
1529: .size() + " ];\n");
1530: } else {
1531: off
1532: .write("Object result[] = new Object[ "
1533: + type
1534: .getSubconstructs()
1535: .size()
1536: + " ];\n");
1537: }
1538: int i = 0;
1539: for (Element sce : getElements(type)) {
1540: Type t = sce.getType();
1541: String variableName = sce.getName()
1542: .getLocalPart();
1543: boolean isArray = sce.getMaxOccurs() > 1;
1544: off.write(resultVariableName + "[" + i
1545: + "] = ");
1546: if (Type.FLAVOR_PRIMITIVE == t
1547: .getFlavor()) {
1548: if (!isArray) {
1549: if (sce.isNillable()
1550: || (sce.getMinOccurs() == 0 && sce
1551: .getMaxOccurs() == 1)) {
1552: off
1553: .write("obj"
1554: + (isA ? "[i]"
1555: : "")
1556: + "."
1557: + getter(variableName)
1558: + "()");
1559: } else {
1560: off
1561: .write(wrapPrimitiveType(
1562: t,
1563: "obj"
1564: + (isA ? "[i]"
1565: : "")
1566: + "."
1567: + getter(variableName)
1568: + "()"));
1569: }
1570: } else {
1571: if (sce.isNillable()) {
1572: off
1573: .write("obj"
1574: + (isA ? "[i]"
1575: : "")
1576: + "."
1577: + getter(variableName)
1578: + "()");
1579: } else {
1580: off.write("???");
1581: }
1582: }
1583: } else if (Type.FLAVOR_SEQUENCE == t
1584: .getFlavor()) {
1585: String tn = t.getName() == null ? sce
1586: .getName().getLocalPart()
1587: : t.getName()
1588: .getLocalPart();
1589: if (!isArray) {
1590: off.write(tn + "_toObject( obj"
1591: + (isA ? "[i]" : "")
1592: + "."
1593: + getter(variableName)
1594: + "())");
1595: } else {
1596: off.write(tn
1597: + "_ArraytoObject( obj"
1598: + (isA ? "[i]" : "")
1599: + "."
1600: + getter(variableName)
1601: + "())");
1602: }
1603: }
1604: off.write(";\n");
1605: i++;
1606: }
1607: if (isA) {
1608: off.write("result[i] = "
1609: + resultVariableName + ";\n");
1610: off.write("}\n");
1611: }
1612: off.write("return result;\n");
1613: off.write("}\n");
1614: }
1615: // fromObject methods
1616: Set<String> usedFromMethods = new HashSet<String>();
1617: for (SchemaConstruct sc : from) {
1618: String typeName = sc.getName()
1619: .getLocalPart();
1620: boolean isA = false;
1621: if (SchemaConstruct.ConstructType.ELEMENT
1622: .equals(sc.getConstructType())) {
1623: Element e = (Element) sc;
1624: if (e.getMaxOccurs() > 1)
1625: isA = true;
1626: typeName = e.getType()
1627: .getJavaTypeName();
1628: if (typeName == null
1629: && e.getType().getName() != null)
1630: typeName = e.getType().getName()
1631: .getLocalPart();
1632: }
1633: String methodName = typeName.replace('.',
1634: '_')
1635: + "_"
1636: + (isA ? "Array" : "")
1637: + "fromObject";
1638: if (usedFromMethods.contains(methodName))
1639: continue;
1640: else
1641: usedFromMethods.add(methodName);
1642: off.write("\n");
1643: off.write("private static " + typeName
1644: + (isA ? "[]" : "") + " "
1645: + methodName
1646: + "( Object obj[] ) {\n");
1647: off.write("if(obj == null) return null;\n");
1648: off.write(typeName + " result"
1649: + (isA ? "[]" : "") + " = new "
1650: + typeName
1651: + (isA ? "[obj.length]" : "()")
1652: + ";\n");
1653: String objectVariableName = (isA ? "oo"
1654: : "obj");
1655: if (isA) {
1656: off
1657: .write("for( int i = 0; i < obj.length; i++ ) {\n");
1658: off.write("result[i] = new " + typeName
1659: + "();\n");
1660: off.write("Object[] "
1661: + objectVariableName
1662: + " = (Object[]) obj[i];\n");
1663: }
1664: Type type = null;
1665: if (SchemaConstruct.ConstructType.ELEMENT
1666: .equals(sc.getConstructType())) {
1667: Element e = (Element) sc;
1668: type = e.getType();
1669: } else if (SchemaConstruct.ConstructType.TYPE
1670: .equals(sc.getConstructType())) {
1671: type = (Type) sc;
1672: } else {
1673: throw new InvalidParameterException(
1674: "Invalid SchemaConstruct type");
1675: }
1676: int i = 0;
1677: for (Element sce : getElements(type)) {
1678: Type t = sce.getType();
1679: String variableName = sce.getName()
1680: .getLocalPart();
1681: boolean isArray = sce.getMaxOccurs() > 1;
1682: if (Type.FLAVOR_PRIMITIVE == t
1683: .getFlavor()) {
1684: off.write("result"
1685: + (isA ? "[i]" : "")
1686: + "."
1687: + setter(variableName)
1688: + "("
1689: + unwrapPrimitiveType(sce,
1690: objectVariableName
1691: + "[" + i
1692: + "]")
1693: + ");\n");
1694: } else if (Type.FLAVOR_SEQUENCE == t
1695: .getFlavor()) {
1696: String tn = t.getName() == null ? sce
1697: .getName().getLocalPart()
1698: : t.getName()
1699: .getLocalPart();
1700: if (!isArray) {
1701: off
1702: .write("result"
1703: + (isA ? "[i]"
1704: : "")
1705: + "."
1706: + setter(variableName)
1707: + "("
1708: + tn
1709: + "_fromObject((Object[]) "
1710: + objectVariableName
1711: + "[" + i
1712: + "] ));\n");
1713: } else {
1714: off
1715: .write("result"
1716: + (isA ? "[i]"
1717: : "")
1718: + "."
1719: + setter(variableName)
1720: + "("
1721: + tn
1722: + "_ArrayfromObject((Object[]) "
1723: + objectVariableName
1724: + "[" + i
1725: + "] ));\n");
1726: }
1727: }
1728: i++;
1729: }
1730: if (isA) {
1731: off.write("}\n");
1732: }
1733: off.write("return result;\n");
1734: off.write("}\n");
1735: }
1736:
1737: // Collect Qnames and Elements
1738: Set<QName> qnames = new HashSet<QName>();
1739: Set<Element> elements = new HashSet<Element>();
1740: for (QName parameterName : usedParameterTypes) {
1741: SchemaConstruct sc;
1742: sc = definition.getSchemaHolder()
1743: .getSchemaElement(parameterName);
1744: if (sc == null)
1745: continue;
1746: traverseParameterTypes(sc, qnames, elements);
1747: }
1748: off.write("\n");
1749: for (QName q : operationQNames) {
1750: off.write("protected static final QName ");
1751: off.write("_qname_operation_"
1752: + q.getLocalPart()
1753: + " = new QName( ");
1754: off.write('"' + q.getNamespaceURI()
1755: + "\", \"" + q.getLocalPart()
1756: + "\" );\n");
1757: }
1758: for (QName q : qnames) {
1759: off.write("protected static final QName ");
1760: off.write("_qname_" + q.getLocalPart()
1761: + " = new QName( ");
1762: off.write('"' + q.getNamespaceURI()
1763: + "\", \"" + q.getLocalPart()
1764: + "\" );\n");
1765: }
1766:
1767: // _type_ static declaration
1768: for (Element e : elements) {
1769: off
1770: .write("protected static final Element _type_"
1771: + e.getName()
1772: .getLocalPart()
1773: + ";\n");
1774: }
1775: off.write("\nstatic {\n");
1776:
1777: // static _type initialization
1778: for (QName parameterName : usedParameterTypes) {
1779: SchemaConstruct sc;
1780: sc = definition.getSchemaHolder()
1781: .getSchemaElement(parameterName);
1782: off.write("_type_"
1783: + sc.getName().getLocalPart()
1784: + " = ");
1785: initTypes(off, sc, qnames, elements);
1786: off.write(";\n");
1787: }
1788: off.write("}\n\n");
1789:
1790: // Static wrapper for complex types
1791: off
1792: .write("private static ComplexType _complexType( Element[] elements ) {\n");
1793: off
1794: .write("ComplexType result = new ComplexType();\n");
1795: off.write("result.elements = elements;\n");
1796: off.write("return result;\n");
1797: off.write("}\n");
1798: off.write("}\n");
1799:
1800: off.close();
1801: }
1802: }
1803: }
1804: }
1805: }
1806:
1807: public List<Element> getElements(Type type) {
1808: List<Element> elements = new ArrayList<Element>();
1809: if (type.getParent() != null) {
1810: elements.addAll(getElements((Type) type.getParent()));
1811: }
1812: for (SchemaConstruct sc : type.getSubconstructs()) {
1813: if (SchemaConstruct.ConstructType.ELEMENT == sc
1814: .getConstructType()) {
1815: Element e = (Element) sc;
1816: elements.add(e);
1817: }
1818: }
1819: return elements;
1820: }
1821:
1822: private Set<SchemaConstruct> traverseObjectElements(
1823: SchemaConstruct schemaConstruct,
1824: Set<SchemaConstruct> elements) {
1825: Set<SchemaConstruct> result = new HashSet<SchemaConstruct>();
1826: if (result.contains(schemaConstruct)) {
1827: return Collections.emptySet();
1828: } else {
1829: elements.add(schemaConstruct);
1830: }
1831: Type type = null;
1832: Element element = null;
1833: if (SchemaConstruct.ConstructType.ELEMENT
1834: .equals(schemaConstruct.getConstructType())) {
1835: element = (Element) schemaConstruct;
1836: type = element.getType();
1837: } else if (SchemaConstruct.ConstructType.TYPE
1838: .equals(schemaConstruct.getConstructType())) {
1839: type = (Type) schemaConstruct;
1840: } else {
1841: throw new InvalidParameterException(
1842: "Invalid SchemaConstruct type");
1843: }
1844: if (Type.FLAVOR_PRIMITIVE == type.getFlavor()) {
1845: if (element != null) {
1846: // Is array
1847: if (element.getMaxOccurs() > 1) {
1848: result.add(element);
1849: return result;
1850: }
1851: }
1852: } else if (Type.FLAVOR_SEQUENCE == type.getFlavor()) {
1853: for (SchemaConstruct sc : type.getSubconstructs()) {
1854: if (SchemaConstruct.ConstructType.ELEMENT.equals(sc
1855: .getConstructType())) {
1856: Element sce = (Element) sc;
1857: Type scetype = sce.getType();
1858: if (Type.FLAVOR_SEQUENCE == scetype.getFlavor()) {
1859: elements.add(sce);
1860: result.addAll(traverseObjectElements(sce,
1861: elements));
1862: }
1863: }
1864: }
1865: }
1866: return result;
1867: }
1868:
1869: private String wrapPrimitiveType(Type type, String value) {
1870: QName typeName = type.getName();
1871: if (SchemaConstants.TYPE_INT.equals(typeName)) {
1872: return "new Integer(" + value + ")";
1873: } else if (SchemaConstants.TYPE_BOOLEAN.equals(typeName)) {
1874: return "new Boolean(" + value + ")";
1875: } else if (SchemaConstants.TYPE_BYTE.equals(typeName)) {
1876: return "new Byte(" + value + ")";
1877: } else if (SchemaConstants.TYPE_DOUBLE.equals(typeName)) {
1878: return "new Double(" + value + ")";
1879: } else if (SchemaConstants.TYPE_FLOAT.equals(typeName)) {
1880: return "new Float(" + value + ")";
1881: } else if (SchemaConstants.TYPE_LONG.equals(typeName)) {
1882: return "new Long(" + value + ")";
1883: } else if (SchemaConstants.TYPE_SHORT.equals(typeName)) {
1884: return "new Short(" + value + ")";
1885: }
1886:
1887: return value;
1888: }
1889:
1890: private String unwrapPrimitiveType(Type type, String value) {
1891: QName typeName = type.getName();
1892: if (SchemaConstants.TYPE_INT.equals(typeName)) {
1893: return "((Integer)" + value + ").intValue()";
1894: } else if (SchemaConstants.TYPE_BOOLEAN.equals(typeName)) {
1895: return "((Boolean)" + value + ").booleanValue()";
1896: } else if (SchemaConstants.TYPE_BYTE.equals(typeName)) {
1897: return "((Byte)" + value + ").byteValue()";
1898: } else if (SchemaConstants.TYPE_DOUBLE.equals(typeName)) {
1899: return "((Double)" + value + ").doubleValue()";
1900: } else if (SchemaConstants.TYPE_FLOAT.equals(typeName)) {
1901: return "((Float)" + value + ").floatValue()";
1902: } else if (SchemaConstants.TYPE_LONG.equals(typeName)) {
1903: return "((Long)" + value + ").longValue()";
1904: } else if (SchemaConstants.TYPE_SHORT.equals(typeName)) {
1905: return "((Short)" + value + ").shortValue()";
1906: } else if (SchemaConstants.TYPE_STRING.equals(typeName)) {
1907: return "(String)" + value;
1908: }
1909: return value;
1910: }
1911:
1912: private String unwrapPrimitiveType(Element element, String value) {
1913: Type type = element.getType();
1914: QName typeName = type.getName();
1915: String unwrapped = "";
1916: if (element.getMaxOccurs() == Type.UNBOUNDED) {
1917: unwrapped = "[]";
1918: }
1919: if (SchemaConstants.TYPE_INT.equals(typeName)) {
1920: unwrapped = "(Integer " + unwrapped + ")";
1921: } else if (SchemaConstants.TYPE_BOOLEAN.equals(typeName)) {
1922: unwrapped = "(Boolean " + unwrapped + ")";
1923: } else if (SchemaConstants.TYPE_BYTE.equals(typeName)) {
1924: unwrapped = "(Byte " + unwrapped + ")";
1925: } else if (SchemaConstants.TYPE_DOUBLE.equals(typeName)) {
1926: unwrapped = "(Double " + unwrapped + ")";
1927: } else if (SchemaConstants.TYPE_FLOAT.equals(typeName)) {
1928: unwrapped = "(Float " + unwrapped + ")";
1929: } else if (SchemaConstants.TYPE_LONG.equals(typeName)) {
1930: unwrapped = "(Long " + unwrapped + ")";
1931: } else if (SchemaConstants.TYPE_SHORT.equals(typeName)) {
1932: unwrapped = "(Short " + unwrapped + ")";
1933: } else {
1934: unwrapped = "(" + type.getJavaTypeName() + " " + unwrapped
1935: + ")";
1936: }
1937: unwrapped += value;
1938: if (element.getMinOccurs() > 0 && !element.isNillable()) {
1939: if (SchemaConstants.TYPE_INT.equals(typeName)) {
1940: unwrapped = "(" + unwrapped + ").intValue()";
1941: } else if (SchemaConstants.TYPE_BOOLEAN.equals(typeName)) {
1942: unwrapped = "(" + unwrapped + ").booleanValue()";
1943: } else if (SchemaConstants.TYPE_BYTE.equals(typeName)) {
1944: unwrapped = "(" + unwrapped + ").byteValue()";
1945: } else if (SchemaConstants.TYPE_DOUBLE.equals(typeName)) {
1946: unwrapped = "(" + unwrapped + ").doubleValue()";
1947: } else if (SchemaConstants.TYPE_FLOAT.equals(typeName)) {
1948: unwrapped = "(" + unwrapped + ").floatValue()";
1949: } else if (SchemaConstants.TYPE_LONG.equals(typeName)) {
1950: unwrapped = "(" + unwrapped + ").longValue()";
1951: } else if (SchemaConstants.TYPE_SHORT.equals(typeName)) {
1952: unwrapped = "(" + unwrapped + ").shortValue()";
1953: }
1954: }
1955:
1956: return unwrapped;
1957: }
1958:
1959: private String parsePrimitiveType(Element element, String value) {
1960: Type type = element.getType();
1961: QName typeName = type.getName();
1962: String parse = wrapPrimitiveType(type, value);
1963: if (element.getMinOccurs() > 0 && !element.isNillable()) {
1964: if (SchemaConstants.TYPE_INT.equals(typeName)) {
1965: parse = "Integer.parseInt(" + value + ")";
1966: } else if (SchemaConstants.TYPE_BOOLEAN.equals(typeName)) {
1967: parse = "value.equals( \"true\" ) ? true : false";
1968: } else if (SchemaConstants.TYPE_BYTE.equals(typeName)) {
1969: parse = "Byte.parseByte(" + value + ")";
1970: } else if (SchemaConstants.TYPE_DOUBLE.equals(typeName)) {
1971: parse = "Double.parseDouble(" + value + ")";
1972: } else if (SchemaConstants.TYPE_FLOAT.equals(typeName)) {
1973: parse = "Float.parseFloat(" + value + ")";
1974: } else if (SchemaConstants.TYPE_LONG.equals(typeName)) {
1975: parse = "Long.parseLong(" + value + ")";
1976: } else if (SchemaConstants.TYPE_SHORT.equals(typeName)) {
1977: parse = "Short.parseShort(" + value + ")";
1978: }
1979: }
1980:
1981: return parse;
1982: }
1983:
1984: private String getter(String variable) {
1985: return "get" + variable.substring(0, 1).toUpperCase()
1986: + variable.substring(1);
1987: }
1988:
1989: private String setter(String variable) {
1990: return "set" + variable.substring(0, 1).toUpperCase()
1991: + variable.substring(1);
1992: }
1993:
1994: private void initTypes(OutputFileFormatter off, SchemaConstruct sc,
1995: Set<QName> qnames, Set<Element> elements) {
1996: if (SchemaConstruct.ConstructType.ELEMENT.equals(sc
1997: .getConstructType())) {
1998: Element e = (Element) sc;
1999: Type t = e.getType();
2000:
2001: QName elementName = e.getName();
2002:
2003: if (Type.FLAVOR_SEQUENCE == e.getType().getFlavor()) {
2004: String ctName = elementName.getLocalPart();
2005:
2006: off.write("new Element( ");
2007: if (qnames.contains(e.getName())) {
2008: off.write("_qname_" + ctName);
2009: } else {
2010: off.write("new QName( \""
2011: + e.getName().getNamespaceURI() + "\", \""
2012: + e.getName().getLocalPart() + "\" )");
2013: }
2014: off.write(", _complexType( new Element[] {\n");
2015:
2016: for (Iterator<Element> scit = getElements(t).iterator(); scit
2017: .hasNext();) {
2018: // for( Iterator<SchemaConstruct> scit = t.getSubconstructs().iterator(); scit.hasNext(); ) {
2019: initTypes(off, scit.next(), qnames, elements);
2020: if (scit.hasNext())
2021: off.write(",\n");
2022: }
2023: if (e.getMinOccurs() != 0 || e.getMaxOccurs() != 1
2024: || e.isNillable()) {
2025: off
2026: .write("}), "
2027: + e.getMinOccurs()
2028: + ", "
2029: + (e.getMaxOccurs() == Type.UNBOUNDED ? "Element.UNBOUNDED"
2030: : e.getMaxOccurs()) + ", "
2031: + e.isNillable() + " )");
2032: } else {
2033: off.write("}))");
2034: }
2035: } else if (Type.FLAVOR_PRIMITIVE == e.getType().getFlavor()) {
2036: String eName = e.getName().getLocalPart();
2037: off.write("new Element( ");
2038: if (qnames.contains(e.getName())) {
2039: off.write("_qname_" + eName + ", ");
2040: } else {
2041: off.write("new QName( \""
2042: + e.getName().getNamespaceURI() + "\", \""
2043: + e.getName().getLocalPart() + "\" ), ");
2044: }
2045: int minOccurs = e.getMinOccurs();
2046: int maxOccurs = e.getMaxOccurs();
2047: if (SchemaConstants.TYPE_STRING.equals(t.getName())) {
2048: off.write("Type.STRING");
2049: } else if (SchemaConstants.TYPE_INT.equals(t.getName())) {
2050: off.write("Type.INT");
2051: } else if (SchemaConstants.TYPE_BOOLEAN.equals(t
2052: .getName())) {
2053: off.write("Type.BOOLEAN");
2054: } else if (SchemaConstants.TYPE_SHORT.equals(t
2055: .getName())) {
2056: off.write("Type.SHORT");
2057: } else if (SchemaConstants.TYPE_BYTE
2058: .equals(t.getName())) {
2059: off.write("Type.BYTE");
2060: } else if (SchemaConstants.TYPE_LONG
2061: .equals(t.getName())) {
2062: off.write("Type.LONG");
2063: } else if (SchemaConstants.TYPE_FLOAT.equals(t
2064: .getName())) {
2065: off.write("Type.FLOAT");
2066: } else if (SchemaConstants.TYPE_DOUBLE.equals(t
2067: .getName())) {
2068: off.write("Type.DOUBLE");
2069: } else if (SchemaConstants.TYPE_BASE64_BINARY.equals(t
2070: .getName())) {
2071: off.write("Type.BYTE");
2072: maxOccurs = Element.UNBOUNDED;
2073: } else if (SchemaConstants.TYPE_HEX_BINARY.equals(t
2074: .getName())) {
2075: off.write("Type.BYTE");
2076: maxOccurs = Element.UNBOUNDED;
2077: } else if (SchemaConstants.TYPE_QNAME.equals(t
2078: .getName())) {
2079: off
2080: .write("new ComplexType() /* TODO: Must implement */");
2081: } else {
2082: // System.err.println("Error type");
2083: }
2084: if (minOccurs != 1 || maxOccurs != 1 || e.isNillable()) {
2085: off.write(", " + minOccurs + ", ");
2086: if (maxOccurs == Element.UNBOUNDED) {
2087: off.write("Element.UNBOUNDED");
2088: } else {
2089: off.write("" + maxOccurs);
2090: }
2091: off.write(", " + e.isNillable());
2092: }
2093: off.write(" )");
2094: }
2095: }
2096: }
2097:
2098: public void traverseParameterTypes(SchemaConstruct sc,
2099: Set<QName> qnames, Set<Element> elements) {
2100: if (SchemaConstruct.ConstructType.ELEMENT.equals(sc
2101: .getConstructType())) {
2102: Element e = (Element) sc;
2103: qnames.add(e.getName());
2104: elements.add(e);
2105: }
2106: }
2107:
2108: public String getUniqueTypeName(QName name) {
2109: Integer index = uniqueTypeName.get(name);
2110: if (index != null) {
2111: return name.getLocalPart() + "_" + index;
2112: }
2113: int max = 0;
2114: for (QName q : uniqueTypeName.keySet()) {
2115: if (q.getLocalPart().equals(name.getLocalPart())) {
2116: int i = uniqueTypeName.get(q);
2117: if (i > max)
2118: max = i;
2119: }
2120: }
2121: max++;
2122: uniqueTypeName.put(name, max);
2123: return name.getLocalPart() + "_" + max;
2124: }
2125: }
|