Search in sources :

Example 31 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class ReflectionServiceFactoryBean method checkForElement.

protected void checkForElement(ServiceInfo serviceInfo, MessagePartInfo mpi) {
    SchemaInfo si = getOrCreateSchema(serviceInfo, mpi.getElementQName().getNamespaceURI(), getQualifyWrapperSchema());
    XmlSchemaElement e = si.getSchema().getElementByName(mpi.getElementQName().getLocalPart());
    if (e != null) {
        mpi.setXmlSchema(e);
        return;
    }
    XmlSchema schema = si.getSchema();
    // cached element is now invalid
    si.setElement(null);
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(mpi.getElementQName().getLocalPart());
    el.setNillable(true);
    XmlSchemaType tp = (XmlSchemaType) mpi.getXmlSchema();
    if (tp == null) {
        throw new ServiceConstructionException(new Message("INTRACTABLE_PART", LOG, mpi.getName(), mpi.getMessageInfo().getName()));
    }
    el.setSchemaTypeName(tp.getQName());
    mpi.setXmlSchema(el);
}
Also used : Message(org.apache.cxf.common.i18n.Message) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 32 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class SchemaUtil method extractSchema.

private void extractSchema(Definition def, SchemaCollection schemaCol, List<SchemaInfo> schemaInfos) {
    Types typesElement = def.getTypes();
    if (typesElement != null) {
        int schemaCount = 1;
        for (Object obj : typesElement.getExtensibilityElements()) {
            org.w3c.dom.Element schemaElem = null;
            if (obj instanceof Schema) {
                Schema schema = (Schema) obj;
                schemaElem = schema.getElement();
            } else if (obj instanceof UnknownExtensibilityElement) {
                org.w3c.dom.Element elem = ((UnknownExtensibilityElement) obj).getElement();
                if (elem.getLocalName().equals("schema")) {
                    schemaElem = elem;
                }
            }
            if (schemaElem != null) {
                synchronized (schemaElem.getOwnerDocument()) {
                    for (Object prefix : def.getNamespaces().keySet()) {
                        String ns = (String) def.getNamespaces().get(prefix);
                        if ("".equals(prefix)) {
                            if (!schemaElem.hasAttribute("xmlns")) {
                                Attr attr = schemaElem.getOwnerDocument().createAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns");
                                attr.setValue(ns);
                                schemaElem.setAttributeNodeNS(attr);
                            }
                        } else if (!schemaElem.hasAttribute("xmlns:" + prefix)) {
                            String namespace = javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
                            Attr attr = schemaElem.getOwnerDocument().createAttributeNS(namespace, "xmlns:" + prefix);
                            attr.setValue(ns);
                            schemaElem.setAttributeNodeNS(attr);
                        }
                    }
                    String systemId = def.getDocumentBaseURI() + "#types" + schemaCount;
                    if (def.getDocumentBaseURI() != null && def.getDocumentBaseURI().toUpperCase().endsWith(".XSD") && def.getTargetNamespace() == null && obj instanceof Schema && ((Schema) obj).getDocumentBaseURI().equals(def.getDocumentBaseURI())) {
                        systemId = def.getDocumentBaseURI();
                    }
                    schemaCol.setBaseUri(def.getDocumentBaseURI());
                    CatalogXmlSchemaURIResolver schemaResolver = new CatalogXmlSchemaURIResolver(bus);
                    schemaCol.setSchemaResolver(schemaResolver);
                    XmlSchema xmlSchema = schemaCol.read(schemaElem, systemId);
                    catalogResolved.putAll(schemaResolver.getResolvedMap());
                    SchemaInfo schemaInfo = new SchemaInfo(xmlSchema.getTargetNamespace());
                    schemaInfo.setSchema(xmlSchema);
                    schemaInfo.setSystemId(systemId);
                    schemaInfo.setElement(schemaElem);
                    schemaInfos.add(schemaInfo);
                    schemaCount++;
                }
            }
        }
    }
}
Also used : Types(javax.wsdl.Types) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) Element(org.w3c.dom.Element) XmlSchema(org.apache.ws.commons.schema.XmlSchema) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) CatalogXmlSchemaURIResolver(org.apache.cxf.catalog.CatalogXmlSchemaURIResolver) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 33 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class ServiceWSDLBuilder method buildTypes.

protected void buildTypes(final Collection<SchemaInfo> schemas, final Map<String, SchemaInfo> imports, final Definition def) {
    Types types = def.createTypes();
    for (SchemaInfo schemaInfo : schemas) {
        Schema schemaImpl = getSchemaImplementation(def);
        schemaImpl.setRequired(true);
        schemaImpl.setElementType(WSDLConstants.QNAME_SCHEMA);
        schemaImpl.setElement(schemaInfo.getElement());
        for (XmlSchemaExternal ext : schemaInfo.getSchema().getExternals()) {
            if (ext.getSchema() == null) {
                continue;
            }
            if (ext instanceof XmlSchemaImport) {
                SchemaImport imp = schemaImpl.createImport();
                imp.setNamespaceURI(((XmlSchemaImport) ext).getNamespace());
                imp.setSchemaLocationURI(((XmlSchemaImport) ext).getSchemaLocation());
                Schema schemaImpl2 = getSchemaImplementation(def);
                schemaImpl2.setRequired(true);
                schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
                schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
                try {
                    schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
                } catch (XmlSchemaSerializerException e) {
                // ignore
                }
                imp.setReferencedSchema(schemaImpl2);
                schemaImpl.addImport(imp);
            } else if (ext instanceof XmlSchemaInclude) {
                SchemaReference imp = schemaImpl.createInclude();
                imp.setSchemaLocationURI(((XmlSchemaInclude) ext).getSchemaLocation());
                Schema schemaImpl2 = getSchemaImplementation(def);
                schemaImpl2.setRequired(true);
                schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
                schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
                try {
                    schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
                } catch (XmlSchemaSerializerException e) {
                // ignore
                }
                imp.setReferencedSchema(schemaImpl2);
                schemaImpl.addInclude(imp);
            } else if (ext instanceof XmlSchemaRedefine) {
                SchemaReference imp = schemaImpl.createRedefine();
                imp.setSchemaLocationURI(((XmlSchemaRedefine) ext).getSchemaLocation());
                Schema schemaImpl2 = getSchemaImplementation(def);
                schemaImpl2.setRequired(true);
                schemaImpl2.setElementType(WSDLConstants.QNAME_SCHEMA);
                schemaImpl2.setDocumentBaseURI(ext.getSchema().getSourceURI());
                try {
                    schemaImpl2.setElement(ext.getSchema().getSchemaDocument().getDocumentElement());
                } catch (XmlSchemaSerializerException e) {
                // ignore
                }
                imp.setReferencedSchema(schemaImpl2);
                schemaImpl.addRedefine(imp);
            }
        }
        types.addExtensibilityElement(schemaImpl);
    }
    def.setTypes(types);
}
Also used : SchemaReference(javax.wsdl.extensions.schema.SchemaReference) Types(javax.wsdl.Types) XmlSchemaRedefine(org.apache.ws.commons.schema.XmlSchemaRedefine) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) Schema(javax.wsdl.extensions.schema.Schema) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) XmlSchemaInclude(org.apache.ws.commons.schema.XmlSchemaInclude) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 34 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class ReflectionServiceFactoryBean method getOrCreateSchema.

private SchemaInfo getOrCreateSchema(ServiceInfo serviceInfo, String namespaceURI, boolean qualified) {
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(namespaceURI)) {
            return s;
        }
    }
    SchemaInfo schemaInfo = new SchemaInfo(namespaceURI);
    SchemaCollection col = serviceInfo.getXmlSchemaCollection();
    XmlSchema schema = col.getSchemaByTargetNamespace(namespaceURI);
    if (schema != null) {
        schemaInfo.setSchema(schema);
        serviceInfo.addSchema(schemaInfo);
        return schemaInfo;
    }
    schema = col.newXmlSchemaInCollection(namespaceURI);
    if (qualified) {
        schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    schemaInfo.setSchema(schema);
    Map<String, String> explicitNamespaceMappings = this.getDataBinding().getDeclaredNamespaceMappings();
    if (explicitNamespaceMappings == null) {
        explicitNamespaceMappings = Collections.emptyMap();
    }
    NamespaceMap nsMap = new NamespaceMap();
    for (Map.Entry<String, String> mapping : explicitNamespaceMappings.entrySet()) {
        nsMap.add(mapping.getValue(), mapping.getKey());
    }
    if (!explicitNamespaceMappings.containsKey(WSDLConstants.NS_SCHEMA_XSD)) {
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
    }
    if (!explicitNamespaceMappings.containsKey(serviceInfo.getTargetNamespace())) {
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, serviceInfo.getTargetNamespace());
    }
    schema.setNamespaceContext(nsMap);
    serviceInfo.addSchema(schemaInfo);
    return schemaInfo;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 35 with SchemaInfo

use of org.apache.cxf.service.model.SchemaInfo in project cxf by apache.

the class ReflectionServiceFactoryBean method createWrappedMessageSchema.

private void createWrappedMessageSchema(ServiceInfo serviceInfo, AbstractMessageContainer wrappedMessage, AbstractMessageContainer unwrappedMessage, SchemaInfo info, QName wrapperName) {
    XmlSchema schema = info.getSchema();
    // the cached schema will be no good
    info.setElement(null);
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(wrapperName.getLocalPart());
    wrappedMessage.getFirstMessagePart().setXmlSchema(el);
    boolean anonymousType = isAnonymousWrapperTypes();
    XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, /*CXF-6783: don't create anonymous top-level types*/
    !anonymousType);
    if (!anonymousType) {
        ct.setName(wrapperName.getLocalPart());
        el.setSchemaTypeName(wrapperName);
    }
    el.setSchemaType(ct);
    XmlSchemaSequence seq = new XmlSchemaSequence();
    ct.setParticle(seq);
    for (MessagePartInfo mpi : unwrappedMessage.getMessageParts()) {
        el = new XmlSchemaElement(schema, Boolean.TRUE.equals(mpi.getProperty(HEADER)));
        Map<Class<?>, Boolean> jaxbAnnoMap = getJaxbAnnoMap(mpi);
        if (mpi.isElement()) {
            addImport(schema, mpi.getElementQName().getNamespaceURI());
            XmlSchemaUtils.setElementRefName(el, mpi.getElementQName());
        } else {
            // We hope that we can't have parts that different only in namespace.
            el.setName(mpi.getName().getLocalPart());
            if (mpi.getTypeQName() != null && !jaxbAnnoMap.containsKey(XmlList.class)) {
                el.setSchemaTypeName(mpi.getTypeQName());
                addImport(schema, mpi.getTypeQName().getNamespaceURI());
            }
            el.setSchemaType((XmlSchemaType) mpi.getXmlSchema());
            if (schema.getElementFormDefault().equals(XmlSchemaForm.UNQUALIFIED)) {
                mpi.setConcreteName(new QName(null, mpi.getName().getLocalPart()));
            } else {
                mpi.setConcreteName(mpi.getName());
            }
        }
        if (!Boolean.TRUE.equals(mpi.getProperty(HEADER))) {
            boolean wasType = !mpi.isElement();
            if (wasType) {
                QName concreteName = mpi.getConcreteName();
                mpi.setElement(true);
                mpi.setElementQName(el.getQName());
                mpi.setConcreteName(concreteName);
            }
            addMimeType(el, getMethodParameterAnnotations(mpi));
            Annotation[] methodAnnotations = getMethodAnnotations(mpi);
            if (methodAnnotations != null) {
                addMimeType(el, methodAnnotations);
            }
            long min = getWrapperPartMinOccurs(mpi);
            long max = getWrapperPartMaxOccurs(mpi);
            boolean nillable = isWrapperPartNillable(mpi);
            Boolean qualified = isWrapperPartQualified(mpi);
            if (qualified == null) {
                qualified = this.isQualifyWrapperSchema();
            }
            if (qualified && StringUtils.isEmpty(mpi.getConcreteName().getNamespaceURI())) {
                QName newName = new QName(wrapperName.getNamespaceURI(), mpi.getConcreteName().getLocalPart());
                mpi.setElement(true);
                mpi.setElementQName(newName);
                mpi.setConcreteName(newName);
                el.setName(newName.getLocalPart());
                el.setForm(XmlSchemaForm.QUALIFIED);
            }
            if (Collection.class.isAssignableFrom(mpi.getTypeClass()) && mpi.getTypeClass().isInterface()) {
                Type type = (Type) mpi.getProperty(GENERIC_TYPE);
                if (!(type instanceof java.lang.reflect.ParameterizedType) && el.getSchemaTypeName() == null && el.getSchemaType() == null) {
                    max = Long.MAX_VALUE;
                    el.setSchemaTypeName(Constants.XSD_ANYTYPE);
                }
            }
            el.setMinOccurs(min);
            el.setMaxOccurs(max);
            if (nillable) {
                el.setNillable(nillable);
            }
            seq.getItems().add(el);
            mpi.setXmlSchema(el);
        }
        if (Boolean.TRUE.equals(mpi.getProperty(HEADER))) {
            QName qn = (QName) mpi.getProperty(ELEMENT_NAME);
            el.setName(qn.getLocalPart());
            SchemaInfo headerSchemaInfo = getOrCreateSchema(serviceInfo, qn.getNamespaceURI(), getQualifyWrapperSchema());
            if (!isExistSchemaElement(headerSchemaInfo.getSchema(), qn)) {
                headerSchemaInfo.getSchema().getItems().add(el);
            }
        }
    }
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) Type(java.lang.reflect.Type) XmlMimeType(javax.xml.bind.annotation.XmlMimeType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) Collection(java.util.Collection) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Aggregations

SchemaInfo (org.apache.cxf.service.model.SchemaInfo)42 XmlSchema (org.apache.ws.commons.schema.XmlSchema)17 QName (javax.xml.namespace.QName)11 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)11 Element (org.w3c.dom.Element)10 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)9 IOException (java.io.IOException)8 HashMap (java.util.HashMap)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 Message (org.apache.cxf.common.i18n.Message)6 FileNotFoundException (java.io.FileNotFoundException)5 URL (java.net.URL)5 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)5 InputSource (org.xml.sax.InputSource)5 SAXException (org.xml.sax.SAXException)5 SAXParseException (org.xml.sax.SAXParseException)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)4