001: /* ExValue.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Fri Aug 31 10:19:38 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zk.xel;
020:
021: import org.zkoss.lang.Classes;
022: import org.zkoss.xel.Expression;
023: import org.zkoss.xel.Expressions;
024: import org.zkoss.xel.XelException;
025:
026: import org.zkoss.lang.Objects;
027: import org.zkoss.zk.ui.Page;
028: import org.zkoss.zk.ui.Component;
029:
030: /**
031: * A string value that might carries an expression.
032: * It is serializable and the expression is parsed by demand.
033: *
034: * @author tomyeh
035: * @since 3.0.0
036: */
037: public class ExValue implements java.io.Serializable {
038: private String _value;
039: private Class _expected;
040: /** Coerced value. Used only if _expr is DUMMY_EXPRESSION. */
041: private transient Object _coercedVal = Objects.UNKNOWN;
042: private transient Expression _expr;
043:
044: /** Constructor.
045: * @param value the value. It can be null.
046: */
047: public ExValue(String value, Class expectedType) {
048: if (expectedType == null)
049: throw new IllegalArgumentException();
050: _value = value;
051: _expected = expectedType;
052: }
053:
054: /** Tests whether it is an expression.
055: * Note: it is a wild guess. In other words, it returns false
056: * only if 100% not an expression.
057: */
058: public boolean isExpression() {
059: return _expr == null ? _value != null
060: && _value.indexOf("${") >= 0
061: : _expr != Expressions.DUMMY_EXPRESSION;
062: }
063:
064: /** Returns the raw value.
065: * The raw value is the value passed to the constructor.
066: * That is, it might contain EL expressions.
067: */
068: public final String getRawValue() {
069: return _value;
070: }
071:
072: /** Sets the raw value.
073: * @param value the value. It can be null.
074: */
075: public void setRawValue(String value) {
076: if (!Objects.equals(value, _value)) {
077: _value = value;
078: _expr = null;
079: _coercedVal = Objects.UNKNOWN;
080: }
081: }
082:
083: /** Returns the expected type.
084: */
085: public final Class getExpectedType() {
086: return _expected;
087: }
088:
089: /** Sets the expected type.
090: */
091: public final void setExpectedType(Class expectedType) {
092: if (expectedType == null)
093: throw new IllegalArgumentException();
094:
095: if (_expected != expectedType) {
096: _expected = expectedType;
097: if (_expr != Expressions.DUMMY_EXPRESSION)
098: _expr = null; //re-parse
099: _coercedVal = Objects.UNKNOWN;
100: }
101: }
102:
103: /** Returns the value after evaluation.
104: */
105: public Object getValue(Evaluator eval, Page page)
106: throws XelException {
107: if (_expr == null)
108: init(eval);
109: return _expr == Expressions.DUMMY_EXPRESSION ? coerce() : eval
110: .evaluate(page, _expr);
111: }
112:
113: /** Returns the value after evaluation.
114: */
115: public Object getValue(Evaluator eval, Component comp)
116: throws XelException {
117: if (_expr == null)
118: init(eval);
119: return _expr == Expressions.DUMMY_EXPRESSION ? coerce() : eval
120: .evaluate(comp, _expr);
121: }
122:
123: private Object coerce() {
124: if (_coercedVal == Objects.UNKNOWN)
125: _coercedVal = Classes.coerce(_expected, _value);
126: return _coercedVal;
127: }
128:
129: private void init(Evaluator eval) throws XelException {
130: if (_value != null && _value.indexOf("${") >= 0) {
131: _expr = eval.parseExpression(_value, _expected);
132: } else {
133: _expr = Expressions.DUMMY_EXPRESSION; //to denote not-an-expr
134: }
135: }
136:
137: private synchronized void readObject(java.io.ObjectInputStream s)
138: throws java.io.IOException, ClassNotFoundException {
139: s.defaultReadObject();
140: _coercedVal = Objects.UNKNOWN;
141: }
142:
143: public String toString() {
144: return _value;
145: }
146:
147: public int hashCode() {
148: return Objects.hashCode(_value);
149: }
150:
151: public boolean equals(Object o) {
152: if (o instanceof ExValue) {
153: final ExValue val = (ExValue) o;
154: return Objects.equals(val._value, _value)
155: && Objects.equals(val._expected, _expected);
156: }
157: return false;
158: }
159: }
|