Source code: com/sun/facelets/el/LegacyELContext.java
1 /**
2 * Licensed under the Common Development and Distribution License,
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.sun.com/cddl/
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package com.sun.facelets.el;
16
17 import java.lang.reflect.Method;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.logging.Logger;
24
25 import javax.el.ELContext;
26 import javax.el.ELException;
27 import javax.el.ELResolver;
28 import javax.el.FunctionMapper;
29 import javax.el.PropertyNotWritableException;
30 import javax.el.VariableMapper;
31 import javax.faces.context.ExternalContext;
32 import javax.faces.context.FacesContext;
33 import javax.faces.el.EvaluationException;
34 import javax.faces.el.PropertyNotFoundException;
35 import javax.faces.el.PropertyResolver;
36 import javax.faces.el.VariableResolver;
37
38 /**
39 *
40 *
41 * @author Jacob Hookom
42 * @version $Id: LegacyELContext.java,v 1.7 2005/09/26 00:38:51 jhook Exp $
43 * @deprecated
44 */
45 public final class LegacyELContext extends ELContext {
46
47 private static final String[] IMPLICIT_OBJECTS = new String[] {
48 "application", "applicationScope", "cookie", "facesContext",
49 "header", "headerValues", "initParam", "param", "paramValues",
50 "request", "requestScope", "session", "sessionScope", "view" };
51
52 private final static FunctionMapper functions = new EmptyFunctionMapper();
53
54 private final FacesContext faces;
55
56 private final ELResolver resolver;
57
58 private final VariableMapper variables;
59
60 public LegacyELContext(FacesContext faces) {
61 this.faces = faces;
62 this.resolver = new LegacyELResolver();
63 this.variables = new DefaultVariableMapper();
64 }
65
66 public ELResolver getELResolver() {
67 return this.resolver;
68 }
69
70 public FunctionMapper getFunctionMapper() {
71 return functions;
72 }
73
74 public VariableMapper getVariableMapper() {
75 return this.variables;
76 }
77
78 public FacesContext getFacesContext() {
79 return this.faces;
80 }
81
82 private final class LegacyELResolver extends ELResolver {
83
84 public Class getCommonPropertyType(ELContext context, Object base) {
85 return Object.class;
86 }
87
88 public Iterator getFeatureDescriptors(ELContext context, Object base) {
89 return Collections.EMPTY_LIST.iterator();
90 }
91
92 private VariableResolver getVariableResolver() {
93 return faces.getApplication().getVariableResolver();
94 }
95
96 private PropertyResolver getPropertyResolver() {
97 return faces.getApplication().getPropertyResolver();
98 }
99
100 public Class getType(ELContext context, Object base, Object property) {
101 if (property == null) {
102 return null;
103 }
104 try {
105 context.setPropertyResolved(true);
106 if (base == null) {
107 Object obj = this.getVariableResolver().resolveVariable(
108 faces, property.toString());
109 return (obj != null) ? obj.getClass() : null;
110 } else {
111 if (base instanceof List || base.getClass().isArray()) {
112 return this.getPropertyResolver().getType(base,
113 Integer.parseInt(property.toString()));
114 } else {
115 return this.getPropertyResolver().getType(base,
116 property);
117 }
118 }
119 } catch (PropertyNotFoundException e) {
120 throw new javax.el.PropertyNotFoundException(e.getMessage(), e
121 .getCause());
122 } catch (EvaluationException e) {
123 throw new ELException(e.getMessage(), e.getCause());
124 }
125 }
126
127 public Object getValue(ELContext context, Object base, Object property) {
128 if (property == null) {
129 return null;
130 }
131 try {
132 context.setPropertyResolved(true);
133 if (base == null) {
134 return this.getVariableResolver().resolveVariable(faces,
135 property.toString());
136 } else {
137 if (base instanceof List || base.getClass().isArray()) {
138 return this.getPropertyResolver().getValue(base,
139 Integer.parseInt(property.toString()));
140 } else {
141 return this.getPropertyResolver().getValue(base,
142 property);
143 }
144 }
145 } catch (PropertyNotFoundException e) {
146 throw new javax.el.PropertyNotFoundException(e.getMessage(), e
147 .getCause());
148 } catch (EvaluationException e) {
149 throw new ELException(e.getMessage(), e.getCause());
150 }
151 }
152
153 public boolean isReadOnly(ELContext context, Object base,
154 Object property) {
155 if (property == null) {
156 return true;
157 }
158 try {
159 context.setPropertyResolved(true);
160 if (base == null) {
161 return false; // what can I do?
162 } else {
163 if (base instanceof List || base.getClass().isArray()) {
164 return this.getPropertyResolver().isReadOnly(base,
165 Integer.parseInt(property.toString()));
166 } else {
167 return this.getPropertyResolver().isReadOnly(base,
168 property);
169 }
170 }
171 } catch (PropertyNotFoundException e) {
172 throw new javax.el.PropertyNotFoundException(e.getMessage(), e
173 .getCause());
174 } catch (EvaluationException e) {
175 throw new ELException(e.getMessage(), e.getCause());
176 }
177 }
178
179 public void setValue(ELContext context, Object base, Object property,
180 Object value) {
181 if (property == null) {
182 throw new PropertyNotWritableException("Null Property");
183 }
184 try {
185 context.setPropertyResolved(true);
186 if (base == null) {
187 if (Arrays.binarySearch(IMPLICIT_OBJECTS, property
188 .toString()) >= 0) {
189 throw new PropertyNotWritableException(
190 "Implicit Variable Not Setable: " + property);
191 } else {
192 Map scope = this.resolveScope(property.toString());
193 this.getPropertyResolver().setValue(scope, property,
194 value);
195 }
196 } else {
197 if (base instanceof List || base.getClass().isArray()) {
198 this.getPropertyResolver().setValue(base,
199 Integer.parseInt(property.toString()), value);
200 } else {
201 this.getPropertyResolver().setValue(base, property,
202 value);
203 }
204 }
205 } catch (PropertyNotFoundException e) {
206 throw new javax.el.PropertyNotFoundException(e.getMessage(), e
207 .getCause());
208 } catch (EvaluationException e) {
209 throw new ELException(e.getMessage(), e.getCause());
210 }
211
212 }
213
214 private final Map resolveScope(String var) {
215 ExternalContext ext = faces.getExternalContext();
216
217 // cycle through the scopes to find a match, if no
218 // match is found, then return the requestScope
219 Map map = ext.getRequestMap();
220 if (!map.containsKey(var)) {
221 map = ext.getSessionMap();
222 if (!map.containsKey(var)) {
223 map = ext.getApplicationMap();
224 if (!map.containsKey(var)) {
225 map = ext.getRequestMap();
226 }
227 }
228 }
229 return map;
230 }
231 }
232
233 private final static class EmptyFunctionMapper extends FunctionMapper {
234
235 public Method resolveFunction(String prefix, String localName) {
236 return null;
237 }
238
239 }
240
241 }