Here you can find the source of toJavaClassName(String str)
public static String toJavaClassName(String str)
//package com.java2s; /**//from www . j a va2s . c o m * * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. * * This library 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 2.1 of the License, or (at your option) any later version. * * This library 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 this library. If not, see <http://www.gnu.org/licenses/>. * **/ public class Main { public static String toJavaClassName(String str) { return toVariableName(str, true, true); } /** * translate a string to a valid variable string * @param str string to translate * @return translated String */ public static String toVariableName(String str) { return toVariableName(str, true, false); } public static String toVariableName(String str, boolean addIdentityNumber, boolean allowDot) { StringBuilder rtn = new StringBuilder(); char[] chars = str.toCharArray(); long changes = 0; boolean doCorrect = true; for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (i == 0 && (c >= '0' && c <= '9')) rtn.append("_" + c); else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '$' || (allowDot && c == '.')) rtn.append(c); else { doCorrect = false; rtn.append('_'); changes += (c * (i + 1)); } } if (addIdentityNumber && changes > 0) rtn.append(changes); //print.ln(" - "+rtn); if (doCorrect) return correctReservedWord(rtn.toString()); return rtn.toString(); } /** * if given string is a keyword it will be replaced with none keyword * @param str * @return corrected word */ private static String correctReservedWord(String str) { char first = str.charAt(0); switch (first) { case 'a': if (str.equals("abstract")) return "_" + str; break; case 'b': if (str.equals("boolean")) return "_" + str; else if (str.equals("break")) return "_" + str; else if (str.equals("byte")) return "_" + str; break; case 'c': if (str.equals("case")) return "_" + str; else if (str.equals("catch")) return "_" + str; else if (str.equals("char")) return "_" + str; else if (str.equals("const")) return "_" + str; else if (str.equals("class")) return "_" + str; else if (str.equals("continue")) return "_" + str; break; case 'd': if (str.equals("default")) return "_" + str; else if (str.equals("do")) return "_" + str; else if (str.equals("double")) return "_" + str; break; case 'e': if (str.equals("else")) return "_" + str; else if (str.equals("extends")) return "_" + str; else if (str.equals("enum")) return "_" + str; break; case 'f': if (str.equals("false")) return "_" + str; else if (str.equals("final")) return "_" + str; else if (str.equals("finally")) return "_" + str; else if (str.equals("float")) return "_" + str; else if (str.equals("for")) return "_" + str; break; case 'g': if (str.equals("goto")) return "_" + str; break; case 'i': if (str.equals("if")) return "_" + str; else if (str.equals("implements")) return "_" + str; else if (str.equals("import")) return "_" + str; else if (str.equals("instanceof")) return "_" + str; else if (str.equals("int")) return "_" + str; else if (str.equals("interface")) return "_" + str; break; case 'n': if (str.equals("native")) return "_" + str; else if (str.equals("new")) return "_" + str; else if (str.equals("null")) return "_" + str; break; case 'p': if (str.equals("package")) return "_" + str; else if (str.equals("private")) return "_" + str; else if (str.equals("protected")) return "_" + str; else if (str.equals("public")) return "_" + str; break; case 'r': if (str.equals("return")) return "_" + str; break; case 's': if (str.equals("short")) return "_" + str; else if (str.equals("static")) return "_" + str; else if (str.equals("strictfp")) return "_" + str; else if (str.equals("super")) return "_" + str; else if (str.equals("switch")) return "_" + str; else if (str.equals("synchronized")) return "_" + str; break; case 't': if (str.equals("this")) return "_" + str; else if (str.equals("throw")) return "_" + str; else if (str.equals("throws")) return "_" + str; else if (str.equals("transient")) return "_" + str; else if (str.equals("true")) return "_" + str; else if (str.equals("try")) return "_" + str; break; case 'v': if (str.equals("void")) return "_" + str; else if (str.equals("volatile")) return "_" + str; break; case 'w': if (str.equals("while")) return "_" + str; break; } return str; } /** * returns string, if given string is null or lengt 0 return default value * @param value * @param defaultValue * @return value or default value */ public static String toString(String value, String defaultValue) { return value == null || value.length() == 0 ? defaultValue : value; } /** * returns string, if given string is null or lengt 0 return default value * @param value * @param defaultValue * @return value or default value */ public static String toString(Object value, String defaultValue) { if (value == null) return defaultValue; return toString(value.toString(), defaultValue); } public static int length(String str) { if (str == null) return 0; return str.length(); } public static int length(String str, boolean trim) { if (str == null) return 0; return str.trim().length(); } }