Search in sources :

Example 26 with SchemaCollection

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

the class WSDLToJavaContainer method generateLocalWSDL.

@SuppressWarnings("unchecked")
private void generateLocalWSDL(ToolContext context) {
    String outputdir = (String) context.get(ToolConstants.CFG_CLASSDIR);
    File wsdlFile = new File(outputdir, (String) context.get(ToolConstants.CFG_WSDLLOCATION));
    Definition def = context.get(Definition.class);
    try {
        // get imported schemas
        int xsdCount = 0;
        SchemaCollection schemas = (SchemaCollection) context.get(ToolConstants.XML_SCHEMA_COLLECTION);
        Map<String, String> sourceMap = new HashMap<>();
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = "schema" + (++xsdCount) + ".xsd";
                File sourceFile = new File(imp.getSourceURI());
                sourceMap.put(createSchemaFileKey(imp.getTargetNamespace(), sourceFile.getName()), schemaFileName);
            }
        }
        // get imported wsdls
        int wsdlImportCount = 0;
        List<Definition> defs = (List<Definition>) context.get(ToolConstants.IMPORTED_DEFINITION);
        Map<String, String> importWSDLMap = new HashMap<>();
        for (Definition importDef : defs) {
            File importedWsdlFile;
            if (!StringUtils.isEmpty(importDef.getDocumentBaseURI())) {
                importedWsdlFile = new File(importDef.getDocumentBaseURI());
            } else {
                importedWsdlFile = new File(importDef.getQName().getLocalPart() + ".wsdl");
            }
            if (!FileUtils.isValidFileName(importedWsdlFile.getName())) {
                importedWsdlFile = new File("import" + (++wsdlImportCount) + ".wsdl");
            }
            importWSDLMap.put(importDef.getTargetNamespace(), importedWsdlFile.getName());
        }
        final OutputStreamCreator outputStreamCreator;
        if (context.get(OutputStreamCreator.class) != null) {
            outputStreamCreator = context.get(OutputStreamCreator.class);
        } else {
            outputStreamCreator = new OutputStreamCreator();
            context.put(OutputStreamCreator.class, outputStreamCreator);
        }
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                File sourceFile = new File(imp.getSourceURI());
                String schemaKey = createSchemaFileKey(imp.getTargetNamespace(), sourceFile.getName());
                String schemaFileName = sourceMap.get(schemaKey);
                File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
                Element el = imp.getSchemaDocument().getDocumentElement();
                updateImports(el, sourceMap);
                updateIncludes(el, sourceMap);
                try (Writer os = new FileWriterUtil(impfile.getParent(), outputStreamCreator).getWriter(impfile, StandardCharsets.UTF_8.name())) {
                    StaxUtils.writeTo(el, os, 2);
                }
            }
        }
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        // change the import location in wsdl file
        try (OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()))) {
            LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
            wsdlWriter.writeWSDL(def, bout);
            Element defEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
            List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(defEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
            for (Element xsdEle : xsdElements) {
                updateImports(xsdEle, sourceMap);
                updateIncludes(xsdEle, sourceMap);
            }
            updateWSDLImports(defEle, importWSDLMap);
            StaxUtils.writeTo(defEle, wsdloutput);
        }
        for (Definition importDef : defs) {
            File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
            try (OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()))) {
                LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
                wsdlWriter.writeWSDL(importDef, bout);
                Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
                List<Element> xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
                for (Element xsdEle : xsdElements) {
                    updateImports(xsdEle, sourceMap);
                    updateIncludes(xsdEle, sourceMap);
                }
                updateWSDLImports(importEle, importWSDLMap);
                StaxUtils.writeTo(importEle, wsdlOs);
            }
        }
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "FAILED_TO_GEN_LOCAL_WSDL", ex);
        Message msg = new Message("FAILED_TO_GEN_LOCAL_WSDL", LOG);
        throw new ToolException(msg, ex);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) Element(org.w3c.dom.Element) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) BadUsageException(org.apache.cxf.tools.common.toolspec.parser.BadUsageException) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) XmlSchema(org.apache.ws.commons.schema.XmlSchema) List(java.util.List) ArrayList(java.util.ArrayList) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) ToolException(org.apache.cxf.tools.common.ToolException) SchemaCollection(org.apache.cxf.common.xmlschema.SchemaCollection) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Writer(java.io.Writer) WSDLWriter(javax.wsdl.xml.WSDLWriter)

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