immf.EmojiUtil.java Source code

Java tutorial

Introduction

Here is the source code for immf.EmojiUtil.java

Source

/*
 * imoten - i mode.net mail tensou(forward)
 *
 * Copyright (C) 2010 shoozhoo (http://code.google.com/p/imoten/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

package immf;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class EmojiUtil {
    private static final Log log = LogFactory.getLog(EmojiUtil.class);
    private static final String URLPrefix = "https://mail.google.com/mail/e/docomo_ne_jp/";
    public static final String UnknownReplace = "[?]";
    private static Map<Character, Emoji> map;
    static {
        map = new TreeMap<Character, Emoji>();
        // 
        add(0xe63e, "", "000");
        add(0xe63f, "", "001");
        add(0xe640, "", "002");
        add(0xe641, "", "003");
        add(0xe642, "", "004");
        add(0xe643, "?", "005");
        add(0xe644, "", "006");
        add(0xe645, "?", "007");
        add(0xe646, "", "02B");
        add(0xe647, "", "02C");
        add(0xe648, "??", "02D");
        add(0xe649, "", "02E");
        add(0xe64a, "??", "02F");
        add(0xe64b, "", "030");
        add(0xe64c, "", "031");
        add(0xe64d, "?", "032");
        add(0xe64e, "", "033");
        add(0xe64f, "", "034");
        add(0xe650, "", "035");
        add(0xe651, "", "036");
        add(0xe652, "?", "7D0");
        add(0xe653, "?", "7D1");
        add(0xe654, "", "7D2");
        add(0xe655, "", "7D3");
        add(0xe656, "", "7D4");
        add(0xe657, "", "7D5");
        add(0xe658, "?", "7D6");
        add(0xe659, "?", "7D7");
        add(0xe65a, "?", "522");
        add(0xe65b, "", "7DF");
        add(0xe65c, "", "7E1");
        add(0xe65d, "", "7E2");
        add(0xe65e, "()", "7E4");
        add(0xe65f, "(RV)", "7E5");
        add(0xe660, "?", "7E6");
        add(0xe661, "", "7E8");
        add(0xe662, "", "7E9");
        add(0xe663, "", "4B0");
        add(0xe664, "", "4B2");
        add(0xe665, "", "4B3");
        add(0xe666, "", "4B4");
        add(0xe667, "", "4B5");
        add(0xe668, "ATM", "4B6");
        add(0xe669, "", "4B7");
        add(0xe66a, "", "4B9");
        add(0xe66b, "", "7F5");
        add(0xe66c, "?", "7F6");
        add(0xe66d, "?", "7F7");
        add(0xe66e, "", "506");
        add(0xe66f, "", "980");
        add(0xe670, "", "981");
        add(0xe671, "?", "982");
        add(0xe672, "", "983");
        add(0xe673, "", "960");
        add(0xe674, "", "4D6");
        add(0xe675, "", "53E");
        add(0xe676, "", "800");
        add(0xe677, "", "801");
        add(0xe678, "??", "AF0");
        add(0xe679, "?", "7FC");
        add(0xe67a, "", "803");
        add(0xe67b, "", "804");
        add(0xe67c, "", "805");
        add(0xe67d, "", "806");
        add(0xe67e, "?", "807");
        add(0xe67f, "", "B1E");
        add(0xe680, "?", "B1F");
        add(0xe681, "", "4EF");
        add(0xe682, "??", "4F0");
        add(0xe683, "", "545");
        add(0xe684, "", "50F");
        add(0xe685, "", "510");
        add(0xe686, "?", "511");
        add(0xe687, "", "523");
        add(0xe688, "?", "525");
        add(0xe689, "", "527");
        add(0xe68a, "TV", "81C");
        add(0xe68b, "", "80A");
        add(0xe68c, "CD", "81D");
        add(0xe68d, "?", "B1A");
        add(0xe68e, "", "B1B");
        add(0xe68f, "", "B1C");
        add(0xe690, "", "B1D");
        add(0xe691, "", "190");
        add(0xe692, "", "191");
        add(0xe693, "()", "B93");
        add(0xe694, "(?)", "B94");
        add(0xe695, "()", "B95");
        add(0xe696, "??", "AF1");
        add(0xe697, "?", "AF2");
        add(0xe698, "", "1DB");
        add(0xe699, "???", "4CC");
        add(0xe69a, "?", "4CD");
        add(0xe69b, "?", "B20");
        add(0xe69c, "", "B65");
        add(0xe69d, "?", "012");
        add(0xe69e, "?", "013");
        add(0xe69f, "", "014");
        add(0xe6a0, "", "015");
        add(0xe6a1, "", "1D8");
        add(0xe6a2, "", "1B8");
        add(0xe6a3, "", "7EA");
        add(0xe6a4, "", "512");
        add(0xe6a5, "?", "AF3");
        add(0xe6ce, "phone to", "526");
        add(0xe6cf, "mail to", "52B");
        add(0xe6d0, "fax to", "528");
        add(0xe6d1, "i", "E10");
        add(0xe6d2, "i(??)", "E11");
        add(0xe6d3, "", "529");
        add(0xe6d4, "??", "E12");
        add(0xe6d5, "?", "E13");
        add(0xe6d6, "", "4E2");
        add(0xe6d7, "", "B21");
        add(0xe6d8, "ID", "B81");
        add(0xe6d9, "", "B82");
        add(0xe6da, "", "B83");
        add(0xe6db, "", "B84");
        add(0xe6dc, "?(?)", "B85");
        add(0xe6dd, "New", "B36");
        add(0xe6de, "?", "B22");
        add(0xe6df, "", "82B");
        add(0xe6e0, "", "82C");
        add(0xe6e1, "?Q", "82D");
        add(0xe6e2, "1", "82E");
        add(0xe6e3, "2", "82F");
        add(0xe6e4, "3", "830");
        add(0xe6e5, "4", "831");
        add(0xe6e6, "5", "832");
        add(0xe6e7, "6", "833");
        add(0xe6e8, "7", "834");
        add(0xe6e9, "8", "835");
        add(0xe6ea, "9", "836");
        add(0xe6eb, "0", "837");
        add(0xe70b, "", "B27");
        add(0xe6ec, "?", "B0C");
        add(0xe6ed, "??", "B0D");
        add(0xe6ee, "?", "B0E");
        add(0xe6ef, "???(?)", "B0F");
        add(0xe6f0, "??(??)", "330");
        add(0xe6f1, "??(??)", "320");
        add(0xe6f2, "???(???)", "323");
        add(0xe6f3, "??(??)", "33F");
        add(0xe6f4, "??", "324");
        add(0xe6f5, "(????)", "AF4");
        add(0xe6f6, "", "813");
        add(0xe6f7, "??()", "7FA");
        add(0xe6f8, "????", "B55");
        add(0xe6f9, "", "823");
        add(0xe6fa, "????(??)", "B60");
        add(0xe6fb, "????", "B56");
        add(0xe6fc, "??()", "B57");
        add(0xe6fd, "?", "B96");
        add(0xe6fe, "", "B58");
        add(0xe6ff, "", "814");
        add(0xe700, "?(????)", "AF5");
        add(0xe701, "?(?)", "B59");
        add(0xe702, "exclamation", "B04");
        add(0xe703, "exclamation&question", "B05");
        add(0xe704, "exclamationx2", "B06");
        add(0xe705, "??(?)", "B5A");
        add(0xe706, "????(?)", "B5B");
        add(0xe707, "??()", "B5C");
        add(0xe708, "(???)", "B5D");
        add(0xe709, "(?1)", "B07");
        add(0xe70a, "(?2)", "B08");
        add(0xe6ac, "?", "808");
        add(0xe6ad, "????", "4F1");
        add(0xe6ae, "", "536");
        add(0xe6b1, "", "19A");
        add(0xe6b2, "??", "537");
        add(0xe6b3, "", "008");
        add(0xe6b7, "soon", "018");
        add(0xe6b8, "on", "019");
        add(0xe6b9, "end", "01A");
        add(0xe6ba, "", "02A");
        // 
        add(0xe70c, "i", "E14");
        add(0xe70d, "i(??)", "E15");
        add(0xe70e, "T()", "4CF");
        add(0xe70f, "???", "4DC");
        add(0xe710, "", "195");
        add(0xe711, "", "4D0");
        add(0xe712, "", "7D8");
        add(0xe713, "?", "4F2");
        add(0xe714, "", "4F3");
        add(0xe715, "", "4DD");
        add(0xe716, "", "538");
        add(0xe717, "", "824");
        add(0xe718, "?", "4C9");
        add(0xe719, "", "539");
        add(0xe71a, "", "4D1");
        add(0xe71b, "", "825");
        add(0xe71c, "", "01B");
        add(0xe71d, "", "7EB");
        add(0xe71e, "??", "984");
        add(0xe71f, "", "01D");
        add(0xe720, "??", "340");
        add(0xe721, "?????", "33E");
        add(0xe722, "", "331");
        add(0xe723, "2", "344");
        add(0xe724, "????????", "33D");
        add(0xe725, "????", "326");
        add(0xe726, "??", "327");
        add(0xe727, "?OK", "B97");
        add(0xe728, "???", "329");
        add(0xe729, "", "347");
        add(0xe72a, "???", "332");
        add(0xe72b, "??", "33C");
        add(0xe72c, "2", "343");
        add(0xe72d, "??", "33A");
        add(0xe72e, "", "339");
        add(0xe72f, "NG", "B28");
        add(0xe730, "", "53A");
        add(0xe731, "", "B29");
        add(0xe732, "", "B2A");
        add(0xe733, "", "7D9");
        add(0xe734, "", "B2B");
        add(0xe735, "", "B2C");
        add(0xe736, "", "B2D");
        add(0xe737, "?", "B23");
        add(0xe738, "?", "B2E");
        add(0xe739, "", "B2F");
        add(0xe73a, "?", "B30");
        add(0xe73b, "", "B31");
        add(0xe73c, "??", "AF6");
        add(0xe73d, "?", "AF7");
        add(0xe73e, "", "4BA");
        add(0xe73f, "", "038");
        add(0xe740, "", "4C3");
        add(0xe741, "?", "03C");
        add(0xe742, "????", "04F");
        add(0xe743, "?", "03D");
        add(0xe744, "?", "050");
        add(0xe745, "?", "051");
        add(0xe746, "", "03E");
        add(0xe747, "??", "03F");
        add(0xe748, "", "040");
        add(0xe749, "???", "961");
        add(0xe74a, "", "962");
        add(0xe74b, "????(?????)", "985");
        add(0xe74c, "??", "963");
        add(0xe74d, "", "964");
        add(0xe74e, "???", "1B9");
        add(0xe74f, "??", "1BA");
        add(0xe750, "", "1BC");
        add(0xe751, "", "1BD");
        add(0xe752, "???!", "32B");
        add(0xe753, "", "333");
        add(0xe754, "", "1BE");
        add(0xe755, "", "1BF");
        add(0xe756, "", "986");
        add(0xe757, "????", "341");
    }

    private static void add(int unicode, String label, String googleImage) {
        char c = (char) unicode;
        map.put(Character.valueOf(c), new Emoji(c, label, googleImage));
    }

    /**
     * ????[]????????
     * @param s
     * @return
     */
    public static String replaceToLabel(String s) {
        StringBuilder buf = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (!isEmoji(c)) {
                buf.append(c);
                continue;
            }
            Emoji e = map.get(c);
            if (e == null) {
                buf.append(UnknownReplace);
            } else {
                buf.append("[" + e.getLabel() + "]");
            }
        }
        return buf.toString();
    }

    /**
     * ????<img src="?URL">?????
     * @param s
     * @return
     */
    public static String replaceToWebLink(String s, String va, String px) {
        StringBuilder buf = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (!isEmoji(c)) {
                buf.append(c);
                continue;
            }
            Emoji e = map.get(c);
            if (e == null) {
                buf.append(UnknownReplace);
            } else {
                String wh = "";
                if (px != null) {
                    wh = " width: " + px + "; height: " + px + ";";
                }
                buf.append("<img src=\"" + URLPrefix + e.getgoogleImage()
                        + "\" style=\"margin: 0pt 0.2ex; vertical-align: " + va + ";" + wh + "\">");
            }
        }
        return buf.toString();
    }

    /**
     * ???URL
     * @param c
     * @return
     */
    public static URL emojiToImageUrl(char c) {
        Emoji e = map.get(c);
        if (e == null) {
            return null;
        }
        try {
            return new URL(URLPrefix + e.getgoogleImage());
        } catch (Exception ex) {
            log.warn(ex);
            return null;
        }
    }

    /**
     * ?????
     * @param c
     * @return
     */
    public static boolean isEmoji(char c) {
        if (Character.UnicodeBlock.of(c) == Character.UnicodeBlock.PRIVATE_USE_AREA) {
            return true;
        } else {
            return false;
        }
    }

    // ??
    public static void main(String[] args) {
        BufferedWriter br = null;
        try {
            br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./emoji.html"), "UTF-8"));

            br.write("<html>" + "<head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
                    + "</head>" + "<body>" + ServerMain.Version + "<br><br>" + "<table>");
            br.write("<th>Unicode</th><th>?</th><th></th>");
            for (Emoji e : map.values()) {
                br.write("<tr>\n");
                br.write("    <td>" + String.format("0x%x", (int) e.getC()) + "</td>" + "<td><img src='"
                        + emojiToImageUrl(e.getC()) + "'></td>" + "<td>" + e.getLabel() + "</td>\n");
                br.write("</tr>\n");
            }
            br.write("</table></body></html>");
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}