All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.seejoke.wechat.util.xml.XStreamTransformer Maven / Gradle / Ivy

package com.seejoke.wechat.util.xml;

import com.seejoke.wechat.entity.*;
import com.thoughtworks.xstream.XStream;

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

/**
 * 指定类的专属XStream
 * @author diaodiaofly
 *
 */
public class XStreamTransformer {

	protected static final Map CLASS_2_XSTREAM_INSTANCE = configXStreamInstance();


	@SuppressWarnings("unchecked")
	public static  T fromXml(Class clazz, String xml) {
		T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
		return object;
	}

	@SuppressWarnings("unchecked")
	public static  T fromXml(Class clazz, InputStream is) {
		T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(is);
		return object;
	}

	public static  String toXml(Class clazz, T object) {
		return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
	}

	private static Map configXStreamInstance() {
		Map map = new HashMap();
		map.put(WxXmlMessage.class, config_WxXmlMessage());
		map.put(WxXmlOutNewsMessage.class, config_WxXmlOutNewsMessage());
		map.put(WxXmlOutTextMessage.class, config_WxXmlOutTextMessage());
		map.put(WxXmlOutImageMessage.class, config_WxXmlOutImageMessage());
		map.put(WxXmlOutVideoMessage.class, config_WxXmlOutVideoMessage());
		map.put(WxXmlOutVoiceMessage.class, config_WxXmlOutVoiceMessage());
		return map;
	}

	private static XStream config_WxXmlMessage() {
		XStream xstream = XStreamInitializer.getInstance();
		xstream.processAnnotations(WxXmlMessage.class);
		xstream.processAnnotations(WxXmlMessage.ScanCodeInfo.class);
		xstream.processAnnotations(WxXmlMessage.SendPicsInfo.class);
		xstream.processAnnotations(WxXmlMessage.SendPicsInfo.Item.class);
		xstream.processAnnotations(WxXmlMessage.SendLocationInfo.class);
		return xstream;
	}

	private static XStream config_WxXmlOutImageMessage() {
		XStream xstream = XStreamInitializer.getInstance();
		xstream.processAnnotations(AbstractWxXmlOutMessage.class);
		xstream.processAnnotations(WxXmlOutImageMessage.class);
		return xstream;
	}

	private static XStream config_WxXmlOutNewsMessage() {
		XStream xstream = XStreamInitializer.getInstance();
		xstream.processAnnotations(AbstractWxXmlOutMessage.class);
		xstream.processAnnotations(WxXmlOutNewsMessage.class);
		xstream.processAnnotations(WxXmlOutNewsMessage.Item.class);
		return xstream;
	}

	private static XStream config_WxXmlOutTextMessage() {
		XStream xstream = XStreamInitializer.getInstance();
		xstream.processAnnotations(AbstractWxXmlOutMessage.class);
		xstream.processAnnotations(WxXmlOutTextMessage.class);
		return xstream;
	}

	private static XStream config_WxXmlOutVideoMessage() {
		XStream xstream = XStreamInitializer.getInstance();
		xstream.processAnnotations(AbstractWxXmlOutMessage.class);
		xstream.processAnnotations(WxXmlOutVideoMessage.class);
		xstream.processAnnotations(WxXmlOutVideoMessage.Video.class);
		return xstream;
	}

	private static XStream config_WxXmlOutVoiceMessage() {
		XStream xstream = XStreamInitializer.getInstance();
		xstream.processAnnotations(AbstractWxXmlOutMessage.class);
		xstream.processAnnotations(WxXmlOutVoiceMessage.class);
		return xstream;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy