FlatRecordWriter Class (Extends RecordWriter)

Overview

As is the case with the CSVRecordWriter class, the FlatRecordWriter is derived from the RecordWriter base class (see Chapter 6). The most important method developed in this class is the writeRecord method. We use the parseRecord method inherited from the RecordWriter base class.

Attributes:

  • Integer Fixed Record Length

  • Integer Record ID Field Offset

  • Integer Record ID Field Length

Methods:

  • Constructor

  • writeRecord

Methods

Constructor

The logic for the FlatRecordWriter constructor method is essentially the same as that for the FlatRecordReader.

Logic for the FlatRecordWriter Constructor Method
Arguments:
  DOM Document File Description Document
  Output Stream

Call RecordWriter base class constructor, passing File
    Description Document and Output Stream
Record Format Element <- Get "RecordFormat" Element from File
    Description Document
Child Element <- Get first childNode from Record Format Element,
    advancing over non-Element Nodes
Fixed Record Length <- 0
IF Child Element NodeName = Fixed
  Fixed Record Length <- Call Child Element's getAttribute for
      "Length"
ELSE
  Record Terminator <- Call Child Element's getAttribute for
      "RecordTerminator"
  Call setTerminator to set the Record Terminators
ENDIF
Tag Info Element <- Get "RecordTagInfo" Element from File
    Description Document
Record ID Field Offset <- Call Tag Info's getAttribute for
    "Offset"
Record ID Field Length <- Call Tag Info's getAttribute for
    "Length"

writeRecord

The writeRecord method handles the aspects of record formatting unique to each legacy format. We should again note two restrictions mentioned at the beginning of the chapter. The record identifier field will be converted if it is present in the input XML document, but the value will be overridden by the value specified in the grammar. If a fixed record length is specified the record is initialized to ASCII spaces. Variable length records are initialized with the null character.

The approach for writing a record to flat files is somewhat different than the approach we used for writing to CSV files. For CSV files we walked through the DataCell Array and built the output buffer from its contents. Column number was key, and if a column was missing, a column delimiter was inserted. Due to the requirement to fill missing fields with a default fill character, we must take a different approach and drive the conversion from the record grammar instead of the contents of the DataCell Array. We will cycle though the FieldDescription Elements of the RecordDescription, find matching fields in the DataCell Array, and build the output buffer accordingly. If a field in the grammar is not present in the DataCell Array built from the input, we fill it using the fill character specified in the grammar.

Logic for the FlatRecordWriter writeRecord Method
Arguments:
  DOM Element Record Grammar

Returns:
  Error status or throws exception

IF Fixed Record Length > 0
  Initialize Output Record Buffer to null characters
ELSE
  Initialize Output Record Buffer to ASCII spaces
ENDIF
Record Length <- 0
Cell Array Index <- 0
Field Grammar NodeList <- Call Record Grammar's
    GetElementsByTagName for "FieldDescription"
DO for all items in Field Grammar NodeList
  Field Grammar Element <- next item from Field Grammar NodeList
  Grammar Field Number <- call Field Grammar Element's
      getAttribute for "FieldNumber"
  Offset <- Call Grammar Element's getAttribute for "Offset"
  Length <- Call Grammar Element's getAttribute for "Length"
  Field Written <- false
  IF (Index <= Highest Cell)
    Field Number <- call CellArray[Index] getFieldNumber
    IF (Field Number = Grammar Field Number)
      Call CellArray[Index] fromXML
      Call CellArray[Index] prepareOutput
      Field Contents <- Call CellArray[Index] getField
      Output Buffer <- Insert Field Contents at Offset for Length
      Clear CellArray[Index]
      Increment Cell Array Index
      Field Written <- true
    ENDIF
  ENDIF
  IF ( Field Written = false)
    Fill Character <- Call Grammar Element's getAttribute for
        "FillCharacter"
    Output Buffer <- Insert Fill Character at Offset for Length
  ENDIF
  Working Length <- Offset + Length
  IF (Working Length > Record Length)
    Record Length = Working Length
  ENDIF
ENDDO
Highest Cell <- -1
Record ID Value <- Call Record Grammar Element's getAttribute
    for "TagValue"
Output Buffer <- Insert Record ID Value at Record ID Field
    Offset for Record ID Field Length
IF Fixed Record Length = 0
  Append base RecordHandler's Record Terminators to Output
      Buffer
  Increment Record Buffer Length
ENDIF
Call language's write routines to do physical write of
    Output Buffer for Record Length
Return success

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset