001    /*
002    * Licensed to the Apache Software Foundation (ASF) under one or more
003    * contributor license agreements.  See the NOTICE file distributed with
004    * this work for additional information regarding copyright ownership.
005    * The ASF licenses this file to You under the Apache License, Version 2.0
006    * (the "License"); you may not use this file except in compliance with
007    * the License.  You may obtain a copy of the License at
008    *
009    *     http://www.apache.org/licenses/LICENSE-2.0
010    *
011    * Unless required by applicable law or agreed to in writing, software
012    * distributed under the License is distributed on an "AS IS" BASIS,
013    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014    * See the License for the specific language governing permissions and
015    * limitations under the License.
016    */
017    
018    package javax.servlet.jsp.el;
019    
020    import java.beans.FeatureDescriptor;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Enumeration;
024    import java.util.Iterator;
025    import java.util.List;
026    
027    import javax.el.ELContext;
028    import javax.el.ELException;
029    import javax.el.ELResolver;
030    import javax.el.PropertyNotFoundException;
031    import javax.el.PropertyNotWritableException;
032    import javax.servlet.jsp.JspContext;
033    import javax.servlet.jsp.PageContext;
034    
035    public class ScopedAttributeELResolver extends ELResolver {
036    
037            public ScopedAttributeELResolver() {
038                    super();
039            }
040    
041            public Object getValue(ELContext context, Object base, Object property)
042                            throws NullPointerException, PropertyNotFoundException, ELException {
043                    if (context == null) {
044                            throw new NullPointerException();
045                    }
046    
047                    if (base == null) {
048                            context.setPropertyResolved(true);
049                            if (property != null) {
050                                    String key = property.toString();
051                                    PageContext page = (PageContext) context
052                                                    .getContext(JspContext.class);
053                                    return page.findAttribute(key);
054                            }
055                    }
056    
057                    return null;
058            }
059    
060            public Class<Object> getType(ELContext context, Object base, Object property)
061                            throws NullPointerException, PropertyNotFoundException, ELException {
062                    if (context == null) {
063                            throw new NullPointerException();
064                    }
065    
066                    if (base == null) {
067                            context.setPropertyResolved(true);
068                            return Object.class;
069                    }
070    
071                    return null;
072            }
073    
074            public void setValue(ELContext context, Object base, Object property,
075                            Object value) throws NullPointerException,
076                            PropertyNotFoundException, PropertyNotWritableException,
077                            ELException {
078                    if (context == null) {
079                            throw new NullPointerException();
080                    }
081    
082                    if (base == null) {
083                            context.setPropertyResolved(true);
084                            if (property != null) {
085                                    String key = property.toString();
086                                    PageContext page = (PageContext) context
087                                                    .getContext(JspContext.class);
088                                    int scope = page.getAttributesScope(key);
089                                    if (scope != 0) {
090                                            page.setAttribute(key, value, scope);
091                                    } else {
092                                            page.setAttribute(key, value);
093                                    }
094                            }
095                    }
096            }
097    
098            public boolean isReadOnly(ELContext context, Object base, Object property)
099                            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    }