commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Smith <Paul.Sm...@lawlex.com.au>
Subject RE: [Digester]: Primitives & Object create rules help
Date Tue, 25 Feb 2003 22:45:47 GMT
Thanks Robert, this works a treat, however I got bitten by the method
signature of the body method... I must be using an older version of digester
(1.3), because extending from Rule only has a body(String) method, and no
namespace aware stuff, and I was scratching my head why my breakpoint &
debug messages inside the rule were never getting fired... 

For anyone else interested, for Digester 1.3, this seems to work great:

  public static class CreateLongRule extends Rule {

    public void body(String text) throws
    Exception {
      digester.push(new Long(text));
    }

    public void end(String name) {
      digester.pop();
    }
  }

Thanks so much Robert for heading me down the right track!

cheers,

Paul Smith

> -----Original Message-----
> From: robert burrell donkin
> [mailto:robertburrelldonkin@blueyonder.co.uk]
> Sent: Wednesday, 26 February 2003 7:21 AM
> To: Jakarta Commons Users List
> Subject: Re: [Digester]: Primitives & Object create rules help
> 
> 
> hi paul
> 
> there isn't such a helper class at the moment (AFAIK no one's 
> asked for 
> one before).
> 
> here's an (untested) Rule that should create a Long and push 
> it onto the 
> stack. (you'll need to add a setNextRule so that the long 
> gets added to 
> your collection.)
> 
> public class CreateLongRule extends Rule {
> 	public void body(String namespace, String name, String 
> text) throws 
> Exception {
> 		digester.push(new Long(text));
> 	}
> 
> 	public vod end(String namespace, String name) {
> 		digester.pop();
> 	}
> }
> 
> primitive conversion rules for this situation might be good 
> additions to 
> digester - possibly using ConvertUtils to provide custom conversions. 
> anyone fancy giving them a go?
> 
> - robert
> 
> On Tuesday, February 25, 2003, at 06:05 AM, Paul Smith wrote:
> 
> > Firstly, I am still pretty new to Digester.
> >
> > I am needing to use Digester to read xml of the form of 
> this test case:
> >
> >   public void test1() throws Exception
> >   {
> >     String xml = "<Collection>" +
> >                         "  <ID>1</ID> " +
> >                         "</Collection>";
> >
> >    Digester digester = new Digester();
> >    digester.addObjectCreate("Collection", 
> java.util.ArrayList.class);
> >    digester.addObjectCreate("Collection/ID", java.lang.Long.class);
> >    digester.addSetNext("Collection/ID", "add" , "java.lang.Object");
> >
> >    Collection collection = (Collection)digester.parse( new 
> > StringReader(xml)
> > );
> >    assertTrue("Should contain 1 instance: size=" + 
> collection.size(),
> > collection.size()==1);
> >    assertTrue("Should contain 1: size=" + collection.size() + ",
> > collection=" + collection, collection.contains(new Long(1)));
> >   }
> > At the end of the day, I want this:
> >
> > <Collection>
> >  <ID>101</ID>
> >  <IDI>105</ID>
> > </Collection>
> >
> > To be digested into a Collection of Long wrappers.
> >
> > But I am of course getting the following exception:
> >
> > java.lang.InstantiationException: java.lang.Long
> >
> > because there is no no-arg constructor for long.  I really 
> am hoping I 
> > don't
> > have to create an ObjectCreationFactory, as I was hoping 
> there'd be some
> > helper class within Digester to assist with primitives as 
> such (such as a
> > NumberObjectCreationFactory?).
> >
> > Any help would be appreciated.
> >
> > cheers,
> > _________________________
> > Paul Smith
> > Lawlex Compliance Solutions
> > phone: +61 3 9278 1511
> > email: paul.smith@lawlex.com.au
> >
> >
> >
> > 
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: 
> commons-user-help@jakarta.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 

Mime
View raw message