commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve Pannier" <Steve_Pannier....@jacada.com>
Subject [JXPath] Problem with maps and variables
Date Tue, 14 Jan 2003 21:37:18 GMT
I am having some problems using JXPath with Maps and variables.
I have one or more maps that I add as variables to a global context.
When I retrieve a value from a map containing repeating data using a
hard-coded index in the expression, it works OK as follows:

  Iterator iter = (Iterator)context.iterate("$map/dataMap/Foo/Bar[3]");

If I replace the hard-coded index value with a simple Integer-type
variable,
it also works:

  context.getVariables.declareVariable("index", new Integer(3));
  Iterator iter = (Iterator)context.iterate("$map/dataMap/Foo/Bar
[$index]");

But, if I use a variable that points to a map containing an Integer value
as the index, I don't get the expected results.

  Iterator iter = (Iterator)context.iterate("$map/dataMap/Foo/Bar
[$map/dataMap/Foo/Count]");

It seems to return the entire contents of "$map/dataMap/Foo/Bar".
Furthermore, if I evaluate the expression "$map/dataMap/Foo/Count",
the result is 3 of type Integer, as I would expect.

Has anyone else seen any problems in this area?

The following is a simple program showing the above problems.


import java.util.*;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;

public class TestMap3 {

    public TestMap3(String args[]) {
    }

    private void run() {
        MapHolder mh1 = new MapHolder();
        JXPathContext context = JXPathContext.newContext(null, null);
        context.getVariables().declareVariable("map1", mh1);
        context.getVariables().declareVariable("index", new Integer(2));

        // Hard-coding array index works OK
        getXPathValue(context, "$map1/dataMap/Foo/Dog[2]");

        // Using a simple Integer variable as the index works OK
        getXPathValue(context, "$map1/dataMap/Foo/Dog[$index]");

        // 1st problem: If Foo/Count is 2, this expression should return
one
        // value only ("St. Bernard"), but instead returns entire list of
dogs.
        // (In fact, no matter what Foo/Count is (1, 2, or 299) it always
returns
        // entire list of dogs.)
        getXPathValue(context, "$map1/dataMap/Foo/Count");  // verify index
value
        getXPathValue(context, "$map1/dataMap/Foo/Dog
[$map1/dataMap/Foo/Count]");

        // Another related problem: Use getValue(): No matter what the
value of
        // Foo/Count is, it always returns 1st dog ("Shepard")
        Object result = context.getValue("$map1/dataMap/Foo/Dog
[$map1/dataMap/Foo/Count]");
        System.out.println("\n   ****** Result using getValue():");
        System.out.println("        " + result + ";\tType: " +
result.getClass().getName());

        System.out.println("\n MainMap: " + mh1.getDataMap());
    }

    /**
     * Evaluate the given expression within the JXPath context and
     * display the results.
     *
     * Returns the result object (if more than one, returns last one)
     */
    private static Object getXPathValue(JXPathContext context, String expr)
{
        java.util.Iterator iter;
        Object result = null;

        System.out.println("\n   ****** Result of \"" + expr + "\":");
        iter = (java.util.Iterator)context.iterate(expr);
        if (!iter.hasNext())
            System.out.println("        EMPTY!!!");
        else {
            while (iter.hasNext()) {
                result = iter.next();
                System.out.println("        " + result + ";\tType: " +
result.getClass().getName());
            }
        }
        return result;
    }

    /**
     *
     */
    public static void main(String args[]) {
        TestMap3 testMap = new TestMap3(args);
        testMap.run();
    }


public class MapHolder {
    private Map dataMap = null;

    public MapHolder() {
        // Populate map contents
        dataMap = new HashMap();
        List l0 = new ArrayList();
        dataMap.put("Foo", l0);

        Map m = new HashMap();
        List l = new ArrayList();
        m.put("Count", l);
        l.add(new Integer(299));
        l0.add(m);

        m = new HashMap();
        l = new ArrayList();
        m.put("Dog", l);
        l.add("Shepard");
        l.add("St. Bernard");
        l0.add(m);
    }

    public Map getDataMap() {
        return dataMap;
    }
}

}



Steve Pannier
Jacada, Inc.
(763) 201-0002 Ext. 219
spannier@jacada.com
http://www.jacada.com



Mime
View raw message