Search in sources :

Example 1 with XmlSchemaExternal

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

the class XmlSchemaUtils method addImportIfNeeded.

/**
 * Assist in managing the required <import namespace='uri'> for imports of peer schemas.
 * @param schema
 * @param namespaceUri
 */
public static void addImportIfNeeded(XmlSchema schema, String namespaceUri) {
    // no need to import nothing or the XSD schema, or the schema we are fixing.
    if ("".equals(namespaceUri) || Constants.URI_2001_SCHEMA_XSD.equals(namespaceUri) || schema.getTargetNamespace().equals(namespaceUri)) {
        return;
    }
    List<XmlSchemaExternal> externals = schema.getExternals();
    for (XmlSchemaExternal what : externals) {
        if (what instanceof XmlSchemaImport) {
            XmlSchemaImport imp = (XmlSchemaImport) what;
            // already there.
            if (namespaceUri.equals(imp.getNamespace())) {
                return;
            }
        }
    }
    XmlSchemaImport imp = new XmlSchemaImport(schema);
    imp.setNamespace(namespaceUri);
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport)

Example 2 with XmlSchemaExternal

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

the class Stax2ValidationUtils method getValidator.

/**
 * Create woodstox validator for a schema set.
 *
 * @throws XMLStreamException
 */
private XMLValidationSchema getValidator(Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException {
    synchronized (endpoint) {
        XMLValidationSchema ret = (XMLValidationSchema) endpoint.get(KEY);
        if (ret == null) {
            if (endpoint.containsKey(KEY)) {
                return null;
            }
            Map<String, EmbeddedSchema> sources = new TreeMap<>();
            for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
                XmlSchema sch = schemaInfo.getSchema();
                String uri = sch.getTargetNamespace();
                if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(uri)) {
                    continue;
                }
                if (sch.getTargetNamespace() == null && sch.getExternals().size() > 0) {
                    for (XmlSchemaExternal xmlSchemaExternal : sch.getExternals()) {
                        addSchema(sources, xmlSchemaExternal.getSchema(), getElement(xmlSchemaExternal.getSchema().getSourceURI()));
                    }
                    continue;
                } else if (sch.getTargetNamespace() == null) {
                    throw new IllegalStateException("An Schema without imports must have a targetNamespace");
                }
                addSchema(sources, sch, schemaInfo.getElement());
            }
            W3CMultiSchemaFactory factory = new W3CMultiSchemaFactory();
            // I don't think that we need the baseURI.
            try {
                ret = factory.loadSchemas(null, sources);
                endpoint.put(KEY, ret);
            } catch (XMLStreamException ex) {
                LOG.log(Level.INFO, "Problem loading schemas. Falling back to slower method.", ret);
                endpoint.put(KEY, null);
            }
        }
        return ret;
    }
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XMLStreamException(javax.xml.stream.XMLStreamException) XmlSchema(org.apache.ws.commons.schema.XmlSchema) TreeMap(java.util.TreeMap) XMLValidationSchema(org.codehaus.stax2.validation.XMLValidationSchema) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 3 with XmlSchemaExternal

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

the class WSDLSchemaManager method attachSchemaToWSDL.

public void attachSchemaToWSDL(Definition definition, XmlSchema schema, boolean isSchemaGenerated) throws Exception {
    Types types = definition.getTypes();
    if (types == null) {
        types = definition.createTypes();
        definition.setTypes(types);
    }
    Schema wsdlSchema = (Schema) definition.getExtensionRegistry().createExtension(Types.class, new QName(Constants.URI_2001_SCHEMA_XSD, "schema"));
    // See if a NamespaceMap has already been added to the schema (this can be the case with object
    // references. If so, simply add the XSD URI to the map. Otherwise, create a new one.
    NamespaceMap nsMap = null;
    try {
        nsMap = (NamespaceMap) schema.getNamespaceContext();
    } catch (ClassCastException ex) {
    // Consume. This will mean that the context has not been set.
    }
    if (nsMap == null) {
        nsMap = new NamespaceMap();
        nsMap.add("xs", Constants.URI_2001_SCHEMA_XSD);
        schema.setNamespaceContext(nsMap);
    } else {
        nsMap.add("xs", Constants.URI_2001_SCHEMA_XSD);
    }
    if (isSchemaGenerated) {
        nsMap.add("tns", schema.getTargetNamespace());
    }
    org.w3c.dom.Element el = schema.getAllSchemas()[0].getDocumentElement();
    wsdlSchema.setElement(el);
    for (XmlSchemaExternal ext : schema.getExternals()) {
        if (ext instanceof XmlSchemaImport) {
            XmlSchemaImport xmlSchemaImport = (XmlSchemaImport) ext;
            SchemaImport schemaimport = wsdlSchema.createImport();
            schemaimport.setNamespaceURI(xmlSchemaImport.getNamespace());
            if (xmlSchemaImport.getSchemaLocation() != null && !ignoreImports) {
                schemaimport.setSchemaLocationURI(xmlSchemaImport.getSchemaLocation());
            }
            wsdlSchema.addImport(schemaimport);
        }
    }
    types.addExtensibilityElement(wsdlSchema);
}
Also used : Types(javax.wsdl.Types) XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) QName(javax.xml.namespace.QName) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport)

Example 4 with XmlSchemaExternal

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

the class WSDLToCorbaHelper method findTypeInSchema.

private XmlSchemaType findTypeInSchema(XmlSchema xmlSchema, QName typeName) {
    XmlSchemaType schemaType = null;
    if (xmlSchema.getElementByName(typeName) != null) {
        XmlSchemaElement schemaElement = xmlSchema.getElementByName(typeName);
        schemaType = schemaElement.getSchemaType();
    } else if (xmlSchema.getTypeByName(typeName) != null) {
        schemaType = xmlSchema.getTypeByName(typeName);
    }
    if (schemaType != null) {
        return schemaType;
    }
    for (XmlSchemaExternal extSchema : xmlSchema.getExternals()) {
        if (!(extSchema instanceof XmlSchemaImport)) {
            schemaType = findTypeInSchema(extSchema.getSchema(), typeName);
            if (schemaType != null) {
                return schemaType;
            }
        }
    }
    return null;
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType)

Example 5 with XmlSchemaExternal

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

the class WSDLSchemaManager method addXmlSchemaImport.

public void addXmlSchemaImport(XmlSchema rootSchema, XmlSchema schema, File file) {
    // Make sure we haven't already imported the schema.
    String importNamespace = schema.getTargetNamespace();
    boolean included = false;
    for (XmlSchemaExternal ext : rootSchema.getExternals()) {
        if (ext instanceof XmlSchemaImport) {
            XmlSchemaImport imp = (XmlSchemaImport) ext;
            if (imp.getNamespace().equals(importNamespace)) {
                included = true;
                break;
            }
        }
    }
    if (!included) {
        XmlSchemaImport importSchema = new XmlSchemaImport(rootSchema);
        if (!ignoreImports) {
            importSchema.setSchemaLocation(file.toURI().toString());
        }
        importSchema.setNamespace(schema.getTargetNamespace());
    }
    if (!importedSchemas.containsKey(file)) {
        importedSchemas.put(file, schema);
    }
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport)

Aggregations

XmlSchemaExternal (org.apache.ws.commons.schema.XmlSchemaExternal)10 XmlSchemaImport (org.apache.ws.commons.schema.XmlSchemaImport)9 XmlSchema (org.apache.ws.commons.schema.XmlSchema)4 Types (javax.wsdl.Types)2 Schema (javax.wsdl.extensions.schema.Schema)2 SchemaImport (javax.wsdl.extensions.schema.SchemaImport)2 QName (javax.xml.namespace.QName)2 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2 XmlSchemaType (org.apache.ws.commons.schema.XmlSchemaType)2 NamespaceMap (org.apache.ws.commons.schema.utils.NamespaceMap)2 TreeMap (java.util.TreeMap)1 SchemaReference (javax.wsdl.extensions.schema.SchemaReference)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XmlSchemaInclude (org.apache.ws.commons.schema.XmlSchemaInclude)1 XmlSchemaRedefine (org.apache.ws.commons.schema.XmlSchemaRedefine)1 XmlSchemaSerializerException (org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException)1 XMLValidationSchema (org.codehaus.stax2.validation.XMLValidationSchema)1