KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > el > ScopedAttributeELResolver


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17
18 package javax.servlet.jsp.el;
19
20 import java.beans.FeatureDescriptor JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
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 JavaDoc;
33 import javax.servlet.jsp.PageContext JavaDoc;
34
35 public class ScopedAttributeELResolver extends ELResolver {
36
37     public ScopedAttributeELResolver() {
38         super();
39     }
40
41     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
42             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException JavaDoc {
43         if (context == null) {
44             throw new NullPointerException JavaDoc();
45         }
46
47         if (base == null) {
48             context.setPropertyResolved(true);
49             if (property != null) {
50                 String JavaDoc key = property.toString();
51                 PageContext JavaDoc page = (PageContext JavaDoc) context
52                         .getContext(JspContext JavaDoc.class);
53                 return page.findAttribute(key);
54             }
55         }
56
57         return null;
58     }
59
60     public Class JavaDoc getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
61             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException JavaDoc {
62         if (context == null) {
63             throw new NullPointerException JavaDoc();
64         }
65
66         if (base == null) {
67             context.setPropertyResolved(true);
68             return Object JavaDoc.class;
69         }
70
71         return null;
72     }
73
74     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
75             Object JavaDoc value) throws NullPointerException JavaDoc,
76             PropertyNotFoundException, PropertyNotWritableException,
77             ELException JavaDoc {
78         if (context == null) {
79             throw new NullPointerException JavaDoc();
80         }
81
82         if (base == null) {
83             context.setPropertyResolved(true);
84             if (property != null) {
85                 String JavaDoc key = property.toString();
86                 PageContext JavaDoc page = (PageContext JavaDoc) context
87                         .getContext(JspContext JavaDoc.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 JavaDoc base, Object JavaDoc property)
99             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException JavaDoc {
100         if (context == null) {
101             throw new NullPointerException JavaDoc();
102         }
103
104         if (base == null) {
105             context.setPropertyResolved(true);
106         }
107
108         return false;
109     }
110
111     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
112
113         PageContext JavaDoc ctxt = (PageContext JavaDoc) context.getContext(JspContext JavaDoc.class);
114         List JavaDoc<FeatureDescriptor JavaDoc> list = new ArrayList JavaDoc<FeatureDescriptor JavaDoc>();
115         Enumeration JavaDoc e;
116         Object JavaDoc value;
117         String JavaDoc name;
118
119         e = ctxt.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
120         while (e.hasMoreElements()) {
121             name = (String JavaDoc) e.nextElement();
122             value = ctxt.getAttribute(name, PageContext.PAGE_SCOPE);
123             FeatureDescriptor JavaDoc descriptor = new FeatureDescriptor JavaDoc();
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 JavaDoc) e.nextElement();
138             value = ctxt.getAttribute(name, PageContext.REQUEST_SCOPE);
139             FeatureDescriptor JavaDoc descriptor = new FeatureDescriptor JavaDoc();
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 JavaDoc) e.nextElement();
155                 value = ctxt.getAttribute(name, PageContext.SESSION_SCOPE);
156                 FeatureDescriptor JavaDoc descriptor = new FeatureDescriptor JavaDoc();
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 JavaDoc) e.nextElement();
172             value = ctxt.getAttribute(name, PageContext.APPLICATION_SCOPE);
173             FeatureDescriptor JavaDoc descriptor = new FeatureDescriptor JavaDoc();
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 JavaDoc c, Enumeration JavaDoc e) {
188         while (e.hasMoreElements()) {
189             c.add(e.nextElement());
190         }
191     }
192
193     public Class JavaDoc<String JavaDoc> getCommonPropertyType(ELContext context, Object JavaDoc base) {
194         if (base == null) {
195             return String JavaDoc.class;
196         }
197         return null;
198     }
199 }
200
Popular Tags