import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.zttx.bert.wx.vo.recv.WxRecvMsg;
import org.zttx.bert.wx.vo.send.WxSendMsg;
/**
* @ClassName: WxMsgKit.java
* @Description: 消息解析
* @author Bert
* 2014-3-31 下午10:13:35
*/
public final class WxMsgKit {
private static final Map<String, WxRecvMsgParser> recvParserMap = new HashMap<String, WxRecvMsgParser>();
static {
// 文本消息解析程序
recvParserMap.put("text", new WxRecvTextMsgParser());
}
/**
* 解析输入流
* @param in
* @return
* @throws JDOMException
* @throws IOException
*/
public static WxRecvMsg parse(InputStream in) throws JDOMException, IOException {
Document dom = new SAXBuilder().build(in);
Element msgType = dom.getRootElement().getChild("MsgType");
if(null != msgType) {
String txt = msgType.getText().toLowerCase();
WxRecvMsgParser parser = recvParserMap.get(txt);
if(null != parser) {
return parser.parser(dom);
} else {
return null;
}
}
return null;
}
public static Document parse(WxSendMsg msg) throws JDOMException {
return msg.toDocument();
}
}
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.zttx.bert.wx.vo.recv.WxRecvMsg;
import org.zttx.bert.wx.vo.send.WxSendMsg;
/**
* @ClassName: WxMsgKit.java
* @Description: 消息解析
* @author Bert
* 2014-3-31 下午10:13:35
*/
public final class WxMsgKit {
private static final Map<String, WxRecvMsgParser> recvParserMap = new HashMap<String, WxRecvMsgParser>();
static {
// 文本消息解析程序
recvParserMap.put("text", new WxRecvTextMsgParser());
}
/**
* 解析输入流
* @param in
* @return
* @throws JDOMException
* @throws IOException
*/
public static WxRecvMsg parse(InputStream in) throws JDOMException, IOException {
Document dom = new SAXBuilder().build(in);
Element msgType = dom.getRootElement().getChild("MsgType");
if(null != msgType) {
String txt = msgType.getText().toLowerCase();
WxRecvMsgParser parser = recvParserMap.get(txt);
if(null != parser) {
return parser.parser(dom);
} else {
return null;
}
}
return null;
}
public static Document parse(WxSendMsg msg) throws JDOMException {
return msg.toDocument();
}
}