1 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 ; 37 import javax.servlet.jsp.el.ScopedAttributeELResolver ; 38 import java.beans.FeatureDescriptor ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 47 public class PageContextELResolver extends AbstractVariableResolver { 48 private final PageContextImpl _pageContext; 49 50 private final ELResolver []_customResolvers; 51 52 private final ImplicitObjectELResolver _implicitResolver 53 = new ImplicitObjectELResolver (); 54 private final ScopedAttributeELResolver _attrResolver 55 = new ScopedAttributeELResolver (); 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 []customResolvers) 66 { 67 _customResolvers = customResolvers; 68 69 _pageContext = pageContext; 70 } 71 72 public ELResolver []getCustomResolvers() 73 { 74 return _customResolvers; 75 } 76 77 @Override 78 public Class <?> getCommonPropertyType(ELContext env, 79 Object base) 80 { 81 Class common = null; 82 83 if (base == null) 84 common = String .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 common(Class a, Class 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 return Object .class; 112 } 113 114 @Override 115 public Iterator <FeatureDescriptor > getFeatureDescriptors(ELContext env, 116 Object base) 117 { 118 ArrayList <FeatureDescriptor > descriptors 119 = new ArrayList <FeatureDescriptor >(); 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 <FeatureDescriptor > descriptors, 140 Iterator <FeatureDescriptor > iter) 141 { 142 if (iter == null) 143 return; 144 145 while (iter.hasNext()) { 146 FeatureDescriptor desc = iter.next(); 147 148 descriptors.add(desc); 149 } 150 } 151 152 @Override 153 public Object getValue(ELContext env, Object base, Object property) 154 { 155 env.setPropertyResolved(false); 156 157 for (int i = 0; i < _customResolvers.length; i++) { 158 Object 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 ) 166 return _mapResolver.getValue(env, base, property); 167 else if (base instanceof List ) 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 ) { 175 env.setPropertyResolved(true); 176 177 return _pageContext.findAttribute(property.toString()); 178 } 179 else 180 return null; 181 } 182 183 @Override 184 public boolean isReadOnly(ELContext env, Object base, Object 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 base, 202 Object property, 203 Object value) 204 { 205 env.setPropertyResolved(false); 206 207 if (base != null) { 208 if (base instanceof Map ) 209 _mapResolver.setValue(env, base, property, value); 210 else if (base instanceof List ) 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 ) { 218 env.setPropertyResolved(true); 219 220 _pageContext.setAttribute(property.toString(), value); 221 } 222 } 223 } 224 | Popular Tags |