001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
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 de.odysseus.el.util;
017:
018: import java.lang.reflect.Method;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.el.ELContext;
023: import javax.el.ELResolver;
024: import javax.el.FunctionMapper;
025: import javax.el.ValueExpression;
026: import javax.el.VariableMapper;
027:
028: /**
029: * Simple context implementation.
030: *
031: * @author Christoph Beck
032: */
033: public class SimpleContext extends ELContext {
034: static class Functions extends FunctionMapper {
035: Map<String, Method> map = new HashMap<String, Method>();
036:
037: public void setFunction(String prefix, String localName,
038: Method method) {
039: map.put(prefix + ":" + localName, method);
040: }
041:
042: @Override
043: public Method resolveFunction(String prefix, String localName) {
044: return map.get(prefix + ":" + localName);
045: }
046: }
047:
048: static class Variables extends VariableMapper {
049: Map<String, ValueExpression> map = new HashMap<String, ValueExpression>();
050:
051: @Override
052: public ValueExpression resolveVariable(String variable) {
053: return map.get(variable);
054: }
055:
056: @Override
057: public ValueExpression setVariable(String variable,
058: ValueExpression expression) {
059: return map.put(variable, expression);
060: }
061: }
062:
063: private Functions functions;
064: private Variables variables;
065: private ELResolver resolver;
066:
067: /**
068: * Create a context.
069: */
070: public SimpleContext() {
071: this (null);
072: }
073:
074: /**
075: * Create a context, use the specified resolver.
076: */
077: public SimpleContext(ELResolver resolver) {
078: this .resolver = resolver;
079: }
080:
081: /**
082: * Define a function
083: */
084: public void setFunction(String prefix, String localName,
085: Method method) {
086: if (functions == null) {
087: functions = new Functions();
088: }
089: functions.setFunction(prefix, localName, method);
090: }
091:
092: /**
093: * Define a variable
094: */
095: public ValueExpression setVariable(String name,
096: ValueExpression expression) {
097: if (variables == null) {
098: variables = new Variables();
099: }
100: return variables.setVariable(name, expression);
101: }
102:
103: /**
104: * Get our function mapper.
105: */
106: @Override
107: public FunctionMapper getFunctionMapper() {
108: return functions;
109: }
110:
111: /**
112: * Get our variable mapper.
113: */
114: @Override
115: public VariableMapper getVariableMapper() {
116: return variables;
117: }
118:
119: /**
120: * Get our resolver.
121: * Lazy initialize a {@link SimpleResolver}.
122: */
123: @Override
124: public ELResolver getELResolver() {
125: if (resolver == null) {
126: resolver = new SimpleResolver();
127: }
128: return resolver;
129: }
130: }
|