You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Kai <lo...@gmx.net> on 2003/06/30 18:58:58 UTC

[Digester DynaBeans] How to combine ?

Hi all,

i am new to digester and i find the philosophy behind it very 
interesting. The following question turned out:

is it possible (and if, how) using Digester to parse my xml file and
creating dynabeans out of the parsed properties ?
My xml file's content changes frequently and therefore i don't want to 
create "hard wired" beans. I would like to have beans instead, whose 
getters and setters dynamically adapt to the parsed properties. I read 
about DynaBeans, but i do not know how to combine Digester and Dynabeans.


An example for clearness:
my xml file is:

<root>
	<field>
		<element name="test" id="1" value="testtest" />
	</field>
</root>

what i did is: i created an "element" bean with "name", "id" and "value"
properties.

but, if the underlying "element" tag changes, like for example:
..
	<element name="test" show="all" sort="asc" />
..
	
i have to adapt my bean, but it is tedious.
I know, i could use map-backed beans, but i would prefer using DynaBeans.

Could anybody give me a hint please ?


Thank you for your time.

Kai



Re: [Digester DynaBeans] How to combine ?

Posted by Kai <lo...@gmx.net>.
Erik Price schrieb:
> 
> 
> Kai wrote:
> 
>> Hi all,
>>
>> i am new to digester and i find the philosophy behind it very 
>> interesting. The following question turned out:
>>
>> is it possible (and if, how) using Digester to parse my xml file and
>> creating dynabeans out of the parsed properties ?
> 
> 
> Do you mean to generate the code to make a dynabean class?  I suppose 
> you would have to write a class such as DynaBeanGenerator which has 
> methods that can be called by a Digester instance as it parses the XML, 
> perhaps writing out the code to a StringBuffer.  Then at the end of the 
> XML file the Digester can instruct the DynaBeanGenerator to write the 
> StringBuffer's contents to a file, or something.
> 
> But note that there are some XDoclet tags that can let you go in the 
> other direction.  I'm not sure about Dynabeans.  But you can incorporate 
> the XDoclet tags into your Java code that automatically generates your 
> struts-config.xml file.
> 
> 
> Erik

Erik,
thanks for your reply, but i think i did not point out my problem 
clearly enough:

i have a class which instantiates Digester and parses / maps my xml file:
..

   Digester digester = new Digester();

     digester.push(this);
     digester.setValidating(true);


     digester.addObjectCreate("fields/element",  ElementBean.class  );
     digester.addSetProperties("fields/element");


  ...

ElementBean is a Bean whith fixed properties. If properties in my xml 
file change, i also have to change ElementBean.
My idea is to generate a DynaBean on the fly by setting the parsed 
properties as bean properties.

In the package description of beanutils i found the following code:

  DynaProperty[] props = new DynaProperty[]{
         new DynaProperty("address", java.util.Map.class),
         new DynaProperty("subordinate", mypackage.Employee[].class),
         new DynaProperty("firstName", String.class),
         new DynaProperty("lastName",  String.class)
       };
     BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);
DynaBean employee = dynaClass.newInstance();
employee.set("address", new HashMap());
..

My understanding is that i can dynamically create properties by filling 
the DynaPropery[] array with DynaProperties and then instantiating the 
bean with it, like above.

i am searching for a way to parse my xml file with digester and create
these dynabean-properties with DynaProperties. Then, after having set up 
my dynabean i want it to be filled with the property values.
I want the whole process to be done by Digester.



Kai



Re: [Digester DynaBeans] How to combine ?

Posted by Erik Price <ep...@ptc.com>.

Kai wrote:
> Hi all,
> 
> i am new to digester and i find the philosophy behind it very 
> interesting. The following question turned out:
> 
> is it possible (and if, how) using Digester to parse my xml file and
> creating dynabeans out of the parsed properties ?

Do you mean to generate the code to make a dynabean class?  I suppose 
you would have to write a class such as DynaBeanGenerator which has 
methods that can be called by a Digester instance as it parses the XML, 
perhaps writing out the code to a StringBuffer.  Then at the end of the 
XML file the Digester can instruct the DynaBeanGenerator to write the 
StringBuffer's contents to a file, or something.

But note that there are some XDoclet tags that can let you go in the 
other direction.  I'm not sure about Dynabeans.  But you can incorporate 
the XDoclet tags into your Java code that automatically generates your 
struts-config.xml file.


Erik