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   * See ValueExpressionToValueBinding
37   * 
38   * @author Stan Silvert
39   */
40  @SuppressWarnings("deprecation")
41  public class ValueExpressionToValueBinding extends ValueBinding implements StateHolder
42  {
43  
44      private ValueExpression _valueExpression;
45  
46      private boolean isTransient = false;
47  
48      // required no-arg constructor for StateHolder
49      public ValueExpressionToValueBinding()
50      {
51          _valueExpression = null;
52      }
53  
54      @Override
55      public int hashCode()
56      {
57          int result = 1;
58          result = 31 * result + ((_valueExpression == null) ? 0 : _valueExpression.hashCode());
59          result = 31 * result + (isTransient ? 1231 : 1237);
60          return result;
61      }
62  
63      @Override
64      public boolean equals(Object obj)
65      {
66          if (this == obj)
67          {
68              return true;
69          }
70          if (obj == null)
71          {
72              return false;
73          }
74          if (getClass() != obj.getClass())
75          {
76              return false;
77          }
78          final ValueExpressionToValueBinding other = (ValueExpressionToValueBinding) obj;
79          if (_valueExpression == null)
80          {
81              if (other._valueExpression != null)
82              {
83                  return false;
84              }
85          }
86          else if (!_valueExpression.equals(other._valueExpression))
87          {
88              return false;
89          }
90          if (isTransient != other.isTransient)
91          {
92              return false;
93          }
94          return true;
95      }
96  
97      /**
98       * @return the valueExpression
99       */
100     public ValueExpression getValueExpression()
101     {
102         return getNotNullValueExpression();
103     }
104 
105     /**
106      * @return the valueExpression
107      */
108     private ValueExpression getNotNullValueExpression()
109     {
110         if (_valueExpression == null)
111         {
112             throw new IllegalStateException("value expression is null");
113         }
114         return _valueExpression;
115     }
116 
117     @Override
118     public String getExpressionString()
119     {
120         return getNotNullValueExpression().getExpressionString();
121     }
122 
123     /** Creates a new instance of ValueExpressionToValueBinding */
124     public ValueExpressionToValueBinding(ValueExpression valueExpression)
125     {
126         if (valueExpression == null)
127         {
128             throw new IllegalArgumentException("value expression must not be null.");
129         }
130         _valueExpression = valueExpression;
131     }
132 
133     @Override
134     public void setValue(FacesContext facesContext, Object value) throws EvaluationException, PropertyNotFoundException
135     {
136         try
137         {
138             getNotNullValueExpression().setValue(facesContext.getELContext(), value);
139         }
140         catch (javax.el.PropertyNotFoundException e)
141         {
142             throw new javax.faces.el.PropertyNotFoundException(e);
143         }
144         catch (ELException e)
145         {
146             throw new EvaluationException(e);
147         }
148     }
149 
150     @Override
151     public boolean isReadOnly(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
152     {
153 
154         try
155         {
156             return getNotNullValueExpression().isReadOnly(facesContext.getELContext());
157         }
158         catch (javax.el.PropertyNotFoundException e)
159         {
160             throw new javax.faces.el.PropertyNotFoundException(e);
161         }
162         catch (ELException e)
163         {
164             throw new EvaluationException(e);
165         }
166 
167     }
168 
169     @Override
170     public Object getValue(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
171     {
172         try
173         {
174             return getNotNullValueExpression().getValue(facesContext.getELContext());
175         }
176         catch (javax.el.PropertyNotFoundException e)
177         {
178             throw new javax.faces.el.PropertyNotFoundException(e);
179         }
180         catch (ELException e)
181         {
182             throw new EvaluationException(e);
183         }
184     }
185 
186     @Override
187     public Class getType(FacesContext facesContext) throws EvaluationException, PropertyNotFoundException
188     {
189         try
190         {
191             return getNotNullValueExpression().getType(facesContext.getELContext());
192         }
193         catch (javax.el.PropertyNotFoundException e)
194         {
195             throw new javax.faces.el.PropertyNotFoundException(e);
196         }
197         catch (ELException e)
198         {
199             throw new EvaluationException(e);
200         }
201     }
202 
203     // -------- StateHolder methods -------------------------------------------
204 
205     public void restoreState(FacesContext facesContext, Object state)
206     {
207         if (state != null)
208         {
209             if (state instanceof ValueExpression)
210             {
211                 _valueExpression = (ValueExpression) state;
212             }
213             else
214             {
215                 Object[] stateArray = (Object[]) state;
216                 _valueExpression
217                         = (ValueExpression) ClassUtils.newInstance((String) stateArray[0], ValueExpression.class);
218                 ((StateHolder) _valueExpression).restoreState(facesContext, stateArray[1]);
219             }
220         }
221     }
222 
223     public Object saveState(FacesContext context)
224     {
225         if (!isTransient)
226         {
227             if (_valueExpression instanceof StateHolder)
228             {
229                 Object[] state = new Object[2];
230                 state[0] = _valueExpression.getClass().getName();
231                 state[1] = ((StateHolder) _valueExpression).saveState(context);
232                 return state;
233             }
234             else
235             {
236                 return _valueExpression;
237             }
238         }
239         return null;
240     }
241 
242     public void setTransient(boolean newTransientValue)
243     {
244         isTransient = newTransientValue;
245     }
246 
247     public boolean isTransient()
248     {
249         return isTransient;
250     }
251 
252 }