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