You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Nuno Miguel Ferreira <Nu...@siemens.com> on 2004/12/10 16:37:17 UTC

[Digester] Object Create Rule

  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.DisplayPreferenc
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 ...

  Thanks,

  Nuno Belo

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


Re: [Digester] Object Create Rule

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
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