|
@@ -0,0 +1,282 @@
|
|
|
+package com.scbfkj.uni.library;
|
|
|
+
|
|
|
+
|
|
|
+import org.dom4j.Attribute;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentHelper;
|
|
|
+import org.dom4j.Element;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据格式转换
|
|
|
+ * <p>
|
|
|
+ * <p>
|
|
|
+ * 1. 添加多线程
|
|
|
+ * 2. 数据项结构项表
|
|
|
+ */
|
|
|
+public class XmlUtil {
|
|
|
+
|
|
|
+ public static Map<String, Object> processSuccess(Object returnData) {
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();//用于当前方法统一返回参数,不存在深拷贝问题
|
|
|
+ returnMap.put("code", "0");
|
|
|
+ returnMap.put("returnData", returnData);
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ //统一错误信息处理
|
|
|
+ public static Map<String, Object> processFail(String errorMessage) {
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();//用于当前方法统一返回参数,不存在深拷贝问题
|
|
|
+ returnMap.put("code", "-1");
|
|
|
+ returnMap.put("message", errorMessage);
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * xml转map 定制化
|
|
|
+ *
|
|
|
+ * @param xml 字符串
|
|
|
+ * @param isTwoDimensionalization 是否需要二维化
|
|
|
+ * @param noDiKaList 是否需要dika
|
|
|
+ * @param translateName 需要转义的名称
|
|
|
+ * @return code message returnData
|
|
|
+ */
|
|
|
+ public static Map<String, Object> customMadexmlToMap(String xml, boolean isTwoDimensionalization, Object noDiKaList, Map<String, String> translateName) {
|
|
|
+ Map<String, Object> returnMap = xmlToMapFormat(xml); // code ,message ,returenData
|
|
|
+ if (returnMap.get("code").equals("-1")) return returnMap;
|
|
|
+ if (isTwoDimensionalization) {
|
|
|
+ Map<String, Object> xmlMap = (Map<String, Object>) returnMap.get("returnData");
|
|
|
+ returnMap = twoDimensionalizationFormat(xmlMap, noDiKaList); // code ,message ,returenData
|
|
|
+ if (returnMap.get("code").equals("-1")) return returnMap;
|
|
|
+ }
|
|
|
+ if (null != translateName && !translateName.isEmpty()) {
|
|
|
+ Object returnData = returnMap.get("returnData"); // code ,message ,returenData
|
|
|
+ if (returnData instanceof Map) {
|
|
|
+ List<Map<String, Object>> inData = new ArrayList<>();
|
|
|
+ inData.add((Map) returnData);
|
|
|
+ returnMap = translateMapName(inData, translateName); // code message retrunData
|
|
|
+ } else if (returnData instanceof List) {
|
|
|
+ returnMap = translateMapName((List) returnData, translateName); // code message retrunData
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Object rsData = returnMap.get("returnData");
|
|
|
+ if (rsData instanceof List<?>) {
|
|
|
+ for (Map<String, Object> tempMap : (List<Map<String, Object>>) rsData) {
|
|
|
+ tempMap.values().removeAll(Collections.singleton(null));
|
|
|
+ tempMap.values().removeAll(Collections.singleton(""));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rsData instanceof Map<?, ?> temMap) {
|
|
|
+ temMap.values().removeAll(Collections.singleton(null));
|
|
|
+ temMap.values().removeAll(Collections.singleton(""));
|
|
|
+ }
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static Map<String, Object> customMadeListxmlToMap(List<String> xmlList, boolean isTwoDimensionalization, Object noDiKaList, Map<String, String> translateName) {
|
|
|
+ if (Objects.isNull(xmlList) || xmlList.size() == 0) {
|
|
|
+ return MapTools.processSuccess(null);
|
|
|
+ }
|
|
|
+ if (xmlList.size() == 1) {
|
|
|
+ return customMadexmlToMap(xmlList.get(0), isTwoDimensionalization, noDiKaList, translateName);
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> batchList = new ArrayList<>();
|
|
|
+ xmlList.forEach(xml -> {
|
|
|
+ Map<String, Object> stringObjectMap = customMadexmlToMap(xml, isTwoDimensionalization, noDiKaList, translateName);
|
|
|
+ if ("-1".equals(stringObjectMap.get("code").toString())) {
|
|
|
+ stringObjectMap.put("returnData", new ArrayList<>());
|
|
|
+ }
|
|
|
+ batchList.addAll((List) stringObjectMap.get("returnData"));
|
|
|
+ });
|
|
|
+ return processSuccess(batchList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * map key值转换
|
|
|
+ *
|
|
|
+ * @param returnDataList
|
|
|
+ * @param translateName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, Object> translateMapName(List<Map<String, Object>> returnDataList, Map<String, String> translateName) {
|
|
|
+ try {
|
|
|
+ List<Map<String, Object>> dataList = new ArrayList<>();
|
|
|
+ for (Map<String, Object> returnData : returnDataList) {
|
|
|
+ Map<String, Object> temMap = new HashMap<>();
|
|
|
+ for (String key : returnData.keySet()) {
|
|
|
+ if (translateName.containsKey(key)) {
|
|
|
+ temMap.put(translateName.get(key), returnData.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dataList.add(temMap);
|
|
|
+ }
|
|
|
+ return processSuccess(dataList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return processFail("translateMapName替换异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * xml 转map 通用化
|
|
|
+ *
|
|
|
+ * @param xml
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, Object> xmlToMapFormat(String xml) {
|
|
|
+ try {
|
|
|
+ String xmlStr = xml.substring(xml.contains("<") ? xml.indexOf("<") : 0, xml.lastIndexOf(">") > 0 ? xml.lastIndexOf(">") + 1 : xml.length());
|
|
|
+ xmlStr = (xmlStr.contains("<?xml") ? "" : "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n").concat(xmlStr);
|
|
|
+ Document doc = DocumentHelper.parseText(xmlStr);
|
|
|
+ Element root = doc.getRootElement();
|
|
|
+ Map<String, Object> returnMap = xmlToMap(root);
|
|
|
+// returnMap.put("souceData", xml);
|
|
|
+ returnMap.put("sourceData", xml);
|
|
|
+
|
|
|
+ return processSuccess(returnMap);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Map<String, Object> errMap = processFail("入参不是规则的xml :" + xml);
|
|
|
+// errMap.put("souceData", xml);
|
|
|
+ errMap.put("sourceData", xml);
|
|
|
+
|
|
|
+ return errMap;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * map 二维化
|
|
|
+ *
|
|
|
+ * @param inMap 需要二维化map
|
|
|
+ * @param noDiKaList 需要dika
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Map<String, Object> twoDimensionalizationFormat(Map<String, Object> inMap, Object noDiKaList) {
|
|
|
+ try {
|
|
|
+ List<Map<String, Object>> returnDataList = new ArrayList<>();
|
|
|
+ Map<String, Object> toInMap = MapTools.clone(inMap);
|
|
|
+ if (Objects.nonNull(toInMap) && !toInMap.isEmpty()) {
|
|
|
+ returnDataList = twoDimensionalization(toInMap, "", noDiKaList); /*生命周期未添加*/
|
|
|
+ }
|
|
|
+ return processSuccess(returnDataList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return processFail(UniReturnUtil.getMessage(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<Map<String, Object>> twoDimensionalization(Map<String, Object> inMap, String keyName, Object noDiKaList) {
|
|
|
+ //初始化一个返回数组List<map<string,object>> returnList
|
|
|
+ List<Map<String, Object>> returnList = new ArrayList<>();
|
|
|
+ // 默认添加一个空MAP----returnList.add(new map)//目的是确保计算迪卡乘积以及最后的单值赋值不出现问题
|
|
|
+ returnList.add(new HashMap<>());
|
|
|
+ //循环入参inMap
|
|
|
+ for (String currentKeyName : inMap.keySet()) {
|
|
|
+// 组装键名itemKeyName = (keyName为空?'':keyName) + '_' + currentKeyName
|
|
|
+ String itemKeyName = (Objects.equals(keyName, "") ? "" : keyName + '_') + currentKeyName;//组装键名
|
|
|
+ Object currentValue = inMap.get(currentKeyName);
|
|
|
+ if (currentValue instanceof List) {
|
|
|
+ List<Map<String, Object>> tempList = new ArrayList<>();
|
|
|
+ List<Object> list = (List<Object>) currentValue;
|
|
|
+ if (Objects.nonNull(noDiKaList) && noDiKaList instanceof List<?> tpList && tpList.contains(itemKeyName)) {
|
|
|
+ Map<String, Object> tempMap = new HashMap<>();
|
|
|
+ tempMap.put(itemKeyName, currentValue.toString());
|
|
|
+ tempList.add(tempMap);
|
|
|
+ } else {
|
|
|
+ for (Object o : list) {
|
|
|
+ if (o instanceof Map) {
|
|
|
+ List<Map<String, Object>> tempListMap = twoDimensionalization((Map<String, Object>) o, itemKeyName, noDiKaList);
|
|
|
+ if (noDiKaList instanceof String inStr && inStr.equals("noDika")) {
|
|
|
+ tempList.addAll(tempListMap);
|
|
|
+ } else {
|
|
|
+ tempListMap = dikaMap(returnList, tempListMap);//计算迪卡乘积
|
|
|
+ tempList.addAll(tempListMap);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ tempList.add(new HashMap<>() {{
|
|
|
+ put(itemKeyName, o);
|
|
|
+ }});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ returnList = tempList;
|
|
|
+ } else {
|
|
|
+ if (currentValue instanceof Map) {
|
|
|
+ List<Map<String, Object>> tempListMap = twoDimensionalization((Map<String, Object>) currentValue, itemKeyName, noDiKaList);//递归下一级
|
|
|
+ List<Map<String, Object>> tempList = new ArrayList<>();
|
|
|
+ if (Objects.nonNull(noDiKaList) && noDiKaList instanceof List<?> tpList && tpList.contains(itemKeyName)) {
|
|
|
+ Map<String, Object> tempMap = new HashMap<>();
|
|
|
+ tempMap.put(itemKeyName, currentValue.toString());
|
|
|
+ tempList.add(tempMap);
|
|
|
+ returnList = tempList;
|
|
|
+ } else {
|
|
|
+ if (noDiKaList instanceof String inStr && inStr.equals("noDika")) {
|
|
|
+ returnList = tempListMap;
|
|
|
+ } else {
|
|
|
+ returnList = dikaMap(returnList, tempListMap);//计算迪卡乘积后替换当前的returnList
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (Map<String, Object> map : returnList) {//循环returnList
|
|
|
+ map.put(itemKeyName, currentValue);////值不为MAP也不是数组则直接添加
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return returnList;//返回returnList
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //MAP二维化辅助方法,返回List<map<string,object>>
|
|
|
+ private static List<Map<String, Object>> dikaMap(List<Map<String, Object>> parentList, List<Map<String, Object>> childList) {
|
|
|
+// 初始化一个返回数组
|
|
|
+ List<Map<String, Object>> returnList = new ArrayList<>();
|
|
|
+ for (Map<String, Object> childwMap : childList) {
|
|
|
+ HashMap<String, Object> childwMapClone = MapTools.clone(childwMap);
|
|
|
+ if (Objects.isNull(childwMapClone)) childwMapClone = new HashMap<>();
|
|
|
+ for (Map<String, Object> parentMap : parentList) {
|
|
|
+ HashMap<String, Object> parentMapClone = MapTools.clone(parentMap);
|
|
|
+ if (Objects.isNull(parentMapClone)) {
|
|
|
+ parentMapClone = new HashMap<>();
|
|
|
+ }
|
|
|
+ parentMapClone.putAll(childwMapClone);
|
|
|
+ returnList.add(parentMapClone);//注意使用HashMap.putall实现深拷贝
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return returnList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, Object> xmlToMap(Element element) {
|
|
|
+ String currentName = element.getName(); //当前节点名称
|
|
|
+ String currentValue = MapTools.getText(element.getTextTrim());// 当前节点值
|
|
|
+
|
|
|
+ Map<String, Object> returnMap = new HashMap<>();//初始化一个返回的结构
|
|
|
+ Map<String, Object> attrMap = new HashMap<>(); //初始化一个当前节点的属性结构
|
|
|
+ List<Attribute> attributes = element.attributes(); //属性处理
|
|
|
+ for (Attribute childAttribute : attributes) { //循环当前节点所有属性
|
|
|
+ attrMap.put(childAttribute.getName(), childAttribute.getValue());//逐个添加属性值到属性结构
|
|
|
+ }
|
|
|
+ List<Element> childNodes = element.elements(); //获取当前节点的所有子节点
|
|
|
+ if (childNodes.isEmpty() && !attrMap.isEmpty()) { // //无子节点且当前节点存在属性则
|
|
|
+ attrMap.put(currentName, currentValue); //当前节点值添加到属性中
|
|
|
+ }
|
|
|
+ for (Element childElement : childNodes) { //循环所有子节点,如果没有子节点不会进入循环
|
|
|
+ String childName = childElement.getName(); //获取子节点名称
|
|
|
+ if (attrMap.containsKey(childName)) { //已经存在相同子节点名称的值代表需要形成数组
|
|
|
+ Object oldValue = attrMap.get(childName); //获取已经存在的值
|
|
|
+ List<Object> newList = new ArrayList<>();
|
|
|
+ if (oldValue instanceof List) { //如果旧值已经是数组则
|
|
|
+ newList = (List<Object>) oldValue; //强制转换为数组
|
|
|
+ } else {
|
|
|
+ newList.add(oldValue); //转换为数组,旧值添加到数组中
|
|
|
+ }
|
|
|
+ newList.add(xmlToMap(childElement).get(childName));//添加子节点的数据,递归获取子节点的数据
|
|
|
+ attrMap.put(childName, newList);//添加到返回结构中;
|
|
|
+ } else {
|
|
|
+ attrMap.put(childName, xmlToMap(childElement).get(childName));//递归获取子节点的数据;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ returnMap.put(currentName, attrMap.isEmpty() ? currentValue : attrMap);//添加当前节点的属性以及值(包括了子节点)
|
|
|
+ return returnMap;
|
|
|
+ }
|
|
|
+}
|