Bläddra i källkod

Merge branch 'master' into debug

rengb 5 år sedan
förälder
incheckning
3ba0a9c200

+ 61 - 0
public/src/main/java/com/lantone/qc/pub/util/EnDecodeUtil.java

@@ -0,0 +1,61 @@
+package com.lantone.qc.pub.util;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @Description: 加解密工具类
+ * @author: ztg
+ * @time: 2018/11/8 14:38
+ */
+public class EnDecodeUtil {
+
+    /**
+     * 采用BASE64算法对字符串进行加密
+     * @param str 原字符串
+     * @return 加密后的字符串
+     */
+    public static String encode(String str) {
+        byte[] b = null;
+        String s = null;
+        try {
+            b = str.getBytes("utf-8");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        if (b != null) {
+            s = new BASE64Encoder().encode(b);
+        }
+        return s;
+    }
+
+    /**
+     * 字符串解密,采用BASE64的算法
+     * @param s 需要解密的字符串
+     * @return 解密后的字符串
+     */
+    public static String decode(String s) {
+        byte[] b = null;
+        String result = null;
+        if (s != null) {
+            BASE64Decoder decoder = new BASE64Decoder();
+            try {
+                b = decoder.decodeBuffer(s);
+                result = new String(b, "utf-8");
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return result;
+    }
+
+
+    public static void main(String[] args) {
+        String abc = "<20;age  >100";
+        String c = encode(abc);
+        System.out.println(c);
+        System.out.println(decode(c));
+    }
+}

+ 0 - 60
trans/src/main/java/com/lantone/qc/trans/comsis/OrdinaryAssistant.java

@@ -1,12 +1,8 @@
 package com.lantone.qc.trans.comsis;
 
-import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-import com.lantone.qc.pub.util.FileUtil;
 import com.lantone.qc.pub.util.MapUtil;
 import com.lantone.qc.pub.util.StringUtil;
-import com.lantone.qc.trans.changx.util.CxXmlUtil;
-import com.lantone.qc.trans.taizhou.util.TzXmlUtil;
 
 import java.util.HashSet;
 import java.util.List;
@@ -50,60 +46,4 @@ public class OrdinaryAssistant {
         return retMap;
     }
 
-    public static void main(String[] args) {
-        changx();
-    }
-
-    public static void taizhou() {
-        String msg = FileUtil.fileRead("C:\\Users\\RGB\\Desktop\\调试\\台州出院小结.xml");
-        Map<String, String> map = TzXmlUtil.getXmlToMapForTZ(msg);
-        map.keySet().forEach(key -> {
-            System.out.println("\"" + key + "=\",");
-        });
-    }
-
-    public static void changx() {
-        String content = FileUtil.fileRead("C:\\Users\\RGB\\Desktop\\调试\\入院记录.txt");
-
-        Map<String, String> xmlNodeValueMap = CxXmlUtil.firstLevelNodeValue("//DocObjContent//Region", content);
-        Map<String, String> structureMap = OrdinaryAssistant.mapKeyContrast(xmlNodeValueMap, keyContrasts);
-
-        String text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent/Region/Content_Text");
-        if (StringUtil.isBlank(text)) {
-            text = CxXmlUtil.getTextByNodePath(content, "//DocObjContent/Region");
-        }
-        Map<String, String> cutWordMap = Preproc.getCutWordMap(true, sourceTitles, text);
-        cutWordMap.putAll(structureMap);
-
-
-        cutWordMap.keySet().forEach(key -> {
-            System.out.println(key + "----" + cutWordMap.get(key));
-        });
-
-
-        //        xmlNodeValueMap.keySet().forEach(key -> {
-        //            System.out.println("\"" + key + "=\",");
-        //
-        //        });
-
-
-    }
-
-    private static List<String> sourceTitles = Lists.newArrayList(
-            "抢救时间",
-            "记录时间",
-            "记录医师",
-            "抢救过程",
-            "参与人员"
-    );
-
-    private static List<String> keyContrasts = Lists.newArrayList(
-            "记录日期=记录时间",
-            "病情变化情况=",
-            "抢救时间及措施++++抢救措施=抢救过程",
-            "参加抢救者的姓名及专业技术职称++++参加现场抢救的医师及护理人员=参与人员",
-            "签名++++=记录医师",
-            "签名时间=抢救时间"
-    );
-
 }

+ 115 - 0
trans/src/main/java/com/lantone/qc/trans/comsis/XmlUtil.java

@@ -0,0 +1,115 @@
+package com.lantone.qc.trans.comsis;
+
+import com.lantone.qc.pub.util.EnDecodeUtil;
+
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @Description: xml公共处理
+ * @author: rengb
+ * @time: 2020/4/17 15:16
+ */
+public class XmlUtil {
+
+    private static String LESS_THAN_SYMBOL_REPLACE = "th@xyh";
+    private static String START_SURROUND_SYMBOL = "th@ksxyh";
+    private static String END_SURROUND_SYMBOL = "th@jsdyh";
+
+    /**
+     * 纠正xml内容,将因为不规范而影响dom4j解析的部分先替换掉,解析完后再替换回来
+     *
+     * @param xml
+     * @return
+     */
+    public static String xmlErrorCorrection(String xml) {
+        StringBuffer sbf = new StringBuffer();
+        Pattern pattern = Pattern.compile("\\<.[^<>]*\\>");
+        Matcher matcher = pattern.matcher(xml);
+        int start = 0, end = 0;
+        String xmlCopy = xml, elementName = null, elementNameReplace = null;
+        while (matcher.find()) {
+            elementName = matcher.group();
+            start = matcher.start();
+            if (end > 0 && start > 0 && end < start) {
+                sbf.append(xml.substring(end, start).replaceAll("<", LESS_THAN_SYMBOL_REPLACE));
+            }
+            if (isCorrectXmlElement(elementName, xmlCopy)) {
+                sbf.append(elementName);
+            } else {
+                elementNameReplace = START_SURROUND_SYMBOL + EnDecodeUtil.encode(elementName) + END_SURROUND_SYMBOL;
+                xmlCopy = xmlCopy.replace(elementName, elementNameReplace);
+                sbf.append(elementNameReplace);
+            }
+            end = matcher.end();
+        }
+        return sbf.toString();
+    }
+
+    /**
+     * 判断某个xml标签,在当前xml文档中,是否是正常的xml标签
+     *
+     * @param elementName 标签
+     * @param xml         当前xml文档
+     * @return
+     */
+    public static boolean isCorrectXmlElement(String elementName, String xml) {
+        //过滤类型:<>、< abc>
+        if (elementName.length() == 2 || elementName.indexOf(" ") == 1) {
+            return false;
+        }
+        //过滤类型:<abc/ >
+        String elementNameCopy = elementName.substring(1, elementName.length() - 1).trim();
+        if (elementNameCopy.endsWith("/") && elementName.lastIndexOf(" ") == elementName.length() - 2) {
+            return false;
+        }
+        //过滤类型:有<abc>,而没有</abc>
+        if (!elementNameCopy.endsWith("/")) {
+            if (elementNameCopy.startsWith("/")) {
+                Pattern pattern = Pattern.compile("\\<" + elementNameCopy.substring(1) + ".[^<>]*\\>");
+                Matcher matcher = pattern.matcher(xml);
+                if (xml.indexOf("<" + elementNameCopy.substring(1) + ">") == -1 && !matcher.find()) {
+                    return false;
+                }
+            } else {
+                int firstBlankIndex = elementNameCopy.indexOf(" ");
+                firstBlankIndex = firstBlankIndex == -1 ? elementNameCopy.length() : firstBlankIndex;
+                elementNameCopy = elementNameCopy.substring(0, firstBlankIndex);
+                if (xml.indexOf("</" + elementNameCopy + ">") == -1) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 对由xml解析得到的map进行遍历,将value值中被替换的部分还原回来,
+     *
+     * @param sourceMap
+     * @return
+     */
+    public static Map<String, String> correctMapOfXml(Map<String, String> sourceMap) {
+        Pattern pattern = Pattern.compile(START_SURROUND_SYMBOL + ".*" + END_SURROUND_SYMBOL);
+        String value = null, valueCopy = null, matMsg = null;
+        Matcher matcher = null;
+        for (String key : sourceMap.keySet()) {
+            value = sourceMap.get(key);
+            valueCopy = value;
+            valueCopy = valueCopy.replaceAll(LESS_THAN_SYMBOL_REPLACE, "<");
+            matcher = pattern.matcher(value);
+            while (matcher.find()) {
+                matMsg = matcher.group();
+                valueCopy = valueCopy.replace(matMsg,
+                        EnDecodeUtil.decode(
+                                matMsg.replace(START_SURROUND_SYMBOL, "").replace(END_SURROUND_SYMBOL, "")
+                        )
+                );
+            }
+            sourceMap.put(key, valueCopy);
+        }
+        return sourceMap;
+    }
+
+}

+ 3 - 2
trans/src/main/java/com/lantone/qc/trans/taizhou/util/TzXmlUtil.java

@@ -2,6 +2,7 @@ package com.lantone.qc.trans.taizhou.util;
 
 import com.google.common.collect.Maps;
 import com.lantone.qc.pub.util.StringUtil;
+import com.lantone.qc.trans.comsis.XmlUtil;
 import com.lantone.qc.trans.util.http.db.DBUtil;
 import org.dom4j.Document;
 import org.dom4j.DocumentHelper;
@@ -35,7 +36,7 @@ public class TzXmlUtil {
     public static Map<String, String> getXmlToMapForTZ(String xml) {
         Map<String, String> retMap = Maps.newHashMap();
         try {
-            Document doc = DocumentHelper.parseText(xml);
+            Document doc = DocumentHelper.parseText(XmlUtil.xmlErrorCorrection(xml));
             List<Element> emrTermElements = doc.getRootElement().element("TermList").elements("EMR-TERM");
             emrTermElements.forEach(emrTermElement -> {
                 if(retMap == null || StringUtil.isEmpty(retMap.get(StringUtil.removeBlank(emrTermElement.attributeValue("ename"))))){
@@ -48,7 +49,7 @@ public class TzXmlUtil {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return retMap;
+        return XmlUtil.correctMapOfXml(retMap);
     }