is Xml Char Reference Part - Java XML

Java examples for XML:XML Encoding

Description

is Xml Char Reference Part

Demo Code

/*/*w w w .j  a  va  2  s . c  o m*/
 * Copyright (c) 2004, simontsui, Chris Leung. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 */
//package com.java2s;

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

    /**
     * NOTE: No well-formness check for the referenced char.
     *
     * @param start Index to char after "&#".
     */
    public static boolean isXmlCharRefPart(CharSequence str, int start) {
        int len = str.length();
        if (start >= len)
            return false;
        char c;
        if (str.charAt(start) == 'x') {
            // &#xhex;
            ++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) == ';');
        }
        // &#dec;
        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) == ';');
    }
}

Related Tutorials