使用java库&amp ;;将表情符号转换为html hex Lucee

时间:2016-04-29 12:28:51

标签: java jar coldfusion cfml lucee

我尝试使用此java库将表情符号转换为html十六进制:https://github.com/vdurmont/emoji-java
(取自此处:http://mvnrepository.com/artifact/com.vdurmont/emoji-java/3.1.0

emojiUtils = createObject( "java", "com.vdurmont.emoji.Emoji", "/componenti/jar/emoji-java-3.1.0.jar" );

var myTitle = privateFunctionGetTitle(); // for example

var cleanTitle = emojiUtils.getHtmlHexidecimal(myTitle);

但我收到此错误: No matching Method for getHtmlHexidecimal(string) found for com.vdurmont.emoji.Emoji

我尝试(用于调试) writeDump(emojiUtils); emojiUtils debug

我做错了什么?

更新

我也试过这个:

EmojiParser = createObject( "java", "com.vdurmont.emoji.EmojiParser", "/componenti/jar/emoji-java-3.1.0.jar" ); 

writeDump(EmojiParser.parseToHtmlDecimal(titoloPagina)); 

但我明白了:

lucee.runtime.exp.NativeException: Could not initialize class 
    com.vdurmont.emoji.EmojiManager
...
Caused by: java.lang.NoClassDefFoundError: 
    Could not initialize class com.vdurmont.emoji.EmojiManager 
... 80 more

StackTrace https://gist.githubusercontent.com/ivanionut/8f4e6e356c0b2c8474d21c52d796df3a/raw/5b23ba3ff5f7c930381a8109ea43e29042cc7813/StackTrace

2 个答案:

答案 0 :(得分:2)

正如有些人建议的那样,你在我写这篇文章的过程中并没有像我计划的那样使用图书馆:)

你永远不应该"创建一个对象"。如果要获取表情符号的十六进制字符串,则应遵循文档并执行:

Emoji wink = EmojiManager.getForAlias("wink");
String hexa = wink.getHexidecimal(); // Yes, "hexidecimal"... It's a typo that is fixed if you update to 3.1.1

如果要将字符串中的所有unicode表情符号更改为十六进制表示,则应执行以下操作:

String str = "An awesome string with a few emojis!";
String resultHexadecimal = EmojiParser.parseToHtmlHexadecimal(str);
System.out.println(resultHexadecimal);
// Prints:
// "An 😀awesome 😃string with a few 😉emojis!"

关于"整合"一方面,您必须自己解决依赖关系和路径:

如果您使用了包管理器(Maven,Gradle ..),那么IMO会更容易。

答案 1 :(得分:0)

(评论太长)

Vicent's answer的一个补充。虽然上面的示例在Eclipse中运行良好,但奇怪的是它对我使用CF11或Lucee 4.5不起作用。原来我的JVM使用的是默认编码Cp1252。因此,当EmojiLoader.java loaded the "emojis.json" file stream on line 52时,内容未被正确解释。

要解决此问题,您可以:

  1. 将JVM默认编码更改为UTF-8,即-Dfile.encoding=UTF-8 ..或
  2. 更改EmojiLoader.java源以指定UTF-8编码并重建jar:

    // In v3.1.0, change line #52 from:
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    
    // ... to this instead
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    
  3. 进行其中一项更改后,CF / Lucee应该可以正常工作。注意,如上所述,库的v3.1.0依赖于org.json v20140107。见http://mvnrepository.com/artifact/org.json/json/20140107

    Lucee示例:

    jarPaths = ["C:\path\your-emoji-jar.jar", "C:\path\json-20140107.jar"];
    EmojiParser = createObject( "java", "com.vdurmont.emoji.EmojiParser", jarPaths); 
    str = "An awesome string with a few emojis!";
    result = EmojiParser.parseToHtmlHexadecimal(str);
    writeDump(result);