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