Hi Mike,

On Thu, 2006-08-31 at 19:05 -0700, Mike J Dougherty wrote:
> I'm not real sure how to ask this question, so let me post some sample 
> code and maybe it will help:
> 
> 
> Here are the classes:
> 
> public class Top {
>         private Foo value = null;
>         public void setValue(Foo value) { this.value = value; }
>         public Foo  getValue()          { return value; }
> }
> 
> public class Foo {
>         private String bar = null;
>         public void   setBar(String str) { this.bar = str; }
>         public String getBar()           { return bar; }
> }
> 
> Here's the input XML:
> 
> <root>
>   <top foo="MyFoo"><![CDATA[Some text]]></top>
> </root>
> 
> And here are the rules I've some up with so far:
> 
> <digester-rules>
>   <pattern value="top">
>     <object-create-rule
>         classname="Top" />
>     <pattern value="@foo">
>       <object-create-rule classname="Foo" />
>       <set-properties-rule />
>       <call-method-rule methodname="setBar" paramcount="0"/>
>     </pattern> 
>     <set-next-rule methodname="setValue" />
>   </pattern>
> <digester-rules>
> 
> What I want is to map the value of the "foo" attribute ("MyFoo") to 
> Foo.bar, and then set that instance of the Foo object to Top.value. In 
> the end: topInstance.getFoo().getBar().equals("MyFoo") == true.
> 
> I've looked at the docs, example, and googled on this, and do not see an 
> obvious way to accomplish my objective. Is this possible? If so, what 
> would the rules be?


There is no support in Digester for firing rules on attributes (eg
"@foo"). However I think you can achieve what you want by:
  <pattern value="top">
    <object-create-rule classname="Top"/>
    <object-create-rule classname="Foo"/>
    <set-next-rule methodname="setValue"/>
    <call-method-rule methodname="setBar" paramcount="1"/>
    <call-param-rule attrname="foo"/>
  </pattern>

Note that both the Top and Foo objects get created when the <top>
element is encountered. Rules fire in the order they are added, so the
Top object is guaranteed to be pushed onto the digester stack before the
Foo object.

Of course this will cause complications if you have other attributes on
the <top> element that you really want to map to the Top instance (the
CallMethodRule's stackoffset parameter might help out there).
Fortunately you don't seem to need to do that...

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to