commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "KARR, DAVID (ATTSI)" <dk0...@att.com>
Subject RE: [digester] Need help with simple digester usage
Date Tue, 21 Dec 2010 04:08:04 GMT
> -----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?

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


Mime
View raw message