Java 怎么实现如果“start”处开始的子字符串是XML实体引用,则返回true

2019年8月9日22:18:08Java 怎么实现如果“start”处开始的子字符串是XML实体引用,则返回true已关闭评论 372 1565字阅读5分13秒

1.简介

Java 怎么实现如果“start”处开始的子字符串是XML实体引用,则返回true?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 


public class Main {
    public static void main(String[] argv) throws Exception {
        CharSequence str = "tah1986.com";
        int start = 2;
        System.out.println(isXmlEntityRef(str, start));
    }

    
    public static boolean isXmlEntityRef(CharSequence str, int start) {
        int len = str.length();
        if (start >= len || str.charAt(start) != '&')
            return false;
        if (++start >= len)
            return false;
        if (str.charAt(start) == '#')
            return isXmlCharRefPart(str, start + 1);
        int i = start;
        if (!isNameStart(str.charAt(i)))
            return false;
        for (++i; i < len; ++i) {
            if (!isName(str.charAt(i)))
                break;
        }
        return (i > start && i < len && str.charAt(i) == ';');
    }

    
    public static boolean isXmlCharRefPart(CharSequence str, int start) {
        int len = str.length();
        if (start >= len)
            return false;
        char c;
        if (str.charAt(start) == 'x') {
            
            ++start;
            int i = start;
            for (; i < len; ++i) {
                c = str.charAt(i);
                if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
                        || (c >= 'A' && c <= 'F'))
                    continue;
                break;
            }
            return (i > start && i < len && str.charAt(i) == ';');
        }
        
        int i = start;
        for (; i < len; ++i) {
            c = str.charAt(i);
            if (c < '0' || c > '9')
                break;
        }
        return (i > start && i < len && str.charAt(i) == ';');
    }

    public static boolean isNameStart(int c) {
        return c == ':' || c >= 'A' && c <= 'Z' || c == '_' || c >= 'a'
                && c <= 'z' || c >= '\u00C0' && c <= '\u00D6'
                || c >= '\u00D8' && c <= '\u00F6' || c >= '\u00F8'
                && c <= '\u02FF' || c >= '\u0370' && c <= '\u037D'
                || c >= '\u037F' && c <= '\u1FFF' || c >= '\u200C'
                && c <= '\u200D' || c >= '\u2070' && c <= '\u218F'
                || c >= '\u2C00' && c <= '\u2FEF' || c >= '\u3001'
                && c <= '\uD7FF' || c >= '\uF900' && c <= '\uFDCF'
                || c >= '\uFDF0' && c <= '\uFFFD'
        
        ;
    }

    public static boolean isName(int c) {
        return isNameStart(c) || c == '-' || c == '.' || c >= '0'
                && c <= '9' || c == '\u00B7' || c >= '\u0300'
                && c <= '\u036F' || c >= '\u203F' && c <= '\u2040';
    }
}

 
继续阅读