2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.el;
19  
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import javax.el.ELContext;
24  import javax.el.ELException;
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.EvaluationException;
27  import javax.faces.el.PropertyResolver;
28  
29  /***
30   * <p><code>ELResolver</code> implementation that wraps the legacy (JSF 1.1)
31   * <code>PropertyResolver</code> chain.  See the JSF 1.2 Specification, section
32   * 5.6.1.6, for requirements implemented by this class.</p>
33   *
34   * @since 1.0.4
35   */
36  public class FacesPropertyResolverChainWrapper extends AbstractELResolver {
37      
38  
39      /***
40       * <p>Return the most general type this resolver accepts for the
41       * <code>property</code> argument.</p>
42       */
43      public Class getCommonPropertyType(ELContext context, Object base) {
44  
45          if (base != null) {
46              return null;
47          } else {
48              return Object.class;
49          }
50  
51      }
52  
53  
54      /***
55       * <p>Return an <code>Iterator</code> over the attributes that this
56       * resolver knows how to deal with.</p>
57       *
58       * @param context <code>ELContext</code> for evaluating this value
59       * @param base Base object against which this evaluation occurs
60       */
61      public Iterator getFeatureDescriptors(ELContext context, Object base) {
62  
63          return null;
64  
65      }
66  
67  
68  
69      /***
70       * <p>Evaluate with the legacy property resolver chain and return
71       * the value.</p>
72       *
73       * @param context <code>ELContext</code> for evaluating this value
74       * @param base Base object against which this evaluation occurs
75       *  (must be null because we are evaluating a top level variable)
76       * @param property Property name to be accessed
77       */
78      public Class getType(ELContext context, Object base, Object property) {
79  
80          if ((base == null) || (property == null)) {
81              return null;
82          }
83  
84          context.setPropertyResolved(true);
85          FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
86          ELContext elContext = fcontext.getELContext();
87          PropertyResolver pr = fcontext.getApplication().getPropertyResolver();
88  
89          if ((base instanceof List) || base.getClass().isArray()) {
90              Integer index = (Integer) fcontext.getApplication().getExpressionFactory().
91                      coerceToType(property, Integer.class);
92              try {
93                  return pr.getType(base, index.intValue());
94              } catch (EvaluationException e) {
95                  context.setPropertyResolved(false);
96                  throw new ELException(e);
97              }
98          } else {
99              try {
100                 return pr.getType(base, property);
101             } catch (EvaluationException e) {
102                 context.setPropertyResolved(false);
103                 throw new ELException(e);
104             }
105         }
106 
107     }
108 
109 
110     /***
111      * <p>Evaluate with the legacy property resolver chain and return
112      * the value.</p>
113      *
114      * @param context <code>ELContext</code> for evaluating this value
115      * @param base Base object against which this evaluation occurs
116      *  (must be null because we are evaluating a top level variable)
117      * @param property Property name to be accessed
118      */
119     public Object getValue(ELContext context, Object base, Object property) {
120 
121         if ((base == null) || (property == null)) {
122             return null;
123         }
124 
125         context.setPropertyResolved(true);
126         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
127         ELContext elContext = fcontext.getELContext();
128         PropertyResolver pr = fcontext.getApplication().getPropertyResolver();
129 
130         if ((base instanceof List) || base.getClass().isArray()) {
131             Integer index = (Integer) fcontext.getApplication().getExpressionFactory().
132                     coerceToType(property, Integer.class);
133             try {
134                 return pr.getValue(base, index.intValue());
135             } catch (EvaluationException e) {
136                 context.setPropertyResolved(false);
137                 throw new ELException(e);
138             }
139         } else {
140             try {
141                 return pr.getValue(base, property);
142             } catch (EvaluationException e) {
143                 context.setPropertyResolved(false);
144                 throw new ELException(e);
145             }
146         }
147 
148     }
149 
150 
151     /***
152      * <p>Return <code>true</code> if the specified property is read only.</p>
153      *
154      * @param context <code>ELContext</code> for evaluating this value
155      * @param base Base object against which this evaluation occurs
156      *  (must be null because we are evaluating a top level variable)
157      * @param property Property name to be accessed
158      */
159     public boolean isReadOnly(ELContext context, Object base, Object property) {
160 
161         if ((base == null) || (property == null)) {
162             return false;
163         }
164 
165         context.setPropertyResolved(true);
166         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
167         ELContext elContext = fcontext.getELContext();
168         PropertyResolver pr = fcontext.getApplication().getPropertyResolver();
169 
170         if ((base instanceof List) || base.getClass().isArray()) {
171             Integer index = (Integer) fcontext.getApplication().getExpressionFactory().
172                     coerceToType(property, Integer.class);
173             try {
174                 return pr.isReadOnly(base, index.intValue());
175             } catch (EvaluationException e) {
176                 context.setPropertyResolved(false);
177                 throw new ELException(e);
178             }
179         } else {
180             try {
181                 return pr.isReadOnly(base, property);
182             } catch (EvaluationException e) {
183                 context.setPropertyResolved(false);
184                 throw new ELException(e);
185             }
186         }
187 
188     }
189 
190 
191 
192     /***
193      * <p>Set the value of a property for the specified name.</p>
194      *
195      * @param context <code>ELContext</code> for evaluating this value
196      * @param base Base object against which this evaluation occurs
197      *  (must be null because we are evaluating a top level variable)
198      * @param property Property name to be accessed
199      * @param value New value to be set
200      */
201     public void setValue(ELContext context, Object base, Object property, Object value) {
202 
203         if ((base == null) || (property == null)) {
204             return;
205         }
206 
207         context.setPropertyResolved(true);
208         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
209         ELContext elContext = fcontext.getELContext();
210         PropertyResolver pr = fcontext.getApplication().getPropertyResolver();
211 
212         if ((base instanceof List) || base.getClass().isArray()) {
213             Integer index = (Integer) fcontext.getApplication().getExpressionFactory().
214                     coerceToType(property, Integer.class);
215             try {
216                 pr.setValue(base, index.intValue(), value);
217             } catch (EvaluationException e) {
218                 context.setPropertyResolved(false);
219                 throw new ELException(e);
220             }
221         } else {
222             try {
223                 pr.setValue(base, property, value);
224             } catch (EvaluationException e) {
225                 context.setPropertyResolved(false);
226                 throw new ELException(e);
227             }
228         }
229 
230     }
231 
232 
233 }