Search in sources :

Example 6 with NamespaceMap

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

the class ObjectReferenceVisitor method addWSAddressingImport.

private void addWSAddressingImport(XmlSchema s) {
    boolean alreadyImported = false;
    for (XmlSchemaExternal ext : s.getExternals()) {
        if (ext instanceof XmlSchemaImport) {
            XmlSchemaImport schemaImport = (XmlSchemaImport) ext;
            if (schemaImport.getNamespace().equals(ReferenceConstants.WSADDRESSING_NAMESPACE)) {
                alreadyImported = true;
                break;
            }
        }
    }
    if (!alreadyImported) {
        // We need to add an import statement to include the WS addressing types
        XmlSchemaImport wsaImport = new XmlSchemaImport(s);
        wsaImport.setNamespace(ReferenceConstants.WSADDRESSING_NAMESPACE);
        wsaImport.setSchemaLocation(ReferenceConstants.WSADDRESSING_LOCATION);
    }
    // Add the addressing namespace to the WSDLs list of namespaces.
    definition.addNamespace(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
    try {
        // This is used to get the correct prefix in the schema section of
        // the wsdl.  If we don't have this, then this namespace gets an
        // arbitrary prefix (e.g. ns5 instead of wsa).
        NamespaceMap nsMap = (NamespaceMap) s.getNamespaceContext();
        if (nsMap == null) {
            nsMap = new NamespaceMap();
            nsMap.add(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
            s.setNamespaceContext(nsMap);
        } else {
            nsMap.add(ReferenceConstants.WSADDRESSING_PREFIX, ReferenceConstants.WSADDRESSING_NAMESPACE);
        }
    } catch (ClassCastException ex) {
    // Consume the exception.  It is still OK with the default prefix,
    // just not as clear.
    }
}
Also used : XmlSchemaExternal(org.apache.ws.commons.schema.XmlSchemaExternal) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaImport(org.apache.ws.commons.schema.XmlSchemaImport)

Example 7 with NamespaceMap

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

the class ImportRepairTest method newSchema.

private XmlSchema newSchema(String uri) {
    XmlSchema schema = collection.newXmlSchemaInCollection(uri);
    NamespaceMap map = new NamespaceMap();
    map.add("", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schema.setNamespaceContext(map);
    return schema;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap)

Example 8 with NamespaceMap

use of org.apache.ws.commons.schema.utils.NamespaceMap 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 9 with NamespaceMap

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

the class AegisDatabinding method createSchemas.

private void createSchemas(Service s, Set<AegisType> deps) {
    Map<String, Set<AegisType>> tns2Type = new HashMap<>();
    for (AegisType t : deps) {
        String ns = t.getSchemaType().getNamespaceURI();
        Set<AegisType> types = tns2Type.get(ns);
        if (types == null) {
            types = new HashSet<>();
            tns2Type.put(ns, types);
        }
        types.add(t);
    }
    for (ServiceInfo si : s.getServiceInfos()) {
        SchemaCollection col = si.getXmlSchemaCollection();
        if (col.getXmlSchemas().length > 1) {
            // someone has already filled in the types
            continue;
        }
    }
    Map<String, String> namespaceMap = getDeclaredNamespaceMappings();
    for (ServiceInfo si : s.getServiceInfos()) {
        // these two must be recalculated per-service-info!
        boolean needXmimeSchema = false;
        boolean needTypesSchema = false;
        for (Map.Entry<String, Set<AegisType>> entry : tns2Type.entrySet()) {
            String schemaNamespaceUri = entry.getKey();
            if (Constants.URI_2001_SCHEMA_XSD.equals(schemaNamespaceUri)) {
                continue;
            }
            if (AegisContext.UTILITY_TYPES_SCHEMA_NS.equals(schemaNamespaceUri)) {
                // we handle this separately.
                continue;
            }
            if (AbstractXOPType.XML_MIME_NS.equals(schemaNamespaceUri)) {
                // similiarly.
                continue;
            }
            SchemaInfo schemaInfo = si.addNewSchema(entry.getKey());
            XmlSchema schema = schemaInfo.getSchema();
            NamespaceMap xmlsNamespaceMap = new NamespaceMap();
            // user-requested prefix mappings.
            if (namespaceMap != null) {
                for (Map.Entry<String, String> e : namespaceMap.entrySet()) {
                    xmlsNamespaceMap.add(e.getValue(), e.getKey());
                }
            }
            // tns: is conventional, and besides we have unit tests that are hardcoded to it.
            if (!xmlsNamespaceMap.containsKey(WSDLConstants.CONVENTIONAL_TNS_PREFIX) && // if some wants something other than TNS, they get it.
            !xmlsNamespaceMap.containsValue(entry.getKey())) {
                xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, entry.getKey());
            }
            // ditto for xsd: instead of just namespace= for the schema schema.
            if (!xmlsNamespaceMap.containsKey("xsd") && !xmlsNamespaceMap.containsValue(Constants.URI_2001_SCHEMA_XSD)) {
                xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
            }
            schema.setNamespaceContext(xmlsNamespaceMap);
            schema.setTargetNamespace(entry.getKey());
            schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
            schema.setAttributeFormDefault(XmlSchemaForm.QUALIFIED);
            for (AegisType t : entry.getValue()) {
                try {
                    t.writeSchema(schema);
                } catch (XmlSchemaException ex) {
                    QName name = t.getSchemaType();
                    String expected = " Schema for namespace '" + name.getNamespaceURI() + "' already contains type '" + name.getLocalPart() + "'";
                    String message = ex.getMessage();
                    if (expected.equals(message)) {
                        continue;
                    }
                    throw ex;
                }
            }
            if (schemaImportsXmime(schema)) {
                needXmimeSchema = true;
            }
            if (AegisContext.schemaImportsUtilityTypes(schema)) {
                needTypesSchema = true;
            }
        }
        if (needXmimeSchema) {
            XmlSchema schema = aegisContext.addXmimeSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
            SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
            schemaInfo.setSchema(schema);
            si.addSchema(schemaInfo);
        }
        if (needTypesSchema) {
            XmlSchema schema = aegisContext.addTypesSchemaDocument(si.getXmlSchemaCollection().getXmlSchemaCollection());
            SchemaInfo schemaInfo = new SchemaInfo(schema.getTargetNamespace());
            schemaInfo.setSchema(schema);
            si.addSchema(schemaInfo);
        }
        // it's quite likely that the code in Aegis missed at least one ...
        si.getXmlSchemaCollection().addCrossImports();
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaException(org.apache.ws.commons.schema.XmlSchemaException) QName(javax.xml.namespace.QName) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) HashMap(java.util.HashMap) Map(java.util.Map) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Aggregations

NamespaceMap (org.apache.ws.commons.schema.utils.NamespaceMap)9 XmlSchema (org.apache.ws.commons.schema.XmlSchema)8 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)4 QName (javax.xml.namespace.QName)3 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)2 XmlSchemaExternal (org.apache.ws.commons.schema.XmlSchemaExternal)2 XmlSchemaImport (org.apache.ws.commons.schema.XmlSchemaImport)2 Field (java.lang.reflect.Field)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Types (javax.wsdl.Types)1 Schema (javax.wsdl.extensions.schema.Schema)1