Java 怎么转义属性值中的XML保留字符

2019年8月2日23:18:07Java 怎么转义属性值中的XML保留字符已关闭评论 466 2182字阅读7分16秒

1.简介

Java 怎么转义属性值中的XML保留字符?——请看下文。

2.示例代码

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

 


public class Main {
    public static void main(String[] argv) throws Exception {
        CharSequence value = "tah1986.com";
        System.out.println(escAttrValue(value));
    }

    public static CharSequence escAttrValue(CharSequence value) {
        return escAttrValue(value, '"');
    }

    
    public static CharSequence escAttrValue(CharSequence value, char delim) {
        if (value == null)
            return "";
        StringBuilder ret = new StringBuilder();
        escAttrValue(ret, value, delim);
        return ret;
    }

    public static void escAttrValue(StringBuilder ret, CharSequence value,
            char delim) {
        for (int i = 0, len = value.length(); i < len; i++) {
            char c = value.charAt(i);
            switch (c) {
            case '&':
                if (isXmlEntityRef(value, i))
                    ret.append(c);
                else
                    ret.append("&");
                break;
            case '<':
                ret.append("<");
                break;
            case '\'':
                if (c == delim)
                    ret.append("'");
                else
                    ret.append(c);
                break;
            case '"':
                if (c == delim)
                    ret.append(""");
                else
                    ret.append(c);
                break;
            default:
                ret.append(c);
            }
        }
    }

    
    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';
    }
}

 
继续阅读
编程十万个怎么办