1 18 package org.apache.beehive.netui.tags; 19 20 import org.apache.beehive.netui.script.ExpressionEvaluationException; 21 import org.apache.beehive.netui.script.ExpressionEvaluator; 22 import org.apache.beehive.netui.script.ExpressionEvaluatorFactory; 23 import org.apache.beehive.netui.script.ExpressionUpdateException; 24 import org.apache.beehive.netui.script.common.ImplicitObjectUtil; 25 import org.apache.beehive.netui.util.Bundle; 26 import org.apache.beehive.netui.util.logging.Logger; 27 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.el.VariableResolver ; 31 32 public class ExpressionHandling 33 { 34 private static final Logger logger = Logger.getInstance(ExpressionHandling.class); 35 36 private ExpressionEvaluator ee = null; private INetuiTag _tag; 38 39 public ExpressionHandling(INetuiTag tag) 40 { 41 _tag = tag; 42 } 43 44 54 62 public String ensureValidExpression(String dataSource, String attrName, String errorId) 63 throws JspException 64 { 65 try { 66 boolean isExpr = isExpression(dataSource); 67 68 if (!isExpr && containsExpression(dataSource)) { 71 String s = Bundle.getString(errorId, new Object []{dataSource}); 72 _tag.registerTagError(s, null); 73 return null; 74 } 75 76 if (!isExpr) { 77 String s = Bundle.getString(errorId, new Object []{dataSource}); 78 _tag.registerTagError(s, null); 79 return null; 80 } 81 } 82 catch (Exception e) { 83 if (e instanceof JspException ) 85 throw (JspException ) e; 86 87 String s = Bundle.getString(errorId, new Object []{dataSource}); 88 _tag.registerTagError(s, e); 89 return null; 90 } 91 return dataSource; 92 } 93 94 100 public Object evaluateExpression(String expression, String attrName, PageContext ctxt) 101 throws JspException 102 { 103 return evaluateExpressionInternal(expression, attrName, ctxt); 104 } 105 106 114 public void updateExpression(String expr, Object value, PageContext pageContext) 115 throws ExpressionUpdateException, JspException 116 { 117 if (isExpression(expr)) { 118 119 VariableResolver vr = ImplicitObjectUtil.getUpdateVariableResolver(pageContext.getRequest(), pageContext.getResponse(), false); 120 ExpressionEvaluatorFactory.getInstance().update(expr, value, vr, false); 121 } 122 else { 123 String s = Bundle.getString("Tags_BindingUpdateExpressionError", new Object []{expr}); 124 _tag.registerTagError(s, null); 125 } 126 } 127 128 133 private boolean containsExpression(String expression) 134 { 135 assert (expression != null) : "The parameter expression must not be null."; 137 return getExpressionEvaluator().containsExpression(expression); 138 } 139 140 142 private boolean isExpression(String expression) 143 { 144 if (expression == null) 145 return false; 146 return getExpressionEvaluator().isExpression(expression); 147 } 148 149 156 private Object evaluateExpressionInternal(String expression, String attrName, PageContext pageContext) 157 throws JspException 158 { 159 if (logger.isDebugEnabled()) logger.debug("evaluate expression=\"" + expression + "\""); 160 161 Object result = null; 162 163 try { 164 result = getExpressionEvaluator().evaluateStrict(expression, pageContext.getVariableResolver()); 165 } 166 catch (ExpressionEvaluationException ee) { 167 170 if (logger.isWarnEnabled()) 171 logger.warn(Bundle.getString("Tags_ExpressionEvaluationFailure", expression)); 172 173 EvalErrorInfo info = new EvalErrorInfo(); 175 info.evalExcp = ee; 176 info.expression = expression; 177 info.attr = attrName; 178 info.tagType = _tag.getTagName(); 179 180 _tag.registerTagError(info); 182 return null; 183 } 184 catch (Exception e) { 185 String s = Bundle.getString("Tags_ExpressionEvaluationException", new Object []{expression, e.toString()}); 186 _tag.registerTagError(s, e); 187 return null; 188 } 189 190 if (logger.isDebugEnabled()) logger.debug("resulting object: " + result); 191 return result; 192 } 193 194 199 private final ExpressionEvaluator getExpressionEvaluator() 200 { 201 if (ee == null) 202 ee = ExpressionEvaluatorFactory.getInstance(); 203 204 assert(ee != null); 205 return ee; 206 } 207 } 208 | Popular Tags |