import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.jxpath.Variables;
import org.apache.commons.jxpath.JXPathContext;

public class VariableBug
{
	// =======================================================
	// Simple implementation of Variables
	// =======================================================

	public static class CustomVariables
	extends HashMap
	implements Variables
	{
		public void declareVariable(String key, Object obj) {
			put(key, obj);
		}

		public Object getVariable(String key) {
			System.err.println("*** fetching variable "+key);
			return get(key);
		}

		public boolean isDeclaredVariable(String key) {
			System.err.println("*** checking for variable "+key);
			return containsKey(key);
		}

		public void undeclareVariable(String key) {
			put(key, null);
		}
	}

	// =======================================================
	// Main
	// =======================================================

	public static void main (String [] args)
	throws Exception
	{
		// create a set of variables and add some data.  notice
		// we use a variable with a prefixed name.
		CustomVariables c = new CustomVariables();
		c.declareVariable("foo:bar", "foobar_val");
		c.declareVariable("foo", "foo_val");

		// create an empty context, and install custom variables
		JXPathContext ctx = JXPathContext.newContext(null);
		ctx.setVariables(c);

		// this will work fine, and we'll get the value we expect
		System.err.println("$foo = "+ctx.getValue("$foo"));
		System.err.println();

		// this will fail because, while you check for the variable
		// with the QName, you fetch it with the simple name
		System.err.println("$foo:bar = "+ctx.getValue("$foo:bar"));
		System.err.println();
	}

}
