commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [Digester] Object Create Rule
Date Sun, 12 Dec 2004 11:47:08 GMT

On 10 Dec 2004, at 15:37, Nuno Miguel Ferreira wrote:

>   Hi there,
>
>   I'm using Digester to parse a XML file that contains some display
> information about a chart. One of the sections in the XML has color
> properties as you can see below:
>
>   THE XML FILE SAMPLE
>   -------------------
>   (...)
>
>   <display-preferences>
> 	<series>
> 		<serie visible="true">
> 			<name>DHTAllocSR</name>
> 			<color r="255" g="0" b="0" a="255"/>
> 		</serie>
>
> 		<serie visible="true">
> 			<name>DHTBUSYRATE</name>
> 			<color r="255" g="200" b="0" a="255"/>
> 		</serie>
> 	</series>
>
>   (...)
>
>
>   THE RULES XML FILE SAMPLE
>   -------------------------
>   (...)
>   <pattern value="display-preferences">
> 	<object-create-rule
> classname="com.siemens.gui.pmonline.reports.setup.model.DisplayPreferen 
> c
> es"/>
>
> 	<pattern value="series">
> 		<object-create-rule classname="java.util.ArrayList"/>
>
>   		   	<pattern value="serie">
>    		   		<object-create-rule
> classname="com.siemens.gui.pmonline.reports.setup.model.Serie"/>
> 	   		   		
>    		   		<set-properties-rule>
> 					<alias attr-name="visible"
> prop-name="visible"/>
> 				</set-properties-rule>
>    		   		
>    		   		<bean-property-setter-rule
> pattern="name"/>
>    		   		
>    		   		<pattern value="color">
>    		   			<object-create-rule
> classname="java.awt.Color"/>
> 					
>    		   		</pattern>
>    		   		
>    		   		<set-next-rule methodname="add"/>
>    		   	</pattern>
>  		   	
> 		<set-next-rule methodname="setSeriesSettings"/>
> 	</pattern>
>   (...)
>
>   I'm parsing this file using a XML rules file like the one above, and
> what I wanted to do is create a java.awt.Color when the parser finds  
> the
> color tag. The problem is the java.awt.Color constructor ... It has no
> empty constructor so the million dollar question is: can I use the
> object-create-rule with extra parameters? Such as r (red), g (green), b
> (blue) and a (alpha) as defined in the XML. I wanted to avoid the
> creation of another object that will then map into a java.awt.Color ...
> This doesn't seem right if I can do it directly ... Is there any way to
> do this? Help is really needed in this matter ...

the usual way to do this (using the standard rules) is to define a  
custom ObjectCreationFactory. however, i'm not sure how well this would  
work when using a rules xml file.

it should be possible to use reflection to create a generic rule that  
would allow attributes to populate a non-empty constructor. i'm not  
sure whether i'll be able to get round to this any time soon but it  
should be pretty easy. anyone fancy volunteering to take this one on?

- robert


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


Mime
View raw message