Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有Java实用程序来验证字符串是否为有效的HTML转义字符?_Java_Html_Escaping_Html Escape Characters - Fatal编程技术网

是否有Java实用程序来验证字符串是否为有效的HTML转义字符?

是否有Java实用程序来验证字符串是否为有效的HTML转义字符?,java,html,escaping,html-escape-characters,Java,Html,Escaping,Html Escape Characters,我需要以下格式的方法: public boolean isValidHtmlEscapeCode(String string); 用途如下: isValidHtmlEscapeCode("A") == false isValidHtmlEscapeCode("ש") == true // Valid unicode character isValidHtmlEscapeCode("ש") == true // same as 1513 but in HE

我需要以下格式的方法:

public boolean isValidHtmlEscapeCode(String string);
用途如下:

isValidHtmlEscapeCode("A") == false
isValidHtmlEscapeCode("ש") == true // Valid unicode character
isValidHtmlEscapeCode("ש") == true // same as 1513 but in HEX
isValidHtmlEscapeCode("�") == false // Invalid unicode character
我找不到任何可以做到这一点的东西——有没有任何实用工具可以做到这一点?
如果不是,有什么聪明的方法吗?

不确定这是否是一个完美的解决方案,但您可以使用Apache Commons Lang:

try {
    return StringEscapeUtils.unescapeHtml4(code).length() < code.length();
} catch (IllegalArgumentException e) {
    return false;
}
试试看{
返回StringEscapeUtils.unescapethml4(code.length()
您可能想看看Apache commons StringUtils: )

使用Unescape HTML,您可以执行以下操作:

String input = "A";
String unescaped = StringEscapeUtils.unescapeHtml(input);
boolean containsValidEscape = !input.equals(a);

尝试使用正则表达式进行匹配:

public boolean isValidHtmlEscapeCode(String string) {
    return string.matches("&;#([0-9]{1,4}|x[0-9a-fA-F]{1,4});");
}
或者,为了节省一些处理周期,可以重用正则表达式进行多次比较:

Pattern pattern = Pattern.compile("&;#([0-9]{1,4}|x[0-9a-fA-F]{1,4});");

public boolean isValidHtmlEscapeCode(String string) {
    return pattern.matches(string);
}

正则表达式的源代码可以在

中找到,这应该是您想要的方法:

public static boolean isValidHtmlEscapeCode(String string) {
String temp = "";
try {
    temp = StringEscapeUtils.unescapeHtml3(string);
} catch (IllegalArgumentException e) {
    return false;
}
return !string.equals(temp);
}
public静态布尔值isValidHtmlEscapeCode(字符串){
if(字符串==null){
返回false;
}
模式p=模式
编译(&(?:#x([0-9a-fA-F]+)|([0-9]+)|([0-9a-Za-z]+);));
匹配器m=p.Matcher(字符串);
if(m.find()){
int码点=-1;
字符串实体=null;
试一试{
如果((实体=m.group(1))!=null){
if(entity.length()>6){
返回false;
}
codePoint=Integer.parseInt(实体,16);
}如果((实体=m.group(2))!=null,则为else{
if(entity.length()>7){
返回false;
}
codePoint=Integer.parseInt(实体,10);
}如果((实体=m.group(3))!=null,则为else{
返回namedEntities.contains(实体);
}

返回0x00关于
&;
ä;
&customEntity;
?我不介意处理这些的函数-但这不是我的要求(换句话说,我对此不偏不倚),为什么您不能检查它是否以
&
开头,以
结尾,中间部分由(I)a-z,0-9(ii)组成#后跟数字(iii)#x后跟十六进制数字?@SalmanA我希望有一个更聪明的方法来做这件事-我不喜欢重新发明Wheels字符串Capeutils不能处理&#xxx;格式的任何东西。因此基于它的代码将不起作用。的确,UnescapeThml4包含NumericeEntityUnescaper,所以它应该处理它们看起来像抛出了
IllegalArgumentException当您传递无效的实体时,所以我更新了我的解决方案一点不错-但是
isValidHtmlEscapeCode(✏;)
将返回true@RonK正则表达式已被更改以更正长度限制。@RonK
𘚟;
是有效的…以十进制表示,最高达
􏿿;
是有效的。@Esailija-谢谢,所以长度验证还不够-还需要验证数字的值。@RonK以及其他内容,例如就连chrome和firefox也不一致地将
�;
视为有效实体,将其转换为
࿽;
,谢谢,这是解决我问题的最佳解决方案-我在模式的开头添加了一个
^
,并对其进行了轻微修改>$
结尾,因此对于像
hello{;world
这样的字符串,它不会返回
true
@RonK cheers,是的,我使用这个正则表达式来提取html实体,但忘了修改它^^
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;
    }
}