commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Baltz, Kenneth" <Kba...@firstam.com>
Subject RE: [Digester] How to specify a <call-param-rule> parameter from top-of-stack
Date Fri, 24 Jan 2003 16:51:14 GMT
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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message