/*
 * Copyright 2006, 2007 Odysseus Software GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.odysseus.el.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.ValueExpression;
import javax.el.VariableMapper;

/**
 * Simple context implementation.
 *
 * @author Christoph Beck
 */
public class SimpleContext extends ELContext {
	static class Functions extends FunctionMapper {
		Map<String,Method> map = new HashMap<String,Method>();
		public void setFunction(String prefix, String localName, Method method) {
			map.put(prefix + ":" + localName, method);
		}
		@Override
		public Method resolveFunction(String prefix, String localName) {
			return map.get(prefix + ":" + localName);
		}
	}
	static class Variables extends VariableMapper {
		Map<String,ValueExpression> map = new HashMap<String,ValueExpression>(); 
		@Override
		public ValueExpression resolveVariable(String variable) {
			return map.get(variable);
		}
		@Override
		public ValueExpression setVariable(String variable, ValueExpression expression) {
			return map.put(variable, expression);
		}
	}

	private Functions functions;
	private Variables variables;
	private ELResolver resolver;

	/**
	 * Create a context.
	 */
	public SimpleContext() {
		this(null);
	}

	/**
	 * Create a context, use the specified resolver.
	 */
	public SimpleContext(ELResolver resolver) {
		this.resolver = resolver;
	}

	/**
	 * Define a function
	 */
	public void setFunction(String prefix, String localName, Method method) {
		if (functions == null) {
			functions = new Functions();
		}
		functions.setFunction(prefix, localName, method);
	}
	
	/**
	 * Define a variable
	 */
	public ValueExpression setVariable(String name, ValueExpression expression) {
		if (variables == null) {
			variables = new Variables();
		}
		return variables.setVariable(name, expression);
	}

	/**
	 * Get our function mapper.
	 */
	@Override
	public FunctionMapper getFunctionMapper() {
		return functions;
	}

	/**
	 * Get our variable mapper.
	 */
	@Override
	public VariableMapper getVariableMapper() {
		return variables;
	}

	/**
	 * Get our resolver.
	 * Lazy initialize a {@link SimpleResolver}.
	 */
	@Override
	public ELResolver getELResolver() {
		if (resolver == null) {
			resolver = new SimpleResolver();
		}
		return resolver;
	}
}

@@ -686,16 +686,47 @@
 ethod;%0D%0A
+import java.util.Collections;%0D%0A
 import j
@@ -1133,56 +1133,69 @@
 p = 
-new HashMap%3CString,Method%3E();%0D%0A%09%09public void set
+Collections.emptyMap();%0D%0A%09%09@Override%0D%0A%09%09public Method resolve
 Func
@@ -1234,31 +1234,16 @@
 Name
-, Method method
 ) %7B%0D%0A%09%09%09
 map.
@@ -1238,22 +1238,29 @@
 ) %7B%0D%0A%09%09%09
+return 
 map.
-pu
+ge
 t(prefix
@@ -1273,32 +1273,24 @@
  + localName
-, method
 );%0D%0A%09%09%7D%0D%0A%09%09@
@@ -1292,42 +1292,23 @@
 %0D%0A%09%09
-@Override%0D%0A%09%09public Method resolve
+public void set
 Func
@@ -1343,37 +1343,117 @@
 ocalName
-) %7B%0D%0A%09%09%09return 
+, Method method) %7B%0D%0A%09%09%09if (map.isEmpty()) %7B%0D%0A%09%09%09%09map = new HashMap%3CString,Method%3E();%0D%0A%09%09%09%7D%0D%0A%09%09%09
 map.
-ge
+pu
 t(prefix
@@ -1462,32 +1462,40 @@
  %22:%22 + localName
+, method
 );%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09st
@@ -1581,43 +1581,28 @@
 p = 
-new HashMap%3CString,ValueExpression%3E
+Collections.emptyMap
 (); 
@@ -1806,24 +1806,105 @@
 ression) %7B%0D%0A
+%09%09%09if (map.isEmpty()) %7B%0D%0A%09%09%09%09map = new HashMap%3CString,ValueExpression%3E();%0D%0A%09%09%09%7D%0D%0A
 %09%09%09return ma
@@ -2293,16 +2293,17 @@
 function
+.
 %0D%0A%09 */%0D%0A
@@ -2531,16 +2531,17 @@
 variable
+.
 %0D%0A%09 */%0D%0A
@@ -2835,32 +2835,98 @@
 tionMapper() %7B%0D%0A
+%09%09if (functions == null) %7B%0D%0A%09%09%09functions = new Functions();%0D%0A%09%09%7D%0D%0A
 %09%09return functio
@@ -3036,16 +3036,82 @@
 er() %7B%0D%0A
+%09%09if (variables == null) %7B%0D%0A%09%09%09variables = new Variables();%0D%0A%09%09%7D%0D%0A
 %09%09return
@@ -3202,16 +3202,29 @@
 esolver%7D
+ if necessary
 .%0D%0A%09 */%0D

fix bug 2590830: SimpleContext.getVariableMapper() (and getFunctionMapper()) returns null