Search in sources :

Example 1 with PlatformLineWriter

use of groovy.io.PlatformLineWriter in project walkmod-core by walkmod.

the class DefaultTemplateVisitor method doPlainOutput.

public void doPlainOutput(String templateResult, VisitorContext context) throws Exception {
    WriterConfig writerConfig = context.getArchitectureConfig().getWriterConfig();
    ChainWriter chainWriter = writerConfig.getModelWriter();
    if (output == null) {
        String fileName = currentTemplate.getName();
        if (context.containsKey(AbstractWalker.ORIGINAL_FILE_KEY)) {
            log.debug("Original file path found");
            File originalFile = (File) context.get(AbstractWalker.ORIGINAL_FILE_KEY);
            String fullPath = originalFile.getPath();
            String readerPath = context.getArchitectureConfig().getReaderConfig().getPath();
            fileName = fullPath.substring(readerPath.length());
            if (fileName.startsWith(File.separator)) {
                fileName = fileName.substring(1);
            }
        } else {
            log.debug("working with the template name");
        }
        int pos = fileName.lastIndexOf(".");
        if (pos != -1) {
            log.debug("Removing the existing suffix");
            fileName = fileName.substring(0, pos);
        }
        log.warn("Setting a default output file! [" + fileName + ".result]");
        VisitorContext auxCtxt = new VisitorContext();
        File defaultOutputFile = new File(writerConfig.getPath(), fileName + "." + suffix);
        if (!defaultOutputFile.exists()) {
            log.info("++" + defaultOutputFile.getAbsolutePath());
            defaultOutputFile.getParentFile().mkdirs();
            defaultOutputFile.createNewFile();
        }
        auxCtxt.put(AbstractWalker.ORIGINAL_FILE_KEY, defaultOutputFile);
        chainWriter.write(templateResult, auxCtxt);
    } else {
        String outputFile = output;
        // validates if it is a template name to reduce
        // computation
        char[] chars = outputFile.toCharArray();
        boolean isGString = false;
        for (int i = 0; i < chars.length && !isGString; i++) {
            isGString = chars[i] == '$' || chars[i] == '<';
        }
        if (isGString) {
            GStringTemplateEngine engine = new GStringTemplateEngine();
            Template templateName = engine.createTemplate(output);
            StringWriter stringWriter = new StringWriter();
            Writer platformWriter = new PlatformLineWriter(stringWriter);
            templateName.make(context).writeTo(platformWriter);
            outputFile = platformWriter.toString();
        }
        File file = new File(outputFile);
        VisitorContext auxCtxt = new VisitorContext();
        auxCtxt.put(AbstractWalker.ORIGINAL_FILE_KEY, file);
        auxCtxt.put("append", Boolean.TRUE);
        if (!file.exists()) {
            log.info("++" + file.getAbsolutePath());
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        chainWriter.write(templateResult, auxCtxt);
    }
}
Also used : ChainWriter(org.walkmod.ChainWriter) VisitorContext(org.walkmod.walkers.VisitorContext) PlatformLineWriter(groovy.io.PlatformLineWriter) GStringTemplateEngine(groovy.text.GStringTemplateEngine) WriterConfig(org.walkmod.conf.entities.WriterConfig) Template(groovy.text.Template) StringWriter(java.io.StringWriter) File(java.io.File) StringWriter(java.io.StringWriter) PlatformLineWriter(groovy.io.PlatformLineWriter) Writer(java.io.Writer) ChainWriter(org.walkmod.ChainWriter)

Aggregations

PlatformLineWriter (groovy.io.PlatformLineWriter)1 GStringTemplateEngine (groovy.text.GStringTemplateEngine)1 Template (groovy.text.Template)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ChainWriter (org.walkmod.ChainWriter)1 WriterConfig (org.walkmod.conf.entities.WriterConfig)1 VisitorContext (org.walkmod.walkers.VisitorContext)1