SourceConverter Base Class (Extends Converter)

Overview

The SourceConverter, derived from the Converter class, is the base class for the CSV, flat file, and EDI source converter classes developed in the next three chapters. The lists below summarize the major attributes and methods.

Attributes:

  • DOM Document Output Document

  • RecordReader Reader

  • String Base Output Directory

  • String Schema Location URL

  • Boolean Validation Option

Methods:

  • Constructor

  • processFile (virtual)

  • processGroup

  • saveDocument

The processGroup method is listed here for completeness but will be developed in Chapter 8.

Methods

Constructor

The operations common to all our legacy format source converters include setting up the base output directory name and implementation-dependent DOM setup for output document validation.

Logic for the SourceConverter Constructor Method
Arguments:
  Boolean Validation Option
  String Base Output Directory

Call base class constructor
Validation Option <- Passed validation option
Base Output Directory <- From passed base output directory,
    Appending directory separator character if required
Perform any implementation specific setup for validating
    output documents before writing them

saveDocument

This operation is coded as a separate function since it may be called within a while loop that reads records from the input file and after exiting the while loop to save the last document.

Logic for the SourceConverter saveDocument Method
Arguments:
  DOM Document Output Document
  String Output File Name
  Boolean Validate

Returns:
  Status value or throws exception

IF Validate is true
  Validate output DOM Document
ENDIF
Save DOM Output Document to passed File Name, dependent on
    implementation

An enhanced version of this utility might give the option of looking up and calling an in-memory XSLT transformation on the DOM Document before it is saved to disk. We might even hand off the DOM Document in memory to a message handling service for e-commerce applications. We won't get that advanced in this book (KISS over efficiency), but the options certainly exist.

Derived Class Method

processFile

This method is declared as a virtual method in the base class and implemented separately in each of the derived classes for our legacy formats. The general processing model is to read a single file with multiple logical business documents, create a separate XML instance document from each logical document in the input file, and write a separate file for each of the XML instance documents. In an e-commerce type of situation where some of the documents are intended for different external organizations, we'll write the documents for each organization to a separate directory. The listing below shows the general logic.

Logic for the SourceConverter processFile Method
Arguments:
  String Input File Name

Initialize Sequence Number
Open Input File
Call RecordReader's setInputStream method, passing the input
    Stream
Record Length <- Call RecordReader's readRecord method
DO while Record Length => 0
  Record Grammar <- get Grammar for Record Type
  Call RecordReader's parseRecord method, passing Record
      Grammar
  Call RecordReader's toXMLType method
  IF Break on Partner ID
    Output Directory <- Base Output Directory + Partner ID
    IF New Partner
      Store new Partner in Partner Array
      Make Output Directory for Partner
    ENDIF
  ENDIF
  IF Break on Document
    IF Output DOM Document != null
      Call saveDocument, passing Output DOM Document and
        Output File Path
      Set Output DOM Document to null
    ENDIF
    Create new Output DOM Document
    Create Root Element and Append to Output DOM Document
    IF Schema Location URL is not null
      Create noNamespaceSchemaLocation Attribute and
          append to Root Element
    ENDIF
    Increment Sequence Number
    Pad Sequence Number to 3 digits with leading zeroes
    Output File Path <- Output Directory + Sequence Number
        + ".xml"
    Call RecordReader's setOutputDocument method, passing
        Output DOM Document
  ENDIF
  Call RecordReader's writeRecord method, passing Parent Element
      and Grammar Element for Record
  Record Length <- Call RecordReader's readRecord method
ENDDO
IF Output DOM Document != null
  Call saveDocument, passing Output DOM Document and
    Output File Path
ENDIF
Close input stream
Display completion message with the number of documents
processed

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

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