📄 emoticon.java
字号:
package com.afuer.chat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Handles the emoticon.
*/
public class EmotIcon {
private static Log log = LogFactory.getLog(EmotIcon.class);
private static String configFile;
private static Map emotMap = new TreeMap();
private static Map privateEmotMap = new TreeMap();
private static Map userEmotMap = new TreeMap();
static void init(String configFile) {
EmotIcon.configFile = configFile;
reInit();
}
static synchronized void reInit() {
InputStream is = null;
try {
emotMap = new TreeMap();
privateEmotMap = new TreeMap();
userEmotMap = new TreeMap();
Properties prop = new Properties();
is = new FileInputStream(configFile);
prop.load(is);
for (Iterator it = prop.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String escapedKey = "";
for (int i = 0; i < key.length(); i++) {
if (Character.isLetter(key.charAt(i)) || Character.isDigit(key.charAt(i))) {
escapedKey = escapedKey + key.charAt(i);
}
else {
escapedKey = escapedKey + "\\" + key.charAt(i);
}
}
String value = prop.getProperty(key);
String[] array = value.split(",");
if (key.startsWith("?")) {
escapedKey = escapedKey.substring(2);
privateEmotMap.put(escapedKey, array);
}
else if (key.startsWith("!")) {
escapedKey = escapedKey.substring(2);
userEmotMap.put(escapedKey, array);
}
else {
emotMap.put(escapedKey, array);
}
}
validate();
}
catch (IOException ex) {
log.error(ex, ex);
}
finally {
if (is != null) {
try {
is.close();
}
catch (IOException ex) {
log.warn("Exception when closing input stream", ex);
}
}
}
}
static String process(String t) {
t = processMap(emotMap, t);
t = processMap(privateEmotMap, t);
return t;
}
static String processUserEmot(String t) {
t = t.trim();
String oldt = t;
t = t.replaceAll("\\ |\\_|\\.|\\-", "");
String t2 = t;
for (Iterator it = userEmotMap.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String[] array = (String[]) userEmotMap.get(key);
String value = array[0];
if (array.length > 1) {
value = array[1];
}
if (value != null && value.length() > 0) {
if (key.equalsIgnoreCase(t)) {
t = "<img src='" + value + "' border='0' />";
break;
}
}
}
// resume to original string if nothing is matched
if (t.equalsIgnoreCase(t2)) {
t = oldt;
}
return t;
}
private static String processMap(Map map, String s) {
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
String[] array = (String[]) map.get(key);
String value = array[0];
if (array.length > 1) {
value = array[1];
}
if (value != null && value.length() > 0) {
s = s.replaceAll(key, "<img src='" + value + "' border='0' />");
}
}
return s;
}
public static Map getEmotMap() {
return emotMap;
}
private static void validate() {
validate(emotMap);
validate(privateEmotMap);
}
private static void validate(Map map) {
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
for (Iterator it2 = emotMap.keySet().iterator(); it2.hasNext();) {
String key2 = (String) it2.next();
if (!key.equals(key2)) {
if (!key.replaceAll(key2, "").equals(key)) {
log.warn("The emoticon " + key + " is conflicted with " + key2);
}
}
}
for (Iterator it2 = privateEmotMap.keySet().iterator(); it2.hasNext();) {
String key2 = (String) it2.next();
if (!key.equals(key2)) {
if (!key.replaceAll(key2, "").equals(key)) {
log.warn("The emoticon " + key + " is conflicted with " + key2);
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -