PageRenderTime 36ms CodeModel.GetById 28ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/main/java/com/google/sitebricks/compiler/template/MvelTemplateCompiler.java

http://github.com/dhanji/sitebricks
Java | 37 lines | 26 code | 7 blank | 4 comment | 0 complexity | 0e7d12dd2f16fbe683e1854d1ab8c9d0 MD5 | raw file
 1package com.google.sitebricks.compiler.template;
 2
 3import com.google.common.collect.ImmutableSet;
 4import com.google.sitebricks.Renderable;
 5import com.google.sitebricks.Respond;
 6import com.google.sitebricks.Template;
 7import com.google.sitebricks.compiler.TemplateCompiler;
 8
 9import org.mvel2.templates.CompiledTemplate;
10import org.mvel2.templates.TemplateRuntime;
11
12import java.util.HashMap;
13import java.util.Set;
14
15/**
16 * Creates renderables, given an MVEL template page.
17 */
18public class MvelTemplateCompiler implements TemplateCompiler {
19
20  public Renderable compile(final Class<?> page, final Template template) {
21    // Compile template immediately.
22    final CompiledTemplate compiledTemplate = org.mvel2.templates.TemplateCompiler.compileTemplate(template.getText());
23
24    return new Renderable() {
25      @Override
26      public void render(Object bound, Respond respond) {
27        assert page.isInstance(bound);
28        respond.write(TemplateRuntime.execute(compiledTemplate, bound, new HashMap()).toString());
29      }
30
31      @Override
32      public <T extends Renderable> Set<T> collect(Class<T> clazz) {
33        return ImmutableSet.of();
34      }
35    };
36  }
37}