commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Naresh Bhatia" <NBha...@sapient.com>
Subject RE: [Digester] How to specify a <call-param-rule> parameter from top-of-stack
Date Fri, 24 Jan 2003 17:25:05 GMT
Yep, that would work - neat trick! Although, I agree with you that the
ability to address the object on the stack as an argument would be
nicer. I thought that this could be done programatically using
Digester.addCallParam(java.lang.String pattern, int paramIndex, boolean
fromStack) - just that I could not find a way to do this via XML.

Thanks again.
Naresh

-----Original Message-----
From: Baltz, Kenneth [mailto:Kbaltz@firstam.com] 
Sent: Friday, January 24, 2003 11:51 AM
To: Jakarta Commons Users List
Subject: RE: [Digester] How to specify a <call-param-rule> parameter
from top-of-stack


Here's the solution I was given.  I think it would be easier if we could
reference the item on the stack as an argument.

The trick is to create a new object that takes care of populating the
HashMap for you.

E.g.

// Has to be public, although an anonymous inner class would be great
public class HashMapProxy {
	HashMap _map;
	public Hashmap Proxy() {
		_map = new HashMap();
	}

	public void addProduct( Product p ) {
		_map.put( p.getPartNumber(), p );
	}

	public HashMap getMap() {
		return _map;
	}
}

Use the following Digester code:


    digester.addObjectCreate( "products", HashMapProxy.class );
    digester.addObjectCreate( "products/product", Product.class );
	... initialize your Product object
    digester.addSetNext( "products/product", "addProduct" );

    HashMapProxy proxy = (HashMapProxy)digester.parse( XML );

    return proxy.getMap();


Hope that helps, hope they find a better way.

K.C.

> -----Original Message-----
> From: Naresh Bhatia [mailto:NBhatia@sapient.com]
> Sent: Thursday, January 23, 2003 9:44 PM
> To: commons-user@jakarta.apache.org
> Subject: [Digester] How to specify a <call-param-rule> parameter from 
> top-of-stack
> 
> 
> I am trying to parse an XML file similar to the one shown below:
> 
> <products>
>   <product partNumber="1000">
>       ...
>   </product>
>   <product partNumber="2000">
>       ...
>   </product>
> </products>
> 
> I want Digester to return a HashMap with partNumber as the key and 
> product as the value. How do I put the product on to the HashMap? I 
> guess I don't know how <call-param-rule> can specify the object from 
> top of the stack as a parameter. Here's my unsuccessful attempt at 
> this:
> 
> <digester-rules>
>   <object-create-rule pattern="products"
> classname="java.util.HashMap"/>
> 
>   <pattern value="products/product">
>     <object-create-rule classname="Product"/>
> 
>     <set-properties-rule>
>       <alias attr-name="partNumber" prop-name="partNumber"/>
>     </set-properties-rule>
> 
>     <call-method-rule methodname="put" paramcount="2"
>       paramTypes="java.lang.String,Product"/>
>     <call-param-rule paramnumber="0" attrname="partNumber"/>
>     <call-param-rule paramnumber="1"/>  <--- what to do here?
>   </pattern>
> 
> </digester-rules>
> 
> Thanks.
> Naresh Bhatia
> 

Mime
View raw message