Commit e61ee922 by chengzhuoshen

修复mx2mt转换代码

parent 2ebeefb6
package com.brilliance.swift;
import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.mt2mx.Mt2MxCreatorManager;
import com.brilliance.swift.mx2element.Mx2ElementCreatorManager;
......@@ -251,7 +252,7 @@ public class SwiftTransfer {
*/
public static boolean validateMx(String xml) {
//1.获取AppHdr报文内容,根据@xmlns获取版本对应的xsd文件
String regex = "(\\<AppHdr[\\w\\W]*\\<\\/AppHdr\\>)";
String regex = "(\\<([\\w]*:)?AppHdr[\\w\\W]*\\<\\/([\\w]*:)?AppHdr\\>)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(xml);
String appHdrXml = "";
......@@ -271,7 +272,7 @@ public class SwiftTransfer {
SwiftTransferUtil.schemaValidate(appHdrXsdName, appHdrXml);
}
//2.获取Document报文内容,根据@xmlns获取版本对应的xsd文件
regex = "(\\<Document[\\w\\W]*\\<\\/Document\\>)";
regex = "(\\<([\\w]*:)?Document[\\w\\W]*\\<\\/([\\w]*:)?Document\\>)";
p = Pattern.compile(regex);
m = p.matcher(xml);
String documentXml = "";
......@@ -319,4 +320,97 @@ public class SwiftTransfer {
return mx2Element(xmlFilePath);
}
}
/**
* 从混合格式(MXMT)的报文中提取MX报文
* @param mixStr
* @return
*/
public static String fetchXmlFromMixMessage(String mixStr) {
String regex = "(\\<([\\w]*:)?AppHdr[\\w\\W]*\\<\\/([\\w]*:)?AppHdr\\>)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(mixStr);
String appHdrXml = "";
if (m.find()) {
appHdrXml = m.group(1);
}
regex = "(\\<([\\w]*:)?Document[\\w\\W]*\\<\\/([\\w]*:)?Document\\>)";
p = Pattern.compile(regex);
m = p.matcher(mixStr);
String documentXml = "";
if (m.find()) {
documentXml = m.group(1);
}
return "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
+ Mx2MtConstants.NEW_LINE
+ "<RequestPayload>"
+ Mx2MtConstants.NEW_LINE
+ (StringUtil.isNotEmpty(appHdrXml) ? appHdrXml + Mx2MtConstants.NEW_LINE : "")
+ (StringUtil.isNotEmpty(documentXml) ? documentXml + Mx2MtConstants.NEW_LINE : "")
+ "</RequestPayload>";
}
public static String fetchXmlFromMixMessage(File mixFile) {
try {
String mixStr = FileUtils.readFileToString(mixFile);
return fetchXmlFromMixMessage(mixStr);
} catch (IOException e) {
throw new SwiftException(e.getMessage());
}
}
public static String fetchXmlFromMixMessage(String mixFilePath, boolean filePathFlag) {
if (filePathFlag) {
File mixFile = new File(mixFilePath);
return fetchXmlFromMixMessage(mixFile);
} else {
return fetchXmlFromMixMessage(mixFilePath);
}
}
/**
* 从混合格式(MXMT)的报文中提取MT报文
* @param mixStr
* @return
*/
public static String fetchFinFromMixMessage(String mixStr) {
String regex = "<!--(.*?)-->";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(mixStr);
String mt = "";
while (m.find()) {
String message = m.group(1);
if (message.indexOf("{4:") > -1) {
mt = message;
}
}
mt = mt.trim().replace("^~", Mx2MtConstants.NEW_LINE);
//去掉首行空格
String[] mts = mt.split(Mx2MtConstants.NEW_LINE);
mt = "";
for (int i=0; i<mts.length; i++) {
mt += mts[i].trim();
if (i < (mts.length-1)) {
mt += Mx2MtConstants.NEW_LINE;
}
}
return mt;
}
public static String fetchFinFromMixMessage(File mixFile) {
try {
String mixStr = FileUtils.readFileToString(mixFile);
return fetchFinFromMixMessage(mixStr);
} catch (IOException e) {
throw new SwiftException(e.getMessage());
}
}
public static String fetchFinFromMixMessage(String mixFilePath, boolean filePathFlag) {
if (filePathFlag) {
File mixFile = new File(mixFilePath);
return fetchFinFromMixMessage(mixFile);
} else {
return fetchFinFromMixMessage(mixFilePath);
}
}
}
......@@ -2293,7 +2293,11 @@ public abstract class AbstractMx2MtTagsGenerate implements Mx2MtTagsGenerate {
} else if (isAllowedClearingChannel && "RTGS".equals(clearingChannal)){
partyIdentifier = mx_to_mtClearingIdentifierAndChannel(agentPath, clearingChannelPath);
}
tag = new Tag(tagName + "D", partyIdentifier + Mx2MtConstants.NEW_LINE + nameAndAddress);
if (StringUtil.isEmpty(partyIdentifier)) {
tag = new Tag(tagName + "D", nameAndAddress);
} else {
tag = new Tag(tagName + "D", partyIdentifier + Mx2MtConstants.NEW_LINE + nameAndAddress);
}
}
return tag;
}
......
package com.brilliance.swift.mx2mt;
import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.mx2mt.mt103.Mx2Mt103Creator;
......@@ -23,13 +24,15 @@ import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.Map;
public class Mx2MtCreatorManager {
public SwiftTranslationReport mx2Mt(String xml, String fileOutputPath, Map<String, Object> extraMap) throws SwiftException {
try {
Document document1 = DocumentHelper.parseText(xml);
xml = handlingXml(xml);
AbstractMX abstractMX = AbstractMX.parse(xml);
String messageType = (abstractMX.getMxId().getBusinessProcess().name()
+ "."
......@@ -145,4 +148,24 @@ public class Mx2MtCreatorManager {
}
return messageType;
}
/**
* 如果传入的xml 没有报文头,导致dom4j无法解析
* 提取xml里面的AppHdr和Document,重新组装成有效的xml报文
*/
private String handlingXml(String xmlStr) {
boolean flag = true;
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.parse(xmlStr);
} catch (Exception e) {
flag = false;
}
if (flag) { //如果是有效xml报文,直接返回
return xmlStr;
} else {
return SwiftTransfer.fetchXmlFromMixMessage(xmlStr);
}
}
}
......@@ -110,8 +110,8 @@ public class Field72Generate extends AbstractMx2MtTagsGenerate {
mt72List.addAll(mtCreditorAgts);
}
//参数6
String mxPurposeCode = getXmlNodeValue(bodyHdrParentElementName, document, ".CdtTrfTxInf.Purp.Cd");
String mxPurposeProprietary = getXmlNodeValue(bodyHdrParentElementName, document, ".CdtTrfTxInf.Purp.Prtry");
String mxPurposeCode = getXmlNodeValue(bodyHdrParentElementName, document, "CdtTrfTxInf.Purp.Cd");
String mxPurposeProprietary = getXmlNodeValue(bodyHdrParentElementName, document, "CdtTrfTxInf.Purp.Prtry");
if (StringUtil.isNotEmpty(mxPurposeProprietary)) {
mt72List.add("/PURP/" + mxPurposeProprietary);
} else if (StringUtil.isNotEmpty(mxPurposeCode)) {
......
......@@ -21,7 +21,7 @@ public class Field13DGenerate extends AbstractMx2MtTagsGenerate {
try {
SwiftMessage swiftMessage = context.get(SwiftMessage.class);
List<Tag> tags = swiftMessage.getBlock4().getTags();
String bookDateStr = getXmlNodeValue(bodyHdrParentElementName, document, "Ntfctn.Ntry.BookgDt");
String bookDateStr = getXmlNodeValue(bodyHdrParentElementName, document, "Ntfctn.Ntry.BookgDt.DtTm");
if (StringUtil.isNotEmpty(bookDateStr)) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date bookDate = fmt.parse(bookDateStr);
......
......@@ -70,9 +70,9 @@ public class Field61Generate extends AbstractMx2MtTagsGenerate {
value += "C";
}
}
}else if(EntryStatusCode.FUTR.value().equals(code)
}else /*if(EntryStatusCode.FUTR.value().equals(code)
|| EntryStatusCode.PDNG.value().equals(code)
|| EntryStatusCode.INFO.value().equals(code)){
|| EntryStatusCode.INFO.value().equals(code))*/{
if (CdtDbtCode.DBIT.value().equals(creditDebitIndicator)) {
value += "ED";
} else {
......
{1:F01PSBCCNBJAXXX0000000000}{2:I210MSBCCNBJXXXXN}{3:{108:ZJQS01795275}}{4:
:20:QSM2105301108867
:25:301010467795
:30:211111
:21:QST2105301108866
:32B:EUR111111,00
:52A:PSBCCNBJXXX
:56A:DEUTDEFFXXX
-}
\ No newline at end of file
......@@ -55,4 +55,25 @@ public class Mt2MxTest {
public void testCamt053001() {
test("/swiftTxt/MT950.txt", null);
}
@Test
public void testCamt054001() {
test("/swiftTxt/MT910.txt", null);
}
@Test
public void testCamt056001_1() {
test("/swiftTxt/MT192.txt", null);
}
@Test
public void testCamt029001_1() {
test("/swiftTxt/MT196.txt", null);
}
@Test
public void testCamt057001() {
test("/swiftTxt/MT210.txt", null);
}
}
......@@ -5,6 +5,7 @@ import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class MxXsdValidator {
......@@ -14,4 +15,10 @@ public class MxXsdValidator {
boolean flag = SwiftTransfer.validateMx(file);
System.out.println(flag);
}
@Test
public void test() throws IOException {
File file = FileUtils.toFile(Mx2MtTest.class.getResource("/swiftXml/MxPacs00800108.xml"));
System.out.println(SwiftTransfer.fetchFinFromMixMessage(file));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment