KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > el > PageContextELResolver


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.el;
31
32 import com.caucho.el.AbstractVariableResolver;
33 import com.caucho.jsp.PageContextImpl;
34
35 import javax.el.*;
36 import javax.servlet.jsp.el.ImplicitObjectELResolver JavaDoc;
37 import javax.servlet.jsp.el.ScopedAttributeELResolver JavaDoc;
38 import java.beans.FeatureDescriptor JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * Variable resolution for JSP variables
46  */

47 public class PageContextELResolver extends AbstractVariableResolver {
48   private final PageContextImpl _pageContext;
49
50   private final ELResolver JavaDoc []_customResolvers;
51   
52   private final ImplicitObjectELResolver JavaDoc _implicitResolver
53     = new ImplicitObjectELResolver JavaDoc();
54   private final ScopedAttributeELResolver JavaDoc _attrResolver
55     = new ScopedAttributeELResolver JavaDoc();
56   
57   private final MapELResolver _mapResolver = new MapELResolver();
58   private final ListELResolver _listResolver = new ListELResolver();
59   private final ArrayELResolver _arrayResolver = new ArrayELResolver();
60   private final ResourceBundleELResolver _bundleResolver
61     = new ResourceBundleELResolver();
62   private final BeanELResolver _beanResolver = new BeanELResolver();
63
64   public PageContextELResolver(PageContextImpl pageContext,
65                    ELResolver JavaDoc []customResolvers)
66   {
67     _customResolvers = customResolvers;
68
69     _pageContext = pageContext;
70   }
71
72   public ELResolver JavaDoc []getCustomResolvers()
73   {
74     return _customResolvers;
75   }
76
77   @Override JavaDoc
78   public Class JavaDoc<?> getCommonPropertyType(ELContext env,
79                     Object JavaDoc base)
80   {
81     Class JavaDoc common = null;
82
83     if (base == null)
84       common = String JavaDoc.class;
85
86     for (int i = 0; i < _customResolvers.length; i++) {
87       common = common(common,
88               _customResolvers[i].getCommonPropertyType(env, base));
89     }
90
91     common = common(common, _mapResolver.getCommonPropertyType(env, base));
92     common = common(common, _listResolver.getCommonPropertyType(env, base));
93     common = common(common, _arrayResolver.getCommonPropertyType(env, base));
94     common = common(common, _beanResolver.getCommonPropertyType(env, base));
95     common = common(common, _bundleResolver.getCommonPropertyType(env, base));
96
97     return common;
98   }
99
100   private static Class JavaDoc common(Class JavaDoc a, Class JavaDoc b)
101   {
102     if (a == null)
103       return b;
104     else if (b == null)
105       return a;
106     else if (a.isAssignableFrom(b))
107       return a;
108     else if (b.isAssignableFrom(a))
109       return b;
110     else // XXX:
111
return Object JavaDoc.class;
112   }
113
114   @Override JavaDoc
115   public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext env,
116                                Object JavaDoc base)
117   {
118     ArrayList JavaDoc<FeatureDescriptor JavaDoc> descriptors
119       = new ArrayList JavaDoc<FeatureDescriptor JavaDoc>();
120
121     for (int i = 0; i < _customResolvers.length; i++) {
122       addDescriptors(descriptors,
123              _customResolvers[i].getFeatureDescriptors(env, base));
124     }
125
126     addDescriptors(descriptors, _mapResolver.getFeatureDescriptors(env, base));
127     addDescriptors(descriptors,
128            _beanResolver.getFeatureDescriptors(env, base));
129     addDescriptors(descriptors,
130            _bundleResolver.getFeatureDescriptors(env, base));
131     addDescriptors(descriptors,
132            _implicitResolver.getFeatureDescriptors(env, base));
133     addDescriptors(descriptors,
134            _attrResolver.getFeatureDescriptors(env, base));
135
136     return descriptors.iterator();
137   }
138
139   private void addDescriptors(ArrayList JavaDoc<FeatureDescriptor JavaDoc> descriptors,
140                   Iterator JavaDoc<FeatureDescriptor JavaDoc> iter)
141   {
142     if (iter == null)
143       return;
144
145     while (iter.hasNext()) {
146       FeatureDescriptor JavaDoc desc = iter.next();
147
148       descriptors.add(desc);
149     }
150   }
151   
152   @Override JavaDoc
153   public Object JavaDoc getValue(ELContext env, Object JavaDoc base, Object JavaDoc property)
154   {
155     env.setPropertyResolved(false);
156
157     for (int i = 0; i < _customResolvers.length; i++) {
158       Object JavaDoc value = _customResolvers[i].getValue(env, base, property);
159
160       if (env.isPropertyResolved())
161     return value;
162     }
163     
164     if (base != null) {
165       if (base instanceof Map JavaDoc)
166     return _mapResolver.getValue(env, base, property);
167       else if (base instanceof List JavaDoc)
168     return _listResolver.getValue(env, base, property);
169       else if (base.getClass().isArray())
170     return _arrayResolver.getValue(env, base, property);
171       else
172     return _beanResolver.getValue(env, base, property);
173     }
174     else if (property instanceof String JavaDoc) {
175       env.setPropertyResolved(true);
176     
177       return _pageContext.findAttribute(property.toString());
178     }
179     else
180       return null;
181   }
182   
183   @Override JavaDoc
184   public boolean isReadOnly(ELContext env, Object JavaDoc base, Object JavaDoc property)
185   {
186     env.setPropertyResolved(false);
187
188     for (int i = 0; i < _customResolvers.length; i++) {
189       boolean value = _customResolvers[i].isReadOnly(env, base, property);
190
191       if (env.isPropertyResolved())
192     return value;
193     }
194
195     env.setPropertyResolved(true);
196
197     return false;
198   }
199     
200   public void setValue(ELContext env,
201                Object JavaDoc base,
202                Object JavaDoc property,
203                Object JavaDoc value)
204   {
205     env.setPropertyResolved(false);
206     
207     if (base != null) {
208       if (base instanceof Map JavaDoc)
209     _mapResolver.setValue(env, base, property, value);
210       else if (base instanceof List JavaDoc)
211     _listResolver.setValue(env, base, property, value);
212       else if (base.getClass().isArray())
213     _arrayResolver.setValue(env, base, property, value);
214       else
215     _beanResolver.setValue(env, base, property, value);
216     }
217     else if (property instanceof String JavaDoc) {
218       env.setPropertyResolved(true);
219     
220       _pageContext.setAttribute(property.toString(), value);
221     }
222   }
223 }
224
Popular Tags