Search in sources :

Example 1 with FileWriterUtil

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

the class AbstractWSDLToProcessor method getOutputWriter.

protected Writer getOutputWriter(String newNameExt) throws ToolException {
    Writer writer = null;
    String newName = null;
    String outputDir;
    if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) {
        newName = (String) env.get(ToolConstants.CFG_OUTPUTFILE);
    } else {
        String oldName = (String) env.get(ToolConstants.CFG_WSDLURL);
        int position = oldName.lastIndexOf("/");
        if (position < 0) {
            position = oldName.lastIndexOf("\\");
        }
        if (position >= 0) {
            oldName = oldName.substring(position + 1, oldName.length());
        }
        if (oldName.toLowerCase().indexOf(WSDL_FILE_NAME_EXT) >= 0) {
            newName = oldName.substring(0, oldName.length() - 5) + newNameExt + WSDL_FILE_NAME_EXT;
        } else {
            newName = oldName + newNameExt;
        }
    }
    if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) {
        outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
        if (!("/".equals(outputDir.substring(outputDir.length() - 1)) || "\\".equals(outputDir.substring(outputDir.length() - 1)))) {
            outputDir = outputDir + "/";
        }
    } else {
        outputDir = "./";
    }
    FileWriterUtil fw = new FileWriterUtil(outputDir, env.get(OutputStreamCreator.class));
    try {
        writer = fw.getWriter("", newName);
    } catch (IOException ioe) {
        Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, env.get(ToolConstants.CFG_OUTPUTDIR) + System.getProperty("file.seperator") + newName);
        throw new ToolException(msg, ioe);
    }
    return writer;
}
Also used : Message(org.apache.cxf.common.i18n.Message) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) OutputStreamCreator(org.apache.cxf.tools.util.OutputStreamCreator) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) Writer(java.io.Writer)

Example 2 with FileWriterUtil

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

the class AbstractGenerator method parseOutputName.

protected Writer parseOutputName(String packageName, String filename, String ext) throws ToolException {
    FileWriterUtil fw = null;
    Writer writer = null;
    if (wantToKeep() && isCollision(packageName, filename, ext)) {
        Message msg = new Message("SKIP_GEN", LOG, packageName + "." + filename + ext);
        LOG.log(Level.INFO, msg.toString());
        return null;
    }
    fw = new FileWriterUtil(getOutputDir(), env.get(OutputStreamCreator.class));
    try {
        if (".java".equals(ext)) {
            writer = fw.getWriter(packageName, filename + ext, (String) getEnvironment().get(ToolConstants.CFG_ENCODING));
        } else {
            writer = fw.getWriter(packageName, filename + ext);
        }
    } catch (IOException ioe) {
        Message msg = new Message("FAIL_TO_WRITE_FILE", LOG, packageName + "." + filename + ext);
        throw new ToolException(msg, ioe);
    }
    return writer;
}
Also used : Message(org.apache.cxf.common.i18n.Message) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) IOException(java.io.IOException) ToolException(org.apache.cxf.tools.common.ToolException) Writer(java.io.Writer)

Example 3 with FileWriterUtil

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

the class AbstractGeneratorTest method testOverwrite.

@Test
public void testOverwrite() 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();
    assertNotNull(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)

Example 4 with FileWriterUtil

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

the class BeanGenerator method generateAndCompile.

public void generateAndCompile(Collection<JavaClass> wrapperClasses, File dir) {
    VelocityGenerator generator = new VelocityGenerator(false);
    generator.setBaseDir(dir.toString());
    List<File> generatedFiles = new ArrayList<>();
    try {
        for (JavaClass wrapperClass : wrapperClasses) {
            generator.setCommonAttributes();
            generator.setAttributes("bean", wrapperClass);
            File file = generator.parseOutputName(wrapperClass.getPackageName(), wrapperClass.getName());
            generatedFiles.add(file);
            generator.doWrite(TEMPLATE, new FileWriterUtil(file.getParent(), getOutputStreamCreator()).getWriter(file, (String) getToolContext().get(ToolConstants.CFG_ENCODING)));
            generator.clearAttributes();
        }
        // compile the classes
        Compiler compiler = new Compiler();
        compiler.setOutputDir(compileToDir);
        List<String> files = new ArrayList<>(generatedFiles.size());
        for (File file : generatedFiles) {
            files.add(file.getAbsolutePath());
        }
        compiler.compileFiles(files.toArray(new String[files.size()]));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Compiler(org.apache.cxf.common.util.Compiler) VelocityGenerator(org.apache.cxf.tools.common.VelocityGenerator) JavaClass(org.apache.cxf.tools.common.model.JavaClass) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) ArrayList(java.util.ArrayList) File(java.io.File)

Example 5 with FileWriterUtil

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

the class DateTypeCustomGenerator method generate.

public File generate(File outputdir) {
    Class<?> dateType = getDateType();
    File xjb = getJAXBCustFile(outputdir);
    if (dateType != null) {
        VelocityGenerator generator = new VelocityGenerator(false);
        generator.setCommonAttributes();
        generator.setAttributes("parseMethod", getAdapterMethod(dateType, ".parseDateTime"));
        generator.setAttributes("printMethod", getAdapterMethod(dateType, ".printDateTime"));
        generator.setAttributes("datetype", dateType.getName());
        if (allowImports()) {
            if (schemaFiles.isEmpty()) {
                return null;
            }
            generator.setAttributes("schemaFiles", schemaFiles);
        } else {
            generator.setAttributes("wsdlName", wsdlName);
            List<String> ns = getSchemaNamespaces();
            if (ns.isEmpty()) {
                return null;
            }
            generator.setAttributes("targetNamespaces", ns);
        }
        try {
            generator.doWrite(getTemplate(), new FileWriterUtil(xjb.getParent(), getOutputStreamCreator()).getWriter(xjb, StandardCharsets.UTF_8.name()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        generator.clearAttributes();
    }
    return xjb;
}
Also used : VelocityGenerator(org.apache.cxf.tools.common.VelocityGenerator) FileWriterUtil(org.apache.cxf.tools.util.FileWriterUtil) File(java.io.File)

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