cn.com.sunjiesh.wechat.helper.WechatMessageConvertDocumentHelper.java Source code

Java tutorial

Introduction

Here is the source code for cn.com.sunjiesh.wechat.helper.WechatMessageConvertDocumentHelper.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.sunjiesh.wechat.helper;

import cn.com.sunjiesh.wechat.model.response.message.WechatReceiveReplayImageMessageResponse;
import cn.com.sunjiesh.wechat.model.response.message.WechatReceiveReplayNewsMessageResponse;
import cn.com.sunjiesh.wechat.model.response.message.WechatReceiveReplayTextMessageResponse;
import cn.com.sunjiesh.wechat.model.response.message.WechatReceiveReplayVoiceMessageResponse;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * ??XML
 *
 * @author tom
 */
public class WechatMessageConvertDocumentHelper {

    /**
     * ???XML
     *
     * @param messageResponse ?
     * @return XML
     */
    public static Document textMessageResponseToDocument(WechatReceiveReplayTextMessageResponse messageResponse) {

        final String toUserName = messageResponse.getToUserName();
        final String fromUserName = messageResponse.getFromUserName();
        final String msgType = messageResponse.getMsgType();

        Document respDoc = initRespDoc(toUserName, fromUserName, msgType);

        Element contentEle = respDoc.getRootElement().addElement("Content");
        contentEle.setText(messageResponse.getContent());

        return respDoc;
    }

    /**
     * ??XML
     * @param messageResponse
     * @return
     */
    public static Document imageMessageResponseToDocumnet(WechatReceiveReplayImageMessageResponse messageResponse) {
        final String toUserName = messageResponse.getToUserName();
        final String fromUserName = messageResponse.getFromUserName();
        final String msgType = messageResponse.getMsgType();

        Document respDoc = initRespDoc(toUserName, fromUserName, msgType);
        Element imageEle = respDoc.getRootElement().addElement("Image");
        Element mediaIdEle = imageEle.addElement("MediaId");
        mediaIdEle.setText(messageResponse.getMediaId());
        return respDoc;
    }

    /**
     * ??XML
     * @param messageResponse
     * @return
     */
    public static Document voiceMessageResponseToDocumnet(WechatReceiveReplayVoiceMessageResponse messageResponse) {
        final String toUserName = messageResponse.getToUserName();
        final String fromUserName = messageResponse.getFromUserName();
        final String msgType = messageResponse.getMsgType();

        Document respDoc = initRespDoc(toUserName, fromUserName, msgType);
        Element voiceEle = respDoc.getRootElement().addElement("Voice");
        Element mediaIdEle = voiceEle.addElement("MediaId");
        mediaIdEle.setText(messageResponse.getMediaId());
        return respDoc;
    }

    /**
     * ???XML
     *
     * @param messageResponse ?
     * @return XML
     */
    public static Document newsMessageResponseToDocument(WechatReceiveReplayNewsMessageResponse messageResponse) {
        final String toUserName = messageResponse.getToUserName();
        final String fromUserName = messageResponse.getFromUserName();
        final String msgType = messageResponse.getMsgType();

        Document respDoc = initRespDoc(toUserName, fromUserName, msgType);
        Element rootEle = respDoc.getRootElement();
        Element articleCountEle = rootEle.addElement("ArticleCount");
        Element articlesEle = rootEle.addElement("Articles");
        messageResponse.getItemList().stream().forEach((item) -> {
            Element itemEle = articlesEle.addElement("item");
            if (!StringUtils.isEmpty(item.getTitle())) {
                Element titleEle = itemEle.addElement("Title");
                titleEle.setText(item.getTitle());
            }
            if (!StringUtils.isEmpty(item.getDescription())) {
                Element descriptionEle = itemEle.addElement("Description");
                descriptionEle.setText(item.getDescription());
            }
            if (!StringUtils.isEmpty(item.getPicUrl())) {
                Element picUrlEle = itemEle.addElement("PicUrl");
                picUrlEle.setText(item.getPicUrl());
            }
            if (!StringUtils.isEmpty(item.getUrl())) {
                Element urlEle = itemEle.addElement("Url");
                urlEle.setText(item.getUrl());
            }
        });
        articleCountEle.setText(String.valueOf(messageResponse.getItemList().size()));

        return respDoc;
    }

    /**
     * ?XML
     *
     * @param toUserName ??OpenID
     * @param fromUserName ??
     * @param msgType ?
     * @return XML
     */
    private static Document initRespDoc(final String toUserName, final String fromUserName, final String msgType) {
        Document respDoc = DocumentHelper.createDocument();
        Element rootEle = respDoc.addElement("xml");
        Element toUserNameEle = rootEle.addElement("ToUserName");
        Element fromUserNameEle = rootEle.addElement("FromUserName");
        Element createTimeEle = rootEle.addElement("CreateTime");
        Element msgTypeEle = rootEle.addElement("MsgType");
        toUserNameEle.setText(toUserName);
        fromUserNameEle.setText(fromUserName);
        createTimeEle.setText(String.valueOf(System.currentTimeMillis()));
        msgTypeEle.setText(msgType);
        return respDoc;
    }
}