1 16 package scriptella.expression; 17 18 import org.apache.commons.jexl.ExpressionFactory; 19 import org.apache.commons.jexl.JexlContext; 20 import org.apache.commons.jexl.parser.TokenMgrError; 21 import scriptella.spi.ParametersCallback; 22 23 import java.util.Collection ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 28 34 public class JexlExpression extends Expression { 35 private JexlContextAdapter adapter = new JexlContextAdapter(); 36 private org.apache.commons.jexl.Expression expression; 37 38 protected JexlExpression(String expression) throws ParseException { 39 super(expression); 40 41 try { 42 this.expression = ExpressionFactory.createExpression(expression); 43 } catch (Exception e) { 44 throw new ParseException(e.getMessage(),e); 45 } catch (TokenMgrError e) { 46 throw new ParseException(e.getMessage(),e); 47 } 48 } 49 50 public Object evaluate(final ParametersCallback callback) 51 throws EvaluationException { 52 try { 53 adapter.setContext(callback); 54 55 return expression.evaluate(adapter); 56 } catch (Exception e) { 57 throw new EvaluationException(e); 58 } finally { 59 adapter.unsetContext(); 60 } 61 } 62 63 66 private static class JexlContextAdapter implements JexlContext { 67 private ParametersCallbackMap map = new ParametersCallbackMap(); 68 69 public void setVars(final Map map) { 70 } 71 72 public Map getVars() { 73 return map; 74 } 75 76 public void setContext(final ParametersCallback parametersCallback) { 77 map.callback = parametersCallback; 78 } 79 80 public void unsetContext() { 81 map.callback = null; 82 } 83 } 84 85 90 private static final class ParametersCallbackMap implements Map { 91 private ParametersCallback callback; 92 93 public int size() { 94 throw new UnsupportedOperationException (); 95 } 96 97 public boolean isEmpty() { 98 throw new UnsupportedOperationException (); 99 } 100 101 public boolean containsKey(final Object key) { 102 throw new UnsupportedOperationException (); 103 } 104 105 public boolean containsValue(final Object value) { 106 throw new UnsupportedOperationException (); 107 } 108 109 public Object get(final Object key) { 110 return callback.getParameter((String ) key); 111 } 112 113 public Object put(final Object key, final Object o1) { 114 throw new UnsupportedOperationException (); 115 } 116 117 public Object remove(final Object key) { 118 throw new UnsupportedOperationException (); 119 } 120 121 public void putAll(final Map map) { 122 throw new UnsupportedOperationException (); 123 } 124 125 public void clear() { 126 throw new UnsupportedOperationException (); 127 } 128 129 public Set keySet() { 130 throw new UnsupportedOperationException (); 131 } 132 133 public Collection values() { 134 throw new UnsupportedOperationException (); 135 } 136 137 public Set <Entry<String , Object >> entrySet() { 138 throw new UnsupportedOperationException (); 139 } 140 } 141 } 142 | Popular Tags |