Site Navigation:

sbxb

A binding example using inheritance

The SoftBean object model

We get a drawing which is a collection of abstract Forms. We cannot instantiate directly this object but we must instantiate objects of sub-classes, according to the "type" property.

The target XML format

<Drawing>

<paper>A4</paper>

<Shape type="CIRCLE">

<position>(100,100)</position>

<center>(0,0)</center>

<radius>50</radius>

</Shape>

<Shape type="RECTANGLE">

<position>(100,100)</position>

<width>100</width>

<height>50</height>

</Shape>

</Drawing>

The binding specification

dbs.org.otvl.sbxbsamp.simple.Drawing.0.paper=paper

dbs.org.otvl.sbxbsamp.simple.Drawing.1.theShapes=Shape(dbs)\

{(org.otvl.sbxbsamp.simple.Circle,@type,CIRCLE)\

(org.otvl.sbxbsamp.simple.Rectangle,@type,RECTANGLE)}

dbs.org.otvl.sbxbsamp.simple.Circle.0.type=@type

dbs.org.otvl.sbxbsamp.simple.Circle.1.position=position

dbs.org.otvl.sbxbsamp.simple.Circle.2.center=center

dbs.org.otvl.sbxbsamp.simple.Circle.3.radius=radius

dbs.org.otvl.sbxbsamp.simple.Rectangle.0.type=@type

dbs.org.otvl.sbxbsamp.simple.Rectangle.1.position=position

dbs.org.otvl.sbxbsamp.simple.Rectangle.2.width=width

dbs.org.otvl.sbxbsamp.simple.Rectangle.3.height=height

Here the 2nd binding rule adds a type specifier between { and }. It is used when creating Shape sub-objects to the property "theShapes" of the Drawing object. This is a collection of rules giving:

  • the class to be instantiated

  • the XML attribute providing information to switch between different rules

  • the value of this XML attribute

The only possibility to switch between rules is to provide a XML attribute in the XML element of the sub-object. If not, it would not be possible for the XML parser to know which object it is handling, and which binding rules to use.