001: /*
002: * Copyright 2004 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: package org.apache.myfaces.el;
017:
018: import java.util.List;
019:
020: import javax.el.ELContext;
021: import javax.el.ELException;
022: import javax.el.ELResolver;
023: import javax.faces.context.FacesContext;
024: import javax.faces.el.EvaluationException;
025: import javax.faces.el.PropertyNotFoundException;
026: import javax.faces.el.PropertyResolver;
027:
028: /**
029: * @author Manfred Geiler (latest modification by $Author: pmcmahan $)
030: * @author Anton Koinov
031: * @version $Revision: 528504 $ $Date: 2007-04-13 16:42:18 +0200 (Fr, 13 Apr 2007) $
032: */
033: @SuppressWarnings("deprecation")
034: public class PropertyResolverImpl extends PropertyResolver {
035: // ~ Public PropertyResolver Methods
036: // ----------------------------------------
037:
038: @Override
039: public Object getValue(final Object base, final Object property)
040: throws EvaluationException, PropertyNotFoundException {
041: return invokeResolver(new ResolverInvoker<Object>(base,
042: property) {
043: public Object invoke(ELResolver resolver, ELContext context) {
044: return getELResolver().getValue(getELContext(), base,
045: property);
046: }
047: });
048: }
049:
050: @Override
051: public Object getValue(final Object base, final int index)
052: throws EvaluationException, PropertyNotFoundException {
053: return getValue(base, new Integer(index));
054: }
055:
056: @Override
057: public void setValue(final Object base, final Object property,
058: final Object newValue) throws EvaluationException,
059: PropertyNotFoundException {
060: if (base == null || property == null)
061: throw new PropertyNotFoundException();
062:
063: invokeResolver(new ResolverInvoker<Object>(base, property) {
064: @Override
065: public Object invoke(ELResolver resolver, ELContext context) {
066: resolver.setValue(context, base, property, newValue);
067: return null;
068: }
069:
070: @Override
071: String getMessage() {
072: return super .getMessage() + " newValue: '" + newValue
073: + "'";
074: }
075: });
076: }
077:
078: @Override
079: public void setValue(Object base, int index, Object newValue)
080: throws EvaluationException, PropertyNotFoundException {
081: if (base == null)
082: throw new PropertyNotFoundException();
083:
084: if (base instanceof Object[]) {
085: if (index < 0 || index >= ((Object[]) base).length) {
086: throw new PropertyNotFoundException();
087: }
088: } else if (base instanceof List) {
089: if (index < 0 || index >= ((List) base).size()) {
090: throw new PropertyNotFoundException();
091: }
092: }
093:
094: setValue(base, new Integer(index), newValue);
095: }
096:
097: @Override
098: public boolean isReadOnly(final Object base, final Object property) {
099: return invokeResolver(new ResolverInvoker<Boolean>(base,
100: property) {
101: public Boolean invoke(ELResolver resolver, ELContext context) {
102: return Boolean.valueOf(getELResolver().isReadOnly(
103: getELContext(), base, property));
104: }
105: });
106: }
107:
108: @Override
109: public boolean isReadOnly(Object base, int index) {
110: return isReadOnly(base, new Integer(index));
111: }
112:
113: @Override
114: public Class getType(final Object base, final Object property) {
115: if (base == null || property == null)
116: throw new PropertyNotFoundException();
117:
118: return invokeResolver(new ResolverInvoker<Class>(base, property) {
119: public Class invoke(ELResolver resolver, ELContext context) {
120: return resolver.getType(context, base, property);
121: }
122: });
123: }
124:
125: @Override
126: public Class getType(Object base, int index) {
127: if (base == null)
128: throw new PropertyNotFoundException();
129:
130: if (base instanceof Object[]) {
131: if (index < 0 || index >= ((Object[]) base).length) {
132: throw new PropertyNotFoundException();
133: }
134: } else if (base instanceof List) {
135: if (index < 0 || index >= ((List) base).size()) {
136: throw new PropertyNotFoundException();
137: }
138: }
139:
140: return getType(base, new Integer(index));
141: }
142:
143: // ~ Internal Helper Methods
144: // ------------------------------------------------
145:
146: ELContext getELContext() {
147: return getFacesContext().getELContext();
148: }
149:
150: ELResolver getELResolver() {
151: return getFacesContext().getApplication().getELResolver();
152: }
153:
154: FacesContext getFacesContext() {
155: return FacesContext.getCurrentInstance();
156: }
157:
158: <T> T invokeResolver(ResolverInvoker<T> invoker)
159: throws EvaluationException, PropertyNotFoundException {
160: try {
161: return invoker.invoke(getELResolver(), getELContext());
162: } catch (javax.el.PropertyNotFoundException e) {
163: throw new PropertyNotFoundException("property not found: "
164: + invoker.getMessage() + ": " + e.getMessage(), e);
165: } catch (ELException e) {
166: throw new EvaluationException("exception: "
167: + invoker.getMessage() + ": " + e.getMessage(), e);
168: } catch (RuntimeException e) {
169: throw new RuntimeException("runtime exception: "
170: + invoker.getMessage() + ": " + e.getMessage(), e);
171: }
172: }
173:
174: abstract static class ResolverInvoker<T> {
175: private final Object _base;
176: private final Object _property;
177:
178: ResolverInvoker(Object base, Object property) {
179: _base = base;
180: _property = property;
181: }
182:
183: abstract T invoke(ELResolver resolver, ELContext context);
184:
185: String getMessage() {
186: return "base: '" + _base + "' property/index: '"
187: + _property + "'";
188: }
189: }
190: }
|