Search in sources :

Example 1 with ParseException

use of org.walkmod.walkers.ParseException in project walkmod-core by walkmod.

the class DefaultTemplateVisitor method visit.

public synchronized void visit(Object node, VisitorContext context) throws Exception {
    if (rootLabel == null) {
        setRootLabel("cu");
    }
    if (propertiesFile == null) {
        setProperties("template.properties");
    }
    context.put(getRootLabel(), node);
    if (templateEngine == null) {
        Object bean = context.getBean("org.walkmod.templates.groovy.GroovyTemplateEngine", null);
        if (bean != null && bean instanceof TemplateEngine) {
            templateEngine = (TemplateEngine) bean;
            log.info("Applying [groovy] as a default template engine");
        } else {
            throw new WalkModException("Template engine not found");
        }
    }
    templateEngine.initialize(context, node);
    if (templateFiles != null && templates != null && templateFiles.size() == templates.size()) {
        for (File template : templateFiles) {
            String templateResult = templateEngine.applyTemplate(template, propertiesFile);
            Object producedNode = null;
            currentTemplate = template;
            if (parser != null) {
                try {
                    producedNode = parser.parse(templateResult, true);
                } catch (ParseException e) {
                    log.warn("Error parsing the template " + template.getAbsolutePath() + ". Dumping contents..");
                    doPlainOutput(templateResult, context);
                }
            } else {
                doPlainOutput(templateResult, context);
            }
            if (producedNode != null) {
                log.debug("Template successfuly parsed");
                context.addResultNode(producedNode);
            }
        }
    } else {
        if (!missingTemplates.isEmpty()) {
            for (String missing : missingTemplates) {
                log.error("The template " + missing + " is missing");
            }
        }
        throw new WalkModException("There are missing or unexitent templates.");
    }
}
Also used : GStringTemplateEngine(groovy.text.GStringTemplateEngine) WalkModException(org.walkmod.exceptions.WalkModException) ParseException(org.walkmod.walkers.ParseException) File(java.io.File)

Aggregations

GStringTemplateEngine (groovy.text.GStringTemplateEngine)1 File (java.io.File)1 WalkModException (org.walkmod.exceptions.WalkModException)1 ParseException (org.walkmod.walkers.ParseException)1