[记录] 表情文字转表情图片URL

即时通讯中,移动端和网页端发送信息,移动端使用表情文字(后面怎么换成图片的不大清楚),而网页端直接插入表情图片,因此相互发送信息时要先对表情信息进行处理。

感觉代码写得不大漂亮,但又不知道怎么改,先暂存。

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

enum Dev {
    MOBILE, WEB
}

public class Emotions {
    /**
     * 移动端表情字符串数组
     */
    private static String[] mobile = { "[/微笑]", "[/瘪嘴]", "[/好色]", "[/瞪眼]",
            "[/得意]", "[/流泪]", "[/害羞]", "[/闭嘴]", "[/睡觉]", "[/大哭]", "[/尴尬]",
            "[/愤怒]", "[/调皮]", "[/呲牙]", "[/惊讶]", "[/难过]", "[/装酷]", "[/冷汗]",
            "[/抓狂]", "[/呕吐]", "[/偷笑]", "[/可爱]", "[/白眼]", "[/傲慢]", "[/饥饿]",
            "[/困]", "[/恐惧]", "[/流汗]", "[/憨笑]", "[/大兵]", "[/奋斗]", "[/咒骂]",
            "[/疑问]", "[/嘘嘘]", "[/晕]", "[/折磨]", "[/衰]", "[/骷髅]", "[/敲打]",
            "[/再见]" };
    /**
     * 网页端表情前缀
     */
    private static String prefix = "<img src='/pathToEmoticons/";
    /**
     * 网页端表情后缀
     */
    private static String end = ".gif'>";

    private static Map<String, String> mobileToWebEmotions = new HashMap<String, String>();
    private static Map<String, String> webToMobileEmotions = new HashMap<String, String>();

    static {
        for (int i = 0; i < mobile.length; i++) {
            mobileToWebEmotions.put(mobile[i], prefix + i + end);
            webToMobileEmotions.put(prefix + i + end, mobile[i]);
        }
    }

    /**
     * 表情转换
     *
     * @param message
     * @param isToMobile
     *            true 为转成手机端表情 false为转为网页端表情
     * @return
     */
    private static String formatEmotion(String message, Dev dev) {
        String regex = null;
        Pattern pattern = null;
        Matcher matcher = null;
        StringBuffer buffer = new StringBuffer();
        if (dev == Dev.WEB) {
            regex = "\\[\\/[\u4E00-\u9FA5]*\\]";
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(message);
            while (matcher.find()) {
                matcher.appendReplacement(buffer,
                        mobileToWebEmotions.get(matcher.group()));
            }
        } else {
            regex = prefix + "[0-9]+" + end;
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher(message);
            while (matcher.find()) {
                matcher.appendReplacement(buffer,
                        webToMobileEmotions.get(matcher.group()));
            }
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String msg = "[/微笑]Hello[/憨笑]";
        System.out.println(Emotions.formatEmotion(msg, Dev.WEB));
    }

} /* Output:
<img src='/pathToEmoticons/0.gif'>Hello<img src='/pathToEmoticons/28.gif'>
 */// :~

版本2,修改枚举

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Emotions {
    /**
     * 移动端表情字符串数组
     */
    private static String[] mobile = { "[/微笑]", "[/瘪嘴]", "[/好色]", "[/瞪眼]",
            "[/得意]", "[/流泪]", "[/害羞]", "[/闭嘴]", "[/睡觉]", "[/大哭]", "[/尴尬]",
            "[/愤怒]", "[/调皮]", "[/呲牙]", "[/惊讶]", "[/难过]", "[/装酷]", "[/冷汗]",
            "[/抓狂]", "[/呕吐]", "[/偷笑]", "[/可爱]", "[/白眼]", "[/傲慢]", "[/饥饿]",
            "[/困]", "[/恐惧]", "[/流汗]", "[/憨笑]", "[/大兵]", "[/奋斗]", "[/咒骂]",
            "[/疑问]", "[/嘘嘘]", "[/晕]", "[/折磨]", "[/衰]", "[/骷髅]", "[/敲打]",
            "[/再见]" };
    /**
     * 网页端表情前缀
     */
    private static String prefix = "<img src='/pathToEmoticons/";
    /**
     * 网页端表情后缀
     */
    private static String end = ".gif'>";

    private static Map<String, String> mobileToWebEmotions = new HashMap<String, String>();
    private static Map<String, String> webToMobileEmotions = new HashMap<String, String>();

    static {
        for (int i = 0; i < mobile.length; i++) {
            mobileToWebEmotions.put(mobile[i], prefix + i + end);
            webToMobileEmotions.put(prefix + i + end, mobile[i]);
        }
    }

    public static enum Dev {
        MOBILE(prefix + "[0-9]+" + end, webToMobileEmotions),
        WEB("\\[\\/[\u4E00-\u9FA5]*\\]", mobileToWebEmotions);

        private String regex;
        private Map<String, String> map;

        Dev(String regex, Map<String, String> map) {
            this.regex = regex;
            this.map = map;
        }
    }
    /**
     * 表情转换
     *
     * @param message
     * @param dev 目标设备
     * @return
     */
    private static String formatEmotion(String message, Dev dev) {
        Pattern pattern = null;
        Matcher matcher = null;
        StringBuffer buffer = new StringBuffer();

        pattern = Pattern.compile(dev.regex);
        matcher = pattern.matcher(message);
        while (matcher.find()) {
            matcher.appendReplacement(buffer,
                    dev.map.get(matcher.group()));
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String msg = "[/微笑]Hello[/憨笑]";
        System.out.println(Emotions.formatEmotion(msg, Dev.WEB));
    }

} /* Output:
<img src='/pathToEmoticons/0.gif'>Hello<img src='/pathToEmoticons/28.gif'>
 */// :~