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