1 15 package org.apache.tapestry.binding; 16 17 import org.apache.hivemind.Location; 18 import org.apache.tapestry.BindingException; 19 import org.apache.tapestry.IComponent; 20 import org.apache.tapestry.coerce.ValueConverter; 21 import org.apache.tapestry.services.ExpressionCache; 22 import org.apache.tapestry.services.ExpressionEvaluator; 23 24 33 34 public class ExpressionBinding extends AbstractBinding 35 { 36 39 40 private final IComponent _root; 41 42 45 46 private String _expression; 47 48 51 52 private boolean _invariant = false; 53 54 57 58 private Object _parsedExpression; 59 60 63 64 private boolean _initialized; 65 66 69 70 private ExpressionEvaluator _evaluator; 71 72 73 74 private ExpressionCache _cache; 75 76 79 80 public ExpressionBinding(String description, Location location, ValueConverter valueConverter, 81 IComponent root, String expression, ExpressionEvaluator evaluator, 82 ExpressionCache cache) 83 { 84 super(description, valueConverter, location); 85 86 _root = root; 87 _expression = expression; 88 _evaluator = evaluator; 89 _cache = cache; 90 } 91 92 98 99 public Object getObject() 100 { 101 initialize(); 102 103 return resolveExpression(); 104 } 105 106 private Object resolveExpression() 107 { 108 try 109 { 110 return _evaluator.readCompiled(_root, _parsedExpression); 111 } 112 catch (Throwable t) 113 { 114 throw new BindingException(t.getMessage(), this, t); 115 } 116 } 117 118 121 122 public boolean isInvariant() 123 { 124 initialize(); 125 126 return _invariant; 127 } 128 129 133 134 private void initialize() 135 { 136 if (_initialized) 137 return; 138 139 _initialized = true; 140 141 try 142 { 143 _parsedExpression = _cache.getCompiledExpression(_expression); 144 145 _invariant = _evaluator.isConstant(_expression); 146 } 147 catch (Exception ex) 148 { 149 throw new BindingException(ex.getMessage(), this, ex); 150 } 151 } 152 153 162 163 public void setObject(Object value) 164 { 165 initialize(); 166 167 if (_invariant) 168 throw createReadOnlyBindingException(this); 169 170 try 171 { 172 _evaluator.writeCompiled(_root, _parsedExpression, value); 173 } 174 catch (Throwable ex) 175 { 176 throw new BindingException(ex.getMessage(), this, ex); 177 } 178 } 179 180 185 186 public String toString() 187 { 188 StringBuffer buffer = new StringBuffer (); 189 190 buffer.append("ExpressionBinding["); 191 buffer.append(_root.getExtendedId()); 192 193 if (_expression != null) 194 { 195 buffer.append(' '); 196 buffer.append(_expression); 197 } 198 199 buffer.append(']'); 200 201 return buffer.toString(); 202 } 203 204 205 public Object getComponent() 206 { 207 return _root; 208 } 209 }
| Popular Tags
|