Search in sources :

Example 6 with FileWriterUtil

use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.

the class WSDL11Generator method generate.

public Definition generate(final File dir) {
    File file = getOutputBase();
    if (file == null && dir != null) {
        if (dir.isDirectory()) {
            file = new File(dir, getServiceModel().getName().getLocalPart() + ".wsdl");
        } else {
            file = dir;
        }
    } else if (dir == null) {
        file = new File(getServiceModel().getName().getLocalPart() + ".wsdl");
    }
    File outputdir = createOutputDir(file);
    Definition def = null;
    try {
        Writer os = new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, StandardCharsets.UTF_8.name());
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        ServiceWSDLBuilder builder = new ServiceWSDLBuilder(getBus(), getServiceModel());
        builder.setUseSchemaImports(this.allowImports());
        String name = file.getName();
        if (name.endsWith(".wsdl")) {
            name = name.substring(0, name.lastIndexOf(".wsdl"));
        }
        builder.setBaseFileName(name);
        Map<String, SchemaInfo> imports = new HashMap<>();
        def = builder.build(imports);
        wsdlWriter.writeWSDL(def, os);
        os.close();
        if (def.getImports().size() > 0) {
            for (Import wsdlImport : WSDLDefinitionBuilder.getImports(def)) {
                Definition wsdlDef = wsdlImport.getDefinition();
                File wsdlFile = null;
                if (!StringUtils.isEmpty(wsdlImport.getLocationURI())) {
                    wsdlFile = new File(outputdir, wsdlImport.getLocationURI());
                } else {
                    wsdlFile = new File(outputdir, wsdlDef.getQName().getLocalPart() + ".wsdl");
                }
                try (OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()))) {
                    wsdlWriter.writeWSDL(wsdlDef, wsdlOs);
                }
            }
        }
        for (Map.Entry<String, SchemaInfo> imp : imports.entrySet()) {
            File impfile = new File(file.getParentFile(), imp.getKey());
            Element el = imp.getValue().getElement();
            updateImports(el, imports);
            FileWriterUtil fileWriterUtil = new FileWriterUtil(impfile.getParent(), getToolContext().get(OutputStreamCreator.class));
            os = fileWriterUtil.getWriter(impfile, StandardCharsets.UTF_8.name());
            StaxUtils.writeTo(el, os, 2);
            os.close();
        }
        customizing(outputdir, name, imports.keySet());
    } catch (WSDLException wex) {
        wex.printStackTrace();
    } catch (FileNotFoundException fnfe) {
        throw new ToolException("Output file " + file + " not found", fnfe);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return def;
}
Also used : Import(javax.wsdl.Import) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) HashMap(java.util.HashMap) WSDLException(javax.wsdl.WSDLException) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) Definition(javax.wsdl.Definition) FileNotFoundException(java.io.FileNotFoundException) WSDLWriter(javax.wsdl.xml.WSDLWriter) IOException(java.io.IOException) ServiceWSDLBuilder(org.apache.cxf.wsdl11.ServiceWSDLBuilder) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) ToolException(org.apache.cxf.tools.common.ToolException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) WSDLWriter(javax.wsdl.xml.WSDLWriter) Writer(java.io.Writer) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 7 with FileWriterUtil

use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.

the class VelocityGenerator method parseOutputName.

public File parseOutputName(String packageName, String filename, String ext) throws ToolException {
    FileUtils.mkDir(new File(this.baseDir));
    FileWriterUtil fw = new FileWriterUtil(this.baseDir, null);
    try {
        return fw.getFileToWrite(packageName, filename + ext);
    } catch (IOException ioe) {
        Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, packageName + "." + filename + ext);
        throw new ToolException(msg, ioe);
    }
}
Also used : Message(org.apache.cxf.common.i18n.Message) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) IOException(java.io.IOException) File(java.io.File)

Example 8 with FileWriterUtil

use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.

the class WSDLUtils method writeWSDL.

public static void writeWSDL(Definition def, String outputdir, String wsdlOutput) throws WSDLException, IOException {
    FileWriterUtil fw = new FileWriterUtil(outputdir, null);
    Writer outputWriter = fw.getWriter("", wsdlOutput);
    writeWSDL(def, outputWriter);
}
Also used : FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) Writer(java.io.Writer) WSDLWriter(javax.wsdl.xml.WSDLWriter)

Example 9 with FileWriterUtil

use of org.apache.cxf.tools.util.FileWriterUtil 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";
                sourceMap.put(imp.getTargetNamespace(), 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 = null;
            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());
        }
        OutputStreamCreator outputStreamCreator = null;
        if (context.get(OutputStreamCreator.class) != null) {
            outputStreamCreator = context.get(OutputStreamCreator.class);
        } else {
            outputStreamCreator = new OutputStreamCreator();
            context.put(OutputStreamCreator.class, outputStreamCreator);
        }
        Writer os = null;
        for (XmlSchema imp : schemas.getXmlSchemas()) {
            if (imp.getSourceURI() != null && !imp.getSourceURI().contains(".wsdl#")) {
                String schemaFileName = sourceMap.get(imp.getTargetNamespace());
                File impfile = new File(wsdlFile.getParentFile(), schemaFileName);
                Element el = imp.getSchemaDocument().getDocumentElement();
                updateImports(el, sourceMap);
                os = new FileWriterUtil(impfile.getParent(), context.get(OutputStreamCreator.class)).getWriter(impfile, StandardCharsets.UTF_8.name());
                StaxUtils.writeTo(el, os, 2);
                os.close();
            }
        }
        // change the import location in wsdl file
        OutputStream wsdloutput = new BufferedOutputStream(Files.newOutputStream(wsdlFile.toPath()));
        WSDLWriter wsdlWriter = WSDLFactory.newInstance().newWSDLWriter();
        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);
        }
        updateWSDLImports(defEle, importWSDLMap);
        StaxUtils.writeTo(defEle, wsdloutput);
        wsdloutput.close();
        for (Definition importDef : defs) {
            File importWsdlFile = new File(outputdir, importWSDLMap.get(importDef.getTargetNamespace()));
            OutputStream wsdlOs = new BufferedOutputStream(Files.newOutputStream(importWsdlFile.toPath()));
            bout = new LoadingByteArrayOutputStream();
            wsdlWriter.writeWSDL(importDef, bout);
            Element importEle = StaxUtils.read(bout.createInputStream()).getDocumentElement();
            xsdElements = DOMUtils.findAllElementsByTagNameNS(importEle, WSDLConstants.NS_SCHEMA_XSD, "schema");
            for (Element xsdEle : xsdElements) {
                updateImports(xsdEle, sourceMap);
            }
            updateWSDLImports(importEle, importWSDLMap);
            StaxUtils.writeTo(importEle, wsdlOs);
            wsdlOs.close();
        }
    } 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)

Example 10 with FileWriterUtil

use of org.apache.cxf.tools.util.FileWriterUtil in project cxf by apache.

the class AbstractGeneratorTest method testKeep.

@Test
public void testKeep() throws Exception {
    gen = new DummyGenerator();
    util = new FileWriterUtil(output.toString(), null);
    context = new ToolContext();
    context.put(ToolConstants.CFG_OUTPUTDIR, output.toString());
    gen.setEnvironment(context);
    Writer writer = util.getWriter(packageName, className + ".java");
    writer.write("hello world");
    writer.flush();
    writer.close();
    context.put(ToolConstants.CFG_GEN_NEW_ONLY, "keep");
    assertNull(gen.parseOutputName(packageName, className));
}
Also used : FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) ToolContext(org.apache.cxf.tools.common.ToolContext) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

FileWriterUtil (org.apache.cxf.tools.util.FileWriterUtil)11 Writer (java.io.Writer)8 IOException (java.io.IOException)6 File (java.io.File)5 Message (org.apache.cxf.common.i18n.Message)5 ToolException (org.apache.cxf.tools.common.ToolException)5 WSDLWriter (javax.wsdl.xml.WSDLWriter)4 OutputStreamCreator (org.apache.cxf.tools.util.OutputStreamCreator)4 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Definition (javax.wsdl.Definition)2 ToolContext (org.apache.cxf.tools.common.ToolContext)2 VelocityGenerator (org.apache.cxf.tools.common.VelocityGenerator)2 Test (org.junit.Test)2 Element (org.w3c.dom.Element)2 FileNotFoundException (java.io.FileNotFoundException)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1