You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jon Brule <br...@rochester.rr.com> on 2004/11/25 17:45:41 UTC

[Digester] DynaBean Hierarchy and set-next-rule XML rule

Greetings!

I am using Digester to consume an XML stream that I intend to be a hierarchy
of Commons DynaBeans. For the sake of flexibility, I am using the XML Rules
mechanism in conjunction with object factories. I can create the whole
hierarchy of beans without a problem. I can even register the child beans
with their parent. I, however, had to make each parent bean extend
BasicDynaBean in order to add special "set" methods (i.e. setChild1(Child1
child)) to allow the set-next-rule to properly function. Each of these
special methods simple calls the DynaBean.set() method with the appropriate
property. 

I'd love to eliminate these special parent DynaBeans and just use
BaseDynaBean.

Is there anyway to use set-next-rule / SetNextRule with DynaBeans directly
(i.e. pass more than one argument, namely the property name as well)? If
not, is there another way with the common rules that I can eliminate these
custom DynaBeans? Or must I write my own rule extension to SetNextRule?

At this point, my thought is that I must extend the SetNextRule to achieve
my desired functionality. Does this mean that I need to extend another class
to handle the set-next-rule XML rule?

I have included abbreviate versions of the core files in question. Please
let me know if more code / information is needed...

Thanks. This component is fantastic. It has REALLY made my life easier!


Jon Brule

-----

Supporting files (stripped down for brevity):

weather.xml:
  <digester-rules>
    <pattern value="weather">
      <factory-create-rule
classname="jrb.service.schedule.sensor.weather.WeatherBeanFactory"/>
      <bean-property-setter-rule pattern="cc/lsup"
propertyname="lastUpdate"/>
      <bean-property-setter-rule pattern="cc/tmp"
propertyname="temperature"/>
      <pattern value="head">
        <factory-create-rule
classname="jrb.service.schedule.sensor.weather.UnitsBeanFactory"/>
        <bean-property-setter-rule pattern="ud" propertyname="distance"/>
        <set-next-rule methodname="setUnits"/>
      </pattern>
  <digester-rules>

WeatherBean:
    public class WeatherBean extends BasicDynaBean {
        public WeatherBean(DynaClass dynaClass) {
		super(dynaClass);
        }	
        public void setUnits(DynaBean units) {
            this.set(WeatherBeanFactory.PROP_UNITS, units);
        }
    }

WeatherBeanfactory:
    public class WeatherBeanFactory extends AbstractObjectCreationFactory {
        static final String BEAN_WEATHER = "Weather";
        static final String PROP_LAST_UPDATE = "lastUpdate";
        static final String PROP_TEMPERATURE = "temperature";
        static final String PROP_UNITS = "units";
	
        public Object createObject(Attributes attributes) throws Exception
{
            DynaClass beanClass = new BasicDynaClass(
                BEAN_WEATHER,
                WeatherBean.class,
                new DynaProperty[] {
                    new DynaProperty(PROP_LAST_UPDATE, Date.class),	
                    new DynaProperty(PROP_TEMPERATURE, Integer.class),	
                    new DynaProperty(PROP_UNITS, DynaBean.class)
                }
            );
            DynaBean dynabean = beanClass.newInstance();
            return dynabean;
        }
    }

UnitsBeanFactory:
    public class UnitsBeanFactory extends AbstractObjectCreationFactory {
        private static final String BEAN_UNITS = "Units";
        private static final String PROP_DISTANCE = "distance";
	
        public Object createObject(Attributes attributes) throws Exception {
            DynaClass beanClass = new BasicDynaClass(
                BEAN_UNITS,
                null,
                new DynaProperty[] {
                    new DynaProperty(PROP_DISTANCE, String.class)
                }
            );
            DynaBean dynabean = beanClass.newInstance();
            return dynabean;
        }	
    }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] DynaBean Hierarchy and set-next-rule XML rule

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Fri, 2004-11-26 at 11:00, robert burrell donkin wrote:
> On 25 Nov 2004, at 18:50, Craig McClanahan wrote:
> 
> > The "Set Next" rule is optimized for the case of attaching a child to
> > a parent via a standard JavaBean property setter, so it doesn't know
> > anything about DynaBeans.  However, you might want to take a look at
> > the "Call Method" rule instead ... you can set up a call to a method
> > that takes parameters, so you should be able to call the standard
> > set() method on the DynaBean directly, passing the property name key
> > and the appropriate value.
> 
> +1
> 
> i'm not sure whether there is an elegant way to fit dynabeans support 
> to set next (and other rules). if there is, then i'd probably support 
> adding it.

I've added a TODO item to the wiki (see 2.1.10):
 http://wiki.apache.org/jakarta-commons/Digester/TODO

I hope I got the details right..

Regards,

Simon




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] DynaBean Hierarchy and set-next-rule XML rule

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On 25 Nov 2004, at 18:50, Craig McClanahan wrote:

> The "Set Next" rule is optimized for the case of attaching a child to
> a parent via a standard JavaBean property setter, so it doesn't know
> anything about DynaBeans.  However, you might want to take a look at
> the "Call Method" rule instead ... you can set up a call to a method
> that takes parameters, so you should be able to call the standard
> set() method on the DynaBean directly, passing the property name key
> and the appropriate value.

+1

i'm not sure whether there is an elegant way to fit dynabeans support 
to set next (and other rules). if there is, then i'd probably support 
adding it.

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Digester] DynaBean Hierarchy and set-next-rule XML rule

Posted by Craig McClanahan <cr...@gmail.com>.
The "Set Next" rule is optimized for the case of attaching a child to
a parent via a standard JavaBean property setter, so it doesn't know
anything about DynaBeans.  However, you might want to take a look at
the "Call Method" rule instead ... you can set up a call to a method
that takes parameters, so you should be able to call the standard
set() method on the DynaBean directly, passing the property name key
and the appropriate value.

Craig



On Thu, 25 Nov 2004 11:45:41 -0500, Jon Brule <br...@rochester.rr.com> wrote:
> Greetings!
> 
> I am using Digester to consume an XML stream that I intend to be a hierarchy
> of Commons DynaBeans. For the sake of flexibility, I am using the XML Rules
> mechanism in conjunction with object factories. I can create the whole
> hierarchy of beans without a problem. I can even register the child beans
> with their parent. I, however, had to make each parent bean extend
> BasicDynaBean in order to add special "set" methods (i.e. setChild1(Child1
> child)) to allow the set-next-rule to properly function. Each of these
> special methods simple calls the DynaBean.set() method with the appropriate
> property.
> 
> I'd love to eliminate these special parent DynaBeans and just use
> BaseDynaBean.
> 
> Is there anyway to use set-next-rule / SetNextRule with DynaBeans directly
> (i.e. pass more than one argument, namely the property name as well)? If
> not, is there another way with the common rules that I can eliminate these
> custom DynaBeans? Or must I write my own rule extension to SetNextRule?
> 
> At this point, my thought is that I must extend the SetNextRule to achieve
> my desired functionality. Does this mean that I need to extend another class
> to handle the set-next-rule XML rule?
> 
> I have included abbreviate versions of the core files in question. Please
> let me know if more code / information is needed...
> 
> Thanks. This component is fantastic. It has REALLY made my life easier!
> 
> Jon Brule
> 
> -----
> 
> Supporting files (stripped down for brevity):
> 
> weather.xml:
>   <digester-rules>
>     <pattern value="weather">
>       <factory-create-rule
> classname="jrb.service.schedule.sensor.weather.WeatherBeanFactory"/>
>       <bean-property-setter-rule pattern="cc/lsup"
> propertyname="lastUpdate"/>
>       <bean-property-setter-rule pattern="cc/tmp"
> propertyname="temperature"/>
>       <pattern value="head">
>         <factory-create-rule
> classname="jrb.service.schedule.sensor.weather.UnitsBeanFactory"/>
>         <bean-property-setter-rule pattern="ud" propertyname="distance"/>
>         <set-next-rule methodname="setUnits"/>
>       </pattern>
>   <digester-rules>
> 
> WeatherBean:
>     public class WeatherBean extends BasicDynaBean {
>         public WeatherBean(DynaClass dynaClass) {
>                 super(dynaClass);
>         }
>         public void setUnits(DynaBean units) {
>             this.set(WeatherBeanFactory.PROP_UNITS, units);
>         }
>     }
> 
> WeatherBeanfactory:
>     public class WeatherBeanFactory extends AbstractObjectCreationFactory {
>         static final String BEAN_WEATHER = "Weather";
>         static final String PROP_LAST_UPDATE = "lastUpdate";
>         static final String PROP_TEMPERATURE = "temperature";
>         static final String PROP_UNITS = "units";
> 
>         public Object createObject(Attributes attributes) throws Exception
> {
>             DynaClass beanClass = new BasicDynaClass(
>                 BEAN_WEATHER,
>                 WeatherBean.class,
>                 new DynaProperty[] {
>                     new DynaProperty(PROP_LAST_UPDATE, Date.class),
>                     new DynaProperty(PROP_TEMPERATURE, Integer.class),
>                     new DynaProperty(PROP_UNITS, DynaBean.class)
>                 }
>             );
>             DynaBean dynabean = beanClass.newInstance();
>             return dynabean;
>         }
>     }
> 
> UnitsBeanFactory:
>     public class UnitsBeanFactory extends AbstractObjectCreationFactory {
>         private static final String BEAN_UNITS = "Units";
>         private static final String PROP_DISTANCE = "distance";
> 
>         public Object createObject(Attributes attributes) throws Exception {
>             DynaClass beanClass = new BasicDynaClass(
>                 BEAN_UNITS,
>                 null,
>                 new DynaProperty[] {
>                     new DynaProperty(PROP_DISTANCE, String.class)
>                 }
>             );
>             DynaBean dynabean = beanClass.newInstance();
>             return dynabean;
>         }
>     }
> 
> ---------------------------------------------------------------------
> 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