01: /**
02: * Licensed under the Common Development and Distribution License,
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at
05: *
06: * http://www.sun.com/cddl/
07: *
08: * Unless required by applicable law or agreed to in writing, software
09: * distributed under the License is distributed on an "AS IS" BASIS,
10: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11: * implied. See the License for the specific language governing
12: * permissions and limitations under the License.
13: */package com.sun.facelets.el;
14:
15: import java.util.HashMap;
16: import java.util.Map;
17:
18: import javax.el.ELException;
19: import javax.el.ValueExpression;
20: import javax.el.VariableMapper;
21:
22: /**
23: * Utility class for wrapping another VariableMapper with a new context,
24: * represented by a {@link java.util.Map Map}. Modifications occur to the Map
25: * instance, but resolve against the wrapped VariableMapper if the Map doesn't
26: * contain the ValueExpression requested.
27: *
28: * @author Jacob Hookom
29: * @version $Id: VariableMapperWrapper.java,v 1.6 2007/09/24 16:06:12 youngm Exp $
30: */
31: public final class VariableMapperWrapper extends VariableMapper {
32:
33: private final VariableMapper target;
34:
35: private Map vars;
36:
37: /**
38: *
39: */
40: public VariableMapperWrapper(VariableMapper orig) {
41: super ();
42: this .target = orig;
43: }
44:
45: /**
46: * First tries to resolve agains the inner Map, then the wrapped
47: * ValueExpression.
48: *
49: * @see javax.el.VariableMapper#resolveVariable(java.lang.String)
50: */
51: public ValueExpression resolveVariable(String variable) {
52: ValueExpression ve = null;
53: try {
54: if (this .vars != null) {
55: ve = (ValueExpression) this .vars.get(variable);
56: }
57: if (ve == null) {
58: return this .target.resolveVariable(variable);
59: }
60: return ve;
61: } catch (StackOverflowError e) {
62: throw new ELException(
63: "Could not Resolve Variable [Overflow]: "
64: + variable, e);
65: }
66: }
67:
68: /**
69: * Set the ValueExpression on the inner Map instance.
70: *
71: * @see javax.el.VariableMapper#setVariable(java.lang.String,
72: * javax.el.ValueExpression)
73: */
74: public ValueExpression setVariable(String variable,
75: ValueExpression expression) {
76: if (this .vars == null) {
77: this .vars = new HashMap();
78: }
79: return (ValueExpression) this.vars.put(variable, expression);
80: }
81: }
|