Skip to content

Commit 42b1b56

Browse files
committed
Initial commit
0 parents  commit 42b1b56

12 files changed

+9074
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Java
2+
target
3+
4+
# IntelliJ files
5+
*.iml
6+
.idea

README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# emoji-java
2+
3+
*The missing emoji library for java.*
4+
5+
Based on the data provided by [github/gemoji project](https://github.com/github/gemoji), **emoji-java** is a lightweight java library that helps you to use Emojis in your java applications.
6+
7+
## How to get it?
8+
9+
Just add the dependency to your maven project:
10+
11+
```
12+
<dependency>
13+
<groupId>com.vdurmont<groupId>
14+
<artifactId>emoji-java<artifactId>
15+
<version>1.0</version>
16+
</dependency>
17+
```
18+
19+
You can also build the project with `mvn clean install` and add the generated jar to your buildpath.
20+
21+
## How to use it?
22+
23+
### EmojiManager
24+
25+
The `EmojiManager` provides a bunch of static methods to search throught the emojis database:
26+
27+
* `getForTag` returns all the emojis for a given tag
28+
* `getForAlias` returns the emoji for an alias
29+
* `getAll` returns all the emojis
30+
* `isEmoji` checks if a string is an emoji
31+
32+
You can also query the metadata:
33+
34+
* `getAllTags` returns the available tags
35+
36+
### Emoji model
37+
38+
An `Emoji` is a POJO (plain old java object), which provides the following methods:
39+
40+
* `getUnicode` returns the unicode representation of the emoji
41+
* `getDescription` returns the (optional) description of the emoji
42+
* `getAliases` returns a list of aliases for this emoji
43+
* `getTags` returns a list of tags for this emoji
44+
45+
### Parse a string
46+
47+
To replace all the aliases by their unicode, use `EmojiParser.parseToUnicode(myString)`.
48+
49+
For example:
50+
51+
```
52+
String str = "An :grinning:awesome :smiley:string with a few :wink:emojis!";
53+
String result = EmojiParser.parseToUnicode(myString);
54+
System.out.println(myString);
55+
// Prints:
56+
// "An 😀awesome 😃string with a few 😉emojis!"
57+
```
58+
59+
To replace all the emoji's unicodes by their aliases, use `EmojiParser.parseToAliases(myString)`.
60+
61+
For example:
62+
63+
```
64+
String str = "An 😀awesome 😃string with a few 😉emojis!";
65+
String result = EmojiParser.parseToAliases(myString);
66+
System.out.println(myString);
67+
// Prints:
68+
// "An :grinning:awesome :smiley:string with a few :wink:emojis!"
69+
```

pom.xml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.vdurmont</groupId>
6+
<artifactId>emoji-java</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>emoji-java</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.json</groupId>
20+
<artifactId>json</artifactId>
21+
<version>20140107</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.11</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* =================
3+
* EMOJI-JAVA
4+
* =================
5+
* The missing emoji library for java.
6+
*
7+
* See http://github.com/vdurmont/emoji-java
8+
*/
9+
package com.vdurmont.emoji;
10+
11+
import java.io.UnsupportedEncodingException;
12+
import java.util.List;
13+
14+
/**
15+
* This class represents an emoji.
16+
*
17+
* @author Vincent DURMONT <[email protected]>
18+
*/
19+
public class Emoji {
20+
private final String description;
21+
private final List<String> aliases;
22+
private final List<String> tags;
23+
private final byte[] bytes;
24+
25+
public Emoji(String description, List<String> aliases, List<String> tags, byte... bytes) {
26+
this.description = description;
27+
this.aliases = aliases;
28+
this.tags = tags;
29+
this.bytes = bytes;
30+
}
31+
32+
public String getDescription() {
33+
return this.description;
34+
}
35+
36+
public List<String> getAliases() {
37+
return this.aliases;
38+
}
39+
40+
public List<String> getTags() {
41+
return this.tags;
42+
}
43+
44+
public String getUnicode() {
45+
try {
46+
return new String(this.bytes, "UTF-8");
47+
} catch (UnsupportedEncodingException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* =================
3+
* EMOJI-JAVA
4+
* =================
5+
* The missing emoji library for java.
6+
*
7+
* See http://github.com/vdurmont/emoji-java
8+
*/
9+
package com.vdurmont.emoji;
10+
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.io.UnsupportedEncodingException;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.Scanner;
20+
21+
/**
22+
* Loads the emojis from a JSON database.
23+
*
24+
* @author Vincent DURMONT <[email protected]>
25+
*/
26+
public class EmojiLoader {
27+
/**
28+
* Loads a JSONArray of emojis from an InputStream, parses it and returns the associated list of {@link com.vdurmont.emoji.Emoji}s
29+
*
30+
* @param stream the stream of the JSONArray
31+
* @return the list of {@link com.vdurmont.emoji.Emoji}s
32+
* @throws IOException if an error occurs while reading the stream or parsing the JSONArray
33+
*/
34+
public static List<Emoji> loadEmojis(InputStream stream) throws IOException {
35+
JSONArray emojisJSON = new JSONArray(inputStreamToString(stream));
36+
List<Emoji> emojis = new ArrayList<Emoji>(emojisJSON.length());
37+
for (int i = 0; i < emojisJSON.length(); i++) {
38+
Emoji emoji = buildEmojiFromJSON(emojisJSON.getJSONObject(i));
39+
if (emoji != null) {
40+
emojis.add(emoji);
41+
}
42+
}
43+
return emojis;
44+
}
45+
46+
private static String inputStreamToString(InputStream stream) {
47+
Scanner s = new Scanner(stream, "UTF-8").useDelimiter("\\A");
48+
return s.hasNext() ? s.next() : "";
49+
}
50+
51+
protected static Emoji buildEmojiFromJSON(JSONObject json) throws UnsupportedEncodingException {
52+
if (!json.has("emoji")) {
53+
return null;
54+
}
55+
56+
byte[] bytes = json.getString("emoji").getBytes("UTF-8");
57+
String description = null;
58+
if (json.has("description")) {
59+
description = json.getString("description");
60+
}
61+
List<String> aliases = jsonArrayToStringList(json.getJSONArray("aliases"));
62+
List<String> tags = jsonArrayToStringList(json.getJSONArray("tags"));
63+
return new Emoji(description, aliases, tags, bytes);
64+
}
65+
66+
private static List<String> jsonArrayToStringList(JSONArray array) {
67+
List<String> strings = new ArrayList<String>(array.length());
68+
for (int i = 0; i < array.length(); i++) {
69+
strings.add(array.getString(i));
70+
}
71+
return strings;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* =================
3+
* EMOJI-JAVA
4+
* =================
5+
* The missing emoji library for java.
6+
*
7+
* See http://github.com/vdurmont/emoji-java
8+
*/
9+
package com.vdurmont.emoji;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.util.Collection;
14+
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.Set;
19+
20+
/**
21+
* Holds the loaded emojis and provides search functions.
22+
*
23+
* @author Vincent DURMONT <[email protected]>
24+
*/
25+
public class EmojiManager {
26+
private static final String PATH = "/emojis-2014-07-13.json";
27+
private static final Map<String, Emoji> EMOJIS_BY_ALIAS = new HashMap<String, Emoji>();
28+
private static final Map<String, Set<Emoji>> EMOJIS_BY_TAG = new HashMap<String, Set<Emoji>>();
29+
30+
static {
31+
try {
32+
InputStream stream = EmojiLoader.class.getResourceAsStream(PATH);
33+
List<Emoji> emojis = EmojiLoader.loadEmojis(stream);
34+
for (Emoji emoji : emojis) {
35+
for (String tag : emoji.getTags()) {
36+
if (EMOJIS_BY_TAG.get(tag) == null) {
37+
EMOJIS_BY_TAG.put(tag, new HashSet<Emoji>());
38+
}
39+
EMOJIS_BY_TAG.get(tag).add(emoji);
40+
}
41+
for (String alias : emoji.getAliases()) {
42+
EMOJIS_BY_ALIAS.put(alias, emoji);
43+
}
44+
}
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
}
49+
50+
/**
51+
* Returns all the {@link com.vdurmont.emoji.Emoji}s for a given tag.
52+
*
53+
* @param tag the tag
54+
* @return the associated {@link com.vdurmont.emoji.Emoji}s, null if the tag is unknown
55+
*/
56+
public static Set<Emoji> getForTag(String tag) {
57+
if (tag == null) {
58+
return null;
59+
}
60+
return EMOJIS_BY_TAG.get(tag);
61+
}
62+
63+
/**
64+
* Returns the {@link com.vdurmont.emoji.Emoji} for a given alias.
65+
*
66+
* @param alias the alias
67+
* @return the associated {@link com.vdurmont.emoji.Emoji}, null if the alias is unknown
68+
*/
69+
public static Emoji getForAlias(String alias) {
70+
if (alias == null) {
71+
return null;
72+
}
73+
return EMOJIS_BY_ALIAS.get(trimAlias(alias));
74+
}
75+
76+
private static String trimAlias(String alias) {
77+
String result = alias;
78+
if (result.startsWith(":")) {
79+
result = result.substring(1, result.length());
80+
}
81+
if (result.endsWith(":")) {
82+
result = result.substring(0, result.length() - 1);
83+
}
84+
return result;
85+
}
86+
87+
/**
88+
* Returns all the {@link com.vdurmont.emoji.Emoji}s
89+
*
90+
* @return all the {@link com.vdurmont.emoji.Emoji}s
91+
*/
92+
public static Collection<Emoji> getAll() {
93+
return EMOJIS_BY_ALIAS.values();
94+
}
95+
96+
/**
97+
* Tests if a given String is an emoji.
98+
*
99+
* @param string the string to test
100+
* @return true if the string is an emoji's unicode, false else
101+
*/
102+
public static boolean isEmoji(String string) {
103+
if (string != null) {
104+
for (Emoji emoji : getAll()) {
105+
if (emoji.getUnicode().equals(string)) {
106+
return true;
107+
}
108+
}
109+
}
110+
return false;
111+
}
112+
113+
/**
114+
* Returns all the tags in the database
115+
*
116+
* @return the tags
117+
*/
118+
public static Collection<String> getAllTags() {
119+
return EMOJIS_BY_TAG.keySet();
120+
}
121+
}

0 commit comments

Comments
 (0)