Search in sources :

Example 1 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project tomee by apache.

the class StaxDataBinding method initialize.

public void initialize(Service service) {
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
        SchemaCollection schemaCollection = serviceInfo.getXmlSchemaCollection();
        if (schemaCollection.getXmlSchemas().length > 1) {
            // Schemas are already populated.
            continue;
        }
        new ServiceModelVisitor(serviceInfo) {

            @Override
            public void begin(MessagePartInfo part) {
                if (part.getTypeQName() != null || part.getElementQName() != null) {
                    return;
                }
                part.setTypeQName(Constants.XSD_ANYTYPE);
            }
        }.walk();
    }
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ServiceModelVisitor(org.apache.cxf.service.ServiceModelVisitor)

Example 2 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class JAXBDataBinding method initialize.

@SuppressWarnings("unchecked")
public synchronized void initialize(Service service) {
    inInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
    inFaultInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
    // context is already set, don't redo it
    if (context != null) {
        return;
    }
    contextClasses = new LinkedHashSet<>();
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
        JAXBContextInitializer initializer = new JAXBContextInitializer(getBus(), serviceInfo, contextClasses, typeRefs, this.getUnmarshallerProperties());
        initializer.walk();
        if (serviceInfo.getProperty("extra.class") != null) {
            Set<Class<?>> exClasses = serviceInfo.getProperty("extra.class", Set.class);
            contextClasses.addAll(exClasses);
        }
    }
    String tns = getNamespaceToUse(service);
    final CachedContextAndSchemas cachedContextAndSchemas;
    try {
        cachedContextAndSchemas = createJAXBContextAndSchemas(contextClasses, tns);
    } catch (JAXBException e1) {
        throw new ServiceConstructionException(e1);
    }
    final JAXBContext ctx = cachedContextAndSchemas.getContext();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "CREATED_JAXB_CONTEXT", new Object[] { ctx, contextClasses });
    }
    setContext(ctx);
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
        SchemaCollection col = serviceInfo.getXmlSchemaCollection();
        if (col.getXmlSchemas().length > 1) {
            // someone has already filled in the types
            justCheckForJAXBAnnotations(serviceInfo);
            continue;
        }
        boolean schemasFromCache = false;
        Collection<DOMSource> schemas = getSchemas();
        if (schemas == null || schemas.isEmpty()) {
            schemas = cachedContextAndSchemas.getSchemas();
            if (schemas != null) {
                schemasFromCache = true;
            }
        } else {
            schemasFromCache = true;
        }
        Set<DOMSource> bi = new LinkedHashSet<>();
        if (schemas == null) {
            schemas = new LinkedHashSet<>();
            try {
                for (DOMResult r : generateJaxbSchemas()) {
                    DOMSource src = new DOMSource(r.getNode(), r.getSystemId());
                    if (isInBuiltInSchemas(r)) {
                        bi.add(src);
                    } else {
                        schemas.add(src);
                    }
                }
                // put any builtins at the end.   Anything that DOES import them
                // will cause it to load automatically and we'll skip them later
                schemas.addAll(bi);
            } catch (IOException e) {
                throw new ServiceConstructionException("SCHEMA_GEN_EXC", LOG, e);
            }
        }
        for (DOMSource r : schemas) {
            if (bi.contains(r)) {
                String ns = ((Document) r.getNode()).getDocumentElement().getAttribute("targetNamespace");
                if (serviceInfo.getSchema(ns) != null) {
                    continue;
                }
            }
            // StaxUtils.print(r.getNode());
            // System.out.println();
            addSchemaDocument(serviceInfo, col, (Document) r.getNode(), r.getSystemId());
        }
        JAXBSchemaInitializer schemaInit = new JAXBSchemaInitializer(serviceInfo, col, context, this.qualifiedSchemas, tns);
        schemaInit.walk();
        if (cachedContextAndSchemas != null && !schemasFromCache) {
            cachedContextAndSchemas.setSchemas(schemas);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) IOException(java.io.IOException) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) CachedContextAndSchemas(org.apache.cxf.common.jaxb.JAXBContextCache.CachedContextAndSchemas)

Example 3 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class WadlGenerator method getSchemaCollection.

private SchemaCollection getSchemaCollection(ResourceTypes resourceTypes, JAXBContext context) {
    if (context == null) {
        return null;
    }
    SchemaCollection xmlSchemaCollection = new SchemaCollection();
    Collection<DOMSource> schemas = new HashSet<>();
    List<String> targetNamespaces = new ArrayList<>();
    try {
        for (DOMResult r : JAXBUtils.generateJaxbSchemas(context, CastUtils.cast(Collections.emptyMap(), String.class, DOMResult.class))) {
            Document doc = (Document) r.getNode();
            ElementQNameResolver theResolver = createElementQNameResolver(context);
            String tns = doc.getDocumentElement().getAttribute("targetNamespace");
            String tnsPrefix = doc.getDocumentElement().lookupPrefix(tns);
            if (tnsPrefix == null) {
                String tnsDecl = doc.getDocumentElement().getAttribute("xmlns:tns");
                tnsPrefix = tnsDecl != null && tnsDecl.equals(tns) ? "tns:" : "";
            } else {
                tnsPrefix += ":";
            }
            if (supportJaxbXmlType) {
                for (Class<?> cls : resourceTypes.getAllTypes().keySet()) {
                    if (isXmlRoot(cls)) {
                        continue;
                    }
                    XmlType root = cls.getAnnotation(XmlType.class);
                    if (root != null) {
                        QName typeName = theResolver.resolve(cls, new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
                        if (typeName != null && tns.equals(typeName.getNamespaceURI())) {
                            QName elementName = resourceTypes.getXmlNameMap().get(cls);
                            if (elementName == null) {
                                elementName = typeName;
                            }
                            Element newElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
                            newElement.setAttribute("name", elementName.getLocalPart());
                            newElement.setAttribute("type", tnsPrefix + typeName.getLocalPart());
                            if (Modifier.isAbstract(cls.getModifiers()) && resourceTypes.getSubstitutions().values().contains(cls)) {
                                newElement.setAttribute("abstract", "true");
                            }
                            doc.getDocumentElement().appendChild(newElement);
                        }
                    }
                }
                if (supportJaxbSubstitutions) {
                    for (Map.Entry<Class<?>, Class<?>> entry : resourceTypes.getSubstitutions().entrySet()) {
                        QName typeName = theResolver.resolve(entry.getKey(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
                        for (Element element : DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(), Constants.URI_2001_SCHEMA_XSD, "element")) {
                            if (element.getAttribute("name").equals(typeName.getLocalPart())) {
                                QName groupName = theResolver.resolve(entry.getValue(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
                                if (groupName != null) {
                                    element.setAttribute("substitutionGroup", tnsPrefix + groupName.getLocalPart());
                                }
                            }
                        }
                    }
                }
            }
            if (supportCollections && !resourceTypes.getCollectionMap().isEmpty()) {
                for (Map.Entry<Class<?>, QName> entry : resourceTypes.getCollectionMap().entrySet()) {
                    QName colQName = entry.getValue();
                    if (colQName == null) {
                        colQName = theResolver.resolve(entry.getKey(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
                        if (colQName != null) {
                            colQName = new QName(colQName.getNamespaceURI(), colQName.getLocalPart() + "s", colQName.getPrefix());
                        }
                    }
                    if (colQName == null) {
                        continue;
                    }
                    if (tns.equals(colQName.getNamespaceURI())) {
                        QName typeName = theResolver.resolve(entry.getKey(), new Annotation[] {}, Collections.<Class<?>, QName>emptyMap());
                        if (typeName != null) {
                            Element newElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
                            newElement.setAttribute("name", colQName.getLocalPart());
                            Element ctElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:complexType");
                            newElement.appendChild(ctElement);
                            Element seqElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:sequence");
                            ctElement.appendChild(seqElement);
                            Element xsElement = doc.createElementNS(Constants.URI_2001_SCHEMA_XSD, "xs:element");
                            seqElement.appendChild(xsElement);
                            xsElement.setAttribute("ref", tnsPrefix + typeName.getLocalPart());
                            xsElement.setAttribute("minOccurs", "0");
                            xsElement.setAttribute("maxOccurs", "unbounded");
                            doc.getDocumentElement().appendChild(newElement);
                        }
                    }
                }
            }
            DOMSource source = new DOMSource(doc, r.getSystemId());
            schemas.add(source);
            if (!StringUtils.isEmpty(tns)) {
                targetNamespaces.add(tns);
            }
        }
    } catch (IOException e) {
        LOG.fine("No schema can be generated");
        return null;
    }
    boolean hackAroundEmptyNamespaceIssue = false;
    for (DOMSource r : schemas) {
        hackAroundEmptyNamespaceIssue = addSchemaDocument(xmlSchemaCollection, targetNamespaces, (Document) r.getNode(), r.getSystemId(), hackAroundEmptyNamespaceIssue);
    }
    return xmlSchemaCollection;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) QName(javax.xml.namespace.QName) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) Annotation(java.lang.annotation.Annotation) XmlType(javax.xml.bind.annotation.XmlType) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) EnumMap(java.util.EnumMap) LinkedHashMap(java.util.LinkedHashMap) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 4 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class SoapBindingFactory method addMessageFromBinding.

protected void addMessageFromBinding(ExtensibilityElement ext, BindingOperationInfo bop, boolean isInput) {
    SoapHeader header = SOAPBindingUtil.getSoapHeader(ext);
    ServiceInfo serviceInfo = bop.getBinding().getService();
    if (header != null && header.getMessage() == null) {
        throw new RuntimeException("Problem with WSDL: soap:header element" + " for operation " + bop.getName() + " under binding " + bop.getBinding().getName() + " does not contain a valid message attribute.");
    }
    if (header != null && serviceInfo.getMessage(header.getMessage()) == null) {
        Definition def = (Definition) serviceInfo.getProperty(WSDLServiceBuilder.WSDL_DEFINITION);
        SchemaCollection schemas = serviceInfo.getXmlSchemaCollection();
        if (def != null && schemas != null) {
            QName qn = header.getMessage();
            javax.wsdl.Message msg = findMessage(qn, def);
            if (msg != null) {
                addOutOfBandParts(bop, msg, schemas, isInput, header.getPart());
                serviceInfo.refresh();
            } else {
                throw new RuntimeException("Problem with WSDL: soap:header element" + " for operation " + bop.getName() + " is referring to an undefined wsdl:message element: " + qn);
            }
        }
    }
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) QName(javax.xml.namespace.QName) Definition(javax.wsdl.Definition) SoapHeader(org.apache.cxf.binding.soap.wsdl.extensions.SoapHeader) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection)

Example 5 with SchemaCollection

use of org.apache.cxf.common.xmlschema.SchemaCollection in project cxf by apache.

the class StaxDataBinding method initialize.

public void initialize(Service service) {
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
        SchemaCollection schemaCollection = serviceInfo.getXmlSchemaCollection();
        if (schemaCollection.getXmlSchemas().length > 1) {
            // Schemas are already populated.
            continue;
        }
        new ServiceModelVisitor(serviceInfo) {

            @Override
            public void begin(MessagePartInfo part) {
                if (part.getTypeQName() != null || part.getElementQName() != null) {
                    return;
                }
                part.setTypeQName(Constants.XSD_ANYTYPE);
            }
        }.walk();
    }
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ServiceModelVisitor(org.apache.cxf.service.ServiceModelVisitor)

Aggregations

SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)26 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)15 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)8 IOException (java.io.IOException)7 QName (javax.xml.namespace.QName)7 ArrayList (java.util.ArrayList)6 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Definition (javax.wsdl.Definition)4 Message (org.apache.cxf.common.i18n.Message)4 ServiceModelVisitor (org.apache.cxf.service.ServiceModelVisitor)4 ServiceConstructionException (org.apache.cxf.service.factory.ServiceConstructionException)4 XmlSchema (org.apache.ws.commons.schema.XmlSchema)4 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 Map (java.util.Map)3 JAXBContext (javax.xml.bind.JAXBContext)3 JAXBException (javax.xml.bind.JAXBException)3 DOMResult (javax.xml.transform.dom.DOMResult)3