Class SwiftParser
- java.lang.Object
-
- 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:
- Block 4 may be a non-text block (for example: {4:{101:xx}{102:xx}})
- Support for unparsed texts (at message, block and tag levels)
- Support for user defined blocks (for example: {S:{T01:xxx}{T02:yyy}})
Field32A This is based in the old SwiftParser2, that is now deprecated.
-
-
Constructor Summary
Constructors Constructor Description SwiftParser()
default constructor.
NOTE: If this constructor is called, setReader must be called to use the parserSwiftParser(java.io.File messageFile)
Create a parser and feed it with the contents of the given fileSwiftParser(java.io.InputStream is)
Constructor with an input stream for parsing a message encoded inStandardCharsets.UTF_8
.SwiftParser(java.io.Reader r)
Constructor with a reader for parsing a messageSwiftParser(java.lang.String message)
Constructor with a String for parsing a message
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected SwiftBlock
consumeBlock(UnparsedTextList unparsedReceiver)
Consume the next block of the message on the reader.protected SwiftBlock4
consumeBlock4(SwiftBlock4 b, java.lang.String s)
Parses a block 4 from an input string.protected SwiftTagListBlock
consumeTagListBlock(SwiftTagListBlock b, java.lang.String s)
consumes a tag list block (i.e: block 3, block 5 or user defined block)protected Tag
createTag(java.lang.String buffer, java.lang.String unparsedText)
Process the input as a tag.protected java.lang.String
findBlockStart()
read on the reader until a block start character or EOF is reached.protected int
findEndOfTagByLineFeed(java.lang.String s, int start, boolean isTextBlock)
Finds the end of a text tag (i.e: ":TAG:VALUE").SwiftParserConfiguration
getConfiguration()
Gets the current parse configurationjava.util.List<java.lang.String>
getErrors()
Get a copy of the errors found during the parsing of the message.protected char
identifyBlock(java.lang.String s)
Identify the block to be consumed.protected boolean
isTagStart(java.lang.String s, int i)
Evaluates if the string at the given position has the format nn[a]: which means it is a proper tag start.SwiftMessage
message()
Parse a SWIFT message into a data structure.static SwiftBlock1
parseBlock1(java.lang.String s)
Parses a string containing an MT message block 1 contentstatic SwiftBlock2
parseBlock2(java.lang.String s)
Parses a string containing an MT message block 2 content.static SwiftBlock2Input
parseBlock2Input(java.lang.String s)
Parses a string containing an MT message block 2 input content (outgoing message sent to SWIFT).static SwiftBlock2Output
parseBlock2Output(java.lang.String s)
Parses a string containing an MT message block 2 output content (incoming message received from SWIFT).static SwiftBlock3
parseBlock3(java.lang.String s)
Parses a string containing an MT message block 3 contentstatic SwiftBlock4
parseBlock4(java.lang.String s)
Parses a string containing the text block of an MT messagestatic SwiftBlock5
parseBlock5(java.lang.String s)
Parses a string containing an MT message block 5 contentprotected java.lang.String
readUntilBlockEnds()
Reads the buffer until end of block is reached.void
setConfiguration(SwiftParserConfiguration configuration)
Sets a new parse configurationvoid
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
-
-
-
Constructor Detail
-
SwiftParser
public SwiftParser(java.io.InputStream is)
Constructor with an input stream for parsing a message encoded inStandardCharsets.UTF_8
.- 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 String 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
-
SwiftParser
public SwiftParser(java.io.File messageFile) throws java.io.IOException
Create a parser and feed it with the contents of the given file- Parameters:
messageFile
- existing, readable file to read- Throws:
java.io.IOException
- if an error occurs during read- Since:
- 7.7
-
-
Method Detail
-
parseBlock4
public static SwiftBlock4 parseBlock4(java.lang.String s)
Parses a string containing the text block of an MT message- Parameters:
s
- block content starting with "{4:\r\n" and ending with "\r\n-}"- Returns:
- content parsed into a block 4 or an empty block 4 if string cannot be parsed
- Since:
- 7.8.4
-
parseBlock3
public static SwiftBlock3 parseBlock3(java.lang.String s)
Parses a string containing an MT message block 3 content- Parameters:
s
- block content starting with "{3:" and ending with "}"- Returns:
- content parsed into a block 3 or an empty block 3 if string cannot be parsed
- Since:
- 7.8.6
-
parseBlock5
public static SwiftBlock5 parseBlock5(java.lang.String s)
Parses a string containing an MT message block 5 content- Parameters:
s
- block content starting with "{5:" and ending with "}"- Returns:
- content parsed into a block 5 or an empty block 5 if string cannot be parsed
- Since:
- 7.8.6
-
parseBlock1
public static SwiftBlock1 parseBlock1(java.lang.String s)
Parses a string containing an MT message block 1 content- Parameters:
s
- block content starting with "{1:" and ending with "}"- Returns:
- content parsed into a block 1 or an empty block 1 if string cannot be parsed
- Since:
- 7.8.6
-
parseBlock2
public static SwiftBlock2 parseBlock2(java.lang.String s)
Parses a string containing an MT message block 2 content.Will return either a
SwiftBlock2Input
orSwiftBlock2Output
depending on the block content.- Parameters:
s
- block content starting with "{2:" and ending with "}"- Returns:
- content parsed into a block 2 or an empty block 2 (output) if string cannot be parsed
- Since:
- 7.8.6
-
parseBlock2Input
public static SwiftBlock2Input parseBlock2Input(java.lang.String s)
Parses a string containing an MT message block 2 input content (outgoing message sent to SWIFT).If you don't know the container message direction, user
parseBlock2(String)
instead.- Parameters:
s
- block content starting with "{2:I" and ending with "}"- Returns:
- content parsed into a block 2 or an empty block 2 if string cannot be parsed
- Since:
- 7.8.6
-
parseBlock2Output
public static SwiftBlock2Output parseBlock2Output(java.lang.String s)
Parses a string containing an MT message block 2 output content (incoming message received from SWIFT).If you don't know the container message direction, user
parseBlock2(String)
instead.- Parameters:
s
- block content starting with "{2:O" and ending with "}"- Returns:
- content parsed into a block 2 or an empty block 2 if string cannot be parsed
- Since:
- 7.8.6
-
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.By default this implementation uses the default parser behaviour which is lenient and will do a best effort to read as much from the message content as possible regardless of the content and block boundaries being valid or not. For instance, it will read the headers even if the value length is incorrect, and it will read the text block (block 4) even if it is missing the closing hyphen and bracket.
For more options check
setConfiguration(SwiftParserConfiguration)
IMPORTANT: Since the parser is initialize with a Reader, this method is not reentrant. Once a message was parsed, the next call to this method will produce a message with null blocks.
- Returns:
- the parsed swift message object
- Throws:
java.io.IOException
- if an error occurs during read
-
consumeBlock
protected SwiftBlock consumeBlock(UnparsedTextList unparsedReceiver) 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:
unparsedReceiver
- may be null, the unparsedTextList that will receive the chunks that can not be identified sas part of the message- 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
-
consumeTagListBlock
protected SwiftTagListBlock consumeTagListBlock(SwiftTagListBlock b, java.lang.String s)
consumes a tag list block (i.e: block 3, block 5 or user defined block)- Parameters:
b
- the block to set up tags intos
- the block data to process- Returns:
- the processed block (the parameter b)
-
consumeBlock4
protected SwiftBlock4 consumeBlock4(SwiftBlock4 b, java.lang.String s)
Parses a block 4 from an input string. This method supports the two possible formats of a swift block 4:
- Text mode: this is the common block 4 for categories 1 to 9.
- Tag mode: this is the same format as for blocks 3 and 5. This format is used by service messages (for example: ACK) and system messages (category 0).
- Parameters:
b
- the block to set up tags intos
- the block data to process- Returns:
- the processed block (the parameter b)
-
findEndOfTagByLineFeed
protected int findEndOfTagByLineFeed(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:
- "[LBR]:[TAG_START]"
- "[LBR]}"
- "[LBR]{"
- "}"
NOTE: the condition "-}" cannot happen because the terminating dash is already removed.
- Parameters:
s
- the FIN input textstart
- the position to start analysis at- Returns:
- the position where the tag ends
-
isTagStart
protected boolean isTagStart(java.lang.String s, int i)
Evaluates if the string at the given position has the format nn[a]: which means it is a proper tag start.This method could be overwritten by a subclass to provide more permissive tag identifiers (for example, to parse non-compliant messages).
- Parameters:
s
- string to evaluatei
- starting position in the string to evaluate- Returns:
- true if at the given position there is a tag start
- Since:
- 7.10.4
-
createTag
protected Tag createTag(java.lang.String buffer, java.lang.String unparsedText)
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 indicating the end of the tag value MUST not be present. If any trailing [CR][LF] is present, it will be considered part of the tag value and will be propagated to the created Tag instance.
- Parameters:
buffer
- the buffer containing the tagunparsedText
- 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
-
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 blockThis 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
- if an error occurred during read
-
findBlockStart
protected java.lang.String 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<java.lang.String> getErrors()
Get a copy of the errors found during the parsing of the message.You can manipulate this copy without affecting the original list.
- Returns:
- a copy of the list of errors found
-
getConfiguration
public SwiftParserConfiguration getConfiguration()
Gets the current parse configuration- Since:
- 7.8
- See Also:
SwiftParserConfiguration
-
setConfiguration
public void setConfiguration(SwiftParserConfiguration configuration)
Sets a new parse configuration- Parameters:
configuration
- new configuration- Since:
- 7.8
- See Also:
SwiftParserConfiguration
-
-