package com.ibeetl.admin.flow.utils;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


public final class XmlUtil {
	private static final Logger logger = LogManager.getLogger(XmlUtil.class);

	/**
	 * 将对象转成String型的xml
	 *
	 * @param <T>
	 * @param beanobject
	 * @return
	 */
	private static <T> String changeXML(T beanobject) {
		StringBuffer xmlsb = new StringBuffer();
		try {
			if (beanobject == null) {
				throw new Exception("Parameter<beanobject> is NULL!");
			}
			Class<? extends Object> bodyClazz = beanobject.getClass();
			Field[] bodyFields = bodyClazz.getDeclaredFields();
			for (Field field : bodyFields) {
				String propertyName = field.getName();
				String propertyNameLetter = captureName(propertyName);
				if ("SerialVersionUID".equals(propertyNameLetter)) {
					continue;
				}
				Method m = bodyClazz.getDeclaredMethod("get" + propertyNameLetter);
				Object propertyValue = m.invoke(beanobject, null);
				if (propertyValue == null || propertyValue == "") {
					propertyValue = "";
				}
				xmlsb.append("<" + propertyName + ">" + propertyValue + "</" + propertyName + ">");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// logger.info("分段xml:"+xmlsb);
		return xmlsb.toString();
	}

	/**
	 * 替换首字母为大写
	 *
	 * @param reflepar
	 * @return
	 */
	public static String captureName(String reflepar) throws Exception {
		if (reflepar == null || reflepar == "") {
			throw new Exception("Parameter<reflepar> is NULL!");
		}
		return reflepar.replaceFirst(reflepar.substring(0, 1), reflepar.substring(0, 1).toUpperCase());
	}

	/**
	 * 将传进来的实体拼成String型xml
	 *
	 * @param <T>
	 * @param header
	 * @param body
	 * @param footer
	 * @return
	 */
	public static <T> String BeantoXML(T header, T body, T footer) {

		StringBuffer xmlsb = new StringBuffer();
		xmlsb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		xmlsb.append("<UNI_ECB_INFO_INPUT>");
		xmlsb.append("<UNI_ECB_HEAD>");
		xmlsb.append(changeXML(header));
		xmlsb.append("</UNI_ECB_HEAD>");
		// ----------------------------------------------------
		xmlsb.append("<UNI_ECB_BODY>");
		xmlsb.append(changeXML(body));
		xmlsb.append("</UNI_ECB_BODY>");
		// ----------------------------------------------------
		xmlsb.append("<UNI_ECB_ATTACHED>");
		xmlsb.append(changeXML(footer));
		xmlsb.append("</UNI_ECB_ATTACHED>");
		// ---------------------------------------------------
		xmlsb.append("</UNI_ECB_INFO_INPUT>");
		logger.info("最终的xml：" + xmlsb.toString());
		return xmlsb.toString();
	}

	// String转换成Json的工具方法
	public static JSONObject xml2JSON(String xml) {
		try {
			logger.info("xml格式化成json传过来的xml是："+xml);
			XMLSerializer xmlSerializer = new XMLSerializer();
			JSON json = xmlSerializer.read(xml);
			return (JSONObject) json;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
