commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rahul Akolkar <rahul.akol...@gmail.com>
Subject Re: [digester] Need help with simple digester usage
Date Tue, 21 Dec 2010 06:17:44 GMT
On Mon, Dec 20, 2010 at 11:08 PM, KARR, DAVID (ATTSI) <dk068x@att.com> wrote:
>> -----Original Message-----
>> From: KARR, DAVID (ATTSI)
>> Sent: Monday, December 20, 2010 7:54 PM
>> To: user@commons.apache.org
>> Subject: [digester] Need help with simple digester usage
>>
>> I'm trying to use Digester for a simple application.  I want to define
>> the structure of a binary tree in XML, and then construct that node
>> tree
>> using Digester.
>>
>> The trivial "Node" class is just this:
>> --------------------
>>     public static class Node {
>>         private Node left;
>>         private Node right;
>>         private String value;
>>
>>         public Node getLeft() { return left; }
>>         public Node getRight() { return right; }
>>         public String getValue() { return value; }
>>
>>         public void setLeft(Node left) { this.left = left; }
>>         public void setRight(Node right) { this.right = right; }
>>         public void setValue(String value) { this.value = value; }
>>     }
>> ------------
>>
>> The XML is composed of nested "node" elements.  The first "node" child
>> gets set as the "left" property of the root, and the second gets set
> as
>> the "right" property.
>>
>> For instance:
>>
>>   <node value="abc">
>>     <node value="def"/>
>>   </node>
>>
>> Should result in a root Node with a "left" property referring to the
>> second node.
>>
>> So, going through the Digester documentation, which I haven't looked
> at
>> in many years, I figured it would be something like this:
>>
>>         digester.addRule("*/node", new
>> ObjectCreateRule(AugmentedNode.class));
>>         digester.addRule("*/node", new SetPropertiesRule());
>>         digester.addRule("*/node", new SetNextRule("addChild",
>> AugmentedNode.class.getName()));
>>
>> Where "AugmentedNode" extends "Node", adding:
>>
>>        public void addChild(Node node) {
>>             if (getLeft() == null)
>>                 setLeft(node);
>>             else
>>                 setRight(node);
>>         }
>>
>> For those well steeped in Digester lore, I imagine you can immediately
>> tell this won't work.  You're right, of course.  This dies with an NPE
>> in "MethodUtils.invokeMethod()" (object is null).  I have no clue
>> what's
>> wrong with this, or what I should be doing instead.
>
> Oh, ok.  I figured out one thing.  I have to push an AugmentedNode on
> the stack before I start parsing, and then I take the result as the
> "left" property of that top node.  Is that all I should have figured
> out?  Is there a better way to do this in the first place?
>
<snip/>

Add this as the first rule, then the parse method will get you the
actual (root) node:

  digester.addRule("node", new ObjectCreateRule(AugmentedNode.class));

This calls out the root as different from other nodes -- the root
doesn't need a SetNextRule (thats the one causing the NPE since it has
no parent).

-Rahul

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message