001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package javax.faces.component;
018:
019: import javax.el.ELException;
020: import javax.el.ValueExpression;
021: import javax.faces.context.FacesContext;
022: import javax.faces.el.EvaluationException;
023: import javax.faces.el.PropertyNotFoundException;
024: import javax.faces.el.ValueBinding;
025:
026: /**
027: * Converter for legacy ValueBinding objects. See JSF 1.2 section 5.8.3
028: *
029: * ATTENTION: If you make changes to this class, treat javax.faces.component.ValueExpressionToValueBinding accordingly.
030: *
031: * @author Stan Silvert
032: * @see javax.faces.component.ValueExpressionToValueBinding
033: */
034: @SuppressWarnings("deprecation")
035: class _ValueExpressionToValueBinding extends ValueBinding implements
036: StateHolder {
037:
038: private ValueExpression _valueExpression;
039:
040: private boolean isTransient = false;
041:
042: // required no-arg constructor for StateHolder
043: protected _ValueExpressionToValueBinding() {
044: _valueExpression = null;
045: }
046:
047: @Override
048: public int hashCode() {
049: final int PRIME = 31;
050: int result = 1;
051: result = PRIME
052: * result
053: + ((_valueExpression == null) ? 0 : _valueExpression
054: .hashCode());
055: result = PRIME * result + (isTransient ? 1231 : 1237);
056: return result;
057: }
058:
059: @Override
060: public boolean equals(Object obj) {
061: if (this == obj)
062: return true;
063: if (obj == null)
064: return false;
065: if (getClass() != obj.getClass())
066: return false;
067: final _ValueExpressionToValueBinding other = (_ValueExpressionToValueBinding) obj;
068: if (_valueExpression == null) {
069: if (other._valueExpression != null)
070: return false;
071: } else if (!_valueExpression.equals(other._valueExpression))
072: return false;
073: if (isTransient != other.isTransient)
074: return false;
075: return true;
076: }
077:
078: /**
079: * @return the valueExpression
080: */
081: public ValueExpression getValueExpression() {
082: return getNotNullValueExpression();
083: }
084:
085: /**
086: * @return the valueExpression
087: */
088: private ValueExpression getNotNullValueExpression() {
089: if (_valueExpression == null) {
090: throw new IllegalStateException("value expression is null");
091: }
092: return _valueExpression;
093: }
094:
095: @Override
096: public String getExpressionString() {
097: return getNotNullValueExpression().getExpressionString();
098: }
099:
100: /** Creates a new instance of ValueExpressionToValueBinding */
101: public _ValueExpressionToValueBinding(
102: ValueExpression valueExpression) {
103: if (valueExpression == null) {
104: throw new IllegalArgumentException(
105: "value expression must not be null.");
106: }
107: _valueExpression = valueExpression;
108: }
109:
110: @Override
111: public void setValue(FacesContext facesContext, Object value)
112: throws EvaluationException, PropertyNotFoundException {
113: try {
114: getNotNullValueExpression().setValue(
115: facesContext.getELContext(), value);
116: } catch (javax.el.PropertyNotFoundException e) {
117: throw new javax.faces.el.PropertyNotFoundException(e);
118: } catch (ELException e) {
119: throw new EvaluationException(e);
120: }
121: }
122:
123: @Override
124: public boolean isReadOnly(FacesContext facesContext)
125: throws EvaluationException, PropertyNotFoundException {
126:
127: try {
128: return getNotNullValueExpression().isReadOnly(
129: facesContext.getELContext());
130: } catch (javax.el.PropertyNotFoundException e) {
131: throw new javax.faces.el.PropertyNotFoundException(e);
132: } catch (ELException e) {
133: throw new EvaluationException(e);
134: }
135:
136: }
137:
138: @Override
139: public Object getValue(FacesContext facesContext)
140: throws EvaluationException, PropertyNotFoundException {
141: try {
142: return getNotNullValueExpression().getValue(
143: facesContext.getELContext());
144: } catch (javax.el.PropertyNotFoundException e) {
145: throw new javax.faces.el.PropertyNotFoundException(e);
146: } catch (ELException e) {
147: throw new EvaluationException(e);
148: }
149: }
150:
151: @Override
152: public Class getType(FacesContext facesContext)
153: throws EvaluationException, PropertyNotFoundException {
154: try {
155: return getNotNullValueExpression().getType(
156: facesContext.getELContext());
157: } catch (javax.el.PropertyNotFoundException e) {
158: throw new javax.faces.el.PropertyNotFoundException(e);
159: } catch (ELException e) {
160: throw new EvaluationException(e);
161: }
162: }
163:
164: // -------- StateHolder methods -------------------------------------------
165:
166: public void restoreState(FacesContext facesContext, Object state) {
167: if (state != null) {
168: if (state instanceof ValueExpression) {
169: _valueExpression = (ValueExpression) state;
170: } else {
171: Object[] stateArray = (Object[]) state;
172: _valueExpression = (ValueExpression) _ClassUtils
173: .newInstance((String) stateArray[0],
174: ValueExpression.class);
175: ((StateHolder) _valueExpression).restoreState(
176: facesContext, stateArray[1]);
177: }
178: }
179: }
180:
181: public Object saveState(FacesContext context) {
182: if (!isTransient) {
183: if (_valueExpression instanceof StateHolder) {
184: Object[] state = new Object[2];
185: state[0] = _valueExpression.getClass().getName();
186: state[1] = ((StateHolder) _valueExpression)
187: .saveState(context);
188: return state;
189: } else {
190: return _valueExpression;
191: }
192: }
193: return null;
194: }
195:
196: public void setTransient(boolean newTransientValue) {
197: isTransient = newTransientValue;
198: }
199:
200: public boolean isTransient() {
201: return isTransient;
202: }
203:
204: }
|