Defining a constructor
When defining a new tag, you may want to influence, how XJConf instantiates an object. This can be done by defining a constructor for the tag:
<tag name="color" type="net.schst.XJConf.Examples.ConstructorColor">
<constructor>
<attribute name="red" type="java.lang.Integer"/>
<attribute name="green" type="java.lang.Integer"/>
<attribute name="blue" type="java.lang.Integer"/>
</constructor>
</tag>
Now this tag can be used to create a new instance of this class:
public class ConstructorColor { private Integer red = null; private Integer green = null; private Integer blue = null; /** * @param red * @param green * @param blue */ public ConstructorColor(Integer red, Integer green, Integer blue) { super(); this.red = red; this.green = green; this.blue = blue; } }
XJConf will convert the values of the attributes red, green and blue to Integer objects and pass them to the constructor.
Passing child elements to the constructor
In the first example you defined a new tag that allowed you to pass three attribute values to the constructor. Now image your XML document looked like this:
<color> <red>100</red> <green>100</green> <blue>100</blue> </color>
You can still use the same class, by just modifying the tag definition, so the cild elements <red/>, <green/> and <blue/> will be used instead of the attributes:
<tag name="color" type="net.schst.XJConf.Examples.ConstructorColor">
<constructor>
<child name="red"/>
<child name="green"/>
<child name="blue"/>
</constructor>
</tag>
<tag name="red" type="java.lang.String"/>
<tag name="green" type="java.lang.String"/>
<tag name="blue" type="java.lang.String"/>
Passing the tag content to the constructor
It is also possible, to pass the character data between the opening and closing element to the constructor of your class and even convert it to any type before creating the new class:
<tag name="complex" type="net.schst.XJConf.Examples.MyClass">
<constructor>
<cdata type="java.lang.Integer"/>
</constructor>
</tag>
You can mix attributes and character data, they will be passed to the constructor in the same order as you defined them in your tag definition.
The default construcutor
If you are not defining a constructor, XJConf will try to call a constructor, which accepts a String as its sole argument and pass the contents of the tag to it. So is the same as:
<tag name="foo" type="java.lang.String">
<constructor>
<cdata type="java.lang.String"/>
</constructor>
</tag>
