1 18 package org.apache.beehive.netui.script.el; 19 20 import javax.servlet.jsp.el.VariableResolver ; 21 22 import org.apache.beehive.netui.script.Expression; 23 import org.apache.beehive.netui.script.ExpressionEvaluator; 24 import org.apache.beehive.netui.script.ExpressionEvaluationException; 25 import org.apache.beehive.netui.script.ExpressionUpdateException; 26 import org.apache.beehive.netui.script.IllegalExpressionException; 27 import org.apache.beehive.netui.script.el.util.ParseUtils; 28 import org.apache.beehive.netui.util.logging.Logger; 29 30 33 public class ExpressionEvaluatorImpl 34 implements ExpressionEvaluator { 35 36 private static final Logger LOGGER = Logger.getInstance(ExpressionEvaluatorImpl.class); 37 private static final boolean DEBUG_ENABLED = LOGGER.isDebugEnabled(); 38 39 public static class NetUIELEngineFactory 40 extends org.apache.beehive.netui.script.ExpressionEngineFactory { 41 42 public ExpressionEvaluator getInstance() { 43 return new ExpressionEvaluatorImpl(); 44 } 45 } 46 47 public Object evaluateStrict(String expression, VariableResolver variableResolver) 48 throws ExpressionEvaluationException { 49 NetUIReadVariableResolver vr = null; 50 try { 51 52 vr = new NetUIReadVariableResolver(variableResolver); 53 54 return ParseUtils.evaluate(expression, vr); 55 } catch(Exception e) { 56 if(DEBUG_ENABLED) 57 LOGGER.debug("Expression evaluation failed in NetUIEL. Cause: " + e, e); 58 59 Exception act = e; 60 61 String contextStr = ParseUtils.getContextString(vr.getAvailableVariables()); 62 String msg = "Caught exception when evaluating expression \"" + expression + "\" with available binding contexts " + 63 contextStr + ". Root cause: " + ParseUtils.getRootCause(act).toString(); 64 65 if(LOGGER.isErrorEnabled()) 66 LOGGER.error(msg, act); 67 68 throw new ExpressionEvaluationException(msg, expression, act); 69 } 70 } 71 72 public void update(String expression, Object value, VariableResolver variableResolver, boolean requestParameter) 73 throws ExpressionUpdateException { 74 assert variableResolver instanceof NetUIVariableResolver; 75 76 NetUIVariableResolver vr = (NetUIVariableResolver)variableResolver; 77 78 try { 79 if(DEBUG_ENABLED) 80 LOGGER.debug("Update expression \"" + expression + "\""); 81 82 ParseUtils.update(expression, value, vr); 83 } catch(Exception e) { 84 if(DEBUG_ENABLED) 85 LOGGER.debug("Expression update failed in NetUIEL. Cause: " + e, e); 86 87 String contextStr = ParseUtils.getContextString(vr.getAvailableVariables()); 88 String msg = "Exception when attempting to update the expression \"" + expression + "\" with available binding contexts " + 89 contextStr + ". Root cause: " + ParseUtils.getRootCause(e).toString(); 90 91 if(LOGGER.isErrorEnabled()) 92 LOGGER.error(msg, e); 93 94 ExpressionUpdateException eee = new ExpressionUpdateException(msg, expression, e); 95 eee.setLocalizedMessage(msg); 96 97 throw eee; 98 } 99 } 100 101 102 public String changeContext(String expression, String oldContext, String newContext, int lookupIndex) 103 throws ExpressionEvaluationException { 104 try { 105 ParsedExpression pe = ParseUtils.parse(expression); 106 return pe.changeContext(oldContext, newContext, new Integer (lookupIndex)); 107 } catch(Exception e) { 108 String msg = "Error when trying to replace old context '" + oldContext + "' with new context '" + 109 newContext + "' and index '" + lookupIndex + "': " + ParseUtils.getRootCause(e).toString(); 110 111 if(LOGGER.isErrorEnabled()) LOGGER.error(msg, e); 112 113 throw new ExpressionEvaluationException(msg, e); 114 } 115 } 116 117 public String qualify(String contextName, String expression) 118 throws ExpressionEvaluationException { 119 try { 120 ParsedExpression pe = ParseUtils.parse(expression); 121 return pe.qualify(contextName); 122 } catch(Exception e) { 123 String msg = "Error when trying to create an expression in namespace \"" + contextName + "\" with fragment \"" + 124 expression + "\". Root cause: " + ParseUtils.getRootCause(e).toString(); 125 126 throw new ExpressionEvaluationException(msg, e); 127 } 128 } 129 130 public boolean isExpression(String expression) { 131 try { 132 ParsedExpression pe = ParseUtils.parse(expression); 133 return pe.isExpression(); 134 } catch(Exception e) { 135 if(LOGGER.isErrorEnabled()) 136 LOGGER.error("Exception parsing expression \"" + expression + "\". Cause: " + 137 ParseUtils.getRootCause(e).toString(), e); 138 139 if(e instanceof IllegalExpressionException) 140 throw (IllegalExpressionException)e; 141 else if(e instanceof ExpressionParseException) 142 throw new IllegalExpressionException(e); 143 else 144 return false; 145 } 146 } 147 148 public boolean containsExpression(String expression) { 149 if(expression == null) return false; 150 151 try { 152 ParsedExpression pe = ParseUtils.parse(expression); 153 assert pe != null; 154 return pe.containsExpression(); 155 } catch(Exception e) { 156 if(LOGGER.isErrorEnabled()) 157 LOGGER.error("Exception checking for expressions in \"" + expression + "\"", e); 158 159 return false; 160 } 161 } 162 163 public Expression parseExpression(String expression) { 164 if(isExpression(expression)) { 165 ParsedExpression pe = ParseUtils.parse(expression); 166 assert pe != null; 167 return pe.getAtomicExpressionTerm(); 168 } else 169 throw new IllegalExpressionException("The expression \"" + expression + "\" can not be parsed as it is not an atomic expression."); 170 } 171 } 172 | Popular Tags |