Search in sources :

Example 11 with Types

use of javax.wsdl.Types in project cxf by apache.

the class IDLToWSDLProcessor method addTypeMapSchemaImports.

public void addTypeMapSchemaImports(Definition def, WSDLASTVisitor visitor) {
    List<CorbaType> types = visitor.getTypeMap().getStructOrExceptionOrUnion();
    ModuleToNSMapper mapper = visitor.getModuleToNSMapper();
    WSDLSchemaManager manager = visitor.getManager();
    Collection<String> namespaces = CastUtils.cast(def.getNamespaces().values());
    Set<Map.Entry<String, String>> userModuleMappings = mapper.getUserMapping().entrySet();
    if (types != null) {
        for (int i = 0; i < types.size(); i++) {
            CorbaType type = types.get(i);
            QName schemaType = type.getType();
            if (schemaType != null) {
                String typeNamespace = schemaType.getNamespaceURI();
                try {
                    // a remote location.
                    if (!namespaces.contains(typeNamespace) && typeNamespace.equals(ReferenceConstants.WSADDRESSING_NAMESPACE)) {
                        // build up the ws-addressing schema import
                        Schema wsdlSchema = (Schema) def.getExtensionRegistry().createExtension(Types.class, new QName(Constants.URI_2001_SCHEMA_XSD, "schema"));
                        SchemaImport schemaimport = wsdlSchema.createImport();
                        schemaimport.setNamespaceURI(ReferenceConstants.WSADDRESSING_NAMESPACE);
                        schemaimport.setSchemaLocationURI(ReferenceConstants.WSADDRESSING_LOCATION);
                        wsdlSchema.addImport(schemaimport);
                        // add the import and the prefix to the definition
                        def.getTypes().addExtensibilityElement(wsdlSchema);
                        CastUtils.cast(def.getNamespaces(), String.class, String.class).put(ReferenceConstants.WSADDRESSING_PREFIX, typeNamespace);
                    } else if (!namespaces.contains(typeNamespace)) {
                        String prefix = getModulePrefixForNamespace(userModuleMappings, mapper, typeNamespace);
                        // prefix = mapper.mapNSToPrefix(typeNamespace);
                        XmlSchema schema = manager.getXmlSchema(typeNamespace);
                        // TODO: REVISIT - Is this the only way we can create the file name for the
                        // imported schema?
                        String importFile = visitor.getOutputDir() + System.getProperty("file.separator") + prefix + ".xsd";
                        manager.addWSDLSchemaImport(def, typeNamespace, importFile);
                        manager.getImportedXmlSchemas().put(new File(importFile), schema);
                        CastUtils.cast(def.getNamespaces(), String.class, String.class).put(prefix, typeNamespace);
                    }
                } catch (Exception ex) {
                    throw new ToolException("Unable to add schema import for namespace" + typeNamespace);
                }
            }
        }
    }
}
Also used : Types(javax.wsdl.Types) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) QName(javax.xml.namespace.QName) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ToolException(org.apache.cxf.tools.common.ToolException) Entry(java.util.Map.Entry) CorbaType(org.apache.cxf.binding.corba.wsdl.CorbaType) XmlSchema(org.apache.ws.commons.schema.XmlSchema) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File)

Example 12 with Types

use of javax.wsdl.Types in project cxf by apache.

the class ServiceWSDLBuilderTest method testXsdImportMultipleSchemas.

@Test
public void testXsdImportMultipleSchemas() throws Exception {
    setupWSDL(WSDL_XSD_IMPORT_PATH, true);
    Types types = newDef.getTypes();
    assertNotNull(types);
    Collection<ExtensibilityElement> schemas = CastUtils.cast(types.getExtensibilityElements(), ExtensibilityElement.class);
    assertEquals(1, schemas.size());
    Schema schema = (Schema) schemas.iterator().next();
    assertEquals(1, schema.getImports().values().size());
    SchemaImport serviceTypesSchemaImport = getImport(schema.getImports(), "http://apache.org/hello_world_soap_http/servicetypes");
    Schema serviceTypesSchema = serviceTypesSchemaImport.getReferencedSchema();
    assertEquals(1, serviceTypesSchema.getImports().values().size());
    SchemaImport typesSchemaImport = getImport(serviceTypesSchema.getImports(), "http://apache.org/hello_world_soap_http/types");
    Schema typesSchema = typesSchemaImport.getReferencedSchema();
    Document doc = typesSchema.getElement().getOwnerDocument();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(outputStream, "utf-8");
    StaxUtils.writeNode(doc, writer, true);
    writer.close();
    // this is a test to make sure any embedded namespaces are properly included
    String savedSchema = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
    assertTrue(savedSchema.contains("http://www.w3.org/2005/05/xmlmime"));
    SchemaImport types2SchemaImport = getImport(typesSchema.getImports(), "http://apache.org/hello_world_soap_http/types2");
    Schema types2Schema = types2SchemaImport.getReferencedSchema();
    assertNotNull(types2Schema);
}
Also used : Types(javax.wsdl.Types) SchemaImport(javax.wsdl.extensions.schema.SchemaImport) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Test(org.junit.Test)

Example 13 with Types

use of javax.wsdl.Types in project cxf by apache.

the class ServiceWSDLBuilderTest method testSchemas.

@Test
public void testSchemas() throws Exception {
    setupWSDL(WSDL_PATH);
    Types types = newDef.getTypes();
    assertNotNull(types);
    Collection<ExtensibilityElement> schemas = CastUtils.cast(types.getExtensibilityElements(), ExtensibilityElement.class);
    assertEquals(1, schemas.size());
    XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
    Element schemaElem = ((Schema) schemas.iterator().next()).getElement();
    XmlSchema newSchema = schemaCollection.read(schemaElem);
    assertEquals("http://apache.org/hello_world_soap_http/types", newSchema.getTargetNamespace());
}
Also used : Types(javax.wsdl.Types) XmlSchema(org.apache.ws.commons.schema.XmlSchema) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) XmlSchema(org.apache.ws.commons.schema.XmlSchema) Schema(javax.wsdl.extensions.schema.Schema) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Test(org.junit.Test)

Example 14 with Types

use of javax.wsdl.Types 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();
                synchronized (elem.getOwnerDocument()) {
                    if ("schema".equals(elem.getLocalName())) {
                        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;
                    String suffix = ".xsd";
                    String baseURI = def.getDocumentBaseURI();
                    if (baseURI != null && baseURI.regionMatches(true, baseURI.length() - suffix.length(), suffix, 0, suffix.length()) && def.getTargetNamespace() == null && obj instanceof Schema && baseURI.equals(((Schema) obj).getDocumentBaseURI())) {
                        systemId = baseURI;
                    }
                    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 15 with Types

use of javax.wsdl.Types 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)

Aggregations

Types (javax.wsdl.Types)17 Schema (javax.wsdl.extensions.schema.Schema)14 XmlSchema (org.apache.ws.commons.schema.XmlSchema)7 Element (org.w3c.dom.Element)7 ExtensibilityElement (javax.wsdl.extensions.ExtensibilityElement)6 SchemaImport (javax.wsdl.extensions.schema.SchemaImport)6 QName (javax.xml.namespace.QName)6 List (java.util.List)3 Import (javax.wsdl.Import)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Definition (javax.wsdl.Definition)2 Message (javax.wsdl.Message)2 WSDLException (javax.wsdl.WSDLException)2 UnknownExtensibilityElement (javax.wsdl.extensions.UnknownExtensibilityElement)2 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)2 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1