001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Stephane Lacoin (Nuxeo EP Software Engineer)
016 */
017package org.nuxeo.ecm.platform.el;
018
019import java.lang.reflect.Method;
020import java.util.HashMap;
021import java.util.Map;
022
023import javax.el.ELContext;
024import javax.el.ELResolver;
025import javax.el.FunctionMapper;
026import javax.el.ValueExpression;
027import javax.el.VariableMapper;
028
029public class ExpressionContext extends ELContext {
030
031    private static class MyVariableMapper extends VariableMapper {
032
033        protected final Map<String, ValueExpression> map = new HashMap<String, ValueExpression>();
034
035        @Override
036        public ValueExpression resolveVariable(String variable) {
037            return map.get(variable);
038        }
039
040        @Override
041        public ValueExpression setVariable(String variable, ValueExpression expression) {
042            return map.put(variable, expression);
043        }
044    }
045
046    private static class MyFunctionMapper extends FunctionMapper {
047
048        private final Map<String, Method> map = new HashMap<String, Method>();
049
050        @SuppressWarnings("unused")
051        public void setFunction(String prefix, String localName, Method method) {
052            map.put(prefix + ":" + localName, method);
053        }
054
055        @Override
056        public Method resolveFunction(String prefix, String localName) {
057            return map.get(prefix + ":" + localName);
058        }
059    }
060
061    protected final ELResolver resolver = new ExpressionResolver();
062
063    protected final FunctionMapper functionMapper = new MyFunctionMapper();
064
065    protected final VariableMapper variableMapper = new MyVariableMapper();
066
067    @Override
068    public ELResolver getELResolver() {
069        return resolver;
070    }
071
072    @Override
073    public FunctionMapper getFunctionMapper() {
074        return functionMapper;
075    }
076
077    @Override
078    public VariableMapper getVariableMapper() {
079        return variableMapper;
080    }
081
082}