1 18 package org.apache.beehive.netui.script.el; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 25 import org.apache.beehive.netui.util.logging.Logger; 26 27 30 public class ParsedExpression { 31 32 private static Logger LOGGER = Logger.getInstance(ParsedExpression.class); 33 34 35 private static final boolean DEBUG_ENABLED = LOGGER.isDebugEnabled(); 36 private static final String EMPTY_STRING = ""; 37 38 private ArrayList _terms = new ArrayList (3); 39 private boolean _isExpression = false; 40 private boolean _containsExpression = false; 41 private ExpressionTerm _atomicExpression = null; 42 private Term[] _termArray = null; 43 private String _exprStr; 44 45 public void seal() { 46 _termArray = new Term[_terms.size()]; 47 48 InternalStringBuilder buf = new InternalStringBuilder(); 49 for(int i = 0; i < _terms.size(); i++) { 50 Term t = (Term)_terms.get(i); 51 t.seal(); 52 53 if(t instanceof ExpressionTerm) { 54 if(_terms.size() == 1) { 55 _atomicExpression = (ExpressionTerm)_terms.get(0); 56 _isExpression = true; 57 } 58 _containsExpression = true; 59 } else if(t instanceof LiteralTerm) { 60 String lit = t.getExpressionString(); 61 if(lit != null && lit.indexOf("{") > -1) 62 _containsExpression = true; 63 } 64 65 _termArray[i] = (Term)_terms.get(i); 66 buf.append(t.getExpressionString()); 67 } 68 _exprStr = buf.toString(); 69 } 70 71 public boolean isExpression() { 72 return _isExpression; 73 } 74 75 public boolean containsExpression() { 76 return _containsExpression; 77 } 78 79 public void addTerm(Term term) { 80 _terms.add(term); 81 } 82 83 public int getTokenCount() { 84 return _terms.size(); 85 } 86 87 public Term getTerm(int i) { 88 assert _termArray != null; 89 assert i > 0 && i < _termArray.length; 90 91 return _termArray[i]; 92 } 93 94 public ExpressionTerm getAtomicExpressionTerm() { 95 return _atomicExpression; 96 } 97 98 public Object evaluate(NetUIVariableResolver vr) { 99 if(DEBUG_ENABLED) 100 LOGGER.debug("evaluate expression: " + _exprStr); 101 102 if(_isExpression) { 103 if(DEBUG_ENABLED) 104 LOGGER.debug("atoimc expression"); 105 106 return _atomicExpression.evaluate(vr); 107 } else { 108 InternalStringBuilder buf = new InternalStringBuilder(); 109 110 for(int i = 0; i < _terms.size(); i++) { 111 if(DEBUG_ENABLED) 112 LOGGER.debug("term[" + i + "]: " + _termArray[i].getClass().getName() + 113 " expression string: " + _termArray[i].getExpressionString()); 114 115 Object result = _termArray[i].evaluate(vr); 116 117 buf.append(result != null ? result.toString() : EMPTY_STRING); 118 } 119 120 return buf.toString(); 121 } 122 } 123 124 public void update(Object value, NetUIVariableResolver vr) { 125 if(!_isExpression) { 126 String msg = "The expression can not be updated because it is not atomic."; 127 if(LOGGER.isErrorEnabled()) 128 LOGGER.error(msg); 129 throw new RuntimeException (msg); 130 } 131 132 _atomicExpression.update(value, vr); 133 } 134 135 public String changeContext(String oldContext, String newContext, Object index) { 136 if(!_isExpression) { 137 String msg = "The expression can not change context because it is not atomic."; 138 139 if(LOGGER.isErrorEnabled()) 140 LOGGER.error(msg); 141 142 throw new RuntimeException (msg); 143 } 144 145 return _atomicExpression.changeContext(oldContext, newContext, index); 146 } 147 148 public String qualify(String contextName) { 149 150 151 return "{" + contextName + "." + getExpressionString() + "}"; 152 } 153 154 public String getExpressionString() { 156 if(_isExpression) 157 return _atomicExpression.getExpressionString(); 158 else 159 return _exprStr; 160 } 161 162 public String toString() { 163 InternalStringBuilder builder = new InternalStringBuilder(); 164 for(Iterator i = _terms.iterator(); i.hasNext();) 165 { 166 Term term = (Term) i.next(); 167 builder.append(term.toString()); 168 } 169 return builder.toString(); 170 } 171 } 172 | Popular Tags |