All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.whaleal.icefrog.extra.emoji.loader.impl.LocalEmojiDataLoader Maven / Gradle / Ivy

There is a newer version: 1.1.71
Show newest version
package com.whaleal.icefrog.extra.emoji.loader.impl;


import com.google.gson.Gson;
import com.whaleal.icefrog.extra.emoji.loader.EmojiDataLoader;
import com.whaleal.icefrog.extra.emoji.model.Emoji;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * emoji data loader
 *
 * @author xuxueli 2018-07-06 20:15:22
 */
public class LocalEmojiDataLoader extends EmojiDataLoader {

    private static final String PATH = "/xxl-tool/emoji/xxl-tool-emoji.json";

    public List loadEmojiData()  {

        InputStream stream = null;
        try {
            // json
            stream = LocalEmojiDataLoader.class.getResourceAsStream(PATH);
            String emojiJson = inputStreamToString(stream);

            // emoji data
            Gson gson = new Gson();
            List emojiArr = gson.fromJson(emojiJson, List.class);
            if (emojiArr==null || emojiArr.size()==0) {
                return null;
            }

            // parse dto
            List emojis = new ArrayList();
            for (Object emojiItem: emojiArr) {
                if (emojiItem instanceof Map){

                    Map emojiItemMap = (Map) emojiItem;

                    String unicode = String.valueOf(emojiItemMap.get("unicode"));

                    List aliases = null;
                    if (emojiItemMap.containsKey("aliases") && emojiItemMap.get("aliases") instanceof List) {
                        aliases = (List) emojiItemMap.get("aliases");
                    }
                    List tags = null;
                    if (emojiItemMap.containsKey("tags") && emojiItemMap.get("tags") instanceof List) {
                        tags = (List) emojiItemMap.get("tags");
                    }

                    boolean supports_fitzpatrick = false;
                    if (emojiItemMap.containsKey("supports_fitzpatrick")) {
                        supports_fitzpatrick = Boolean.valueOf(String.valueOf(emojiItemMap.get("supports_fitzpatrick")));
                    }

                    Emoji emojiObj = new Emoji(unicode, aliases, tags, supports_fitzpatrick);
                    emojis.add(emojiObj);

                }
            }

            return emojis;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream!=null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static String inputStreamToString(InputStream stream) throws IOException {
        StringBuilder sb = new StringBuilder();
        InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String read;
        while ((read = br.readLine()) != null) {
            sb.append(read);
        }
        br.close();
        return sb.toString();
    }

}