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  package org.apache.myfaces.trinidadbuild.test;
20  
21  import java.beans.FeatureDescriptor;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.el.ELResolver;
27  import javax.el.ELContext;
28  
29  import javax.faces.context.FacesContext;
30  import javax.faces.el.VariableResolver;
31  import javax.faces.el.PropertyResolver;
32  
33  /**
34   * Mock ELResolver based on PropertyResolver and VariableResolver
35   */
36  public class MockELResolver extends ELResolver
37  {
38    public MockELResolver(VariableResolver vr, PropertyResolver pr)
39    {
40      _vr = vr;
41      _pr = pr;
42    }
43  
44    public Class<?> getCommonPropertyType(ELContext context, Object base) 
45    {
46      if (base == null)
47        return Object.class;
48  
49      if (base instanceof List || base.getClass().isArray())
50        return Integer.class;
51  
52      return String.class;
53    }
54  
55    public Iterator<FeatureDescriptor> getFeatureDescriptors(
56      ELContext context, Object base)
57    {
58      // Need a proper implementation
59      throw new UnsupportedOperationException();
60    }
61  
62    public  Class<?> getType(ELContext context, Object base, Object property)
63    {
64      FacesContext fc = FacesContext.getCurrentInstance();
65      if (base == null)
66      {
67        if (property != null)
68        {
69          Object o = _vr.resolveVariable(fc, property.toString());
70          if (o != null)
71          {
72            context.setPropertyResolved(true);
73            return o.getClass();
74          }
75        }
76      }
77      else
78      {
79        if (property != null)
80        {
81          context.setPropertyResolved(true);
82          if (property instanceof Number)
83            return _pr.getType(base, ((Number) property).intValue());
84          return _pr.getType(base, property);
85        }
86      }
87      
88      return null;
89    }
90  
91    public Object getValue(ELContext context, Object base, Object property)
92    {
93      FacesContext fc = FacesContext.getCurrentInstance();
94      if (base == null)
95      {
96        if (property != null)
97        {
98          Object o = _vr.resolveVariable(fc, property.toString());
99          if (o != null)
100         {
101           context.setPropertyResolved(true);
102           return o;
103         }
104       }
105     }
106     else
107     {
108       if (property != null)
109       {
110         context.setPropertyResolved(true);
111         if (property instanceof Number)
112           return _pr.getValue(base, ((Number) property).intValue());
113         return _pr.getValue(base, property);
114       }
115     }
116 
117     return null;
118   }
119 
120   public boolean isReadOnly(ELContext context, Object base, Object property)
121   {
122     FacesContext fc = FacesContext.getCurrentInstance();
123     if (base == null)
124     {
125       // Can't handle this case - don't set "property resolved"
126       return false;
127     }
128     else
129     {
130       if (property != null)
131       {
132         context.setPropertyResolved(true);
133         if (property instanceof Number)
134           return _pr.isReadOnly(base, ((Number) property).intValue());
135         return _pr.isReadOnly(base, property);
136       }
137     }
138 
139     return false;
140   }
141 
142   public void setValue(ELContext context, Object base, Object property, Object value) 
143   {
144     FacesContext fc = FacesContext.getCurrentInstance();
145     if (base == null)
146     {
147       // No support for this case.
148     }
149     else
150     {
151       if (property != null)
152       {
153         context.setPropertyResolved(true);
154         if (property instanceof Number)
155           _pr.setValue(base, ((Number) property).intValue(), value);
156         _pr.setValue(base, property, value);
157       }
158     }
159   }
160 
161   private final PropertyResolver _pr;
162   private final VariableResolver _vr;
163 }