com.prowidesoftware.swift.io.parser
Class SwiftParser

java.lang.Object
  extended by com.prowidesoftware.swift.io.parser.SwiftParser

public class SwiftParser
extends java.lang.Object

FIN Parser. This implementation now properly supports all system messages (i.e: messages for MT 0xx) and service messages (for example: ACK).
As part of this, the following is now also accepted:


This is based in the old SwiftParser2, that is now deprecated.

Author:
www.prowidesoftware.com

Field Summary
static java.lang.String EOL
          Helper constant with the content of System.getProperty("line.separator", "\n")
 
Constructor Summary
SwiftParser()
          default constructor.
NOTE: If this constructor is called, setReader must be called to use the parser
SwiftParser(java.io.InputStream is)
          Constructor with an input stream for parsing a message
SwiftParser(java.io.Reader r)
          Constructor with a reader for parsing a message
SwiftParser(java.lang.String message)
          Constructor with a reader for parsing a message
 
Method Summary
protected  SwiftBlock4 block4Consume(SwiftBlock4 b, java.lang.String s)
          Parses a block 4 from an input string.
protected  SwiftBlock consumeBlock()
          Consume the next block of the message on the reader.
protected  Tag consumeTag(java.lang.String buffer, java.lang.String unparsedText)
          Process the input as a tag.
protected  void findBlockStart()
          read on the reader until a block start character or EOF is reached.
 java.util.List getErrors()
          Get a copy of the errors found.
protected  char identifyBlock(java.lang.String s)
          Identify the block to be consumed.
 SwiftMessage message()
          Parse a SWIFT message into a data structure
 SwiftMessage parse(java.lang.String message)
          Sets the parameter string as this parser data and returns the parsed object.
protected  java.lang.String readUntilBlockEnds()
          Reads the buffer until end of block is reached.
 void setData(java.lang.String data)
          sets the input data to the received string.
 void setReader(java.io.Reader r)
          sets the input reader.
NOTE: this resets the internal buffer
protected  SwiftTagListBlock tagListBlockConsume(SwiftTagListBlock b, java.lang.String s)
          consumes a tag list block (i.e: block 3, block 5 or user defined block)
protected  int textTagEndBlock4(java.lang.String s, int start, boolean isTextBlock)
          finds the end of a text tag (i.e: ":TAG:VALUE").
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOL

public static final java.lang.String EOL
Helper constant with the content of System.getProperty("line.separator", "\n")

Constructor Detail

SwiftParser

public SwiftParser(java.io.InputStream is)
Constructor with an input stream for parsing a message

Parameters:
is - stream to read

SwiftParser

public SwiftParser(java.io.Reader r)
Constructor with a reader for parsing a message

Parameters:
r - the Reader with the swift message to read

SwiftParser

public SwiftParser(java.lang.String message)
Constructor with a reader for parsing a message

Parameters:
message - the String with the swift message to read

SwiftParser

public SwiftParser()
default constructor.
NOTE: If this constructor is called, setReader must be called to use the parser

Method Detail

setReader

public void setReader(java.io.Reader r)
sets the input reader.
NOTE: this resets the internal buffer

Parameters:
r - the reader to use

setData

public void setData(java.lang.String data)
sets the input data to the received string.

Parameters:
data - the data to use as input

message

public SwiftMessage message()
                     throws java.io.IOException
Parse a SWIFT message into a data structure

Returns:
the parsed swift message object
Throws:
java.io.IOException

parse

public SwiftMessage parse(java.lang.String message)
                   throws java.io.IOException
Sets the parameter string as this parser data and returns the parsed object.

Parameters:
message - the String with the swift message to parse
Returns:
the parsed swift message object
Throws:
java.io.IOException
Since:
6.0

consumeBlock

protected SwiftBlock consumeBlock()
                           throws java.io.IOException
Consume the next block of the message on the reader. This methods seeks to a block start, then identifies the block and calls the proper method to consume the block type that is coming, not all blocks are parsed in the same manner.

Parameters:
blockHint -
Returns:
the next block in the reader or null if none was found (i.e: end of input)
Throws:
java.io.IOException - if an error occurred during read

tagListBlockConsume

protected SwiftTagListBlock tagListBlockConsume(SwiftTagListBlock b,
                                                java.lang.String s)
                                         throws java.io.IOException
consumes a tag list block (i.e: block 3, block 5 or user defined block)

Parameters:
b - the block to set up tags into
s - the block data to process
Returns:
the processed block (the parameter b)
Throws:
java.io.IOException

block4Consume

protected SwiftBlock4 block4Consume(SwiftBlock4 b,
                                    java.lang.String s)
                             throws java.io.IOException
Parses a block 4 from an input string. This method supports the two possible formats of a swift block 4:

Parameters:
b - the block to set up tags into
s - the block data to process
Returns:
the processed block (the parameter b)
Throws:
java.io.IOException

textTagEndBlock4

protected int textTagEndBlock4(java.lang.String s,
                               int start,
                               boolean isTextBlock)
finds the end of a text tag (i.e: ":TAG:VALUE"). This is used to parse block 4.
The function search the string looking for the occurrence of any of the sequences:
where "[LBR]" stands for any of: "[CR]", "[LF]" or "[CR][LF]" and "[X]" is any character other than [CR] and [LF].
Then considers the end of the tag as NOT containing the found sequence.
NOTE: the condition "-}" cannot happen because the terminating dash is already removed.
renamed to state clearly that this search is only used in block4Consume

Parameters:
s - the FIN input text
start - the position to start analysis at
Returns:
the position where the tag ends (excluding the )

consumeTag

protected Tag consumeTag(java.lang.String buffer,
                         java.lang.String unparsedText)
                  throws java.io.IOException
Process the input as a tag. That is: split name and value (and possibly unparsed texts).
The received buffer contains only the pertinent data for the tag (name and value). Trailing [CR][LF] on the text MUST not be present.

Parameters:
buffer - the buffer containing the tag
unparsedText - the unparsed text to assign (use null if none is wanted). This single text is fragmented in multiple texts if there are more than one message.
Returns:
a swift Tag
Throws:
java.io.IOException

identifyBlock

protected char identifyBlock(java.lang.String s)
Identify the block to be consumed.

Parameters:
s - the block identifier
Returns:
the block identifier or a space if the block can not be identified

readUntilBlockEnds

protected java.lang.String readUntilBlockEnds()
                                       throws java.io.IOException
Reads the buffer until end of block is reached. The initial character of the block must be already consumed and the reader has to be ready to consume the first character inside the block

This method assumes that the starting block character was consumed because that is required in order to identify the start of a block, and call this method which reads until this block ends.

Returns:
a string with the block contents
Throws:
java.io.IOException

findBlockStart

protected void findBlockStart()
                       throws java.io.IOException
read on the reader until a block start character or EOF is reached.

Throws:
java.io.IOException - if thrown during read

getErrors

public java.util.List getErrors()
Get a copy of the errors found. users can manipulate this copy without affecting the original.

Returns:
the list of errors found