【问题标题】:Convert emoji to html hex with java library & Lucee使用 java 库和 Lucee 将表情符号转换为 html 十六进制
【发布时间】:2016-04-29 12:28:50
【问题描述】:

我正在尝试使用此 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);

我做错了什么?


更新

我也试过这个:

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

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

【问题讨论】:

  • try emojiUtils.init(myTitle).getHtmlHexidecimal(), getHtmlHexidecimal 不接受任何参数。
  • @Rejith R Krishnan: No matching Constructor for com.vdurmont.emoji.Emoji(string) found
  • 您需要从 Java 示例开始工作。这个例子没有初始化 - 甚至没有任何参数 execmpt 用于 getUnicode()。
  • @MarkAKruger:好的,很抱歉,但我对 java 对象一无所知。这种情况我该怎么办?
  • @Ivan - 构造函数受到保护,因此您不能在包外实例化它。如果您尝试替换,项目的略读建议您应该改用 EmojiParser 类。请尝试使用静态方法EmojiParser.parseToHtmlDecimal(string),它:..replace[s] 通过 html 表示在字符串中找到的所有表情符号的 unicode

标签: java jar coldfusion cfml lucee


【解决方案1】:

正如一些人所建议的,你没有像我写它时那样使用这个库:)

您永远不必“创建对象”。如果您想获取表情符号的十六进制字符串,您应该按照文档进行操作:

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 会更容易。

【讨论】:

  • 很高兴直接从源获得响应 :) RE: ...此行必须指向正确的文件。他们使用的是pre-compiled jar from mvnrepository,它在根文件夹中包含“emojis.json”。如果 PATH 没有改变,getResourceAsStream 会默认加载那个文件吗?
  • 是的,它应该工作。虽然不能保证:D 我强烈建议使用包管理器而不是自己导入 jar。通过这样做,您将避免一整类错误:)
  • 是的,总是有变量在起作用,所以我知道......“这取决于”:) Ivan 实际上是在自定义 servlet 中使用 jar。 jar 通过使用动态类加载器的功能加载。一些方法在 servlet 中起作用,但到目前为止还不是 EmojiParser.parseToHtmlHexadecimal。在 servlet 中,它为我返回原始字符串。虽然我怀疑这与环境有关,但我将不得不在 Eclipse 中尝试相同的方法,看看是否能找出不同之处。
  • 啊,发现问题了。没有正确加载 emjoi.json (see my comment below)。
【解决方案2】:

(对于 cmets 来说太长了)

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"));
    

进行其中一项更改后,它应该可以在 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);

【讨论】:

    猜你喜欢
    • 2020-12-15
    • 2015-02-01
    • 2017-03-27
    • 2015-09-27
    • 2017-06-28
    • 2017-06-23
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多