package com.lantone.qc.dbanaly.facade.changx;

import com.google.common.collect.Maps;
import com.lantone.qc.pub.util.FileUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;
import java.util.Map;

/**
 * @Description: xml解析工具
 * @author: rengb
 * @time: 2020/3/28 14:23
 */
public class CxXmlUtil {

    public static Map<String, String> firstLevelNodeValue(String content) {
        Map<String, String> retMap = Maps.newLinkedHashMap();
        try {
            String helpTip, controlName, contentText, key;
            Element contentTextElement;
            Document doc = DocumentHelper.parseText(content);
            Element rootElement = null;
            if (doc.selectNodes("//DocObjContent//Region").size() == 0) {
                rootElement = (Element) doc.selectNodes("//DocObjContent").get(0);
            } else {
                rootElement = (Element) doc.selectNodes("//DocObjContent//Region").get(0);
            }
            List<Element> sonElements = rootElement.elements();
            for (Element sonElement : sonElements) {
                helpTip = sonElement.attributeValue("HelpTip");
                controlName = sonElement.attributeValue("ControlName");
                if (StringUtil.isBlank(helpTip) && StringUtil.isBlank(controlName)) {
                    continue;
                }
                if (StringUtil.equals(helpTip, controlName)) {
                    key = helpTip;
                } else {
                    key = (StringUtil.isBlank(helpTip) ? "" : helpTip) + "++++" + (StringUtil.isBlank(controlName) ? "" : controlName);
                }
                if (!keyAvailability(key)) {
                    continue;
                }
                contentTextElement = sonElement.element("Content_Text");
                if (contentTextElement == null) {
                    contentText = sonElement.getStringValue().trim();
                } else {
                    contentText = contentTextElement.getTextTrim();
                }
                retMap.put(StringUtil.removeBlank(key), contentText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retMap;
    }

    /**
     * 获取xml某一路径节点的内容
     *
     * @param xml
     * @param nodePath 节点在xml中的路径位置
     * @return
     */
    private static String getTextByNodePath(String xml, String nodePath) {
        String content = null;
        try {
            Document doc = DocumentHelper.parseText(xml);
            Element element = (Element) doc.selectNodes(nodePath).get(0);
            content = element.getStringValue().trim();
        } catch (Exception e) {
        }
        return content;
    }

    /**
     * 获取xml的文本内容
     *
     * @param xml
     * @return
     */
    public static String getXmlText(String xml) {
        String text = null;
        String[] nodePaths = {
                "//DocObjContent/Region/Content_Text",
                "//DocObjContent/Region",
                "//DocObjContent/Content_Text",
                "//DocObjContent"
        };
        for (String nodePath : nodePaths) {
            text = CxXmlUtil.getTextByNodePath(xml, nodePath);
            if (StringUtil.isNotBlank(text)) {
                break;
            }
        }
        return text;
    }

    /**
     * 判断key是否可用
     *
     * @param key
     * @return
     */
    public static boolean keyAvailability(String key) {
        boolean flag = true;
        String[] regexs = {
                "\\d*[+]+\\d*"
        };
        for (String regex : regexs) {
            if (key.matches(regex)) {
                flag = false;
                break;
            }
        }
        return flag;
    }


    public static void main(String[] args) {
        try {
            String context = FileUtil.fileRead("C:\\Users\\dell\\Desktop\\调试\\test1.txt");
            Map<String, String> sourceMap = CxXmlUtil.firstLevelNodeValue(context);
            for (String key : sourceMap.keySet()) {
                System.out.println("\"" + key + "=\",");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}