Search in sources :

Example 1 with NamespaceMap

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

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

the class SchemaInfo method getElement.

/**
 * Build and return a DOM tree for this schema.
 * @return a DOM Element representation of the schema
 */
public synchronized Element getElement() {
    // if someone recently used this DOM tree, take advantage.
    Element element = cachedElement == null ? null : cachedElement.get();
    if (element != null) {
        return element;
    }
    if (getSchema() == null) {
        throw new RuntimeException("No XmlSchema in SchemaInfo");
    }
    XmlSchema sch = getSchema();
    synchronized (sch) {
        XmlSchema schAgain = getSchema();
        // Some unit tests really want to see 'tns:'.
        if (schAgain.getNamespaceContext() == null) {
            NamespaceMap nsMap = new NamespaceMap();
            nsMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
            nsMap.add("tns", schAgain.getTargetNamespace());
            schAgain.setNamespaceContext(nsMap);
        }
        Document serializedSchema;
        try {
            serializedSchema = schAgain.getSchemaDocument();
        } catch (XmlSchemaSerializerException e) {
            throw new RuntimeException("Error serializing Xml Schema", e);
        }
        element = serializedSchema.getDocumentElement();
        cachedElement = new SoftReference<Element>(element);
    }
    // The aegis databinding tests demonstrate this particularly.
    if (element.getPrefix() == null && !Constants.URI_2001_SCHEMA_XSD.equals(element.getAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE))) {
        Attr attr = element.getOwnerDocument().createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE);
        attr.setValue(Constants.URI_2001_SCHEMA_XSD);
        element.setAttributeNodeNS(attr);
    }
    return element;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) Element(org.w3c.dom.Element) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) XmlSchemaSerializerException(org.apache.ws.commons.schema.XmlSchemaSerializer.XmlSchemaSerializerException) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Example 3 with NamespaceMap

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

the class JAXBSchemaInitializer method createBridgeXsElement.

private void createBridgeXsElement(MessagePartInfo part, QName qn, QName typeName) {
    XmlSchemaElement el = null;
    SchemaInfo schemaInfo = serviceInfo.getSchema(qn.getNamespaceURI());
    if (schemaInfo != null) {
        el = schemaInfo.getElementByQName(qn);
        if (el == null) {
            createXsElement(schemaInfo.getSchema(), part, typeName, schemaInfo);
        } else if (!typeName.equals(el.getSchemaTypeName())) {
            throw new Fault(new Message("CANNOT_CREATE_ELEMENT", LOG, qn, typeName, el.getSchemaTypeName()));
        }
        return;
    }
    XmlSchema schema = schemas.newXmlSchemaInCollection(qn.getNamespaceURI());
    if (qualifiedSchemas) {
        schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
    }
    schemaInfo = new SchemaInfo(qn.getNamespaceURI(), qualifiedSchemas, false);
    schemaInfo.setSchema(schema);
    el = createXsElement(schema, part, typeName, schemaInfo);
    NamespaceMap nsMap = new NamespaceMap();
    nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, schema.getTargetNamespace());
    nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
    schema.setNamespaceContext(nsMap);
    serviceInfo.addSchema(schemaInfo);
}
Also used : Message(org.apache.cxf.common.i18n.Message) XmlSchema(org.apache.ws.commons.schema.XmlSchema) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) Fault(org.apache.cxf.interceptor.Fault) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 4 with NamespaceMap

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

the class JAXBSchemaInitializer method buildExceptionType.

private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
    SchemaInfo schemaInfo = null;
    for (SchemaInfo s : serviceInfo.getSchemas()) {
        if (s.getNamespaceURI().equals(part.getElementQName().getNamespaceURI())) {
            schemaInfo = s;
            break;
        }
    }
    XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
    XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
    String[] propertyOrder = null;
    boolean respectXmlTypeNS = false;
    XmlSchema faultBeanSchema = null;
    if (xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.namespace()) && !xmlTypeAnno.namespace().equals(part.getElementQName().getNamespaceURI())) {
        respectXmlTypeNS = true;
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, xmlTypeAnno.namespace());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        SchemaInfo faultBeanSchemaInfo = createSchemaIfNeeded(xmlTypeAnno.namespace(), nsMap);
        faultBeanSchema = faultBeanSchemaInfo.getSchema();
    }
    if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
        propertyOrder = xmlTypeAnno.propOrder();
    // TODO: handle @XmlAccessOrder
    }
    XmlSchema schema = null;
    if (schemaInfo == null) {
        NamespaceMap nsMap = new NamespaceMap();
        nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, part.getElementQName().getNamespaceURI());
        nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
        schemaInfo = createSchemaIfNeeded(part.getElementQName().getNamespaceURI(), nsMap);
    }
    schema = schemaInfo.getSchema();
    // Before updating everything, make sure we haven't added this
    // type yet.  Multiple methods that throw the same exception
    // types will cause duplicates.
    String faultTypeName = xmlTypeAnno != null && !StringUtils.isEmpty(xmlTypeAnno.name()) ? xmlTypeAnno.name() : part.getElementQName().getLocalPart();
    XmlSchemaType existingType = schema.getTypeByName(faultTypeName);
    if (existingType != null) {
        return;
    }
    XmlSchemaElement el = new XmlSchemaElement(schema, true);
    el.setName(part.getElementQName().getLocalPart());
    part.setXmlSchema(el);
    schemaInfo.setElement(null);
    if (respectXmlTypeNS) {
        // create complexType in the new created schema for xmlType
        schema = faultBeanSchema;
    }
    XmlSchemaComplexType ct = new XmlSchemaComplexType(schema, true);
    ct.setName(faultTypeName);
    el.setSchemaTypeName(ct.getQName());
    XmlSchemaSequence seq = new XmlSchemaSequence();
    ct.setParticle(seq);
    String namespace = part.getElementQName().getNamespaceURI();
    XmlAccessType accessType = Utils.getXmlAccessType(cls);
    // 
    for (Field f : Utils.getFields(cls, accessType)) {
        // map field
        Type type = Utils.getFieldType(f);
        // generic return type.
        if ((type == null) && (f.getGenericType() instanceof ParameterizedType)) {
            type = f.getGenericType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, f);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                XmlElement xmlElementAnno = f.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, f.getName()), isArray(type), xmlElementAnno);
            }
        }
    }
    for (Method m : Utils.getGetters(cls, accessType)) {
        // map method
        Type type = Utils.getMethodReturnType(m);
        // generic return type.
        if ((type == null) && (m.getGenericReturnType() instanceof ParameterizedType)) {
            type = m.getGenericReturnType();
        }
        if (generateGenericType(type)) {
            buildGenericElements(schema, seq, m, type);
        } else {
            JAXBBeanInfo beanInfo = getBeanInfo(type);
            if (beanInfo != null) {
                int idx = m.getName().startsWith("get") ? 3 : 2;
                String name = m.getName().substring(idx);
                name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
                XmlElement xmlElementAnno = m.getAnnotation(XmlElement.class);
                addElement(schema, seq, beanInfo, new QName(namespace, name), isArray(type), xmlElementAnno);
            }
        }
    }
    // Create element in xsd:sequence for Exception.class
    if (Exception.class.isAssignableFrom(cls)) {
        addExceptionMessage(cls, schema, seq);
    }
    if (propertyOrder != null) {
        if (propertyOrder.length == seq.getItems().size()) {
            sortItems(seq, propertyOrder);
        } else if (propertyOrder.length > 1 || (propertyOrder.length == 1 && !propertyOrder[0].isEmpty())) {
            LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all schema elements :" + Arrays.toString(propertyOrder));
        }
    }
    if (xmlAccessorOrder != null && xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL) && propertyOrder == null) {
        sort(seq);
    }
    schemas.addCrossImports();
    part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION", Boolean.TRUE);
}
Also used : XmlAccessorOrder(javax.xml.bind.annotation.XmlAccessorOrder) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap) JAXBBeanInfo(org.apache.cxf.common.jaxb.JAXBBeanInfo) XmlElement(javax.xml.bind.annotation.XmlElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 5 with NamespaceMap

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

the class AbstractAegisTest method newXmlSchema.

protected XmlSchema newXmlSchema(String targetNamespace) {
    XmlSchema s = new XmlSchema();
    s.setTargetNamespace(targetNamespace);
    NamespaceMap xmlsNamespaceMap = new NamespaceMap();
    s.setNamespaceContext(xmlsNamespaceMap);
    // tns: is conventional, and besides we have unit tests that are hardcoded to it.
    xmlsNamespaceMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, targetNamespace);
    // ditto for xsd: instead of just namespace= for the schema schema.
    xmlsNamespaceMap.add("xsd", Constants.URI_2001_SCHEMA_XSD);
    return s;
}
Also used : XmlSchema(org.apache.ws.commons.schema.XmlSchema) NamespaceMap(org.apache.ws.commons.schema.utils.NamespaceMap)

Aggregations

NamespaceMap (org.apache.ws.commons.schema.utils.NamespaceMap)11 XmlSchema (org.apache.ws.commons.schema.XmlSchema)10 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)6 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)5 QName (javax.xml.namespace.QName)4 Field (java.lang.reflect.Field)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)2 XmlAccessorOrder (javax.xml.bind.annotation.XmlAccessorOrder)2 XmlElement (javax.xml.bind.annotation.XmlElement)2 XmlType (javax.xml.bind.annotation.XmlType)2 Message (org.apache.cxf.common.i18n.Message)2 JAXBBeanInfo (org.apache.cxf.common.jaxb.JAXBBeanInfo)2 SchemaCollection (org.apache.cxf.common.xmlschema.SchemaCollection)2 Fault (org.apache.cxf.interceptor.Fault)2