1 16 17 package org.apache.commons.jexl; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.apache.commons.jexl.parser.SimpleNode; 23 24 32 class ExpressionImpl implements Expression { 33 34 35 protected List preResolvers; 36 37 38 protected List postResolvers; 39 40 44 protected String expression; 45 46 49 protected SimpleNode node; 50 51 57 ExpressionImpl(String expr, SimpleNode ref) { 58 expression = expr; 59 node = ref; 60 } 61 62 65 public Object evaluate(JexlContext context) throws Exception { 66 Object val = null; 67 68 71 if (preResolvers != null) { 72 val = tryResolver(preResolvers, context); 73 74 if (val != JexlExprResolver.NO_VALUE) { 75 return val; 76 } 77 } 78 79 val = node.value(context); 80 81 84 if (val == null && postResolvers != null) { 85 val = tryResolver(postResolvers, context); 86 87 if (val != JexlExprResolver.NO_VALUE) { 88 return val; 89 } 90 } 91 92 return val; 93 } 94 95 102 protected Object tryResolver(List resolverList, JexlContext context) { 103 Object val = JexlExprResolver.NO_VALUE; 104 String expr = getExpression(); 105 106 for (int i = 0; i < resolverList.size(); i++) { 107 JexlExprResolver jer = (JexlExprResolver) resolverList.get(i); 108 109 val = jer.evaluate(context, expr); 110 111 114 if (val != JexlExprResolver.NO_VALUE) { 115 return val; 116 } 117 } 118 119 return val; 120 } 121 122 125 public String getExpression() { 126 return expression; 127 } 128 129 132 public void addPreResolver(JexlExprResolver resolver) { 133 if (preResolvers == null) { 134 preResolvers = new ArrayList (); 135 } 136 preResolvers.add(resolver); 137 } 138 139 142 public void addPostResolver(JexlExprResolver resolver) { 143 if (postResolvers == null) { 144 postResolvers = new ArrayList (); 145 } 146 postResolvers.add(resolver); 147 } 148 149 154 public String toString() { 155 String expr = getExpression(); 156 return expr == null ? "" : expr; 157 } 158 159 } 160 | Popular Tags |