001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.myfaces.el.unified.resolver.implicitobject;
018:
019: import javax.el.*;
020: import java.beans.FeatureDescriptor;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: /**
026: * See JSF 1.2 spec sections 5.6.1.1 and 5.6.2.1
027: *
028: * @author Stan Silvert
029: */
030: public class ImplicitObjectResolver extends ELResolver {
031:
032: private List<ImplicitObject> implicitObjects;
033:
034: /**
035: * Static factory for an ELResolver for resolving implicit objects in JSPs.
036: * See JSF 1.2 spec section 5.6.1.1
037: */
038: public static ELResolver makeResolverForJSP() {
039: List<ImplicitObject> forJSPList = new ArrayList<ImplicitObject>(
040: 2);
041: forJSPList.add(new FacesContextImplicitObject());
042: forJSPList.add(new ViewImplicitObject());
043: return new ImplicitObjectResolver(forJSPList);
044: }
045:
046: /**
047: * Static factory for an ELResolver for resolving implicit objects in all of Faces.
048: * See JSF 1.2 spec section 5.6.1.2
049: */
050: public static ELResolver makeResolverForFaces() {
051: List<ImplicitObject> forFacesList = new ArrayList<ImplicitObject>(
052: 14);
053: forFacesList.add(new ApplicationImplicitObject());
054: forFacesList.add(new ApplicationScopeImplicitObject());
055: forFacesList.add(new CookieImplicitObject());
056: forFacesList.add(new FacesContextImplicitObject());
057: forFacesList.add(new HeaderImplicitObject());
058: forFacesList.add(new HeaderValuesImplicitObject());
059: forFacesList.add(new InitParamImplicitObject());
060: forFacesList.add(new ParamImplicitObject());
061: forFacesList.add(new ParamValuesImplicitObject());
062: forFacesList.add(new RequestImplicitObject());
063: forFacesList.add(new RequestScopeImplicitObject());
064: forFacesList.add(new SessionImplicitObject());
065: forFacesList.add(new SessionScopeImplicitObject());
066: forFacesList.add(new ViewImplicitObject());
067: return new ImplicitObjectResolver(forFacesList);
068: }
069:
070: private ImplicitObjectResolver() {
071: super ();
072: this .implicitObjects = new ArrayList<ImplicitObject>();
073: }
074:
075: /** Creates a new instance of ImplicitObjectResolverForJSP */
076: private ImplicitObjectResolver(List<ImplicitObject> implicitObjects) {
077: this ();
078: this .implicitObjects = implicitObjects;
079: }
080:
081: public void setValue(ELContext context, Object base,
082: Object property, Object value) throws NullPointerException,
083: PropertyNotFoundException, PropertyNotWritableException,
084: ELException {
085:
086: if (base != null)
087: return;
088: if (property == null)
089: throw new PropertyNotFoundException();
090: if (!(property instanceof String))
091: return;
092:
093: String strProperty = castAndIntern(property);
094:
095: for (ImplicitObject obj : implicitObjects) {
096: if (strProperty.equals(obj.getName())) {
097: throw new PropertyNotWritableException();
098: }
099: }
100: }
101:
102: public boolean isReadOnly(ELContext context, Object base,
103: Object property) throws NullPointerException,
104: PropertyNotFoundException, ELException {
105:
106: if (base != null)
107: return false;
108: if (property == null)
109: throw new PropertyNotFoundException();
110: if (!(property instanceof String))
111: return false;
112:
113: String strProperty = castAndIntern(property);
114:
115: for (ImplicitObject obj : implicitObjects) {
116: if (strProperty.equals(obj.getName())) {
117: context.setPropertyResolved(true);
118: return true;
119: }
120: }
121:
122: return false;
123: }
124:
125: public Object getValue(ELContext context, Object base,
126: Object property) throws NullPointerException,
127: PropertyNotFoundException, ELException {
128:
129: if (base != null)
130: return null;
131: if (property == null)
132: throw new PropertyNotFoundException();
133: if (!(property instanceof String))
134: return null;
135:
136: String strProperty = castAndIntern(property);
137:
138: for (ImplicitObject obj : implicitObjects) {
139: if (strProperty.equals(obj.getName())) {
140: context.setPropertyResolved(true);
141: return obj.getValue(context);
142: }
143: }
144:
145: return null;
146: }
147:
148: public Class<?> getType(ELContext context, Object base,
149: Object property) throws NullPointerException,
150: PropertyNotFoundException, ELException {
151:
152: if (base != null)
153: return null;
154: if (property == null)
155: throw new PropertyNotFoundException();
156: if (!(property instanceof String))
157: return null;
158:
159: String strProperty = castAndIntern(property);
160:
161: for (ImplicitObject obj : implicitObjects) {
162: if (strProperty.equals(obj.getName())) {
163: context.setPropertyResolved(true);
164: }
165: }
166:
167: return null;
168: }
169:
170: public Iterator<FeatureDescriptor> getFeatureDescriptors(
171: ELContext context, Object base) {
172: if (base != null)
173: return null;
174:
175: ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>(
176: implicitObjects.size());
177:
178: for (ImplicitObject obj : implicitObjects) {
179: descriptors.add(obj.getDescriptor());
180: }
181:
182: return descriptors.iterator();
183: }
184:
185: public Class<?> getCommonPropertyType(ELContext context, Object base) {
186: if (base != null)
187: return null;
188:
189: return String.class;
190: }
191:
192: protected String castAndIntern(Object o) {
193: String s = (String) o;
194: return s.intern();
195: }
196:
197: }
|