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 Fri, 14 Jan 2011 05:47:35 GMT
> -----Original Message-----
> From: Rahul Akolkar [mailto:rahul.akolkar@gmail.com]
> Sent: Thursday, January 13, 2011 7:42 PM
> To: Commons Users List
> Subject: Re: [digester] Need help with simple digester usage
> 
> On Thu, Jan 13, 2011 at 7:40 PM, KARR, DAVID (ATTSI) <dk068x@att.com>
> wrote:
> >> -----Original Message-----
> >> From: Rahul Akolkar
> >> Sent: Thursday, January 13, 2011 2:07 PM
> >> To: Commons Users List
> >> Subject: Re: [digester] Need help with simple digester usage
> >>
> >> On Thu, Jan 13, 2011 at 12:39 PM, KARR, DAVID (ATTSI)
> <dk068x@att.com>
> >> wrote:
> >> >> -----Original Message-----
> >> >> From: Rahul Akolkar
> >> >> Sent: Monday, December 20, 2010 10:18 PM
> >> >> To: Commons Users List
> >> >> Subject: Re: [digester] Need help with simple digester usage
> >> >>
> >> >> 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).
> >> >
> >> > I'm still not quite sure how to transform my existing code so I
> can
> >> use the top node, not the "left" of the top node.
> >> >
> >> > My existing code is this:
> >> >
> >> >        digester.push(new AugmentedNode());
> >> >
> >> >        digester.addRule("*/node", new
> >> ObjectCreateRule(AugmentedNode.class));
> >> >        digester.addRule("*/node", new SetPropertiesRule());
> >> >        digester.addRule("*/node", new SetNextRule("addChild",
> >> AugmentedNode.class.getName()));
> >> >
> >> >        return ((Node) digester.parse(new
> >> StringReader(xml))).getLeft();
> >> >
> >> > I've tried several variations with adding that line you suggest,
> but
> >> I can't get it to work, and I'm not sure exactly what it's trying to
> >> accomplish.
> >> >
> >> <snip/>
> >>
> >> Try this, per previous email:
> >>
> >>     digester.addRule("node", new
> >> ObjectCreateRule(AugmentedNode.class));
> >>     // above line is better match for root (not using push)
> <snip/>
> 
> Given the root node can also have a value attribute, need this (insert
> after the line above):
> 
>     digester.addRule("node", new SetPropertiesRule());

Ah.  Got it.  That worked.  Thanks.

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


Mime
View raw message