PageRenderTime 218ms CodeModel.GetById 208ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/sitebricks/src/test/java/com/google/sitebricks/rendering/DynTypedMvelEvaluatorCompiler.java

http://github.com/dhanji/sitebricks
Java | 71 lines | 49 code | 16 blank | 6 comment | 2 complexity | 5de2f7f4891aae4d76bac8481c50cc9c MD5 | raw file
 1package com.google.sitebricks.rendering;
 2
 3import com.google.sitebricks.Evaluator;
 4import com.google.sitebricks.compiler.EvaluatorCompiler;
 5import com.google.sitebricks.compiler.ExpressionCompileException;
 6import com.google.sitebricks.compiler.Parsing;
 7import com.google.sitebricks.compiler.Token;
 8
 9import org.jetbrains.annotations.Nullable;
10import org.mvel2.MVEL;
11
12import java.io.Serializable;
13import java.util.Collection;
14import java.util.List;
15import java.util.Map;
16import java.util.concurrent.ConcurrentHashMap;
17import java.util.concurrent.ConcurrentMap;
18
19
20/**
21 * Temporary class to enable dynamic typing for collection projections (since we don't have
22 * a good mechanism in MVEL to reflect on parametric types yet)
23 *
24 * @author Dhanji R. Prasanna (dhanji@gmail.com)
25 */
26public class DynTypedMvelEvaluatorCompiler implements EvaluatorCompiler {
27
28    public DynTypedMvelEvaluatorCompiler(Map<String, Class<?>> map) {
29    }
30
31    public Evaluator compile(final String expression) throws ExpressionCompileException {
32        return new Evaluator() {
33            private final ConcurrentMap<String, Serializable> map = new ConcurrentHashMap<String, Serializable>();
34
35            @Nullable
36            public Object evaluate(String ___expr, Object bean) {
37                Serializable serializable = map.get(expression);
38
39                if (null == serializable) {
40                    serializable = MVEL.compileExpression(expression);
41                    map.put(expression, serializable);
42                }
43                
44                return MVEL.executeExpression(serializable, bean);
45            }
46
47            public void write(String expr, Object bean, Object value) {
48            }
49
50            public Object read(String property, Object contextObject) {
51                return MVEL.getProperty(property, contextObject);
52            }
53        };
54    }
55
56    public Class<?> resolveCollectionTypeParameter(String expression) throws ExpressionCompileException {
57        return Object.class;
58    }
59
60    public List<Token> tokenizeAndCompile(String template) throws ExpressionCompileException {
61        return Parsing.tokenize(template, this);
62    }
63
64    public Class<?> resolveEgressType(String expression) throws ExpressionCompileException {
65        return Collection.class;
66    }
67
68  public boolean isWritable(String property) throws ExpressionCompileException {
69    return true;
70  }
71}