001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Stephane Lacoin (Nuxeo EP Software Engineer)
018 */
019package org.nuxeo.ecm.platform.el;
020
021import java.lang.reflect.Method;
022import java.util.HashMap;
023import java.util.Map;
024
025import javax.el.ELContext;
026import javax.el.ELResolver;
027import javax.el.FunctionMapper;
028import javax.el.ValueExpression;
029import javax.el.VariableMapper;
030
031public class ExpressionContext extends ELContext {
032
033    private static class MyVariableMapper extends VariableMapper {
034
035        protected final Map<String, ValueExpression> map = new HashMap<String, ValueExpression>();
036
037        @Override
038        public ValueExpression resolveVariable(String variable) {
039            return map.get(variable);
040        }
041
042        @Override
043        public ValueExpression setVariable(String variable, ValueExpression expression) {
044            return map.put(variable, expression);
045        }
046    }
047
048    private static class MyFunctionMapper extends FunctionMapper {
049
050        private final Map<String, Method> map = new HashMap<String, Method>();
051
052        @SuppressWarnings("unused")
053        public void setFunction(String prefix, String localName, Method method) {
054            map.put(prefix + ":" + localName, method);
055        }
056
057        @Override
058        public Method resolveFunction(String prefix, String localName) {
059            return map.get(prefix + ":" + localName);
060        }
061    }
062
063    protected final ELResolver resolver = new ExpressionResolver();
064
065    protected final FunctionMapper functionMapper = new MyFunctionMapper();
066
067    protected final VariableMapper variableMapper = new MyVariableMapper();
068
069    @Override
070    public ELResolver getELResolver() {
071        return resolver;
072    }
073
074    @Override
075    public FunctionMapper getFunctionMapper() {
076        return functionMapper;
077    }
078
079    @Override
080    public VariableMapper getVariableMapper() {
081        return variableMapper;
082    }
083
084}