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