Search in sources :

Example 51 with XmlSchemaType

use of org.apache.ws.commons.schema.XmlSchemaType in project hale by halestudio.

the class XmlSchemaReader method loadSchema.

/**
 * Load the feature types defined by the given schema
 *
 * @param schemaLocation the schema location
 * @param xmlSchema the schema
 * @param imports the imports/includes that were already loaded or where
 *            loading has been started
 * @param progress the progress indicator
 * @param mainSchema states if this is a main schema and therefore elements
 *            declared here should be flagged mappable
 */
protected void loadSchema(String schemaLocation, XmlSchema xmlSchema, Map<String, String> imports, ProgressIndicator progress, boolean mainSchema) {
    String namespace = xmlSchema.getTargetNamespace();
    if (namespace == null) {
        namespace = XMLConstants.NULL_NS_URI;
    }
    // add namespace prefixes
    NamespacePrefixList namespaces = xmlSchema.getNamespaceContext();
    addPrefixes(namespaces, namespace, mainSchema);
    // the schema items
    XmlSchemaObjectCollection items = xmlSchema.getItems();
    // go through all schema items
    for (int i = 0; i < items.getCount(); i++) {
        XmlSchemaObject item = items.getItem(i);
        if (item instanceof XmlSchemaElement) {
            // global element declaration
            XmlSchemaElement element = (XmlSchemaElement) item;
            // determine type
            XmlTypeDefinition elementType = null;
            if (element.getSchemaTypeName() != null) {
                // reference to type
                elementType = index.getOrCreateType(element.getSchemaTypeName());
            } else if (element.getSchemaType() != null) {
                // element has internal type definition, generate anonymous
                // type name
                QName typeName = new QName(element.getQName().getNamespaceURI(), // $NON-NLS-1$
                element.getQName().getLocalPart() + "_AnonymousType");
                // create type
                elementType = createType(element.getSchemaType(), typeName, schemaLocation, namespace, mainSchema);
            } else if (element.getQName() != null) {
                // element with no type
                elementType = index.getOrCreateType(XmlTypeUtil.NAME_ANY_TYPE);
            }
            if (elementType != null) {
                // the element name
                // XXX use element QName instead?
                QName elementName = new QName(namespace, element.getName());
                // the substitution group
                QName subGroup = element.getSubstitutionGroup();
                // TODO do we also need an index for substitutions?
                // create schema element
                XmlElement schemaElement = new XmlElement(elementName, elementType, subGroup);
                // set metadata
                setMetadata(schemaElement, element, schemaLocation);
                // extend XmlElements constraint
                XmlElements xmlElements = elementType.getConstraint(XmlElements.class);
                xmlElements.addElement(schemaElement);
                // set custom display name
                elementType.setConstraint(new ElementName(xmlElements));
                // set Mappable constraint (e.g. Mappable)
                // for types with an associated element it can be determined
                // on the spot if it is mappable
                configureMappingRelevant(elementType, mainSchema);
                // XXX needed? may result in conflicts when defining
                // mappable types manually XXX the element is also marked
                // with the Mappable constraint, to help with cases where
                // multiple elements are defined for one
                // schemaElement.setConstraint(MappableFlag.get(mainSchema));
                // store element in index
                index.getElements().put(elementName, schemaElement);
            } else {
                reporter.error(new IOMessageImpl(MessageFormat.format("No type for element {0} found.", element.getName()), null, element.getLineNumber(), element.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaType) {
            // complex or simple type
            createType((XmlSchemaType) item, null, schemaLocation, namespace, mainSchema);
        } else if (item instanceof XmlSchemaAttribute) {
            // schema attribute that might be referenced somewhere
            XmlSchemaAttribute att = (XmlSchemaAttribute) item;
            if (att.getQName() != null) {
                XmlTypeDefinition type = getAttributeType(att, null, schemaLocation);
                if (type == null) {
                    // XXX if this occurs we might need a attribute
                    // referencing attribute
                    reporter.error(new IOMessageImpl("Could not determine attribute type", null, att.getLineNumber(), att.getLinePosition()));
                } else {
                    XmlAttribute attribute = new XmlAttribute(att.getQName(), type);
                    index.getAttributes().put(attribute.getName(), attribute);
                }
            } else {
                reporter.warn(new IOMessageImpl(MessageFormat.format("Attribute could not be processed: {0}", att.getName()), null, att.getLineNumber(), att.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaAttributeGroup) {
            // schema attribute group that might be referenced somewhere
            XmlSchemaAttributeGroup attributeGroup = (XmlSchemaAttributeGroup) item;
            if (attributeGroup.getName() != null) {
                String groupIdent = attributeGroup.getName().getNamespaceURI() + "/" + attributeGroup.getName().getLocalPart();
                XmlAttributeGroup attGroup = new XmlAttributeGroup(groupIdent, true);
                createAttributes(attributeGroup, attGroup, "", schemaLocation, namespace);
                index.getAttributeGroups().put(attributeGroup.getName(), attGroup);
            } else {
                reporter.warn(new IOMessageImpl("Attribute group could not be processed", null, attributeGroup.getLineNumber(), attributeGroup.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaGroup) {
            // group that might be referenced somewhere
            XmlSchemaGroup schemaGroup = (XmlSchemaGroup) item;
            if (schemaGroup.getName() != null) {
                String groupIdent = schemaGroup.getName().getNamespaceURI() + "/" + schemaGroup.getName().getLocalPart();
                XmlGroup group = new XmlGroup(groupIdent, true);
                createPropertiesFromParticle(group, schemaGroup.getParticle(), schemaLocation, namespace, false);
                index.getGroups().put(schemaGroup.getName(), group);
            } else {
                reporter.warn(new IOMessageImpl("Group could not be processed", null, schemaGroup.getLineNumber(), schemaGroup.getLinePosition()));
            }
        } else if (item instanceof XmlSchemaImport || item instanceof XmlSchemaInclude) {
        // ignore, is treated separately
        } else if (item instanceof XmlSchemaNotation) {
        // notations are ignored
        } else {
            reporter.error(new IOMessageImpl("Unrecognized global definition: " + item.getClass().getSimpleName(), null, item.getLineNumber(), item.getLinePosition()));
        }
    }
    // Set of include locations
    Set<String> includes = new HashSet<String>();
    // handle imports
    XmlSchemaObjectCollection externalItems = xmlSchema.getIncludes();
    if (externalItems.getCount() > 0) {
        // $NON-NLS-1$
        _log.info("Loading includes and imports for schema at " + schemaLocation);
    }
    for (int i = 0; i < externalItems.getCount(); i++) {
        try {
            XmlSchemaExternal imp = (XmlSchemaExternal) externalItems.getItem(i);
            XmlSchema importedSchema = imp.getSchema();
            String location = importedSchema.getSourceURI();
            String targetNamespace = importedSchema.getTargetNamespace();
            if (!imports.containsKey(location)) {
                // only add schemas that
                // were not already
                // added
                boolean addedBefore = imports.entrySet().stream().anyMatch(e -> {
                    // and "https") and they have the same target namespace.
                    return targetNamespace.equals(e.getValue()) && schemeIndependentEquals(location, e.getKey());
                });
                if (!addedBefore) {
                    // place a marker in the map to prevent loading the
                    // location in the call to loadSchema
                    imports.put(location, targetNamespace);
                    loadSchema(location, importedSchema, imports, progress, mainSchema && imp instanceof XmlSchemaInclude);
                // is part of main schema if it's a main schema include
                }
            }
            if (imp instanceof XmlSchemaInclude) {
                includes.add(location);
            }
        } catch (Throwable e) {
            reporter.error(new IOMessageImpl("Error adding imported schema from " + schemaLocation, // $NON-NLS-1$
            e));
        }
    }
    // $NON-NLS-1$
    _log.info("Creating types for schema at " + schemaLocation);
    progress.setCurrentTask(// $NON-NLS-1$
    MessageFormat.format(Messages.getString("ApacheSchemaProvider.33"), namespace));
}
Also used : XmlAttribute(eu.esdihumboldt.hale.io.xsd.model.XmlAttribute) XmlAttributeGroup(eu.esdihumboldt.hale.io.xsd.model.XmlAttributeGroup) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) NamespacePrefixList(org.apache.ws.commons.schema.utils.NamespacePrefixList) XmlGroup(eu.esdihumboldt.hale.io.xsd.model.XmlGroup) XmlElements(eu.esdihumboldt.hale.io.xsd.constraint.XmlElements) XmlSchemaObject(org.apache.ws.commons.schema.XmlSchemaObject) XmlTypeDefinition(eu.esdihumboldt.hale.io.xsd.reader.internal.XmlTypeDefinition) ElementName(eu.esdihumboldt.hale.io.xsd.reader.internal.constraint.ElementName) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaObjectCollection(org.apache.ws.commons.schema.XmlSchemaObjectCollection) HashSet(java.util.HashSet) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) XmlSchemaAttributeGroup(org.apache.ws.commons.schema.XmlSchemaAttributeGroup) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlSchemaAttribute(org.apache.ws.commons.schema.XmlSchemaAttribute) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaGroup(org.apache.ws.commons.schema.XmlSchemaGroup) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaNotation(org.apache.ws.commons.schema.XmlSchemaNotation) XmlElement(eu.esdihumboldt.hale.io.xsd.model.XmlElement) XmlSchemaInclude(org.apache.ws.commons.schema.XmlSchemaInclude)

Example 52 with XmlSchemaType

use of org.apache.ws.commons.schema.XmlSchemaType in project tomee by apache.

the class SchemaCollection method addElementCrossImportsElement.

private void addElementCrossImportsElement(XmlSchema schema, XmlSchemaElement item) {
    XmlSchemaElement element = item;
    XmlSchemaUtils.addImportIfNeeded(schema, element.getRef().getTargetQName());
    XmlSchemaUtils.addImportIfNeeded(schema, element.getSchemaTypeName());
    // if there's an anonymous type, it might have element refs in it.
    XmlSchemaType schemaType = element.getSchemaType();
    if (!crossImportsAdded(schema, schemaType)) {
        addCrossImportsType(schema, schemaType);
    }
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType)

Example 53 with XmlSchemaType

use of org.apache.ws.commons.schema.XmlSchemaType in project cxf by apache.

the class ServiceJavascriptBuilder method generateGlobalElementDictionary.

private void generateGlobalElementDictionary() {
    // to handle 'any', we need a dictionary of all the global elements of all the schemas.
    utils.appendLine("this.globalElementSerializers = [];");
    utils.appendLine("this.globalElementDeserializers = [];");
    for (XmlSchema schemaInfo : xmlSchemaCollection.getXmlSchemas()) {
        for (Map.Entry<QName, XmlSchemaElement> e : schemaInfo.getElements().entrySet()) {
            QName name = e.getKey();
            XmlSchemaElement element = e.getValue();
            // That comes later to improve deserialization.
            if (JavascriptUtils.notVeryComplexType(element.getSchemaType())) {
                continue;
            }
            // If the element uses a named type, we use the functions for the type.
            XmlSchemaComplexType elementType = (XmlSchemaComplexType) element.getSchemaType();
            if (elementType != null && elementType.getQName() != null) {
                name = elementType.getQName();
            }
            utils.appendLine("this.globalElementSerializers['" + name + "'] = " + nameManager.getJavascriptName(name) + "_serialize;");
            utils.appendLine("this.globalElementDeserializers['" + name + "'] = " + nameManager.getJavascriptName(name) + "_deserialize;");
        }
        for (Map.Entry<QName, XmlSchemaType> e : schemaInfo.getSchemaTypes().entrySet()) {
            QName name = e.getKey();
            XmlSchemaType type = e.getValue();
            // For now, at least, don't handle simple types.
            if (JavascriptUtils.notVeryComplexType(type)) {
                continue;
            }
            // the names are misleading, but that's OK.
            utils.appendLine("this.globalElementSerializers['" + name + "'] = " + nameManager.getJavascriptName(name) + "_serialize;");
            utils.appendLine("this.globalElementDeserializers['" + name + "'] = " + nameManager.getJavascriptName(name) + "_deserialize;");
        }
    }
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 54 with XmlSchemaType

use of org.apache.ws.commons.schema.XmlSchemaType in project cxf by apache.

the class ServiceJavascriptBuilder method buildOperationFunction.

private void buildOperationFunction(StringBuilder parameterList) {
    String responseCallbackParams = "";
    if (!currentOperation.isOneWay()) {
        responseCallbackParams = "successCallback, errorCallback";
    }
    MessageInfo inputMessage = currentOperation.getInput();
    code.append("//\n");
    code.append("// Operation ").append(currentOperation.getName()).append('\n');
    if (!isWrapped) {
        code.append("// - bare operation. Parameters:\n");
        for (ParticleInfo ei : unwrappedElementsAndNames) {
            code.append("// - ").append(getElementObjectName(ei)).append('\n');
        }
    } else {
        code.append("// Wrapped operation.\n");
        QName contextQName = inputWrapperComplexType.getQName();
        if (contextQName == null) {
            contextQName = inputWrapperElement.getQName();
        }
        XmlSchemaSequence sequence = JavascriptUtils.getSequence(inputWrapperComplexType);
        XmlSchema wrapperSchema = xmlSchemaCollection.getSchemaByTargetNamespace(contextQName.getNamespaceURI());
        for (int i = 0; i < sequence.getItems().size(); i++) {
            code.append("// parameter ").append(inputParameterNames.get(i)).append('\n');
            XmlSchemaSequenceMember sequenceItem = sequence.getItems().get(i);
            ParticleInfo itemInfo = ParticleInfo.forLocalItem((XmlSchemaObject) sequenceItem, wrapperSchema, xmlSchemaCollection, prefixAccumulator, contextQName);
            if (itemInfo.isArray()) {
                code.append("// - array\n");
            }
            // null for an any.
            XmlSchemaType type = itemInfo.getType();
            if (type instanceof XmlSchemaComplexType) {
                QName baseName;
                if (type.getQName() != null) {
                    baseName = type.getQName();
                } else {
                    baseName = ((XmlSchemaElement) sequenceItem).getQName();
                }
                code.append("// - Object constructor is ").append(nameManager.getJavascriptName(baseName)).append('\n');
            } else if (type != null) {
                code.append("// - simple type ").append(type.getQName());
            }
        }
    }
    code.append("//\n");
    code.append("function " + opFunctionGlobalName + "(" + responseCallbackParams + ((parameterList.length() > 0 && !currentOperation.isOneWay()) ? ", " : "") + parameterList + ") {\n");
    utils.appendLine("this.client = new CxfApacheOrgClient(this.jsutils);");
    utils.appendLine("var xml = null;");
    if (inputMessage != null) {
        utils.appendLine("var args = new Array(" + inputParameterNames.size() + ");");
        int px = 0;
        for (String param : inputParameterNames) {
            utils.appendLine("args[" + px + "] = " + param + ";");
            px++;
        }
        utils.appendLine("xml = this." + getFunctionPropertyName(inputMessagesWithNameConflicts, inputMessage, inputMessage.getName()) + "_serializeInput" + "(this.jsutils, args);");
    }
    // functions.
    if (!currentOperation.isOneWay()) {
        utils.appendLine("this.client.user_onsuccess = successCallback;");
        utils.appendLine("this.client.user_onerror = errorCallback;");
        utils.appendLine("var closureThis = this;");
        // client will pass itself and the response XML.
        utils.appendLine("this.client.onsuccess = function(client, responseXml) { closureThis." + opFunctionPropertyName + "_onsuccess(client, responseXml); };");
        // client will pass itself.
        utils.appendLine("this.client.onerror = function(client) { closureThis." + opFunctionPropertyName + "_onerror(client); };");
    }
    utils.appendLine("var requestHeaders = [];");
    if (soapBindingInfo != null) {
        String action = soapBindingInfo.getSoapAction(currentOperation);
        utils.appendLine("requestHeaders['SOAPAction'] = '" + action + "';");
    }
    // default 'method' by passing null. Is there some place this lives in the
    // service model?
    String syncAsyncFlag;
    if (currentOperation.isOneWay()) {
        utils.appendLine("this.jsutils.trace('oneway operation');");
        syncAsyncFlag = "false";
    } else {
        utils.appendLine("this.jsutils.trace('synchronous = ' + this.synchronous);");
        syncAsyncFlag = "this.synchronous";
    }
    utils.appendLine("this.client.request(this.url, xml, null, " + syncAsyncFlag + ", requestHeaders);");
    code.append("}\n\n");
    code.append(currentInterfaceClassName).append(".prototype.").append(opFunctionPropertyName).append(" = ").append(opFunctionGlobalName).append(";\n\n");
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchema(org.apache.ws.commons.schema.XmlSchema) QName(javax.xml.namespace.QName) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) XmlSchemaSequenceMember(org.apache.ws.commons.schema.XmlSchemaSequenceMember) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Example 55 with XmlSchemaType

use of org.apache.ws.commons.schema.XmlSchemaType in project cxf by apache.

the class ServiceJavascriptBuilder method getElementsForParts.

/**
 * Collect information about the parts of an unwrapped message.
 * @param message
 * @param elements
 */
private void getElementsForParts(MessageInfo message, List<ParticleInfo> elements) {
    for (MessagePartInfo mpi : message.getMessageParts()) {
        XmlSchemaElement element = null;
        XmlSchemaType type;
        QName diagnosticName = mpi.getName();
        if (mpi.isElement()) {
            element = (XmlSchemaElement) mpi.getXmlSchema();
            if (element == null) {
                element = XmlSchemaUtils.findElementByRefName(xmlSchemaCollection, mpi.getElementQName(), serviceInfo.getTargetNamespace());
            }
            diagnosticName = element.getQName();
            type = element.getSchemaType();
            if (type == null) {
                type = getElementType(xmlSchemaCollection, null, element, null);
            }
        } else {
            // RPC (!isElement)
            type = (XmlSchemaType) mpi.getXmlSchema();
            if (type == null) {
                type = xmlSchemaCollection.getTypeByQName(mpi.getTypeQName());
                diagnosticName = type.getQName();
            }
        }
        boolean empty = isEmptyType(type, diagnosticName);
        String partJavascriptVar = JavascriptUtils.javaScriptNameToken(mpi.getConcreteName().getLocalPart());
        String elementXmlRef = prefixAccumulator.xmlElementString(mpi.getConcreteName());
        ParticleInfo elementInfo = ParticleInfo.forPartElement(element, xmlSchemaCollection, partJavascriptVar, elementXmlRef);
        // the type may have been recalculated above.
        elementInfo.setType(type);
        elementInfo.setEmpty(empty);
        elements.add(elementInfo);
    }
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) ParticleInfo(org.apache.cxf.javascript.ParticleInfo) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo)

Aggregations

XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)81 QName (javax.xml.namespace.QName)47 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)37 CorbaType (org.apache.cxf.binding.corba.wsdl.CorbaType)23 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)23 XmlSchema (org.apache.ws.commons.schema.XmlSchema)21 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)16 XmlSchemaSequence (org.apache.ws.commons.schema.XmlSchemaSequence)12 AST (antlr.collections.AST)8 XmlSchemaObject (org.apache.ws.commons.schema.XmlSchemaObject)8 XmlSchemaComplexContentExtension (org.apache.ws.commons.schema.XmlSchemaComplexContentExtension)7 XmlSchemaObjectCollection (org.apache.ws.commons.schema.XmlSchemaObjectCollection)6 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)5 XmlSchemaGroupBase (org.apache.ws.commons.schema.XmlSchemaGroupBase)5 XmlSchemaParticle (org.apache.ws.commons.schema.XmlSchemaParticle)5 ArrayList (java.util.ArrayList)4 CorbaTypeImpl (org.apache.cxf.binding.corba.wsdl.CorbaTypeImpl)4 Message (org.apache.cxf.common.i18n.Message)4 ParticleInfo (org.apache.cxf.javascript.ParticleInfo)4 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)4