Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * 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
 *
 * Contributors:
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

public class Main {
    /**
     * Make sure an element/attribute name is a valid NCname. Note that the name is assumed non-qualified, i.e. no
     * namespaces (hence the Non Colon Name).
     * 
     * @param name Proposed non-qualified name for an element/attribute.
     * @return Same string, with any invalid characters replaced by the underscore '_' character.
     */
    public static String makeValidNCName(String name) {
        if (name == null || name.length() <= 0)
            throw new IllegalArgumentException("Invalid NCName: null or empty not allowed!");
        if (name.equalsIgnoreCase("XML"))
            throw new IllegalArgumentException("Invalid NCName: 'XML' name is reserved!");
        StringBuffer s = new StringBuffer(name);
        char c = name.charAt(0);
        if (!Character.isLetter(c) && c != '_')
            s.setCharAt(0, '_');
        for (int i = 1; i < name.length(); i++) {
            c = name.charAt(i);
            if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_' && c != '.' && c != '-')
                s.setCharAt(i, '_');
        }
        return s.toString();
    }
}