Commit 43bda1f4 by chengzhuoshen

增加mx报文的schema校验,并提交appHdr.xsd

parent 99d4aada
......@@ -8,15 +8,28 @@ import com.brilliance.swift.swiftdto2mx.SwiftDto2MxCreatorManager;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.util.XmlUtil;
import com.brilliance.swift.vo.SwiftDto;
import com.prowidesoftware.swift.model.MxId;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import com.prowidesoftware.swift.model.mx.NamespaceReader;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.xml.sax.SAXException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SwiftTransfer {
......@@ -110,4 +123,78 @@ public class SwiftTransfer {
return new Mx2MapCreatorManager().mx2Map(xmlStr);
}
/**
* 根据xml的内容获取对应的xsd文件并校验
* 失败则抛出异常
* @param xml
* @return
*/
public static boolean validateMx(String xml) {
//1.获取AppHdr报文内容,根据@xmlns获取版本对应的xsd文件
String regex = "(\\<AppHdr[\\w\\W]*\\<\\/AppHdr\\>)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(xml);
String appHdrXml = "";
if (m.find()) {
appHdrXml = m.group(1);
}
String appHdrXsdName = null;
Optional<String> namespace = NamespaceReader.findAppHdrNamespace(xml);
if (namespace.isPresent()) {
MxId appHdrMxId = new MxId(namespace.get());
appHdrXsdName = appHdrMxId.getBusinessProcess().name()
+ appHdrMxId.getFunctionality()
+ appHdrMxId.getVariant()
+ appHdrMxId.getVersion();
}
if (StringUtil.isNotEmpty(appHdrXsdName) && StringUtil.isNotEmpty(appHdrXml)) {
schemaValidate(appHdrXsdName, appHdrXml);
}
//2.获取Document报文内容,根据@xmlns获取版本对应的xsd文件
regex = "(\\<Document[\\w\\W]*\\<\\/Document\\>)";
p = Pattern.compile(regex);
m = p.matcher(xml);
String documentXml = "";
if (m.find()) {
documentXml = m.group(1);
}
String documentXsdName = "";
namespace = NamespaceReader.findDocumentNamespace(xml);
if (namespace.isPresent()) {
MxId documentMxId = new MxId(namespace.get());
documentXsdName = documentMxId.getBusinessProcess().name()
+ documentMxId.getFunctionality()
+ documentMxId.getVariant()
+ documentMxId.getVersion();
}
if (StringUtil.isNotEmpty(documentXsdName) && StringUtil.isNotEmpty(documentXml)) {
schemaValidate(documentXsdName, documentXml);
}
return true;
}
public static void schemaValidate(String xsdName, String xml) {
try {
//建立schema工厂
SchemaFactory schemaFactory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
URL url = SwiftTransfer.class.getResource("/xsd/"+xsdName+".xsd");
//利用schema工厂,接收验证文档文件对象生成Schema对象
Schema schema = schemaFactory.newSchema(url);
// 开始验证
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
} catch (SAXException e) {
throw new SwiftException("Error", e.getMessage());
} catch (IOException e) {
throw new SwiftException("Error", e.getMessage());
}
}
public static void main(String[] args) throws IOException {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800108.xml");
String xmlStr = FileUtils.readFileToString(file);
validateMx(xmlStr);
}
}
package com.brilliance.mx2mtmap.mt103;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800109.xml");
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800108.xml");
String xmlStr = FileUtils.readFileToString(file);
//String xmlStr = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxCamt05400108_CREDIT.xml"));
//Map<String, Object> extraMap = new HashMap<>();
......@@ -33,27 +30,7 @@ public class Test {
AbstractMX abstractMX = AbstractMX.parse(xmlStr);
String gsonStr = abstractMX.toJson();
System.out.println(gsonStr);
AbstractMX abstractMX1 = AbstractMX.fromJson(gsonStr);
System.out.println(abstractMX1.message());
Map<String, Object> mxGsonMaps = JSON.parseObject(gsonStr);
//String swiftGsonStr = new JSONObject(mxGsonMaps).toJSONString();
//AbstractMX abstractMX1 = AbstractMX.fromJson(swiftGsonStr);
// System.out.println(abstractMX1.toString());
Map<String, Object> maps = (Map<String, Object>)mxGsonMaps.get("fiToFICstmrCdtTrf");
JSONArray jsonArrays = (JSONArray)maps.get("cdtTrfTxInf");
int size = jsonArrays.size();
for (int i=0; i<size; i++) {
Map<String, Object> tmpMaps = (Map<String, Object>)jsonArrays.get(i);
Map<String, Object> maps1 = (Map<String, Object>)tmpMaps.get("cdtr");
maps1 = (Map<String, Object>)maps1.get("pstlAdr");
JSONArray objs = (JSONArray)maps1.get("adrLine");
int objSize = objs.size();
for (int j=0; j<objSize; j++) {
System.out.println(objs.get(j).getClass());
}
}
//System.out.println(mxGsonMaps);
}
}
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