Created
May 12, 2022 09:45
-
-
Save GeorgeAnt/4f7f7810ef8aad2103799ea7095590db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gr.datamation.mx.ScripsMessage; | |
import gr.datamation.mx.message.head.BusinessApplicationHeader02; | |
import gr.datamation.mx.message.pacs.FinancialInstitutionCreditTransfer08; | |
import gr.datamation.validation.error.ValidationError; | |
import gr.datamation.validation.error.ValidationErrorList; | |
import java.io.ByteArrayInputStream; | |
public class ParseAndValidateScripsMessage { | |
public static void main(String... args) { | |
execute(); | |
} | |
public static void execute() { | |
constructScripsMessage(); | |
parseAndValidateScripsMessage(); | |
autoParseAndValidateScripsMessage(); | |
} | |
public static void constructScripsMessage() { | |
try { | |
//Initialize the header object | |
BusinessApplicationHeader02 businessApplicationHeader = new BusinessApplicationHeader02(); | |
businessApplicationHeader.parseXML(validScripsPacs009CoreHeaderString); | |
//Initialize the document object | |
FinancialInstitutionCreditTransfer08 financialInstitutionCreditTransfer08 = new FinancialInstitutionCreditTransfer08(); | |
financialInstitutionCreditTransfer08.parseXML(validScripsPacs009CoreDocumentString); | |
//We fill the elements of the message object using setters | |
//financialInstitutionCreditTransfer08.getMessage().setGrpHdr(new GroupHeader93()) | |
//financialInstitutionCreditTransfer08.getMessage().getGrpHdr().setMsgId("1234") | |
//or setElement() | |
//financialInstitutionCreditTransfer08.setElement("GrpHdr/MsgId", "1234") | |
//Construct the SCRIPS message object using two separate objects, header, document | |
ScripsMessage<BusinessApplicationHeader02, FinancialInstitutionCreditTransfer08> scripsMessage = new ScripsMessage<>(businessApplicationHeader, financialInstitutionCreditTransfer08); | |
//Perform validation in both header and message object using scripsMessage | |
//Use ScripsMessage.ScripsMsgType enumeration object to select the matching schema (check the table of supported SCRIPS messages below | |
//ScripsMessage.extractScripsMsgType() can also be used | |
ValidationErrorList validationErrorList = scripsMessage.validate(ScripsMessage.ScripsMsgType.PACS_009_CORE); | |
if (validationErrorList.isEmpty()) { | |
System.out.println("Message is valid"); | |
System.out.println(scripsMessage.convertToXML()); //Get the generated xmls for head and document | |
} else { | |
handleValidationError(validationErrorList); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println(e.getMessage()); | |
} | |
} | |
public static void parseAndValidateScripsMessage() { | |
try { | |
//Initialize the scripsMessage | |
ScripsMessage<BusinessApplicationHeader02, FinancialInstitutionCreditTransfer08> scripsMessage = new ScripsMessage<>(new BusinessApplicationHeader02(), new FinancialInstitutionCreditTransfer08()); | |
//Fill the scripsMessage with data from xml and validate SCRIPS against the xml schema | |
ValidationErrorList validationErrorList = scripsMessage.autoParseAndValidateXml(new ByteArrayInputStream(validScripsPacs009CoreString.getBytes())); | |
if (!validationErrorList.isEmpty()) { | |
handleValidationError(validationErrorList); | |
return; | |
} | |
//Perform validation in both header and message object using scripsMessage | |
//Use ScripsMessage.ScripsMsgType enumeration object to select the matching schema (check the table of supported SCRIPS messages below | |
//ScripsMessage.extractScripsMsgType() can also be used | |
validationErrorList.addAll(scripsMessage.validate(ScripsMessage.ScripsMsgType.PACS_009_CORE)); | |
if (validationErrorList.isEmpty()) { | |
System.out.println("Message is valid"); | |
System.out.println(scripsMessage.convertToXML()); //Get the generated xmls for head and document | |
} else { | |
handleValidationError(validationErrorList); | |
} | |
//Extract the header and the core message from scripsMessage object | |
BusinessApplicationHeader02 businessApplicationHeader = scripsMessage.getAppHdr(); | |
FinancialInstitutionCreditTransfer08 financialInstitutionCreditTransfer08 = scripsMessage.getDocument(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println(e.getMessage()); | |
} | |
} | |
public static void autoParseAndValidateScripsMessage() { | |
try { | |
//Initialize the scripsMessage | |
ScripsMessage<BusinessApplicationHeader02, FinancialInstitutionCreditTransfer08> scripsMessage = new ScripsMessage<>(); | |
//Fill the scripsMessage with data from xml and validate SCRIPS against the xml schema | |
ValidationErrorList validationErrorList = scripsMessage.autoParseAndValidateXml(new ByteArrayInputStream(validScripsPacs009CoreString.getBytes())); | |
if (!validationErrorList.isEmpty()) { | |
handleValidationError(validationErrorList); | |
return; | |
} | |
//Perform validation in both header and message object using scripsMessage | |
validationErrorList.addAll(scripsMessage.autoValidate()); | |
if (validationErrorList.isEmpty()) { | |
System.out.println("Message is valid"); | |
System.out.println(scripsMessage.convertToXML()); //Get the generated xmls for head and document | |
} else { | |
handleValidationError(validationErrorList); | |
} | |
//Extract the header and the core message from scripsMessage object | |
BusinessApplicationHeader02 businessApplicationHeader = scripsMessage.getAppHdr(); | |
FinancialInstitutionCreditTransfer08 financialInstitutionCreditTransfer08 = scripsMessage.getDocument(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println(e.getMessage()); | |
} | |
} | |
private static void handleValidationError(ValidationErrorList validationErrorList) { | |
System.err.println("Message is invalid, and the errors are the following:"); | |
for (ValidationError error : validationErrorList) { | |
System.err.println(error.toString()); | |
System.err.println( | |
"Error Code: " + error.getErrorCode() + "\n" + | |
"Error Description: " + error.getDescription() + "\n" + | |
"Line number in error inside the tag: " + error.getLine() + "\n" | |
); | |
} | |
} | |
private static final String validScripsPacs009CoreString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<RequestPayload>\n" + | |
" <AppHdr xmlns=\"urn:iso:std:iso:20022:tech:xsd:head.001.001.02\">\n" + | |
" <Fr>\n" + | |
" <FIId>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>CCCCGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </FIId>\n" + | |
" </Fr>\n" + | |
" <To>\n" + | |
" <FIId>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>MMMMGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </FIId>\n" + | |
" </To>\n" + | |
" <BizMsgIdr>UTA1030123456789-CANC</BizMsgIdr>\n" + | |
" <MsgDefIdr>pacs.009.001.08</MsgDefIdr>\n" + | |
" <BizSvc>mas.scrips.02</BizSvc>\n" + | |
" <CreDt>2008-09-29T04:49:45+03:00</CreDt>\n" + | |
" </AppHdr>\n" + | |
" <Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.009.001.08\">\n" + | |
" <FICdtTrf>\n" + | |
" <GrpHdr>\n" + | |
" <MsgId>BBBB/120928-FICT/JPY/430</MsgId>\n" + | |
" <CreDtTm>2012-09-28T16:00:00+03:00</CreDtTm>\n" + | |
" <NbOfTxs>1</NbOfTxs>\n" + | |
" <SttlmInf>\n" + | |
" <SttlmMtd>CLRG</SttlmMtd>\n" + | |
" <ClrSys>\n" + | |
" <Cd>MEP</Cd>\n" + | |
" </ClrSys>\n" + | |
" </SttlmInf>\n" + | |
" </GrpHdr>\n" + | |
" <CdtTrfTxInf>\n" + | |
" <PmtId>\n" + | |
" <InstrId>BBBB/120928-FICT/JPY/430/1</InstrId>\n" + | |
" <EndToEndId>ABC/4562/2012-09-08</EndToEndId>\n" + | |
" <TxId>BBBB/120928-CCT/123/1</TxId>\n" + | |
" <UETR>00000000-0000-4000-8000-000000000000</UETR>\n" + | |
" </PmtId>\n" + | |
" <IntrBkSttlmAmt Ccy=\"JPY\">10000000</IntrBkSttlmAmt>\n" + | |
" <IntrBkSttlmDt>2012-09-29</IntrBkSttlmDt>\n" + | |
" <InstgAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>BBBBUS33</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </InstgAgt>\n" + | |
" <InstdAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>CCCCJPJT</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </InstdAgt>\n" + | |
" <Dbtr>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>BBBBUS33</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </Dbtr>\n" + | |
" <CdtrAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>AAAAJPJT</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </CdtrAgt>\n" + | |
" <Cdtr>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>AAAAGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </Cdtr>\n" + | |
" </CdtTrfTxInf>\n" + | |
" </FICdtTrf>\n" + | |
" </Document>\n" + | |
"</RequestPayload>\n"; | |
private static final String validScripsPacs009CoreHeaderString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
" <AppHdr xmlns=\"urn:iso:std:iso:20022:tech:xsd:head.001.001.02\">\n" + | |
" <Fr>\n" + | |
" <FIId>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>CCCCGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </FIId>\n" + | |
" </Fr>\n" + | |
" <To>\n" + | |
" <FIId>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>MMMMGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </FIId>\n" + | |
" </To>\n" + | |
" <BizMsgIdr>UTA1030123456789-CANC</BizMsgIdr>\n" + | |
" <MsgDefIdr>pacs.009.001.08</MsgDefIdr>\n" + | |
" <BizSvc>mas.scrips.02</BizSvc>\n" + | |
" <CreDt>2008-09-29T04:49:45+03:00</CreDt>\n" + | |
" </AppHdr>\n"; | |
private static final String validScripsPacs009CoreDocumentString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
" <Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.009.001.08\">\n" + | |
" <FICdtTrf>\n" + | |
" <GrpHdr>\n" + | |
" <MsgId>BBBB/120928-FICT/JPY/430</MsgId>\n" + | |
" <CreDtTm>2012-09-28T16:00:00+03:00</CreDtTm>\n" + | |
" <NbOfTxs>1</NbOfTxs>\n" + | |
" <SttlmInf>\n" + | |
" <SttlmMtd>CLRG</SttlmMtd>\n" + | |
" <ClrSys>\n" + | |
" <Cd>MEP</Cd>\n" + | |
" </ClrSys>\n" + | |
" </SttlmInf>\n" + | |
" </GrpHdr>\n" + | |
" <CdtTrfTxInf>\n" + | |
" <PmtId>\n" + | |
" <InstrId>BBBB/120928-FICT/JPY/430/1</InstrId>\n" + | |
" <EndToEndId>ABC/4562/2012-09-08</EndToEndId>\n" + | |
" <TxId>BBBB/120928-CCT/123/1</TxId>\n" + | |
" <UETR>00000000-0000-4000-8000-000000000000</UETR>\n" + | |
" </PmtId>\n" + | |
" <IntrBkSttlmAmt Ccy=\"JPY\">10000000</IntrBkSttlmAmt>\n" + | |
" <IntrBkSttlmDt>2012-09-29</IntrBkSttlmDt>\n" + | |
" <InstgAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>BBBBUS33</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </InstgAgt>\n" + | |
" <InstdAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>CCCCJPJT</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </InstdAgt>\n" + | |
" <Dbtr>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>BBBBUS33</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </Dbtr>\n" + | |
" <CdtrAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>AAAAJPJT</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </CdtrAgt>\n" + | |
" <Cdtr>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>AAAAGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </Cdtr>\n" + | |
" </CdtTrfTxInf>\n" + | |
" </FICdtTrf>\n" + | |
" </Document>\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment