1 /*
2 * $Id: ELContextImpl.java 836180 2009-11-14 14:00:02Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.el;
23
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.el.ELContext;
29 import javax.el.ELResolver;
30 import javax.el.FunctionMapper;
31 import javax.el.ValueExpression;
32 import javax.el.VariableMapper;
33
34 /**
35 * Implementation of ELContext.<br>
36 * Copied from Apache Tomcat 6.0.16 source code.
37 *
38 * @since 2.2.1
39 */
40 public class ELContextImpl extends ELContext {
41
42 /**
43 * A null function mapper.
44 */
45 private static final FunctionMapper NULL_FUNCTION_MAPPER = new FunctionMapper() {
46 @Override
47 public Method resolveFunction(String prefix, String localName) {
48 return null;
49 }
50 };
51
52 /**
53 * Default implementation for the variable mapper.
54 */
55 private static final class VariableMapperImpl extends VariableMapper {
56
57 /**
58 * The mapped variables.
59 */
60 private Map<String, ValueExpression> vars;
61
62 /** {@inheritDoc} */
63 @Override
64 public ValueExpression resolveVariable(String variable) {
65 if (vars == null) {
66 return null;
67 }
68 return vars.get(variable);
69 }
70
71 /** {@inheritDoc} */
72 @Override
73 public ValueExpression setVariable(String variable,
74 ValueExpression expression) {
75 if (vars == null) {
76 vars = new HashMap<String, ValueExpression>();
77 }
78 return vars.put(variable, expression);
79 }
80
81 }
82
83 /**
84 * The EL resolver to use.
85 */
86 private final ELResolver resolver;
87
88 /**
89 * The function mapper to use.
90 */
91 private FunctionMapper functionMapper = NULL_FUNCTION_MAPPER;
92
93 /**
94 * The variable mapper to use.
95 */
96 private VariableMapper variableMapper;
97
98 /**
99 * Constructor.
100 *
101 * @param resolver The resolver to use.
102 */
103 public ELContextImpl(ELResolver resolver) {
104 this.resolver = resolver;
105 }
106
107 /** {@inheritDoc} */
108 @Override
109 public ELResolver getELResolver() {
110 return this.resolver;
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 public FunctionMapper getFunctionMapper() {
116 return this.functionMapper;
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public VariableMapper getVariableMapper() {
122 if (this.variableMapper == null) {
123 this.variableMapper = new VariableMapperImpl();
124 }
125 return this.variableMapper;
126 }
127
128 /**
129 * Sets the function mapper to use.
130 *
131 * @param functionMapper The function mapper.
132 * @since 2.2.1
133 */
134 public void setFunctionMapper(FunctionMapper functionMapper) {
135 this.functionMapper = functionMapper;
136 }
137
138 /**
139 * Sets the variable mapper to use.
140 *
141 * @param variableMapper The variable mapper.
142 * @since 2.2.1
143 */
144 public void setVariableMapper(VariableMapper variableMapper) {
145 this.variableMapper = variableMapper;
146 }
147 }