Search in sources :

Example 16 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project mvel by mikebrock.

the class CompiledNamedIncludeNode method eval.

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
    if (cPreExpression != null) {
        MVEL.executeExpression(cPreExpression, ctx, factory);
    }
    if (next != null) {
        String namedTemplate = MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class);
        CompiledTemplate ct = runtime.getNamedTemplateRegistry().getNamedTemplate(namedTemplate);
        if (ct == null)
            throw new TemplateError("named template does not exist: " + namedTemplate);
        return next.eval(runtime, appender.append(String.valueOf(TemplateRuntime.execute(ct, ctx, factory, runtime.getNamedTemplateRegistry()))), ctx, factory);
    // return next.eval(runtime,
    // appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory))), ctx, factory);
    } else {
        return appender.append(String.valueOf(TemplateRuntime.execute(runtime.getNamedTemplateRegistry().getNamedTemplate(MVEL.executeExpression(cIncludeExpression, ctx, factory, String.class)), ctx, factory, runtime.getNamedTemplateRegistry())));
    }
}
Also used : TemplateError(org.mvel2.templates.TemplateError) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 17 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project mvel by mikebrock.

the class TemplateTests method testMVEL244.

public void testMVEL244() {
    Foo244 foo = new Foo244("plop");
    String template = "@foreach{val : foo.liste[0].liste} plop @end{}";
    CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("foo", foo);
    System.out.println(TemplateRuntime.execute(compiledTemplate, new ParserContext(), new MapVariableResolverFactory(model)));
}
Also used : MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) ParserContext(org.mvel2.ParserContext) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 18 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project drools by kiegroup.

the class AccumulateTemplateTest method getRuleTemplateRegistry.

private TemplateRegistry getRuleTemplateRegistry() {
    TemplateRegistry ruleRegistry = new SimpleTemplateRegistry();
    CompiledTemplate compiled = TemplateCompiler.compileTemplate(JavaRuleBuilderHelper.class.getResourceAsStream("javaRule.mvel"), (Map<String, Class<? extends Node>>) null);
    TemplateRuntime.execute(compiled, null, ruleRegistry);
    return ruleRegistry;
}
Also used : TemplateRegistry(org.mvel2.templates.TemplateRegistry) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) Node(org.mvel2.templates.res.Node) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) JavaRuleBuilderHelper(org.drools.mvel.java.JavaRuleBuilderHelper) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 19 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project drools by kiegroup.

the class DefaultGenerator method getTemplate.

private CompiledTemplate getTemplate(String templateName) throws IOException {
    CompiledTemplate contents;
    if (!registry.contains(templateName)) {
        RuleTemplate template = ruleTemplates.get(templateName);
        contents = TemplateCompiler.compileTemplate(template.getContents());
        registry.addNamedTemplate(templateName, contents);
    } else {
        contents = registry.getNamedTemplate(templateName);
    }
    return contents;
}
Also used : CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 20 with CompiledTemplate

use of org.mvel2.templates.CompiledTemplate in project mvel by mvel.

the class MVELIncludeTest method testEvalFile1.

/**
 * evalFile with sub template in utf-8 encoding
 *
 * @throws IOException
 */
public void testEvalFile1() throws IOException {
    final CompiledTemplate template = TemplateCompiler.compileTemplate(file);
    final String tr = (String) new TemplateRuntime(template.getTemplate(), null, template.getRoot(), "./samples/scripts/").execute(new StringAppender(), new HashMap<String, String>(), new ImmutableDefaultFactory());
    assertEquals("Hello mister Gaël Périé", tr);
}
Also used : 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)

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