commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [Digester]: Primitives & Object create rules help
Date Tue, 25 Feb 2003 20:20:52 GMT
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
>


Mime
View raw message