Search in sources :

Example 1 with ChainWriter

use of org.walkmod.ChainWriter 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)

Example 2 with ChainWriter

use of org.walkmod.ChainWriter in project walkmod-core by walkmod.

the class DefaultChainWalkerAdapter method prepare.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void prepare() throws WalkModException {
    walker.setResource(getModel());
    walker.setRootNamespace(config.getRootNamespace());
    ChainWriter mw = ap.getChainWriter();
    mw.setExcludes(config.getChainConfig().getWriterConfig().getExcludes());
    mw.setIncludes(config.getChainConfig().getWriterConfig().getIncludes());
    walker.setWriter(ap.getChainWriter());
    walker.setChainConfig(config.getChainConfig());
    ChainConfig ac = config.getChainConfig();
    Object visitor;
    Configuration c = ac.getConfiguration();
    Parser parser = null;
    String parserType = config.getParserConfig().getType();
    if (parserType != null) {
        Object parserInstance = c.getBean(parserType, config.getParserConfig().getParameters());
        if (parserInstance != null) {
            if (parserInstance instanceof Parser) {
                parser = (Parser) parserInstance;
                walker.setParser(parser);
            } else {
                throw new WalkModException("The parser " + parserType + " must implement " + Parser.class.getName());
            }
        } else {
            throw new WalkModException("The parser " + parserType + " does not exist.");
        }
    }
    Collection<TransformationConfig> cfgs = getTransformationConfig();
    if (cfgs != null) {
        for (TransformationConfig config : cfgs) {
            setName(config.getName());
            visitor = config.getVisitorInstance();
            if (visitor == null || "".equals(config.getType())) {
                visitor = c.getBean(config.getType(), config.getParameters());
            }
            if (visitor instanceof ResourceModifier) {
                ((ResourceModifier) visitor).setResource(getModel());
            }
            if (visitor instanceof ParserAware) {
                ((ParserAware) visitor).setParser(parser);
            }
            if (visitor != null) {
                LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].walker = " + walker.getClass().getName());
                LOG.debug("setting chain[\"" + ac.getName() + "\"].transformation[\"" + getName() + "\"].visitor = " + visitor.getClass().getName());
                visitors.add(visitor);
            } else {
                walker = null;
                visitor = null;
                LOG.debug("Transformation[" + getName() + "] without walker and visitor");
            }
        }
    }
    walker.setVisitors(visitors);
    wi.init(this);
}
Also used : ChainWriter(org.walkmod.ChainWriter) ResourceModifier(org.walkmod.ResourceModifier) WalkModException(org.walkmod.exceptions.WalkModException) Configuration(org.walkmod.conf.entities.Configuration) TransformationConfig(org.walkmod.conf.entities.TransformationConfig) ParserAware(org.walkmod.walkers.ParserAware) ChainConfig(org.walkmod.conf.entities.ChainConfig) Parser(org.walkmod.walkers.Parser)

Example 3 with ChainWriter

use of org.walkmod.ChainWriter in project walkmod-core by walkmod.

the class DefaultChainAdapter method prepare.

@Override
public void prepare() throws WalkModException {
    setName(ac.getName());
    ReaderConfig readerConfig = ac.getReaderConfig();
    WriterConfig writerConfig = ac.getWriterConfig();
    String modelName = readerConfig.getPath();
    String modelType = readerConfig.getType();
    ChainReader reader = readerConfig.getModelReader();
    if (reader == null) {
        try {
            reader = (ChainReader) ac.getConfiguration().getBean(modelType, readerConfig.getParameters());
        } catch (Exception e2) {
            throw new WalkModException("The model " + modelName + ", whose type is " + modelType + "in the architecture " + getName() + " cannot be loaded ", e2);
        }
    }
    readerConfig.setModelReader(reader);
    reader.setPath(readerConfig.getPath());
    reader.setExcludes(readerConfig.getExcludes());
    reader.setIncludes(readerConfig.getIncludes());
    try {
        setResource(reader.read());
        LOG.debug("Model " + modelName + " loaded");
    } catch (Exception e2) {
        throw new WalkModException("The model " + modelName + ", whose type is " + modelType + "in the architecture " + getName() + " cannot be read ", e2);
    }
    WalkerConfig wc = ac.getWalkerConfig();
    ChainWalkerAdapter wa = new DefaultChainWalkerAdapter();
    setWalkerAdapter(wa);
    ChainWalker walker = wc.getWalker();
    if (walker == null) {
        walker = (ChainWalker) ac.getConfiguration().getBean(wc.getType(), wc.getParams());
    }
    wc.setWalker(walker);
    wa.setWalker(walker);
    wa.setWalkerConfig(wc);
    wa.setArchitectureProxy(this);
    wa.setWalkerInvocation(new DefaultChainWalkerInvocation());
    ChainWriter writer = writerConfig.getModelWriter();
    if (writer == null) {
        try {
            writer = (ChainWriter) ac.getConfiguration().getBean(writerConfig.getType(), writerConfig.getParams());
        } catch (Exception e2) {
            throw new WalkModException("The writer " + ", whose type is " + writerConfig.getType() + "in the architecture " + getName() + " cannot be read ", e2);
        }
    }
    writerConfig.setModelWriter(writer);
    writer.setPath(writerConfig.getPath());
    setChainWriter(writer);
    wa.prepare();
    ai.init(this);
}
Also used : ChainWalker(org.walkmod.ChainWalker) ChainWriter(org.walkmod.ChainWriter) WalkModException(org.walkmod.exceptions.WalkModException) ChainReader(org.walkmod.ChainReader) WalkerConfig(org.walkmod.conf.entities.WalkerConfig) ReaderConfig(org.walkmod.conf.entities.ReaderConfig) ChainWalkerAdapter(org.walkmod.ChainWalkerAdapter) WalkModException(org.walkmod.exceptions.WalkModException) WriterConfig(org.walkmod.conf.entities.WriterConfig)

Aggregations

ChainWriter (org.walkmod.ChainWriter)3 WriterConfig (org.walkmod.conf.entities.WriterConfig)2 WalkModException (org.walkmod.exceptions.WalkModException)2 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 ChainReader (org.walkmod.ChainReader)1 ChainWalker (org.walkmod.ChainWalker)1 ChainWalkerAdapter (org.walkmod.ChainWalkerAdapter)1 ResourceModifier (org.walkmod.ResourceModifier)1 ChainConfig (org.walkmod.conf.entities.ChainConfig)1 Configuration (org.walkmod.conf.entities.Configuration)1 ReaderConfig (org.walkmod.conf.entities.ReaderConfig)1 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)1 WalkerConfig (org.walkmod.conf.entities.WalkerConfig)1 Parser (org.walkmod.walkers.Parser)1 ParserAware (org.walkmod.walkers.ParserAware)1