Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2013-2014 Josef Hardi <josef.hardi@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Determines if a character sequence is a QName.
     * <p>
     * A QName is either:
     * <ul>
     * <li>an NCName (LocalName), or</li>
     * <li>an NCName followed by a colon and by another NCName
     * (PrefixName:LocalName)</li>
     * </ul>
     *
     * Source: http://www.w3.org/TR/xml-names/#NT-QName
     *
     * @param s
     *           The character sequence to test.
     * @return Returns <code>true</code> if the character sequence
     *         <code>cs</code> is a QName, or <code>false</code> otherwise.
     */
    public static boolean isQName(CharSequence s) {
        if (isEmpty(s)) {
            return false;
        }
        boolean foundColon = false;
        boolean inNCName = false;
        for (int i = 0; i < s.length();) {
            int c = Character.codePointAt(s, i);
            if (c == ':') { //$NON-NLS-1$
                if (foundColon) {
                    return false;
                }
                foundColon = true;
                if (!inNCName) {
                    return false;
                }
                inNCName = false;
            } else {
                if (!inNCName) {
                    if (!isXmlNameStartChar(c)) {
                        return false;
                    }
                    inNCName = true;
                } else {
                    if (!isXmlNameChar(c)) {
                        return false;
                    }
                }
            }
            i += Character.charCount(c);
        }
        return true;
    }

    /**
     * Determines if a character sequence is <code>null</code> or empty.
     * 
     * @param s
     *           The character sequence.
     * @return Returns <code>true</code> if the character sequence is
     *         <code>null</code> or empty, or <code>false</code> otherwise.
     */
    private static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * Determines if the input character <code>c</code> is an XML name start
     * character.
     *
     * Source: http://www.w3.org/TR/xml/#NT-NameStartChar
     * 
     * @param c
     *           the input character value to test (according to UTF-8 or UTF-16)
     * @return Returns <code>true</code> if the input character is a name start
     *         character, or <code>false</code> otherwise.
     */
    public static boolean isXmlNameStartChar(int c) {
        return c == ':' //$NON-NLS-1$
                || (c >= 'A' && c <= 'Z') //$NON-NLS-1$ //$NON-NLS-2$
                || c == '_' //$NON-NLS-1$
                || (c >= 'a' && c <= 'z') //$NON-NLS-1$ //$NON-NLS-2$
                || (c >= 0xC0 && c <= 0xD6) || (c >= 0xD8 && c <= 0xF6) || (c >= 0xF8 && c <= 0x2FF)
                || (c >= 0x370 && c <= 0x37D) || (c >= 0x37F && c <= 0x1FFF) || (c >= 0x200C && c <= 0x200D)
                || (c >= 0x2070 && c <= 0x218F) || (c >= 0x2C00 && c <= 0x2FEF) || (c >= 0x3001 && c <= 0xD7FF)
                || (c >= 0xF900 && c <= 0xFDCF) || (c >= 0xFDF0 && c <= 0xFFFD) || (c >= 0x10000 && c <= 0xEFFFF);
    }

    /**
     * Determines if the input character <code>c</code> is an XML name character.
     * 
     * Source: http://www.w3.org/TR/xml/#NT-NameChar
     *
     * @param c
     *           the input character value to test (according to UTF-8 or UTF-16)
     * @return Returns <code>true</code> if the input character is a name
     *         character, or <code>false</code> otherwise.
     */
    public static boolean isXmlNameChar(int c) {
        return isXmlNameStartChar(c) || c == '-' //$NON-NLS-1$
                || c == '.' //$NON-NLS-1$
                || c >= '0' && c <= '9' //$NON-NLS-1$ //$NON-NLS-2$
                || c == 0xB7 || c >= 0x0300 && c <= 0x036F || c >= 0x203F && c <= 0x2040;
    }
}