Context Methods

All the customization of the data processing in TextConverter should be implemented as part of the context methods. The context methods are called by TextConverter during the conversion process and are described in the table below.

#

Context Method

Auto

Manual/
Delimited

Frequency of Calls

Description

1.      

OnSetup

+

+

Once

Called before processing started. Used for input and output files programmatic initialization.

2.      

OnStartProcess

+

+

Once or once per input file in case of batch processing

Called before the first input record is processed. Used for any processing initialization.

3.      

OnInputField(template, field, code)

+

 

Multiple

Allows to create scripted definitions of the following tag field properties:

Code       Property
tag           Search tag
ttag          Top tag
btag         Bottom Tag
skip          Skip Line or Skip From
skipto      Skip To

A corresponding field property should be marked as <script> in order for this method to get called.
OnInputField should return true if a requested property was recognized.
Search tag script should also return a starting position and a width of a found tag through a call to template.SetTag. It can also return a tag field value by calling template.SetTagFIeld. The option indicator in this case should be <script_val>.

4.      

OnInputLine

+

 

Multiple

Called on processing each input line. Used for custom parsing of each line obtained using the this.GetLine() function. Usage of input and output dictionary fields is not recommended. 

5.      

OnInputRecord(template)

+

 

Multiple

Called when a segment of input lines is recognized as belong to an input template. Most useful in conjunction with Detailed Manual templates.

6.      

OnTemplate(template, code)

+

 

Multiple

Allows to create scripted definitions of the following input template properties:

Code       Property
trange     Top Range
brange    Bottom Range
skip         Skip Line or Skip From
skipto      Skip To

A corresponding template property should be marked as <script> in order for this method to get called.
OnTemplate should return true if a requested property was recognized.

7.      

OnOutputRecord

+

 

Multiple

Called before generating output record. Used for implementing custom data transformations.

8.      

IsNewRecord

 

+

Multiple

Called on each input line. Used for implementing arbitrary record delimiters.

9.      

OnRecord

 

+

Multiple

Called on each input line. Used for implementing manual parsing rules.

10.    

OnFinishProcess

+

+

Once

Called after the last input record is processed. Used for implementing post one input file processing actions.

11.    

OnFinishBatch

+

+

Once

Called after the batch processing is finished. Used for implementing post processing actions.


Below are the samples of the context methods' implementation:

OnSetUp

OnSetUp is called before connections to all datasources used or referenced are established allowing you to alter input/output datasources safely.  Any and all setup or alteration of input and output datasources should be done at this time.

Function: OnSetUp

Parameters:
none

Return value:
none

VB sample This sample creates and opens a text file to be used during the conversion procedure:

Function OnSetUp
dim inputA
inputA = "C:\Users\Name\Desktop\dataquick.pdf"
this.SetInputFile(inputA)
End Function

OnStartProcess

OnStartProcess is called before the first input record is processed. Implement this method if you want to make any kind of custom preparation for the process like creating a new file for custom output, setting up a database connection to be used as an additional reference during the conversion and so on.

Function: OnStartProcess

Parameters:
none

Return value:
none

VB sample This sample creates and opens a text file to be used during the conversion procedure:

Dim fso, file
Function OnStartProcess
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set file = fso.CreateTextFile( "c:/work.txt", true )
file.WriteLine( "Process Started" )
End Function

OnFinishProcess

This method is called after the last input record is processed. Implement this method if you want to customize the final actions after the conversion process is done.

Function OnFinishProcess

Parameters:
none

Return value:
none

VB sample

This sample closes the text file created in the previous sample:

Function OnFinishProcess
file.WriteLine( "Process Ended" )
file.Close()
End Function 

IsNewRecord

IsNewRecord is called for each new line of the input text. An implementation of this method is needed to handle an irregular record delimiter. For example, a record number is a designated record delimiter and several input text lines belong to a single input record. You can not use the record delimiter option for a non-constant delimiter like this, but you can easily solve the problem by implementing IsNewRecord.

Function IsNewRecord( text_line )

Parameter:
string text_line - a line of the input text, which should be analyzed to make a decision if a new record begins with this line

Return value:
boolean true|false
   return true to indicate that the line is a beginning of a new record; otherwise, return false .

VB sample

This sample illustrates how to handle a non constant record delimiter (record number)

Function IsNewRecord( text_line )
Dim words
words = target.SplitString( text_line )
If IsArray( words ) And IsNumeric( words(0) ) Then IsNewRecord = true Else IsNewRecord = false
End Function 

OnRecord

OnRecord is called for each input record before the corresponding output record is inserted into the output database table. That is the primary method to be implemented for custom calculations of output values, input record analysis, filtering of output records and other tasks.  (See samples and walkthroughs for more information)

Function OnRecord

Parameters:
none

Return value:
none

VB sample

This sample shows how to implement OnRecord to have an output field as a function of two input fields

Function OnRecord
DictOut.Field.value = Left( DictIn.Field1.value, 3 ) & Left( DictIn.Field2.value, 4 )
End Function

OnRecordDone

OnRecordDone is called after TextConverter has inserted a new output record into the output database table. You would rarely implement this method.  Its primary use is handling of custom errors.

Function OnRecordDone( ok )

Parameter:
boolean ok - true if the record was inserted successfully, false if errors occurred.

Return value:
none

VB sample

This sample demonstrates custom error log implementation. db_log is a DataObject variable

Function OnRecordDone( ok )
If ok Then Exit Function
db_log.AppendStart()
db_log.SetCelllValue "Description", "Record failed"
db_log.SetCelllValue "RecordInfo", DictOut.Field.value
db_log.AppendFinish
End Function

Related Sections

TextConverter's Methods

TARGET Object

TextConverter's Concept

Setting up a Conversion: Step-by-Step

Samples and Walkthroughs

Scripting

Automation


Comments