package com.iamberry.wechat.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.web.socket.TextMessage;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.zxing.Writer;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;


public class MessageUtil {
	private final static XStream stream = new XStream(new DomDriver());
	
	
	
	/**
	 * 将一个XML格式的字符串转换为Java对象
	 * @param xml
	 * @param classT
	 * @return
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public static <T> Object xmlToBean(String xml, Class<T> classT) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		//创建XStream对象
//		XStream stream = new XStream(new DomDriver());
		//替换XML中的所有xml标签
		xml = xml.replaceAll("xml", classT.getName().toString());
		//通过Java反射创建对象
		Object object = classT.newInstance();
		//解析
		object = stream.fromXML(xml, object);
		//返回
		return object;
	}
	
	/**
	 * 将一个对象转换为一个XML格式的字符串
	 * @param object
	 * @return
	 */
	public static String beanToXML(Object object) {
		stream.alias("xml", object.getClass());
		return stream.toXML(object);
	}
	
	/**
	 * 转换JSON格式字符串为Bean
	 * @param json
	 * @param object
	 * @return
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	@SuppressWarnings("unchecked")
	public static Object jsonToBean(String json, @SuppressWarnings("rawtypes") Class classType) throws InstantiationException, IllegalAccessException {
		Gson gson = new Gson();
		//JsonElement root = gson.toJsonTree(str);
		Object object = classType.newInstance();
		object = gson.fromJson(json, classType);
		return object;
	}
	
	/**
	 * 将一个Object对象转换为一个String字符串
	 * @param object
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 */
	public static String objectToString(Object object) throws JsonGenerationException, JsonMappingException, IOException {
		// 对象转换字符串
		ObjectMapper objectMapper = new ObjectMapper();
		String resultStr = objectMapper.writeValueAsString(object);
		return resultStr;
	}
	
	 /** 
     * 解析微信发来的请求（XML） 
     *  
     * @param request 
     * @return 
     * @throws Exception 
     */ 
    @SuppressWarnings("unchecked") 
    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception { 
        // 将解析结果存储在HashMap中 
        Map<String, String> map = new HashMap<String, String>(); 
 
        // 从request中取得输入流 
        InputStream inputStream = request.getInputStream(); 
        // 读取输入流 
        SAXReader reader = new SAXReader(); 
        Document document = reader.read(inputStream); 
        // 得到xml根元素 
        Element root = document.getRootElement(); 
        // 得到根元素的所有子节点 
        List<Element> elementList = root.elements(); 
 
        // 遍历所有子节点 
        for (Element e : elementList) 
            map.put(e.getName(), e.getText()); 
 
        // 释放资源 
        inputStream.close(); 
        inputStream = null; 
 
        return map; 
    } 
    
}
