package ink.wgink.util.xml;

import ink.wgink.util.ReflectUtil;
import org.apache.commons.collections.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: XMLUtil
 * @Description: XML工具类
 * @Author: wanggeng
 * @Date: 2021/4/28 9:44 上午
 * @Version: 1.0
 */
public class XMLUtil {

    /**
     * XML转简单bean，bean属性类型为字符串
     *
     * @param xml
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> T xml2SampleBean(String xml, Class<T> clazz) throws Exception {
        if (StringUtils.isBlank(xml)) {
            return null;
        }
        Field[] fields = clazz.getDeclaredFields();
        Map<String, String[]> getSetMethodMap = ReflectUtil.fieldGetSetMethod(fields);
        if (getSetMethodMap == null) {
            return null;
        }
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
        Element rootElement = document.getRootElement();
        List<Element> elements = rootElement.elements();
        if (elements.isEmpty()) {
            return null;
        }
        T t = clazz.newInstance();
        for (Element element : elements) {
            String elementName = element.getName();
            String firstLower = elementName.substring(0, 1).toLowerCase();
            String firstLowerMethodName = firstLower + elementName.substring(1);
            String[] getSetMethodArray = getSetMethodMap.get(firstLowerMethodName);
            if (getSetMethodArray == null) {
                continue;
            }
            Method setMethod = clazz.getMethod(getSetMethodArray[1], String.class);
            if (setMethod == null) {
                continue;
            }
            String elementText = element.getTextTrim();
            setMethod.invoke(t, StringUtils.isBlank(elementText) ? "" : elementText);
        }
        return t;
    }

}
