View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.el.convert;
20  
21  import javax.el.ELException;
22  import javax.el.ValueExpression;
23  import javax.faces.component.StateHolder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.el.EvaluationException;
26  import javax.faces.el.PropertyNotFoundException;
27  import javax.faces.el.ValueBinding;
28  
29  import org.apache.myfaces.shared.util.ClassUtils;
30  
31  /**
32   * Converter for legacy ValueBinding objects. See JSF 1.2 section 5.8.3
33   * 
34   * ATTENTION: If you make changes to this class, treat javax.faces.component.ValueExpressionToValueBinding accordingly.
35   * 
36   * @author Stan Silvert
37   * @see javax.faces.component.ValueExpressionToValueBinding
38   */
39  @SuppressWarnings("deprecation")
40  public class ValueExpressionToValueBinding extends ValueBinding implements StateHolder
41  {
42  
43      private ValueExpression _valueExpression;
44  
45      private boolean isTransient = false;
46  
47      // required no-arg constructor for StateHolder
48      public ValueExpressionToValueBinding()
49      {
50          _valueExpression = null;
51      }
52  
53      @Override
54      public int hashCode()
55      {
56          int result = 1;
57          result = 31 * result + ((_valueExpression == null) ? 0 : _valueExpression.hashCode());
58          result = 31 * result + (isTransient ? 1231 : 1237);
59          return result;
60      }
61  
62      @Override
63      public boolean equals(Object obj)
64      {
65          if (this == obj)
66              return true;
67          if (obj == null)
68              return false;
69          if (getClass() != obj.getClass())
70              return false;
71          final ValueExpressionToValueBinding other = (ValueExpressionToValueBinding) obj;
72          if (_valueExpression == null)
73          {
74              if (other._valueExpression != null)
75                  return false;
76          }
77          else if (!_valueExpression.equals(other._valueExpression))
78              return false;
79          if (isTransient != other.isTransient)
80              return false;
81          return true;
82      }
83  
84      /**
85       * @return the valueExpression
86       */
87      public ValueExpression getValueExpression()
88      {
89          return getNotNullValueExpression();
90      }
91  
92      /**
93       * @return the valueExpression
94       */
95      private ValueExpression getNotNullValueExpression()
96      {
97          if (_valueExpression == null)
98          {
99              throw new IllegalStateException("value expression is null");
100         }
101         return _valueExpression;
102     }
103 
104     @Override
105     public String getExpressionString()
106     {
107         return getNotNullValueExpression().getExpressionString();
108     }
109 
110     /** Creates a new instance of ValueExpressionToValueBinding */
111     public ValueExpressionToValueBinding(ValueExpression valueExpression)
112     {
113         if (valueExpression == null)
114         {
115             throw new IllegalArgumentException("value expression must not be null.");
116         }
117         _valueExpression = valueExpression;
118     }
119 
120     @Override
121     public void setValue(FacesContext facesContext, Object value) throws EvaluationException, PropertyNotFoundException
122     {
123         try
124         {
125             getNotNullValueExpression().setValue(facesContext.getELContext(), value);
126         }
127         catch (javax.el.PropertyNotFoundException e)
128         {
129             throw new javax.faces.el.PropertyNotFoundException(e);
130         }
131         catch (ELException e)
132         {
133             throw new EvaluationException(e);
134         }
135     }
136 
137     @Override
138     public boolean isReadOnly(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
139     {
140 
141         try
142         {
143             return getNotNullValueExpression().isReadOnly(facesContext.getELContext());
144         }
145         catch (javax.el.PropertyNotFoundException e)
146         {
147             throw new javax.faces.el.PropertyNotFoundException(e);
148         }
149         catch (ELException e)
150         {
151             throw new EvaluationException(e);
152         }
153 
154     }
155 
156     @Override
157     public Object getValue(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
158     {
159         try
160         {
161             return getNotNullValueExpression().getValue(facesContext.getELContext());
162         }
163         catch (javax.el.PropertyNotFoundException e)
164         {
165             throw new javax.faces.el.PropertyNotFoundException(e);
166         }
167         catch (ELException e)
168         {
169             throw new EvaluationException(e);
170         }
171     }
172 
173     @Override
174     public Class getType(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
175     {
176         try
177         {
178             return getNotNullValueExpression().getType(facesContext.getELContext());
179         }
180         catch (javax.el.PropertyNotFoundException e)
181         {
182             throw new javax.faces.el.PropertyNotFoundException(e);
183         }
184         catch (ELException e)
185         {
186             throw new EvaluationException(e);
187         }
188     }
189 
190     // -------- StateHolder methods -------------------------------------------
191 
192     public void restoreState(FacesContext facesContext, Object state)
193     {
194         if (state != null)
195         {
196             if (state instanceof ValueExpression)
197             {
198                 _valueExpression = (ValueExpression) state;
199             }
200             else
201             {
202                 Object[] stateArray = (Object[]) state;
203                 _valueExpression = (ValueExpression) ClassUtils.newInstance((String) stateArray[0], ValueExpression.class);
204                 ((StateHolder) _valueExpression).restoreState(facesContext, stateArray[1]);
205             }
206         }
207     }
208 
209     public Object saveState(FacesContext context)
210     {
211         if (!isTransient)
212         {
213             if (_valueExpression instanceof StateHolder)
214             {
215                 Object[] state = new Object[2];
216                 state[0] = _valueExpression.getClass().getName();
217                 state[1] = ((StateHolder) _valueExpression).saveState(context);
218                 return state;
219             }
220             else
221             {
222                 return _valueExpression;
223             }
224         }
225         return null;
226     }
227 
228     public void setTransient(boolean newTransientValue)
229     {
230         isTransient = newTransientValue;
231     }
232 
233     public boolean isTransient()
234     {
235         return isTransient;
236     }
237 
238 }