Instantiating formatters

To instantiate a formatter, use definitions and code as shown in the following examples:

Example 1: Instantiate a formatter defined in the formatters file and format (convert from integer to string) and unformat (convert from string to integer) its data element.

Context definition:

<context id="myContext" type="test" parent="nil">
  <refKColl refId="contextCollection">
  </refKColl>
</context> 

Data definition:

<kColl id="contextCollection">
  <field id="field1"/>
  <field id="field2"/>
</kColl>

Formatter definition:

<fmtDef id="format1">
        <fInteger dataName="field2"/>
</fmtDef>

Code:

FormatElement fmt1 = (FormatElement) FormatElement.readObject("format1");
string1 = fmt1.format(Context.getContextNamed("myContext").getElementAt("field2"));
fmt1.unformat(string1,Context.getContextNamed("myContext").getElementAt("field2")); 
/* Unformat using DataElement */

Or to unformat using the context:

fmt1.unformat(string1,Context.getContextNamed("myContext")); 

Example 2: Instantiate a RecordFormat defined in the format definition file and format (convert from each data type to string) and unformat (convert from string to each data type) its data elements. Note that a Delimiter separates the two data fields so that the RecordFormat knows where the first one ends and the second one starts. You can use other decorators such as FixedLength or Identifier to achieve the same result.

Context definition:

<context id="myContext" type="test" parent="nil">
  <refKColl refId="contextCollection">
  </refKColl>
</context>

Data definition:

<kColl id="contextCollection">
  <field id="field1"/>
  <field id="field2"/>
  <refData refId="testCollection"/>
</kColl>
<kColl id="testCollection">
  <field id="field11"/>
  <field id="field22"/>
  <field id="field33"/>
  <field id="field44"/>
</kColl>

Formatter definition:

<fmtDef id="format1">
  <record dataName="testCollection">
    <fInteger dataName="field11"/>
    <fDate dataName="field22"/>
    <fString dataName="field33"/> <delim delimChar="#"/>
    <fString dataName="field44"/>
  </record>
</fmtDef>

Code:

FormatElement fmt1 = (FormatElement) FormatElement.readObject("format1");
string1 = fmt1.format(Context.getContextNamed("myContext"));
fmt1.unformat(string1,Context.getContextNamed("myContext"));
/* Unformat using Context */

Example 3: Instantiate an IndexedCollectionFormat defined in the format definition file and format (convert from each data type to string) and unformat (convert from string to each data type) its data elements.

Context definition:

<context id="myContext" type="test" parent="nil">
  <refKColl refId="contextCollection">
  </refKColl>
</context>

Data definition:

<kColl id="contextCollection">
  <field id="field1"/>
  <field id="field2"/>
  <refData refId="indexCollection"/>
</kColl>
<iColl id="indexCollection">
  <field id="field1"/>
</iColl>

Formatter definition:

<fmtDef id="format1">
  <iCollF dataName="indexCollection">
    <fString dataName="field1"/> <fixedLength length="5"/>
  </iCollF>
</fmtDef>
Code:
FormatElement fmt1 = (FormatElement) FormatElement.
    readObject("format1"); 
string1 = fmt1.format(ContextFactory.getContextNamed ("myContext"). 
    getElementAt("indexCollection"));
fmt1.unformat(string1,ContextFactory.getContextNamed("myContext").
    getElementAt("indexCollection")); 

Example 4: Dynamically create, using the default constructor, a RecordFormat with an Integer decorated with a FixedLength decorator and a Date.

Context definition:

<context id="myContext" type="test" parent="nil">
  <refKColl refId="contextCollection">
  </refKColl>
</context>

Data definition:

<kColl id="contextCollection">
  <field id="field1"/>
  <field id="field2"/>
  <refData refId="testCollection"/>
</kColl>
<kColl id="testCollection">
  <field id="field11"/>
  <field id="field22"/>
</kColl>

Formatter definition: None

Code:
IntegerFormat if1 = new com.ibm.btt.base.IntegerFormat(); 
/* Create IntegerFormat format */
if1.setDataElementName("field1");

FixedLength fl1 = new com.ibm.btt.base.FixedLength(); 
/* Create FixedLength decorator */
fl1.setLength(8);
fl1.setPadCharacter("0".charAt(0));
fl1.setDecorated(if1); 
/* Link decorator to formatter */

DateFormat df1 = new com.ibm.btt.base.DateFormat(); 
/* Create DateFormat formatter */
df1.setDataElementName("field2");

RecordFormat rf1 = new com.ibm.btt.base.RecordFormat(); 
/* Create RecordFormat formatter */
rf1.setName("format1");
rf1.setDataElementName("testCollection");
rf1.add(fl1);
rf1.add(df1);

String string1;
string1 = rf1.format(Context.getContextNamed("myContext"));
rf1.unformat(string1,Context.getContextNamed("myContext"));
/* Unformat using Context */

The code creates a formatter that would have the following definition in the format definition file:

<fmtDef id="format1">
  <record dataName="testCollection">
    <fInteger dataName="field1"/> <fixedLength length="8" padChar="0"/>
    <fDate dataName="field2"/>
  </record>
</fmtDef>

You would instantiate the definition using the following code:

FormatElement rf1 = (FormatElement) FormatElement.readObject("format1");

Example 5: Instantiate an XMLRecordFormat defined in the format definition file and format (convert from each data type to string) and unformat (convert from string to each data type) its data elements. Note that a Delimiter decorator separates the two data fields so that the RecordFormat knows where the first one ends and the second one starts. You can use other decorators such as FixedLength or Identifier to achieve the same result.

Context definition:

<context id="myContext" type="test" parent="nil">
  <refKColl refId="branchManager">
  </refKColl>
</context>

Data definition:

<kColl id="branchManager">
  <field id="Name" value="Robert"/>
  <field id="LastName" value="Silver"/>
</kColl>

Formatter definition:

<fmtDef id=xml1c>
  <fXML dataName="branchManager" includeDataType="true">
    <fString dataName="Name"/> <delim delimChar="#"/>
    <fString dataName="LastName"/> <delim delimChar="#"/>
  </fXML>
</fmtDef>

Code:

XMLFormat fmt1 = new (XMLFormat) 
FormatElement.readObject("xml1c");
string1 = fmt1.format(Context.getContextNamed("myContext"));
fmt1.unformat(string1,Context.getContextNamed("myContext"));
/* Unformat using Context */ 

The result of unformatting string1 is an object that has the following definition:

<branchManager dataType=keyed>
  <Name dataType=field>Robert#</Name>
  <LastName dataType=field>Silver#</LastName>
</branchManager>

Example 6: Instantiate a DynamicRecordFormat defined in the format file and format (convert from each data type to string) and unformat (convert from string to each data type) its data elements. Note that the formatter creates the missing data elements in the data collection during the unformatting process.

Data definition:

<kColl id="branchKColl"l>
  <field id="branchName" value="Branch01"/>
  <field id="branchAddress" value="Downtown"/>
  <field id="branchId" value="222"/>
  <kColl id="branchManager">
    <field id="Name" value="Robert"/>
    <field id="LastName" value="Silver"/>
  </kColl>
</kColl>

Formatter definition:

<fmtDef id="drec1">
  <dRecord>
    <fDate dataName="CurrentDate"/>
    <fString dataName="branchName"/> <delim delimChar="#"/>
    <fString dataName="branchAddress"/> <delim delimChar="#"/>
    <fString dataName="branchId"/> <delim delimChar="#"/>
    <record dataName="branchManager">
      <fString dataName="Name"/><delim delimChar="#"/>
      <fString dataName="LastName"/><delim delimChar="#"/>
    </record>
    <record dataName="Customer">
      <fString dataName="Name"/><delim delimChar="#"/>
      <fString dataName="LastName"/><delim delimChar="#"/>
    </record>
  </dRecord>
</fmtDef>

The results of the unformatting process is an object that has the following definition:

<kColl id="branchKColl">
  <field id="branchName" value="Branch01Barcelona"/>
  <field id="branchAddress" value="Downtown"/>
  <field id="branchId" value="222"/>
  <kColl id="branchManager">
    <field id="Name" value="Robert" />
    <field id="LastName" value="Silver"/>
  </kColl >
  <field id="CurrentDate" value="Thu Sep 09 00:00:00 CEST 1999"/>
  <kColl id="Customer">
    <field id="Name" value="Thomas"/>
    <field id="LastName" value="Gold"/>
  </kColl >
</kColl >