Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * This file is part of the DiffX library.
 *
 * For licensing information please see the file license.txt included in the release.
 * A copy of this licence can also be found at
 *   http://www.opensource.org/licenses/artistic-license-2.0.php
 */

public class Main {
    /**
     * Return a valid element name from the given string.
     *
     * <p>Letters are put to lower case and other characters are replaced by hyphens.
     * If the first character is not a letter it is replaced by 'x'.
     *
     * @param name The candidate element name
     *
     * @return A valid element name
     */
    public static String toElementName(String name) {
        if (name == null)
            return null;
        char[] elementAsChars = name.toCharArray();
        if (!Character.isLetter(elementAsChars[0])) {
            elementAsChars[0] = 'x';
        } else {
            elementAsChars[0] = Character.toLowerCase(elementAsChars[0]);
        }
        for (int i = 1; i < elementAsChars.length; i++) {
            if (!Character.isLetter(elementAsChars[i])) {
                elementAsChars[i] = '-';
            } else {
                elementAsChars[i] = Character.toLowerCase(elementAsChars[i]);
            }
        }
        return new String(elementAsChars);
    }
}