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 org.apache.myfaces.el.convert;
018:
019: import javax.el.ELException;
020: import javax.el.ValueExpression;
021: import javax.faces.component.StateHolder;
022: import javax.faces.context.FacesContext;
023: import javax.faces.el.EvaluationException;
024: import javax.faces.el.PropertyNotFoundException;
025: import javax.faces.el.ValueBinding;
026:
027: import org.apache.myfaces.shared_impl.util.ClassUtils;
028:
029: /**
030: * Converter for legacy ValueBinding objects. See JSF 1.2 section 5.8.3
031: *
032: * ATTENTION: If you make changes to this class, treat javax.faces.component.ValueExpressionToValueBinding accordingly.
033: *
034: * @author Stan Silvert
035: * @see javax.faces.component.ValueExpressionToValueBinding
036: */
037: @SuppressWarnings("deprecation")
038: public class ValueExpressionToValueBinding extends ValueBinding
039: implements StateHolder {
040:
041: private ValueExpression _valueExpression;
042:
043: private boolean isTransient = false;
044:
045: // required no-arg constructor for StateHolder
046: public ValueExpressionToValueBinding() {
047: _valueExpression = null;
048: }
049:
050: @Override
051: public int hashCode() {
052: final int PRIME = 31;
053: int result = 1;
054: result = PRIME
055: * result
056: + ((_valueExpression == null) ? 0 : _valueExpression
057: .hashCode());
058: result = PRIME * result + (isTransient ? 1231 : 1237);
059: return result;
060: }
061:
062: @Override
063: public boolean equals(Object obj) {
064: if (this == obj)
065: return true;
066: if (obj == null)
067: return false;
068: if (getClass() != obj.getClass())
069: return false;
070: final ValueExpressionToValueBinding other = (ValueExpressionToValueBinding) obj;
071: if (_valueExpression == null) {
072: if (other._valueExpression != null)
073: return false;
074: } else if (!_valueExpression.equals(other._valueExpression))
075: return false;
076: if (isTransient != other.isTransient)
077: return false;
078: return true;
079: }
080:
081: /**
082: * @return the valueExpression
083: */
084: public ValueExpression getValueExpression() {
085: return getNotNullValueExpression();
086: }
087:
088: /**
089: * @return the valueExpression
090: */
091: private ValueExpression getNotNullValueExpression() {
092: if (_valueExpression == null) {
093: throw new IllegalStateException("value expression is null");
094: }
095: return _valueExpression;
096: }
097:
098: @Override
099: public String getExpressionString() {
100: return getNotNullValueExpression().getExpressionString();
101: }
102:
103: /** Creates a new instance of ValueExpressionToValueBinding */
104: public ValueExpressionToValueBinding(ValueExpression valueExpression) {
105: if (valueExpression == null) {
106: throw new IllegalArgumentException(
107: "value expression must not be null.");
108: }
109: _valueExpression = valueExpression;
110: }
111:
112: @Override
113: public void setValue(FacesContext facesContext, Object value)
114: throws EvaluationException, PropertyNotFoundException {
115: try {
116: getNotNullValueExpression().setValue(
117: facesContext.getELContext(), value);
118: } catch (javax.el.PropertyNotFoundException e) {
119: throw new javax.faces.el.PropertyNotFoundException(e);
120: } catch (ELException e) {
121: throw new EvaluationException(e);
122: }
123: }
124:
125: @Override
126: public boolean isReadOnly(FacesContext facesContext)
127: throws EvaluationException, PropertyNotFoundException {
128:
129: try {
130: return getNotNullValueExpression().isReadOnly(
131: facesContext.getELContext());
132: } catch (javax.el.PropertyNotFoundException e) {
133: throw new javax.faces.el.PropertyNotFoundException(e);
134: } catch (ELException e) {
135: throw new EvaluationException(e);
136: }
137:
138: }
139:
140: @Override
141: public Object getValue(FacesContext facesContext)
142: throws EvaluationException, PropertyNotFoundException {
143: try {
144: return getNotNullValueExpression().getValue(
145: facesContext.getELContext());
146: } catch (javax.el.PropertyNotFoundException e) {
147: throw new javax.faces.el.PropertyNotFoundException(e);
148: } catch (ELException e) {
149: throw new EvaluationException(e);
150: }
151: }
152:
153: @Override
154: public Class getType(FacesContext facesContext)
155: throws EvaluationException, PropertyNotFoundException {
156: try {
157: return getNotNullValueExpression().getType(
158: facesContext.getELContext());
159: } catch (javax.el.PropertyNotFoundException e) {
160: throw new javax.faces.el.PropertyNotFoundException(e);
161: } catch (ELException e) {
162: throw new EvaluationException(e);
163: }
164: }
165:
166: // -------- StateHolder methods -------------------------------------------
167:
168: public void restoreState(FacesContext facesContext, Object state) {
169: if (state != null) {
170: if (state instanceof ValueExpression) {
171: _valueExpression = (ValueExpression) state;
172: } else {
173: Object[] stateArray = (Object[]) state;
174: _valueExpression = (ValueExpression) ClassUtils
175: .newInstance((String) stateArray[0],
176: ValueExpression.class);
177: ((StateHolder) _valueExpression).restoreState(
178: facesContext, stateArray[1]);
179: }
180: }
181: }
182:
183: public Object saveState(FacesContext context) {
184: if (!isTransient) {
185: if (_valueExpression instanceof StateHolder) {
186: Object[] state = new Object[2];
187: state[0] = _valueExpression.getClass().getName();
188: state[1] = ((StateHolder) _valueExpression)
189: .saveState(context);
190: return state;
191: } else {
192: return _valueExpression;
193: }
194: }
195: return null;
196: }
197:
198: public void setTransient(boolean newTransientValue) {
199: isTransient = newTransientValue;
200: }
201:
202: public boolean isTransient() {
203: return isTransient;
204: }
205:
206: }
|