com.github.utils.mycollect.util.EmojiUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.github.utils.mycollect.util.EmojiUtil.java

Source

/**
 * Copyright 1999-2016 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com
 */
package com.github.utils.mycollect.util;

import org.apache.commons.lang3.StringUtils;

/**
 * Emoji?
 *
 * 
 * ?urf8 ?emoji,emojiutf8?utf8??emoji
 * ???utf8mb4,??htmlunicode????
 *
 * 
 * 1.containsEmoji()?emoji
 * 2.convertEmojiToUnicode()emoji?unicode ?
 * 3.?? filterEmoji
 *
 * @author zhouwenzhe
 *
 */
public class EmojiUtil {

    /**
     * ?emoji
     *
     * @param source
     * @return true
     */
    public static boolean containsEmoji(String source) {
        if (StringUtils.isBlank(source)) {

            return false;
        }

        for (int i = 0; i < source.length(); i++) {
            char codePoint = source.charAt(i);
            if (isEmojiCharacter(codePoint)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Emoji
     *
     * @param content
     * @return
     */
    public static String filterEmoji(String content) {

        StringBuilder result = new StringBuilder();
        if (StringUtils.isNotBlank(content)) {
            for (int i = 0; i < content.length(); i++) {
                char c = content.charAt(i);
                //CJK Compatibility,??
                Integer cInt = Integer.valueOf(c);
                if (cInt > 9470 && cInt < 12288) {
                    if (Integer.valueOf(content.charAt(i + 1)) == 65039) {
                        i++;
                    }
                    continue;
                }
                if (!isEmojiCharacter(c)) {

                    result.append(c);
                }
            }

        }
        return result.toString();

    }

    /**
     * Emoji?
     * ?emoji
     *
     * @param content
     * @param placeHolder ??????
     * @return
     */
    public static String convertEmojiToUnicode(String content, boolean placeHolder) {

        StringBuilder result = new StringBuilder();
        if (StringUtils.isNotBlank(content)) {
            for (int i = 0; i < content.length(); i++) {
                char c = content.charAt(i);
                //CJK Compatibility,??
                Integer cInt = Integer.valueOf(c);
                if (cInt > 9470 && cInt < 12288) {
                    result.append("&#").append(Integer.valueOf(c)).append(";");
                    if (Integer.valueOf(content.charAt(i + 1)) == 65039) {
                        i++;
                    }
                    continue;
                }
                if (isEmojiCharacter(c)) {
                    Integer unicodeInt = codePointToExtUnicode(c, content.charAt(i + 1));
                    if (unicodeInt < 1) {
                        //[int]???
                        if (placeHolder) {
                            result.append("[").append(Integer.valueOf(c)).append("]");
                        }
                        continue;
                    }
                    result.append("&#").append(unicodeInt).append(";");
                    i++;
                } else {
                    result.append(c);
                }
            }

        }
        return result.toString();
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }

    /**
     * Unicode??
     *
     * @param one
     * @param two
     * @return ??codePoint
     */
    private static Integer codePointToExtUnicode(int one, int two) {

        if (one > 0 && two > 0) {
            //U+D80055296,U+DC0056320
            String binStrHigh = Integer.toBinaryString(one - 55296);
            String binStrLow = Integer.toBinaryString(two - 56320);
            if (StringUtils.isNotBlank(binStrHigh) && StringUtils.isNotBlank(binStrLow)) {
                StringBuilder binStrRes = new StringBuilder();
                binStrRes.append(StringUtils.right(binStrHigh, 10));
                if (StringUtils.length(binStrLow) < 10) {
                    binStrRes.append(StringUtils.leftPad(binStrLow, 10, "0"));
                } else {
                    binStrRes.append(StringUtils.right(binStrLow, 10));
                }
                Integer resInt = Integer.parseInt(binStrRes.toString(), 2);
                //U+10000
                return resInt + 65536;
            }
        }

        return 0;
    }

}