Returns true if the given character is a tag name character by the XML standard, otherwise false. - Java XML

Java examples for XML:XML String

Description

Returns true if the given character is a tag name character by the XML standard, otherwise false.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        char c = 'a';
        System.out.println(isNameCharacter(c));
    }//  ww w . j av a 2  s  .c o  m

    /**
     * Returns <code>true</code> if the given character is a tag name character
     * by the XML standard, otherwise <code>false</code>.
     */
    public static boolean isNameCharacter(char c) {
        if (isNameStartCharacter(c)) {
            return true;
        }
        if (c == '-' || c == '.' || c == 0xB7) {
            return true;
        }
        if (intervalContains('0', '9', c)
                || intervalContains(0x0300, 0x036F, c)
                || intervalContains(0x203F, 0x2040, c)) {
            return true;
        }
        return false;
    }

    /**
     * Returns <code>true</code> if the given character is a tag name start
     * character by the XML standard, otherwise <code>false</code>.
     */
    public static boolean isNameStartCharacter(char c) {
        if (c == ':' || c == '_') {
            return true;
        }
        if (intervalContains('a', 'z', c) || intervalContains('A', 'Z', c)) {
            return true;
        }
        if (intervalContains(0x00C0, 0x00D6, c)
                || intervalContains(0x00D8, 0x00F6, c)) {
            return true;
        }
        if (intervalContains(0x00F8, 0x02FF, c)
                || intervalContains(0x0370, 0x037D, c)) {
            return true;
        }
        if (intervalContains(0x037F, 0x1FFF, c)
                || intervalContains(0x200C, 0x200D, c)) {
            return true;
        }
        if (intervalContains(0x2070, 0x218F, c)
                || intervalContains(0x2C00, 0x2FEF, c)) {
            return true;
        }
        if (intervalContains(0x3001, 0xD7FF, c)
                || intervalContains(0xF900, 0xFDCF, c)) {
            return true;
        }
        if (intervalContains(0xFDF0, 0xFFFD, c)) {
            return true;
        }
        return false;
    }

    private static boolean intervalContains(int start, int end, int search) {
        assert start <= end;
        return start <= search && end >= search;
    }
}

Related Tutorials