1 17 18 package javax.servlet.jsp.el; 19 20 import java.beans.FeatureDescriptor ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.el.ELContext; 28 import javax.el.ELException; 29 import javax.el.ELResolver; 30 import javax.el.PropertyNotFoundException; 31 import javax.el.PropertyNotWritableException; 32 import javax.servlet.jsp.JspContext ; 33 import javax.servlet.jsp.PageContext ; 34 35 public class ScopedAttributeELResolver extends ELResolver { 36 37 public ScopedAttributeELResolver() { 38 super(); 39 } 40 41 public Object getValue(ELContext context, Object base, Object property) 42 throws NullPointerException , PropertyNotFoundException, ELException { 43 if (context == null) { 44 throw new NullPointerException (); 45 } 46 47 if (base == null) { 48 context.setPropertyResolved(true); 49 if (property != null) { 50 String key = property.toString(); 51 PageContext page = (PageContext ) context 52 .getContext(JspContext .class); 53 return page.findAttribute(key); 54 } 55 } 56 57 return null; 58 } 59 60 public Class getType(ELContext context, Object base, Object property) 61 throws NullPointerException , PropertyNotFoundException, ELException { 62 if (context == null) { 63 throw new NullPointerException (); 64 } 65 66 if (base == null) { 67 context.setPropertyResolved(true); 68 return Object .class; 69 } 70 71 return null; 72 } 73 74 public void setValue(ELContext context, Object base, Object property, 75 Object value) throws NullPointerException , 76 PropertyNotFoundException, PropertyNotWritableException, 77 ELException { 78 if (context == null) { 79 throw new NullPointerException (); 80 } 81 82 if (base == null) { 83 context.setPropertyResolved(true); 84 if (property != null) { 85 String key = property.toString(); 86 PageContext page = (PageContext ) context 87 .getContext(JspContext .class); 88 int scope = page.getAttributesScope(key); 89 if (scope != 0) { 90 page.setAttribute(key, value, scope); 91 } else { 92 page.setAttribute(key, value); 93 } 94 } 95 } 96 } 97 98 public boolean isReadOnly(ELContext context, Object base, Object property) 99 throws NullPointerException , PropertyNotFoundException, ELException { 100 if (context == null) { 101 throw new NullPointerException (); 102 } 103 104 if (base == null) { 105 context.setPropertyResolved(true); 106 } 107 108 return false; 109 } 110 111 public Iterator <FeatureDescriptor > getFeatureDescriptors(ELContext context, Object base) { 112 113 PageContext ctxt = (PageContext ) context.getContext(JspContext .class); 114 List <FeatureDescriptor > list = new ArrayList <FeatureDescriptor >(); 115 Enumeration e; 116 Object value; 117 String name; 118 119 e = ctxt.getAttributeNamesInScope(PageContext.PAGE_SCOPE); 120 while (e.hasMoreElements()) { 121 name = (String ) e.nextElement(); 122 value = ctxt.getAttribute(name, PageContext.PAGE_SCOPE); 123 FeatureDescriptor descriptor = new FeatureDescriptor (); 124 descriptor.setName(name); 125 descriptor.setDisplayName(name); 126 descriptor.setExpert(false); 127 descriptor.setHidden(false); 128 descriptor.setPreferred(true); 129 descriptor.setShortDescription("page scoped attribute"); 130 descriptor.setValue("type", value.getClass()); 131 descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE); 132 list.add(descriptor); 133 } 134 135 e = ctxt.getAttributeNamesInScope(PageContext.REQUEST_SCOPE); 136 while (e.hasMoreElements()) { 137 name = (String ) e.nextElement(); 138 value = ctxt.getAttribute(name, PageContext.REQUEST_SCOPE); 139 FeatureDescriptor descriptor = new FeatureDescriptor (); 140 descriptor.setName(name); 141 descriptor.setDisplayName(name); 142 descriptor.setExpert(false); 143 descriptor.setHidden(false); 144 descriptor.setPreferred(true); 145 descriptor.setShortDescription("request scope attribute"); 146 descriptor.setValue("type", value.getClass()); 147 descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE); 148 list.add(descriptor); 149 } 150 151 if (ctxt.getSession() != null) { 152 e = ctxt.getAttributeNamesInScope(PageContext.SESSION_SCOPE); 153 while (e.hasMoreElements()) { 154 name = (String ) e.nextElement(); 155 value = ctxt.getAttribute(name, PageContext.SESSION_SCOPE); 156 FeatureDescriptor descriptor = new FeatureDescriptor (); 157 descriptor.setName(name); 158 descriptor.setDisplayName(name); 159 descriptor.setExpert(false); 160 descriptor.setHidden(false); 161 descriptor.setPreferred(true); 162 descriptor.setShortDescription("session scoped attribute"); 163 descriptor.setValue("type", value.getClass()); 164 descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE); 165 list.add(descriptor); 166 } 167 } 168 169 e = ctxt.getAttributeNamesInScope(PageContext.APPLICATION_SCOPE); 170 while (e.hasMoreElements()) { 171 name = (String ) e.nextElement(); 172 value = ctxt.getAttribute(name, PageContext.APPLICATION_SCOPE); 173 FeatureDescriptor descriptor = new FeatureDescriptor (); 174 descriptor.setName(name); 175 descriptor.setDisplayName(name); 176 descriptor.setExpert(false); 177 descriptor.setHidden(false); 178 descriptor.setPreferred(true); 179 descriptor.setShortDescription("application scoped attribute"); 180 descriptor.setValue("type", value.getClass()); 181 descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE); 182 list.add(descriptor); 183 } 184 return list.iterator(); 185 } 186 187 private static void appendEnumeration(Collection c, Enumeration e) { 188 while (e.hasMoreElements()) { 189 c.add(e.nextElement()); 190 } 191 } 192 193 public Class <String > getCommonPropertyType(ELContext context, Object base) { 194 if (base == null) { 195 return String .class; 196 } 197 return null; 198 } 199 } 200 | Popular Tags |