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