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:

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:

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>
<kColl id="indexCollection">
  <field id="field1"/>
</kColl>

Formatter definition:

<fmtDef id="format1">
  <iCollF dataName="indexCollection">
    <fString dataName="field1"/> <fixedLength length="5"/>
  </iCollF>
</fmtDef>

Code:

Example 4: 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:

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 5: 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 >

Example 6: Using the default constructor, dynamically create formatter that builds a RecordFormat with a Date and an Integer that is decorated with a FixedLength decorator. This sample is only for the toolkit application presentation layer entities or client entities, which access the formatters directly instead of through the CHA Formatter Service.

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:

com.ibm.dse.base.IntegerFormat if1 = new
com.ibm.dse.base.IntegerFormat(); 
/* Create IntegerFormat format */
if1.setDataElementName("field1");
 
com.ibm.dse.base.FixedLength fl1 = new com.ibm.dse.base.FixedLength(); 
/* Create FixedLength decorator */
fl1.setLength(8);
fl1.setPadCharacter("0".charAt(0));
fl1.setDecorated(if1); 
/* Link decorator to formatter */
com.ibm.dse.base.DateFormat df1 = new com.ibm.dse.base.DateFormat(); 
/* Create DateFormat formatter */
df1.setDataElementName("field2");
 
com.ibm.dse.base.RecordFormat rf1 = new com.ibm.dse.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 with the following definition:

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

To instantiate the formatter:

com.ibm.dse.base.FormatElement rf1 = (com.ibm.dse.base.FormatElement) com.ibm.dse.base.FormatElement.readObject("format1");