1 17 18 package org.apache.el; 19 20 import java.io.Externalizable ; 21 import java.io.IOException ; 22 import java.io.ObjectInput ; 23 import java.io.ObjectOutput ; 24 25 import javax.el.ELContext; 26 import javax.el.ELException; 27 import javax.el.ELResolver; 28 import javax.el.Expression; 29 import javax.el.ExpressionFactory; 30 import javax.el.FunctionMapper; 31 import javax.el.PropertyNotFoundException; 32 import javax.el.PropertyNotWritableException; 33 import javax.el.ValueExpression; 34 import javax.el.VariableMapper; 35 36 import org.apache.el.lang.ELSupport; 37 import org.apache.el.lang.EvaluationContext; 38 import org.apache.el.lang.ExpressionBuilder; 39 import org.apache.el.parser.AstLiteralExpression; 40 import org.apache.el.parser.Node; 41 import org.apache.el.util.ReflectionUtil; 42 43 44 92 public final class ValueExpressionImpl extends ValueExpression implements 93 Externalizable { 94 95 private Class expectedType; 96 97 private String expr; 98 99 private FunctionMapper fnMapper; 100 101 private VariableMapper varMapper; 102 103 private transient Node node; 104 105 public ValueExpressionImpl() { 106 107 } 108 109 112 public ValueExpressionImpl(String expr, Node node, FunctionMapper fnMapper, 113 VariableMapper varMapper, Class expectedType) { 114 this.expr = expr; 115 this.node = node; 116 this.fnMapper = fnMapper; 117 this.varMapper = varMapper; 118 this.expectedType = expectedType; 119 } 120 121 126 public boolean equals(Object obj) { 127 return (obj instanceof ValueExpressionImpl && obj.hashCode() == this 128 .hashCode()); 129 } 130 131 136 public Class getExpectedType() { 137 return this.expectedType; 138 } 139 140 150 public String getExpressionString() { 151 return this.expr; 152 } 153 154 158 private Node getNode() throws ELException { 159 if (this.node == null) { 160 this.node = ExpressionBuilder.createNode(this.expr); 161 } 162 return this.node; 163 } 164 165 170 public Class getType(ELContext context) throws PropertyNotFoundException, 171 ELException { 172 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 173 this.varMapper); 174 return this.getNode().getType(ctx); 175 } 176 177 182 public Object getValue(ELContext context) throws PropertyNotFoundException, 183 ELException { 184 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 185 this.varMapper); 186 Object value = this.getNode().getValue(ctx); 187 if (this.expectedType != null) { 188 return ELSupport.coerceToType(value, this.expectedType); 189 } 190 return value; 191 } 192 193 198 public int hashCode() { 199 return this.expr.hashCode(); 200 } 201 202 207 public boolean isLiteralText() { 208 try { 209 return this.getNode() instanceof AstLiteralExpression; 210 } catch (ELException ele) { 211 return false; 212 } 213 } 214 215 220 public boolean isReadOnly(ELContext context) 221 throws PropertyNotFoundException, ELException { 222 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 223 this.varMapper); 224 return this.getNode().isReadOnly(ctx); 225 } 226 227 public void readExternal(ObjectInput in) throws IOException , 228 ClassNotFoundException { 229 this.expr = in.readUTF(); 230 String type = in.readUTF(); 231 if (!"".equals(type)) { 232 this.expectedType = ReflectionUtil.forName(type); 233 } 234 this.fnMapper = (FunctionMapper) in.readObject(); 235 this.varMapper = (VariableMapper) in.readObject(); 236 } 237 238 244 public void setValue(ELContext context, Object value) 245 throws PropertyNotFoundException, PropertyNotWritableException, 246 ELException { 247 EvaluationContext ctx = new EvaluationContext(context, this.fnMapper, 248 this.varMapper); 249 this.getNode().setValue(ctx, value); 250 } 251 252 public void writeExternal(ObjectOutput out) throws IOException { 253 out.writeUTF(this.expr); 254 out.writeUTF((this.expectedType != null) ? this.expectedType.getName() 255 : ""); 256 out.writeObject(this.fnMapper); 257 out.writeObject(this.varMapper); 258 } 259 260 public String toString() { 261 return "ValueExpression["+this.expr+"]"; 262 } 263 } 264 | Popular Tags |