| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.loader.xml; |
| 17 | |
|
| 18 | |
import javax.el.ExpressionFactory; |
| 19 | |
import javax.el.CompositeELResolver; |
| 20 | |
import javax.el.ArrayELResolver; |
| 21 | |
import javax.el.ListELResolver; |
| 22 | |
import javax.el.MapELResolver; |
| 23 | |
import javax.el.BeanELResolver; |
| 24 | |
import javax.el.ELContext; |
| 25 | |
import javax.el.ValueExpression; |
| 26 | |
import javax.el.ELResolver; |
| 27 | |
import javax.el.FunctionMapper; |
| 28 | |
import javax.el.VariableMapper; |
| 29 | |
import java.util.HashMap; |
| 30 | |
import java.util.Map; |
| 31 | |
import java.lang.reflect.Method; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public class ELEngine { |
| 37 | |
|
| 38 | |
private final ExpressionFactory factory; |
| 39 | |
private final CompositeELResolver resolver; |
| 40 | |
private ELContext context; |
| 41 | |
|
| 42 | 10 | public ELEngine() { |
| 43 | 10 | factory = ExpressionFactory.newInstance(); |
| 44 | |
|
| 45 | 10 | resolver = new CompositeELResolver(); |
| 46 | 10 | resolver.add(new ArrayELResolver()); |
| 47 | 10 | resolver.add(new ListELResolver()); |
| 48 | 10 | resolver.add(new MapELResolver()); |
| 49 | 10 | resolver.add(new BeanELResolver()); |
| 50 | 10 | } |
| 51 | |
|
| 52 | |
public void init() { |
| 53 | 10 | context = new SimpleContext(resolver); |
| 54 | 10 | } |
| 55 | |
|
| 56 | |
public <T> void setVariable(String key, T value) { |
| 57 | 14 | setVariable(key, value, value.getClass()); |
| 58 | 14 | } |
| 59 | |
|
| 60 | |
public <T> void setVariable(String key, T value, Class<? extends T> type) { |
| 61 | 15 | VariableMapper variableMapper = context.getVariableMapper(); |
| 62 | 15 | ValueExpression valueExpression = factory.createValueExpression(value, type); |
| 63 | 15 | variableMapper.setVariable(key, valueExpression); |
| 64 | 15 | } |
| 65 | |
|
| 66 | |
public void setFunction(String prefix, Method method) { |
| 67 | 1 | Functions functions = (Functions) context.getFunctionMapper(); |
| 68 | 1 | functions.setFunction(prefix, method.getName(), method); |
| 69 | 1 | } |
| 70 | |
|
| 71 | |
public void setFunction(String prefix, String name, Method method) { |
| 72 | 0 | Functions functions = (Functions) context.getFunctionMapper(); |
| 73 | 0 | functions.setFunction(prefix, name, method); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
public String resolve(String expression) { |
| 77 | 836794 | ValueExpression expr = factory.createValueExpression(context, expression, String.class); |
| 78 | 836794 | return (String) expr.getValue(context); |
| 79 | |
} |
| 80 | |
|
| 81 | |
static class SimpleContext extends ELContext { |
| 82 | |
|
| 83 | 10 | private Functions functions = new Functions(); |
| 84 | 10 | private Variables variables = new Variables(); |
| 85 | |
private ELResolver resolver; |
| 86 | |
|
| 87 | 10 | SimpleContext(ELResolver resolver) { |
| 88 | 10 | this.resolver = resolver; |
| 89 | 10 | } |
| 90 | |
|
| 91 | |
public ELResolver getELResolver() { |
| 92 | 4 | return resolver; |
| 93 | |
} |
| 94 | |
|
| 95 | |
@Override |
| 96 | |
public FunctionMapper getFunctionMapper() { |
| 97 | 836795 | return functions; |
| 98 | |
} |
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public VariableMapper getVariableMapper() { |
| 102 | 836809 | return variables; |
| 103 | |
} |
| 104 | |
|
| 105 | |
} |
| 106 | |
|
| 107 | 10 | static class Functions extends FunctionMapper { |
| 108 | 10 | final Map<String, Method> map = new HashMap<String, Method>(); |
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public Method resolveFunction(String prefix, String localName) { |
| 112 | 1 | return map.get(prefix + ":" + localName); |
| 113 | |
} |
| 114 | |
|
| 115 | |
public void setFunction(String prefix, String localName, Method method) { |
| 116 | 1 | map.put(prefix + ":" + localName, method); |
| 117 | 1 | } |
| 118 | |
} |
| 119 | |
|
| 120 | 10 | static class Variables extends VariableMapper { |
| 121 | 10 | final Map<String, ValueExpression> map = new HashMap<String, ValueExpression>(); |
| 122 | |
|
| 123 | |
@Override |
| 124 | |
public ValueExpression resolveVariable(String variable) { |
| 125 | 25 | return map.get(variable); |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public ValueExpression setVariable(String variable, ValueExpression expression) { |
| 130 | 15 | return map.put(variable, expression); |
| 131 | |
} |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
} |