当前最新版3.9.3已经可以支持Emoji
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在为领航信息开发 eMessage 支持的时候,我们曾使用著名的开源 XMPP 服务器软件 Openfire。但在使用中遇到了几个问题,并通过修改源代码将这些问题解决掉了。接下来的几篇文章,我会介绍一下这些问题并讲述是如何解决掉的。
先介绍一下背景。XMPP 是一个开放的即时通讯协议,非常不错,有很多开源软件实现了 XMPP 协议,Openfire 算是实现得比较全的,而且安装配置比较容易。其他比较流行的开源 XMPP 服务器还有 Tigase 和 ejabberd。我们现在已经切换到了 ejabberd 上,毕竟 WhatsApp 最初也使用的是 ejabberd 嘛,呵呵。读者如果有兴趣,我也可以跟大家讲讲这几个 XMPP 服务器的区别和潜在问题。
问题描述
解决方案
- /**
- * Makes sure that each individual character is a valid XML character.
- *
- * Note that when MXParser is being modified to handle multibyte chars correctly, this method needs to change (as
- * then, there are more codepoints to check).
- */
- @Override
- protected char more() throws IOException, XmlPullParserException {
- final char codePoint = super.more(); // note - this does NOT return a codepoint now, but simply a (single byte) char
- er!
- if ((codePoint == 0x0) || // 0x0 is not allowed, but flash clients insist on sending this as the very first
- racter of a stream. We should stop allowing this codepoint after the first byte has been parsed.
- (codePoint == 0x9) ||
- (codePoint == 0xA) ||
- (codePoint == 0xD) ||
- ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
- ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))) {
- return codePoint;
- throw new XmlPullParserException("Illegal XML character: " + Integer.parseInt(codePoint+"", 16));
- }
先看看这个函数的作用。如注释所言,这个函数用来判定特定字符是否是一个合法的 XML 字符。XML 一般要求按照 UTF8 编码的方式存储和传输字符,在程序处理时,会直接转换成 UNICODE 的 UCS 形式,这样便于程序做处理。
- The following byte sequences are used to represent a character. The sequence to be used depends on the UCS code
- number of the character:
- 0x00000000 - 0x0000007F:
- 0xxxxxxx
- 0x00000080 - 0x000007FF:
- 110xxxxx 10xxxxxx
- 0x00000800 - 0x0000FFFF:
- 1110xxxx 10xxxxxx 10xxxxxx
- 0x00010000 - 0x001FFFFF:
- 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- 0x00200000 - 0x03FFFFFF:
- 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
- 0x04000000 - 0x7FFFFFFF:
- 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
从上面的对应关系就可以知悉,UNICODE 可能的码位(code point 或者 code number)最大可以到 0x7FFFFFFF!而 Openfire 判断是否为合法 XML 字符的范围只到 0xFFFD!显然,不能显示正确处理 Emoji 字符的原因是 Openfire 将用来表示 Emoji 字符的那些码位范围给当成非法 XML 字符了。一种比较简单粗暴的修改办法是:
- /**
- * Makes sure that each individual character is a valid XML character.
- *
- * Note that when MXParser is being modified to handle multibyte chars correctly, this method needs to change (as
- * then, there are more codepoints to check).
- */
- @Override
- protected char more() throws IOException, XmlPullParserException {
- final char codePoint = super.more(); // note - this does NOT return a codepoint now, but simply a (single byte) char
- r!
- if ((codePoint == 0x0) || // 0x0 is not allowed, but flash clients insist on sending this as the very first
- acter of a stream. We should stop allowing this codepoint after the first byte has been parsed.
- (codePoint == 0x9) ||
- (codePoint == 0xA) ||
- (codePoint == 0xD) ||
- ((codePoint >= 0x20) && (codePoint <= 0xFFFD)) ||
- ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
- return codePoint;
- }
- throw new XmlPullParserException("Illegal XML character: " + Integer.parseInt(codePoint+"", 16));
- }
但马上有高手指出,这个方法太粗暴了,可能带来一些安全隐患(参见:http://community.igniterealtime.org/thread/48846),所以更加正确的方法是:
- @Override
- protected char more() throws IOException, XmlPullParserException {
- final char codePoint = super.more(); // note - this does NOT return a codepoint now, but simply a (double byte) character!
- boolean validCodepoint = false;
- boolean isLowSurrogate = Character.isLowSurrogate(codePoint);
- if ((codePoint == 0x0)|| // 0x0 is not allowed, but flash clients insist on sending this as the very first character of a stream. We should stop allowing this codepoint after the first byte has been parsed.
- (codePoint == 0x9) ||
- (codePoint == 0xA) ||
- (codePoint == 0xD)||
- ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
- ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))) {
- validCodepoint = true;
- }
- else if (highSurrogateSeen) {
- if (isLowSurrogate) {
- validCodepoint = true;
- } else {
- throw new XmlPullParserException(
- "High surrogate followed by non low surrogate '0x"
- + String.format("%x", (int) codePoint) + "'");
- }
- }
- else if (isLowSurrogate) {
- throw new XmlPullParserException("Low surrogate '0x "+ String.format("%x", (int) codePoint)+ " without preceeding high surrogate");
- }
- else if (Character.isHighSurrogate(codePoint)) {
- highSurrogateSeen = true;// Return here so that highSurrogateSeen is not reset
- return codePoint;
- }
- // Always reset high surrogate seen
- highSurrogateSeen = false;
- if (validCodepoint)
- return codePoint;
- throw new XmlPullParserException("Illegal XML character '0x"+ String.format("%x", (int) codePoint) + "'");
- }