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,
042: Object property) throws NullPointerException,
043: PropertyNotFoundException, ELException {
044: if (context == null) {
045: throw new NullPointerException();
046: }
047:
048: if (base == null) {
049: context.setPropertyResolved(true);
050: if (property != null) {
051: String key = property.toString();
052: PageContext page = (PageContext) context
053: .getContext(JspContext.class);
054: return page.findAttribute(key);
055: }
056: }
057:
058: return null;
059: }
060:
061: public Class<Object> getType(ELContext context, Object base,
062: Object property) throws NullPointerException,
063: PropertyNotFoundException, ELException {
064: if (context == null) {
065: throw new NullPointerException();
066: }
067:
068: if (base == null) {
069: context.setPropertyResolved(true);
070: return Object.class;
071: }
072:
073: return null;
074: }
075:
076: public void setValue(ELContext context, Object base,
077: Object property, Object value) throws NullPointerException,
078: PropertyNotFoundException, PropertyNotWritableException,
079: ELException {
080: if (context == null) {
081: throw new NullPointerException();
082: }
083:
084: if (base == null) {
085: context.setPropertyResolved(true);
086: if (property != null) {
087: String key = property.toString();
088: PageContext page = (PageContext) context
089: .getContext(JspContext.class);
090: int scope = page.getAttributesScope(key);
091: if (scope != 0) {
092: page.setAttribute(key, value, scope);
093: } else {
094: page.setAttribute(key, value);
095: }
096: }
097: }
098: }
099:
100: public boolean isReadOnly(ELContext context, Object base,
101: Object property) throws NullPointerException,
102: PropertyNotFoundException, ELException {
103: if (context == null) {
104: throw new NullPointerException();
105: }
106:
107: if (base == null) {
108: context.setPropertyResolved(true);
109: }
110:
111: return false;
112: }
113:
114: public Iterator<FeatureDescriptor> getFeatureDescriptors(
115: ELContext context, Object base) {
116:
117: PageContext ctxt = (PageContext) context
118: .getContext(JspContext.class);
119: List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
120: Enumeration e;
121: Object value;
122: String name;
123:
124: e = ctxt.getAttributeNamesInScope(PageContext.PAGE_SCOPE);
125: while (e.hasMoreElements()) {
126: name = (String) e.nextElement();
127: value = ctxt.getAttribute(name, PageContext.PAGE_SCOPE);
128: FeatureDescriptor descriptor = new FeatureDescriptor();
129: descriptor.setName(name);
130: descriptor.setDisplayName(name);
131: descriptor.setExpert(false);
132: descriptor.setHidden(false);
133: descriptor.setPreferred(true);
134: descriptor.setShortDescription("page scoped attribute");
135: descriptor.setValue("type", value.getClass());
136: descriptor
137: .setValue("resolvableAtDesignTime", Boolean.FALSE);
138: list.add(descriptor);
139: }
140:
141: e = ctxt.getAttributeNamesInScope(PageContext.REQUEST_SCOPE);
142: while (e.hasMoreElements()) {
143: name = (String) e.nextElement();
144: value = ctxt.getAttribute(name, PageContext.REQUEST_SCOPE);
145: FeatureDescriptor descriptor = new FeatureDescriptor();
146: descriptor.setName(name);
147: descriptor.setDisplayName(name);
148: descriptor.setExpert(false);
149: descriptor.setHidden(false);
150: descriptor.setPreferred(true);
151: descriptor.setShortDescription("request scope attribute");
152: descriptor.setValue("type", value.getClass());
153: descriptor
154: .setValue("resolvableAtDesignTime", Boolean.FALSE);
155: list.add(descriptor);
156: }
157:
158: if (ctxt.getSession() != null) {
159: e = ctxt
160: .getAttributeNamesInScope(PageContext.SESSION_SCOPE);
161: while (e.hasMoreElements()) {
162: name = (String) e.nextElement();
163: value = ctxt.getAttribute(name,
164: PageContext.SESSION_SCOPE);
165: FeatureDescriptor descriptor = new FeatureDescriptor();
166: descriptor.setName(name);
167: descriptor.setDisplayName(name);
168: descriptor.setExpert(false);
169: descriptor.setHidden(false);
170: descriptor.setPreferred(true);
171: descriptor
172: .setShortDescription("session scoped attribute");
173: descriptor.setValue("type", value.getClass());
174: descriptor.setValue("resolvableAtDesignTime",
175: Boolean.FALSE);
176: list.add(descriptor);
177: }
178: }
179:
180: e = ctxt
181: .getAttributeNamesInScope(PageContext.APPLICATION_SCOPE);
182: while (e.hasMoreElements()) {
183: name = (String) e.nextElement();
184: value = ctxt.getAttribute(name,
185: PageContext.APPLICATION_SCOPE);
186: FeatureDescriptor descriptor = new FeatureDescriptor();
187: descriptor.setName(name);
188: descriptor.setDisplayName(name);
189: descriptor.setExpert(false);
190: descriptor.setHidden(false);
191: descriptor.setPreferred(true);
192: descriptor
193: .setShortDescription("application scoped attribute");
194: descriptor.setValue("type", value.getClass());
195: descriptor
196: .setValue("resolvableAtDesignTime", Boolean.FALSE);
197: list.add(descriptor);
198: }
199: return list.iterator();
200: }
201:
202: private static void appendEnumeration(Collection c, Enumeration e) {
203: while (e.hasMoreElements()) {
204: c.add(e.nextElement());
205: }
206: }
207:
208: public Class<String> getCommonPropertyType(ELContext context,
209: Object base) {
210: if (base == null) {
211: return String.class;
212: }
213: return null;
214: }
215: }
|