Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * ElementNode
 * by Keith Gaughan <kmgaughan@eircom.net>
 *
 * Copyright (c) Keith Gaughan, 2006. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v1.0, which accompanies this
 * distribution and is available at http://www.opensource.org/licenses/cpl1.0.php
 */

public class Main {
    /** Character flags. */
    private static final byte[] CHARS = new byte[1 << 16];
    /** Name start character mask. */
    private static final int MASK_NAME_START = 0x04;
    /** Name character mask. */
    private static final int MASK_NAME = 0x08;

    /**
     * Check to see if a string is a valid Name according to [5] in the
     * XML 1.0 Recommendation.
     *
     * @param  name  String to check.
     *
     * @return true if name is a valid Name.
     */
    public static boolean isValidName(final String name) {
        if (name == null || name.length() == 0 || !isNameStart(name.charAt(0))) {
            return false;
        }
        for (int i = 1; i < name.length(); ++i) {
            if (!isName(name.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns true if the specified character is a valid name start
     * character as defined by production [5] in the XML 1.0 specification.
     *
     * @param  c  The character to check.
     */
    private static boolean isNameStart(final int ch) {
        return ch < 0x10000 && (CHARS[ch] & MASK_NAME_START) != 0;
    }

    /**
     * Returns true if the specified character is a valid name character as
     * defined by production [4] in the XML 1.0 specification.
     *
     * @param  ch  The character to check.
     */
    private static boolean isName(final int ch) {
        return ch < 0x10000 && (CHARS[ch] & MASK_NAME) != 0;
    }
}