Takes a name and returns the same if it is a Java identifier; else it substitutes the illegal characters so that it can be an identifier - Java java.lang

Java examples for java.lang:char

Description

Takes a name and returns the same if it is a Java identifier; else it substitutes the illegal characters so that it can be an identifier

Demo Code

/*/* w  w w.jav  a 2 s.co  m*/
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2013 Jaspersoft Corporation. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Contributors:
 * Gaganis Giorgos - gaganis@users.sourceforge.net
 */
 
//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String name = "java2s.com";
        System.out.println(getJavaIdentifier(name));
    }

    protected static final String JAVA_IDENTIFIER_PREFIX = "j";

    /**
     * Takes a name and returns the same if it is a Java identifier;
     * else it substitutes the illegal characters so that it can be an identifier
     *
     * @param name
     */
    public static String getJavaIdentifier(String name) {
        if (isValidJavaIdentifier(name)) {
            return name;
        }

        StringBuffer buffer = new StringBuffer(name.length() + 5);

        char[] literalChars = new char[name.length()];
        name.getChars(0, literalChars.length, literalChars, 0);

        for (int i = 0; i < literalChars.length; i++) {
            if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) {
                buffer.append(JAVA_IDENTIFIER_PREFIX);
                buffer.append((int) literalChars[i]);
            } else if (i != 0
                    && !Character.isJavaIdentifierPart(literalChars[i])) {
                buffer.append((int) literalChars[i]);
            } else {
                buffer.append(literalChars[i]);
            }
        }

        return buffer.toString();
    }

    /**
     * Checks if the input is a valid Java identifier
     * @param literal
     * @author Gaganis Giorgos (gaganis@users.sourceforge.net) 
     */
    private static boolean isValidJavaIdentifier(String literal) {
        boolean result = true;

        char[] literalChars = new char[literal.length()];
        literal.getChars(0, literalChars.length, literalChars, 0);

        for (int i = 0; i < literalChars.length; i++) {
            if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) {
                result = false;
                break;
            }

            if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) {
                result = false;
                break;
            }
        }

        return result;
    }
}

Related Tutorials