Ron*_*onK 5 html java escaping html-escape-characters
我想要一个以下格式的方法:
public boolean isValidHtmlEscapeCode(String string);
Run Code Online (Sandbox Code Playgroud)
用法是:
isValidHtmlEscapeCode("A") == false
isValidHtmlEscapeCode("ש") == true // Valid unicode character
isValidHtmlEscapeCode("ש") == true // same as 1513 but in HEX
isValidHtmlEscapeCode("�") == false // Invalid unicode character
Run Code Online (Sandbox Code Playgroud)
我无法找到任何可以做到这一点的东西 - 是否有任何实用程序可以做到这一点?如果没有,有什么聪明的方法吗?
public static boolean isValidHtmlEscapeCode(String string) {
if (string == null) {
return false;
}
Pattern p = Pattern
.compile("&(?:#x([0-9a-fA-F]+)|#([0-9]+)|([0-9A-Za-z]+));");
Matcher m = p.matcher(string);
if (m.find()) {
int codePoint = -1;
String entity = null;
try {
if ((entity = m.group(1)) != null) {
if (entity.length() > 6) {
return false;
}
codePoint = Integer.parseInt(entity, 16);
} else if ((entity = m.group(2)) != null) {
if (entity.length() > 7) {
return false;
}
codePoint = Integer.parseInt(entity, 10);
} else if ((entity = m.group(3)) != null) {
return namedEntities.contains(entity);
}
return 0x00 <= codePoint && codePoint < 0xd800
|| 0xdfff < codePoint && codePoint <= 0x10FFFF;
} catch (NumberFormatException e) {
return false;
}
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一组命名实体http://pastebin.com/XzzMYDjF