Commit 65e80205 by chengzhuoshen

配置文件可以通过方法反射获取值

parent 6109e928
......@@ -4,13 +4,16 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.util.StringUtil;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
......@@ -39,9 +42,39 @@ public abstract class AbstractMx2ElementCreator implements Mx2ElementCreator{
this.xmlStr = xmlStr;
}
private Object invokeHere(String methodName, Object where) {
try {
Method method = getClass().getMethod(methodName);
return method.invoke(where);
} catch (final NoSuchMethodException e) {
throw new SwiftException("Method " + methodName + " does not exist in " + getClass());
} catch (Exception e) {
throw new SwiftException(e.getMessage());
}
}
public List<String> convertToLines(InputStream is) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(is));
List<String> list = new ArrayList<>();
String line = null;
try {
while((line = bReader.readLine())!=null){
list.add(line);
}
} catch (IOException e) {
throw new SwiftException(e.getMessage());
} finally {
try {
bReader.close();
} catch (IOException e) {
throw new SwiftException(e.getMessage());
}
}
return list;
}
@Override
public Map<String, Object> buildElement() {
try {
//AbstractMX abstractMX = AbstractMX.parse(xmlStr);
String messageType = abstractMX.getMxId().getBusinessProcess().name()
+ abstractMX.getMxId().getFunctionality()
......@@ -51,21 +84,14 @@ public abstract class AbstractMx2ElementCreator implements Mx2ElementCreator{
String gsonStr = abstractMX.toJson();
Map<String, Object> mxGsonMaps = JSON.parseObject(gsonStr);
String fileName = "/template/"+messageType+".properties";
/*InputStream resourceAsStream = AbstractMx2ElementCreator.class.getResourceAsStream(fileName);
Properties properties = new Properties();
properties.load(resourceAsStream);
Set<Object> keySet = properties.keySet();
for (Object keyObj : keySet) {
String key = (String) keyObj;
String path = properties.getProperty(key);
Object value = getXmlValue(mxGsonMaps, path);
if (value != null) {
maps.put(key, value);
}
}*/
URL url = AbstractMx2ElementCreator.class.getResource(fileName);
File propertyFile = new File(url.getPath());
List<String> properties = FileUtils.readLines(propertyFile);
InputStream resourceAsStream = AbstractMx2ElementCreator.class.getResourceAsStream(fileName);
if (resourceAsStream == null) {
int versionLength = abstractMX.getMxId().getVersion().length();
fileName = "/template/"+messageType.substring(0, messageType.length()-versionLength)+".properties";
resourceAsStream = AbstractMx2ElementCreator.class.getResourceAsStream(fileName);
}
List<String> properties = StringUtil.inputStreamToLines(resourceAsStream);
if (properties.size() > 0) {
for (String property : properties) {
if (property.startsWith("#")) {
......@@ -83,14 +109,15 @@ public abstract class AbstractMx2ElementCreator implements Mx2ElementCreator{
}
}
}
} catch (IOException e) {
throw new SwiftException(e.getMessage());
}
return maps;
}
protected Object getXmlValue(Map<String, Object> jsonMaps, String path) {
Object value = null;
if (path.startsWith("@")) { //如果以@开始 表示取值规则采用的函数
return invokeHere(path.substring(1), this);
}
if (path.indexOf("?") > 0) {
String[] childPaths = path.split("\\?");
for (String childPath : childPaths) {
......@@ -109,16 +136,21 @@ public abstract class AbstractMx2ElementCreator implements Mx2ElementCreator{
//需要用. split,然后循环遍历json获取值
String[] paths = path.split("\\.");
Map<String, Object> tmpMaps = jsonMaps;
for (String p : paths) {
for (int i=0; i<paths.length; i++) {
String p = paths[i];
Object obj = tmpMaps.get(p);
if (obj == null) return null;
if (obj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray)obj;
obj = jsonArray.get(0);
if (obj instanceof JSONObject) {
tmpMaps = (Map<String, Object>)obj;
} else {
if (i == paths.length-1) {
return jsonArray;
} else {
obj = jsonArray.get(0);
if (obj instanceof JSONObject) {
tmpMaps = (Map<String, Object>)obj; //不是最后一层
} else {
return jsonArray;
}
}
} else if (obj instanceof JSONObject) {
tmpMaps = (Map<String, Object>)obj;
......
package com.brilliance.swift.mx2element.pacs008001;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.mx2element.AbstractMx2ElementCreator;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.util.XmlUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import java.util.Map;
import java.math.BigDecimal;
import java.util.*;
public class Mx2ElementPacs008001Creator extends AbstractMx2ElementCreator {
@Override
public Map<String, Object> buildElement() {
return super.buildElement();
}
public Object buildChrgInfo() {
try {
Document document = DocumentHelper.parseText(xmlStr);
Map<String, String> parentElementMaps = XmlUtil.getParentElementMaps(document);
String bodyParentPath = parentElementMaps.get("CdtTrfTxInf");
int chrgsInfCount = XmlUtil.getChildrenCount(document, bodyParentPath+".CdtTrfTxInf.ChrgsInf", null);
if (chrgsInfCount > 0) {
List<Map<String, Object>> list = new ArrayList<>();
for (int i=0; i<chrgsInfCount; i++) {
String amt = XmlUtil.getXmlNodeValue(document, bodyParentPath+".CdtTrfTxInf.ChrgsInf("+i+").Amt");
String ccy = XmlUtil.getXmlNodeValue(document, bodyParentPath+".CdtTrfTxInf.ChrgsInf("+i+").Amt@Ccy");
if (StringUtil.isNotEmpty(ccy) && StringUtil.isNotEmpty(amt)) {
Map<String, Object> maps = new LinkedHashMap<>();
maps.put("ccy", ccy);
maps.put("amt", new BigDecimal(amt));
list.add(maps);
}
}
return list;
} else {
return null;
}
} catch (DocumentException e) {
throw new SwiftException(e.getMessage());
}
}
public Object buildRgltryRptg() {
try {
Document document = DocumentHelper.parseText(xmlStr);
Map<String, String> parentElementMaps = XmlUtil.getParentElementMaps(document);
String bodyParentPath = parentElementMaps.get("CdtTrfTxInf");
List<String> list = new ArrayList<>();
int mxRegulatoryReportingCount = XmlUtil.getChildrenCount(document, bodyParentPath+".CdtTrfTxInf.RgltryRptg", null);
if (mxRegulatoryReportingCount > 0) {
for (int i=0; i<mxRegulatoryReportingCount; i++) {
int mxRegulatoryReportingDetailCount = XmlUtil.getChildrenCount(document,bodyParentPath+".CdtTrfTxInf.RgltryRptg("+i+").Dtls", null);
if (mxRegulatoryReportingDetailCount > 0) {
for (int j=0; j<mxRegulatoryReportingDetailCount; j++) {
int mxRegulatoryReportingDetailInfCount = XmlUtil.getChildrenCount(document,bodyParentPath+".CdtTrfTxInf.RgltryRptg("+i+").Dtls("+j+").Inf", null);
if (mxRegulatoryReportingDetailInfCount > 0) {
for (int k=0; k<mxRegulatoryReportingDetailInfCount; k++) {
String inf = XmlUtil.getXmlNodeValue(document, bodyParentPath+".CdtTrfTxInf.RgltryRptg("+i+").Dtls("+j+").Inf("+k+")");
list.add(inf);
}
}
}
}
}
}
if (list.size() > 0) {
return list;
} else {
return null;
}
} catch (DocumentException e) {
throw new SwiftException(e.getMessage());
}
}
public Object buildRltdRmtInf() {
try {
Document document = DocumentHelper.parseText(xmlStr);
Map<String, String> parentElementMaps = XmlUtil.getParentElementMaps(document);
String bodyParentPath = parentElementMaps.get("CdtTrfTxInf");
int rltdRmtInfCount = XmlUtil.getChildrenCount(document, bodyParentPath+".CdtTrfTxInf.RltdRmtInf", null);
List<String> list = new ArrayList<>();
if (rltdRmtInfCount > 0) {
for (int i=0; i<rltdRmtInfCount; i++) {
String rmtId = XmlUtil.getXmlNodeValue(document, bodyParentPath+".CdtTrfTxInf.RltdRmtInf("+i+").RmtId");
if (StringUtil.isNotEmpty(rmtId)) {
list.add(rmtId);
}
}
}
if (list.size() > 0) {
return list;
} else {
return null;
}
} catch (DocumentException e) {
throw new SwiftException(e.getMessage());
}
}
public Object buildRmtInfStrdAddtlRmtInf() {
try {
Document document = DocumentHelper.parseText(xmlStr);
Map<String, String> parentElementMaps = XmlUtil.getParentElementMaps(document);
String bodyParentPath = parentElementMaps.get("CdtTrfTxInf");
List<String> list = new ArrayList<>();
int strdCount = XmlUtil.getChildrenCount(document, bodyParentPath+".CdtTrfTxInf.RmtInf.Strd", null);
if (strdCount > 0) {
for (int i=0; i<strdCount; i++) {
int addtlRmtInfCount = XmlUtil.getChildrenCount(document,bodyParentPath+".CdtTrfTxInf.RmtInf.Strd("+i+").AddtlRmtInf", null);
if (addtlRmtInfCount > 0) {
for (int j=0; j<addtlRmtInfCount; j++) {
String inf = XmlUtil.getXmlNodeValue(document, bodyParentPath+".CdtTrfTxInf.RmtInf.Strd("+i+").AddtlRmtInf("+j+")");
list.add(inf);
}
}
}
}
if (list.size() > 0) {
return list;
} else {
return null;
}
} catch (DocumentException e) {
throw new SwiftException(e.getMessage());
}
}
}
......@@ -640,7 +640,7 @@ public abstract class AbstractMx2MtTagsGenerate implements Mx2MtTagsGenerate {
}
String mxAddress3CtrySubDvsn = XmlUtil.getXmlNodeValue(document, nameAddressPrefixPath + ".PstlAdr.CtrySubDvsn");
if (StringUtil.isNotEmpty(mxAddress3CtrySubDvsn)) {
mxAddress3 += "," + mxAddress3TwnNm;
mxAddress3 += "," + mxAddress3CtrySubDvsn;
}
String mxAddress3TwnLctnNm = XmlUtil.getXmlNodeValue(document, nameAddressPrefixPath + ".PstlAdr.TwnLctnNm");
if (StringUtil.isNotEmpty(mxAddress3TwnLctnNm)) {
......
package com.brilliance.swift.util;
import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.exception.SwiftException;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
......@@ -405,6 +403,26 @@ public abstract class StringUtil {
return list;
}
public static List<String> inputStreamToLines(InputStream is) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(is));
List<String> list = new ArrayList<>();
String line = null;
try {
while((line = bReader.readLine())!=null){
list.add(line);
}
} catch (IOException e) {
throw new SwiftException(e.getMessage());
} finally {
try {
bReader.close();
} catch (IOException e) {
throw new SwiftException(e.getMessage());
}
}
return list;
}
public static void main(String[] args) {
String str = "/INS/qwerqwertrewertdfgrfrhfsedfgyhokjoknksdnkqwerqwertrewertdfgrfrhfsedfgyhokjoknksdnk";
List<String> list = outStringList(str, 35, "//");
......
#APPLICATION HEADER
frBic=appHdr.fr.fiId.finInstnId.bicfi?appHdr.fr.orgId.id.orgId.anyBIC
toBic=appHdr.to.fiId.finInstnId.bicfi?appHdr.to.orgId.id.orgId.anyBIC
bizMsgIdr=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtId.instrId?fiToFICstmrCdtTrf.grpHdr.msgId?appHdr.bizMsgIdr
msgDefIdr=appHdr.msgDefIdr
bizSvc=appHdr.bizSvc
creDt=fiToFICstmrCdtTrf.grpHdr.creDtTm?appHdr.creDt
priority=appHdr.prty
#BODY
endToEndId=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtId.endToEndId
uetr=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtId.uetr
clrSysRef=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtId.clrSysRef
clrChanl=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtTpInf.clrChanl
#SvcLvl ??
lclInstrm=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtTpInf.lclInstrm.cd?fiToFICstmrCdtTrf.cdtTrfTxInf.pmtTpInf.lclInstrm.prtry
ctgyPurp=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtTpInf.ctgyPurp.cd?fiToFICstmrCdtTrf.cdtTrfTxInf.pmtTpInf.ctgyPurp.prtry
sttlmMtd=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmMtd
sttlmAcctIdIban=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.id.iban
sttlmAcctId=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.id.othr.id
sttlmAcctIdType=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.id.othr.schmeNm.cd?fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.id.othr.schmeNm.prtry
sttlmAcctType=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.tp.cd?fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.tp.prtry
sttlmAcctCcy=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.ccy
sttlmAcctNm=fiToFICstmrCdtTrf.grpHdr.sttlmInf.sttlmAcct.nm
clrSys=fiToFICstmrCdtTrf.grpHdr.sttlmInf.clrSys.cd?fiToFICstmrCdtTrf.grpHdr.sttlmInf.clrSys.prtry
intrBkSttlmAmt=fiToFICstmrCdtTrf.cdtTrfTxInf.intrBkSttlmAmt.value&bigdecimal
intrBkSttlmCcy=fiToFICstmrCdtTrf.cdtTrfTxInf.intrBkSttlmAmt.ccy
intrBkSttlmDt=fiToFICstmrCdtTrf.cdtTrfTxInf.intrBkSttlmDt
dbtDtTm=fiToFICstmrCdtTrf.cdtTrfTxInf.sttlmTmIndctn.dbtDtTm
cdtDtTm=fiToFICstmrCdtTrf.cdtTrfTxInf.sttlmTmIndctn.cdtDtTm
clsTm=fiToFICstmrCdtTrf.cdtTrfTxInf.sttlmTmReq.clsTm
tillTm=fiToFICstmrCdtTrf.cdtTrfTxInf.sttlmTmReq.tillTm
frTm=fiToFICstmrCdtTrf.cdtTrfTxInf.sttlmTmReq.frTm
rjctTm=fiToFICstmrCdtTrf.cdtTrfTxInf.sttlmTmReq.rjctTm
instdAmt=fiToFICstmrCdtTrf.cdtTrfTxInf.instdAmt.value&bigdecimal
instdAmtCcy=fiToFICstmrCdtTrf.cdtTrfTxInf.instdAmt.ccy
xchgRate=fiToFICstmrCdtTrf.cdtTrfTxInf.xchgRate&bigdecimal
chrgBr=fiToFICstmrCdtTrf.cdtTrfTxInf.chrgBr
#chrgInfo ??
dbtrNm=fiToFICstmrCdtTrf.cdtTrfTxInf.nm
dbtrAdrLines=fiToFICstmrCdtTrf.cdtTrfTxInf.dbtr.pstlAdr.adrLine
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