I have an XML document that looks like this:
<!-- language: xml -->
<items>
<item type="java.lang.Boolean" name="foo" value="true" />
</items>
I'd like the <root>
element to create a java.util.Map
object and have each <item>
element create an object of the appropriate type and then add an entry to the Map
-- similar to a SetNextRule
but with an argument to the call coming from the stack.
I've already created a custom Rule
that will create an object of the type specified in the type
attribute (java.lang.Boolean
in this case) using the value in the value
attribute and push it on the stack.
Now, I'd like to pop the item off the top of the stack and use it as an argument to the put
method on the Map
object (which is just "under" the Boolean
object on the stack).
Here's the code I have written so far:
<!-- language: lang-java -->
Digester digester = new Digester();
digester.addObjectCreate("items", HashMap.class);
digester.addRule(new MyObjectCreateRule()); // This knows how to create e.g. java.lang.Boolean objects
digester.addCallMethod("items/item", "put", 2, new Class<?>[] { String.class, Object.class });
digester.addCallParam("items/item", 0, "name");
digester.addCallParam("items/item", 1, true); // take argument from stack
I'm getting the error that the method put
can't be found in the java.lang.Boolean
class. So, the problem is that the e.g. Boolean
object is on the top of the stack, and I want to use it as an argument to the put
method called on the next-to-top element on the stack:
Stack:
java.lang.Boolean value=true <-- top of stack, desired call param
java.util.HashMap contents = {} <-- desired call target
Is there a way to do this with existing commons-digester rules, or do I have to create another custom rule that performs this type of operation?