Runtime components : Core components : Data elements : Tasks : Extending data elements
  
Extending data elements
The data element classes can be easily extended to support application needs. For example, to extend a data field with the myAttribute instance variable, follow these steps:
1 Create a new class, for example, MyDataField extending DataField.
2 In the configuration file for the client, add the relationship between the class name and the tag name.
For example, the following data field definition would be added to the keyed collection of data definitions in the btt.xml file:
<kColl id="data">
  <field id="extFile" value="dsedata.xml" />
  <field id="initializer" value="com.ibm.btt.base.DataInitializer" />
  <kColl id="classTable">
    <field id="otherType" value="myPackage.MyDataField"/>
  </kColl>
</kColl>
Depending on how the processes using this data field are distributed, the definition may need to exist in the configuration files for both the client and the server.
Alternatively, you can use implClass attribute to specify the class when you are defining an instance of the new entity in the data definition file. For example, you could add the following definition to the data file:
<otherType id="myName" implClass="myPackage.MyDataField"/>
Since you have already specified the implementation class name, you do not need to add the mapping between the tag name "otherType" and the class name "myPackage.MyDataField" in the tags keyed collection of btt.xml.
3 In MyDataField, create a variable such as myAttribute to hold the new attribute.
4 In MyDataField, create the getter and setter methods for the new variable such as setMyAttribute and getMyAttribute.
5 In MyDataField, override the initializeFrom(Tag) method.
This method instantiates the object with its attributes specified in the XML file. For example:
/**
* Initialize MyDataField with the Tag attributes.
* @return java.lang.Object
* @param aTag com.ibm.btt.base.Tag
* @exception java.io.IOException
*/
public Object initializeFrom(Tag aTag) throws java.io.IOException {
value = null;
super.initializeFrom(aTag);
for (int i=0; i<aTag.getAttrList().size(); i++) {
TagAttribute attribute = (TagAttribute) aTag.getAttrList().elementAt(i);
if (attribute.getName().equals("myAttribute")) {
setMyAttribute((String)(attribute.getValue()));
}
}
return this;
}
6 In MyDataField, override the toString() method.
This method gives a visual representation of the object. For example:
public String toString() {
  String s="<otherType ";
  s=JavaExtensions.addAttribute(s,"id",getName());
  s=JavaExtensions.addAttribute(s,"value",""+getValue());
  s=JavaExtensions.addAttribute(s,"description",getDescription());
  s=JavaExtensions.addAttribute(s,"myAttribute",getMyAttribute());
s=s+">";
  return s;
}
The visual representation of an instance of this new data element type appears in the XML file as follows:
<otherType id="myName" value="100" myAttribute="xxx"/>
Note After completing this task you can also add attribute to a data field without extending a class. See Adding a new attribute to a data field.
See also
Tasks