FlatTargetConverter Class (Extends TargetConverter)

Overview

The FlatTargetConverter again is very similar to the CSVTargetConverter. However, like the FlatSourceConverter, we use a recursive algorithm to process the logical record groups in input documents. We use the base TargetConverter's processGroup method, described in this subsection.

Attributes:

  • None

Methods:

  • Constructor

  • processDocument

  • processGroup (base class method)

Methods

Constructor

The constructor method for our FlatTargetConverter object sets up that object and the FlatRecordWriter object.

Logic for the FlatTargetConverter Constructor Method
Arguments:
  Output Stream Flat Output file
  File Description Document Name

Call base class constructor, passing File
    Description Document Name
Create FlatRecordWriter object, passing:
    File Description DOM Document and Output Stream

processDocument

The bulk of the processing is performed in the FlatTargetConverter's processDocument and processGroup methods. This method converts one input XML Document and writes it to the flat file output stream based on the input parameters. Most of the actual work is done in the processGroup method.

Logic for the FlatTargetConverter processDocument Method
Arguments:
  DOM Document Input Document

Returns:
  Error status or throws exception

Root Element <- Get Document's documentElement attribute
Root Element Name <- Get Root Element's tagName attribute
IF Root Element Name != Grammar Root Element Name
  Return error
ENDIF
Call processGroup, passing Root Element and Grammar Element
Return success

processGroup (Base Class TargetConverter Method)

As with the processGroup method of the SourceConverter we use a recursive algorithm, but the details are a little bit different for the termination case. This method is used for both flat file and EDI conversions.

Logic for the FlatTargetConverter processGroup Method
Arguments:
  DOM Element Parent Element
  DOM Element Group Grammar

Returns:
  Error status or throws exception

Record Grammar Node <- Get firstChild from Grammar Element
    skipping over non-Element Nodes
Record Grammar Element <- Record Grammar Node

//  Process the Group's starting Record Element
Record Element <- Get first childNode from Parent Element,
    skipping over non-Element Nodes

Call RecordWriter's parseRecord, passing Record Element and
    Record Grammar Element
Call RecordWriter derived class's writeRecord, passing Record
    Grammar Element

// This advance makes sure that we don't repeat starting record
//  in group
Record Grammar Element <- Get next Record Element from
    Group Grammar, skipping over non-Element Nodes
Grammar Tag = call Record Grammar Element's getAttribute for
    "ElementName"

Record Element <- Get Record Element's nextSibling, skipping over
    non-Element Nodes

DO until all child Elements of Parent have been processed
  Record Tag <- call Record Element's getNodeName
  DO until Grammar Tag = Record Tag
    Record Grammar Element <- Get next Record Element from
        Group Grammar, skipping over non-Element Nodes
    IF Record Grammar Element is NULL
      Return error  //  This record is not part of the group
    ENDIF
  ENDDO
  Grammar Element Name <- Call Record Grammar Element's
      getNodeName
  IF Grammar Element Name = "GroupDescription"
    Call processGroup, passing Record Element and Record Grammar
        Element
  ELSE
    Call RecordWriter's parseRecord, passing Record Element and
        Record Grammar Element
    Call RecordWriter derived class's writeRecord, passing
        Record Grammar Element
  ENDIF
  Record Element <- Get Record Element's nextSibling, skipping
      over non-Element Nodes
ENDDO

This method is called the first time from processDocument, which passes it the root Element of the Document and the complete Grammar Element. It processes the Element that represents the header record of the logical document, then advances to process its sibling records and groups. Record Elements are processed in the same fashion as the header record Element. Again, recall that in our XML representation of flat files a group of records is explicitly represented by an Element, with the group members (records or other groups) as child Elements. When we encounter an Element that represents a group (that is, when the Element's grammar Element has the name "GroupDescription"), we make a recursive call.

The termination cases are a bit different than those in the FlatSource Converter. In the XML representation the groups are explicitly represented, so we don't have a normal case of encountering an Element that isn't in the current group grammar. If we do hit one, this is an error; we return an error or throw an exception. For normal processing the method continues execution until all the Elements in the source document's group have been processed, then we return back to the caller.

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

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