Search in sources :

Example 11 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project fabric8 by jboss-fuse.

the class Auditor method toJson.

protected String toJson(String eventType, MessageReference messageReference) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(Auditor.class.getClassLoader());
        CompiledTemplate template = getTemplate(eventType, messageReference);
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("event", eventType);
        vars.put("host", System.getProperty("runtime.id"));
        vars.put("timestamp", new Date());
        vars.put("message", messageReference.getMessage());
        vars.put("messageReference", messageReference);
        return TemplateRuntime.execute(template, context, vars).toString();
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Date(java.util.Date) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 12 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project fabric8 by jboss-fuse.

the class Auditor method getTemplate.

private CompiledTemplate getTemplate(String event, MessageReference message) {
    String source = getTemplateSource(event, message);
    CompiledTemplate template = templates.get(source);
    if (template == null) {
        template = TemplateCompiler.compileTemplate(source, context);
        templates.put(source, template);
    }
    return template;
}
Also used : CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 13 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project grafikon by jub77.

the class TextTemplateMvel method initialize.

private void initialize() {
    TemplateCompiler compiler = new TemplateCompiler(this.getTemplate());
    compiledTemplate = compiler.compile();
}
Also used : TemplateCompiler(org.mvel2.templates.TemplateCompiler)

Example 14 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project vertx-web by vert-x3.

the class MVELTemplateEngineImpl method render.

@Override
public void render(RoutingContext context, String templateDirectory, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
    try {
        templateFileName = templateDirectory + templateFileName;
        CompiledTemplate template = isCachingEnabled() ? cache.get(templateFileName) : null;
        if (template == null) {
            // real compile
            String loc = adjustLocation(templateFileName);
            String templateText = Utils.readFileToString(context.vertx(), loc);
            if (templateText == null) {
                throw new IllegalArgumentException("Cannot find template " + loc);
            }
            template = TemplateCompiler.compileTemplate(templateText);
            if (isCachingEnabled()) {
                cache.put(templateFileName, template);
            }
        }
        Map<String, RoutingContext> variables = new HashMap<>(1);
        variables.put("context", context);
        final VertxInternal vertxInternal = (VertxInternal) context.vertx();
        String directoryName = vertxInternal.resolveFile(templateFileName).getParent();
        handler.handle(Future.succeededFuture(Buffer.buffer((String) new TemplateRuntime(template.getTemplate(), null, template.getRoot(), directoryName).execute(new StringAppender(), variables, new ImmutableDefaultFactory()))));
    } catch (Exception ex) {
        handler.handle(Future.failedFuture(ex));
    }
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) VertxInternal(io.vertx.core.impl.VertxInternal) ImmutableDefaultFactory(org.mvel2.integration.impl.ImmutableDefaultFactory) HashMap(java.util.HashMap) StringAppender(org.mvel2.util.StringAppender) TemplateRuntime(org.mvel2.templates.TemplateRuntime) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 15 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project gfm_viewer by satyagraha.

the class TransformerDefault method transformMarkdownFile.

@Override
public void transformMarkdownFile(File mdFile, File htFile) throws IOException {
    String mdText = FileUtils.readFileToString(mdFile, UTF_8);
    String htText = transformMarkdownText(mdText);
    CompiledTemplate htmlTemplate = TemplateCompiler.compileTemplate(config.getHtmlTemplate());
    Map<String, Object> vars = new TreeMap<String, Object>();
    vars.put("title", htFile.toString());
    vars.put("content", htText);
    vars.put("cssText", config.getCssText());
    vars.put("cssUris", config.getCssUris());
    vars.put("jsText", config.getJsText());
    vars.put("jsUris", config.getJsUris());
    // LOGGER.fine("template vars: " + vars);
    String rendered = (String) TemplateRuntime.execute(htmlTemplate, vars);
    FileUtils.writeStringToFile(htFile, rendered, UTF_8);
}
Also used : TreeMap(java.util.TreeMap) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

CompiledTemplate (org.mvel2.templates.CompiledTemplate)32 HashMap (java.util.HashMap)8 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)6 Node (org.mvel2.templates.res.Node)5 InputStream (java.io.InputStream)4 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)4 TemplateRegistry (org.mvel2.templates.TemplateRegistry)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ParserContext (org.mvel2.ParserContext)3 TemplateCompiler (org.mvel2.templates.TemplateCompiler)3 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JavaRuleBuilderHelper (org.drools.mvel.java.JavaRuleBuilderHelper)2 ImmutableDefaultFactory (org.mvel2.integration.impl.ImmutableDefaultFactory)2 TemplateError (org.mvel2.templates.TemplateError)2 TemplateRuntime (org.mvel2.templates.TemplateRuntime)2 TestPluginNode (org.mvel2.tests.templates.tests.res.TestPluginNode)2 StringAppender (org.mvel2.util.StringAppender)2 VertxInternal (io.vertx.core.impl.VertxInternal)1