Runtime components : Core components : Formatters : Tasks : Handling binary message : Integrating with other UDTT components : Integrating new formatter with other UDTT components
  
Integrating new formatter with other UDTT components
Generally speaking, it is suggested that you use the new formatter in your application. The new formatter is an independent component, you can use it anywhere whenever you need it. There are two ways to integrate new formatter with other toolkit components.
Using FormatFacade
The following example demonstrates how to use FormatFacade in other toolkit components:
public class FormatFacade {
public static byte[] formatContext(String formatID, Context ctx)
throws FormatterException {
ReadAdapter adapter = new ContextReadAdapter(ctx);
FormatElement format = FormatFactory.getFormatElement(formatID);
Message message = format.format(adapter);
return message.toBytes();
}
public static void unformatContext(String formatID, byte[] data,
Context ctx) throws FormatterException {
WriteAdapter adapter = new ContextWriteAdapter(ctx);
FormatElement format = FormatFactory.getFormatElement(formatID);
format.unformat(new Message(data), adapter);
}
}
Using FormatAdapter
Sometimes, you cannot use the new formatter directly. In this case, you can use FormatAdapter to integrate the new formatter with other toolkit components.
Configure old formatter component to create FormatAdapter:
You need to configure the old formatter component and new formatter to use the same definition file, and configure the old formatter to create FormatAdapter when necessary. Following is an example:
<kColl id="format">
<field id="extFile" value="format.xml" />
<field id="initializer" value="com.ibm.btt.base.FormatInitializer" />
<field id="Format61RootTagName" value="format"/>
<kColl id="classTable">
</kColl>
</kColl>
<kColl id="format61">
<field id="initializer" value="com.ibm.btt.format.FormatInitializer" />
<field id="extFile" value="format.xml" />
<kColl id="classTable">
<field id="format" value="com.ibm.btt.format.impl.FormatDefine" />
<field id="record" value="com.ibm.btt.format.impl.RecordFormat" />
<field id="fInteger" value="com.ibm.btt.format.impl.IntegerFormat" />
<field id="fString" value="com.ibm.btt.format.impl.StringFormat" />
<field id="fHexDelim" value="com.ibm.btt.format.impl.HexDelimiter" />
<field id="refFmt" value="com.ibm.btt.format.impl.ReferenceFormat" />
<field id="iColl" value="com.ibm.btt.format.impl.IndexedFormat" />
<field id="fixedLength" value="com.ibm.btt.format.impl.FixedLength" />
<field id="delim" value="com.ibm.btt.format.impl.Delimiter" />
</kColl>
</kColl>
In this example, externalizer in the old formatter component creates the a FormatAdapter when the root tag is format.
Define new FormatElement:
Then you need to define FormatElement in the way of new formatter component. The root tag is different with the old formatter. In the old formatter component, the root tag can only be fmtDef. But in the new formatter component, you can configure the root tag in classTable.
Following is an example:
<?xml version="1.0"?>
<format.xml>
<format id="sampleFmt">
<record>
<fString dataName="f1" />
<delim delimChar="#" />
<fString dataName="f2" />
<delim delimChar="#" />
</record>
</format>
</format.xml>
Use FormatAdapter in other UDTT components:
Then you can use the new formatter in the way of the old formatter:
com.ibm.btt.base.FormatElement format = (com.ibm.btt.base.FormatElement)
com.ibm.btt.base.FormatElement.readObject("sampleFmt");
Context ctx = ContextFactory.createContext("testCtx");
String str1 = format.format(ctx);
In this way, the format instance is created from FormatElement. FormatElement.readObject("sampleFmt") is an instance of com.ibm.btt.format.impl.FormatAdapter. FormatAdapter extends com.ibm.btt.base.FormatElement and it delegates all the logic to the new formatter component.
Go up to
Integrating with other UDTT components