Commit fd4db0c7 by chengzhuoshen

1.增加MX转换map方法

2.增加MX转换json字符串方法
parent 45675193
package com.brilliance.swift;
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.mt2SwiftDto.Mt2SwiftDtoCreateManager;
import com.brilliance.swift.mx2swiftdto.Mx2SwiftDtoCreatorManager;
import com.brilliance.swift.swiftdto2mt.SwiftDto2MtCreatorManager;
import com.brilliance.swift.swiftdto2mx.SwiftDto2MxCreatorManager;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.vo.SwiftDto;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class SwiftTransfer {
......@@ -159,4 +165,97 @@ public class SwiftTransfer {
return maps;
}
/**
* 将MX xml转换成gson 字符串
* @param xmlStr
* @return
*/
public static String mx2Gson(String xmlStr) {
if (StringUtil.isEmpty(xmlStr)) return null;
AbstractMX abstractMX = AbstractMX.parse(xmlStr);
String gsonStr = abstractMX.toJson();
return gsonStr;
}
/**
* 将MX xml转换成Map<String, String>
* @param xmlStr
* @return
*/
public static Map<String, String> mx2Map(String xmlStr) {
if (StringUtil.isEmpty(xmlStr)) return null;
Map<String, String> maps = new HashMap<>();
AbstractMX abstractMX = AbstractMX.parse(xmlStr);
String gsonStr = abstractMX.toJson();
Map<String, Object> mxGsonMaps = (Map<String, Object>) JSON.parseObject(gsonStr);
parseMxGsonMap(maps, mxGsonMaps, null);
return maps;
}
/**
* 把mxGsonMaps解析成Map<String, String>,回调函数然后拼接前缀
* @param maps
* @param mxGsonMaps
* @param prefix
*/
public static void parseMxGsonMap(Map<String, String> maps, Map<String, Object> mxGsonMaps, String prefix) {
Set<String> keys = mxGsonMaps.keySet();
for (String key : keys) {
Object object = (Object)mxGsonMaps.get(key);
if (object == null) continue;
if (object instanceof JSONArray) {
if (StringUtil.isEmpty(prefix)) {
parseMxGsonArray(maps, (JSONArray)object, key);
} else {
parseMxGsonArray(maps, (JSONArray)object, prefix + "." + key);
}
} else if (object instanceof JSONObject) {
if (StringUtil.isEmpty(prefix)) {
parseMxGsonMap(maps, (Map<String, Object>)object, key);
} else {
parseMxGsonMap(maps, (Map<String, Object>)object, prefix + "." + key);
}
} else {
if (StringUtil.isEmpty(prefix)) {
maps.put(key, String.valueOf(object));
} else {
maps.put(prefix + "." + key, String.valueOf(object));
}
}
}
}
/**
* jsonArray 是json数组格式 回调函数
* 如果长度只有1,那么就不需要下标
* 否则需要下标[0] [1] [2] [3]
* @param maps
* @param jsonArray
* @param prefix
*/
public static void parseMxGsonArray(Map<String, String> maps, JSONArray jsonArray, String prefix) {
Object[] jsonArrays = jsonArray.toArray();
if (jsonArrays.length == 1) {
Object object = jsonArrays[0];
if (object instanceof JSONArray) {
parseMxGsonArray(maps, (JSONArray)object, prefix);
} else if (object instanceof JSONObject) {
parseMxGsonMap(maps, (Map<String, Object>)object, prefix);
} else {
maps.put(prefix, String.valueOf(object));
}
} else {
for (int i=0; i<jsonArrays.length; i++) {
Object object = jsonArrays[i];
if (object instanceof JSONArray) {
parseMxGsonArray(maps, (JSONArray)object, prefix+"["+i+"]");
} else if (object instanceof JSONObject) {
parseMxGsonMap(maps, (Map<String, Object>)object, prefix+"["+i+"]");
} else {
maps.put(prefix+"["+i+"]", String.valueOf(object));
}
}
}
}
}
package com.brilliance.mx2mtmap.mt103;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.util.StringUtil;
import com.google.gson.*;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) throws IOException {
String xmlStr = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800109.xml"));
Map<String, Object> extraMap = new HashMap<>();
extraMap.put("bnkBic", "BANKANC0XXX");//发报
String xmlStr = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxCamt05400108_CREDIT.xml"));
//Map<String, Object> extraMap = new HashMap<>();
//extraMap.put("bnkBic", "BANKANC0XXX");//发报
//extraMap.put("bnkBic", "FOOBARC0");//收报
Map<String, String> maps = SwiftTransfer.mx2MtMap(xmlStr, extraMap);
//Map<String, String> maps = SwiftTransfer.mx2MtMap(xmlStr, extraMap);
//maps.forEach((k, v) -> System.out.println(k + "=" + v));
//String gson = SwiftTransfer.mx2Gson(xmlStr);
//System.out.println(gson);
Map<String, String> maps = SwiftTransfer.mx2Map(xmlStr);
maps.forEach((k, v) -> System.out.println(k + "=" + v));
}
}
......@@ -39,7 +39,7 @@ public class XMLGregorianCalendarAdapter implements JsonSerializer<XMLGregorianC
@Override
public JsonElement serialize(XMLGregorianCalendar cal, Type type, JsonSerializationContext jsonSerializationContext) {
JsonObject obj = new JsonObject();
/*JsonObject obj = new JsonObject();
obj.addProperty(YEAR, cal.getYear());
obj.addProperty(MONTH, cal.getMonth());
obj.addProperty(DAY, cal.getDay());
......@@ -48,15 +48,15 @@ public class XMLGregorianCalendarAdapter implements JsonSerializer<XMLGregorianC
obj.addProperty(MINUTE, cal.getMinute());
obj.addProperty(SECOND, cal.getSecond());
obj.addProperty(FRACTIONAL, cal.getFractionalSecond());
return obj;
return obj;*/
// this alternative implementation is not working
//return new JsonPrimitive(cal.toXMLFormat());
return new JsonPrimitive(cal.toXMLFormat());
}
@Override
public XMLGregorianCalendar deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) {
try {
JsonObject obj = jsonElement.getAsJsonObject();
/*JsonObject obj = jsonElement.getAsJsonObject();
XMLGregorianCalendar xmlGregCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(
obj.get(YEAR).getAsInt(),
obj.get(MONTH).getAsInt(),
......@@ -72,9 +72,9 @@ public class XMLGregorianCalendarAdapter implements JsonSerializer<XMLGregorianC
} else {
xmlGregCalendar.setFractionalSecond(null);
}
return xmlGregCalendar;
return xmlGregCalendar;*/
// use the line below as implementation in Java 8
//return DatatypeFactory.newInstance().newXMLGregorianCalendar(jsonElement.getAsString());
return DatatypeFactory.newInstance().newXMLGregorianCalendar(jsonElement.getAsString());
} catch (Exception e) {
e.printStackTrace();
return null;
......
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