back Parser (SWIFT to Java)

The parser provides functionality to convert SWIFT messages FIN text into the Java message model. By doing this, you can work with SWIFT messages focusing on the data and not having to deal with low level syntax details.

The parser always performs a best effort heuristic to parse the message even if it is not well formed. All the information is read and put in a suitable object of the message model regardless of the message being SWIFT compliant.

The parser functionality has different entry point depending on the use case:

Generic processing of unknown messages

In situations where the message type is unknown, the easiest and recommended way to read and parse messages is by means of the abstract class AbstractMT. This class provides static methods to parse an unknown message from a String, File or InputStream.

For example consider the following SWIFT message example:

{1:F01BANKDEFMAXXX2039063581}{2:O1031609050901BANKDEFXAXXX89549829458949811609N}{4:
:20:007505327853
:23B:CRED
:32A:050902JPY3520000,
:33B:JPY3520000,
:50K:EUROXXXEI
:52A:FEBXXXM1
:53A:MHCXXXJT
:54A:FOOBICXX
:59:/13212312
RECEIVER NAME S.A
:70:FUTURES
:71A:SHA
:71F:EUR12,00
:71F:EUR2,34
-} 

Assuming the message is in a String variable "fin" (analogous API exists for File and InputStream), the following code will parse the FIN message into a message object object, where several getters are available to read message header information.

AbstractMT msg = AbstractMT.parse(fin);
String sender = msg.getSender();
String type = msg.getMessageType();

Even though the message type is unknown the returned object will be parsed into its specific MTnnn model object. So once the message type is known a simple cast is enough to access detail information of the text block for reading or manipulating its content. In the example:

System.out.println(msg.getMessageType());
if (msg.isType(103)) {
    MT103 mt = (MT103) msg;
    // print the message reference
    System.out.println("Reference: "+mt.getField20().getValue());
    // read components of a specific field
    Field32A f = mt.getField32A();
    Calendar date = f.getComponent1AsCalendar();
    Number  amount = f.getComponent3AsNumber();
}

Notice MTnnn classes provide API that is exclusively related to the fields that can be present in each message type.

top

Parsing an unkown message for persistence

While in the context of a parsign an unknown message, if the ultimate requirement is to read just headers information, persist the message into a database or to process large bursts of messages, the recommended way to read and parse messages is by means of the MtSwiftMessage class. This class also implements several ways to create the message instance from String, InputStream and File; but only header information is read and the model is kept generic after the parse.

Assuming the message is in a String, File or InputStream variable "fin" , the following code will parse the FIN message into a MtSwiftMessage object, and getters are used to access message header information.

MtSwiftMessage msg = MtSwftMessage.parse(fin);
String sender = msg.getSender();
String type = msg.getMessageType();

The MtSwiftMessage is a lightweight representation of the message where only the header and trailer blocks are parsed and modeled, while the text block (the actual business payload) is kept in its raw unparsed format.

top

Specializing an unkown message

The above way of reading messages is the most efficient in terms of performance and it is useful to process large burst of messages; but it is limited for content manipulation.
However, once the message object is created and properly identified, it can be specialize to access the specific text block content, for example:

MT103 mt = new MT103(msg);
Field32A f = mt.getField32A();
Calendar date = f.getComponent1AsCalendar();
Number amount = f.getComponent3AsNumber();

Notice that specializing a generic message from an AbstractMT requires only a cast to the specific MTnnn, while doing the same from an MtSwiftMessage requires a constructor.

Another important thing to know when deciding to use the AbstractMT or the MtSwiftMessage is that parsing by an AbstractMT implies that the message type can be determined from the block 2 header, that's because internally the parser will create the corresponding message type object (MTnnn). While parsing by an MtSwiftMessage does not impose anything to the message content, that could be malformed or incomplete and the message object will be created anyway.

top

Specific processing of a known message type

When the message to parse is known, the specific model class can be used directly to parse and read the content. for example:

MT103 model = MT103.parse(fin);

Where the fin parameter can be a String, File or InputStream with the message in its FIN format, and the parsing can be called using the static parse method (as in the example) or by an MTnnn constructor.
Once the object is created all its specific content can be read using getters for specific sequences and fields.

Related API documentation and examples can be found online at AbstractMT and MtSwiftMessage

More parse examples can be found at github

top