Java Java String Format toJavaStaticIdentifier(String string)

Here you can find the source of toJavaStaticIdentifier(String string)

Description

Fashion a static identifier from the given string.

License

LGPL

Parameter

Parameter Description
string a parameter

Return

A valid java static identifier. Upper Case with underscores.

Declaration

public static String toJavaStaticIdentifier(String string) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*  w w w . j  av a 2  s . c o  m*/
     * Fashion a static identifier from the given string.
     * @param string
     * @return A valid java static identifier.  Upper Case with underscores.
     */
    public static String toJavaStaticIdentifier(String string) {
        String javaIdentifierName = toJavaInstanceIdentifier(string);
        StringBuilder sb = new StringBuilder(javaIdentifierName.length() + 5);

        for (int i = 0, l = javaIdentifierName.length(); i < l; i++) {
            char ch = javaIdentifierName.charAt(i);
            if (Character.isUpperCase(ch)) {
                boolean nextCharLowerCase = false;
                boolean prevCharLowerCase = false;
                int nextIndex = i + 1;
                int prevIndex = i - 1;
                if (nextIndex < l) {
                    char nextChar = javaIdentifierName.charAt(nextIndex);
                    nextCharLowerCase = Character.isLowerCase(nextChar);
                }
                if (prevIndex >= 0) {
                    char prevChar = javaIdentifierName.charAt(prevIndex);
                    prevCharLowerCase = Character.isLowerCase(prevChar);
                }

                // if the previous char was upper case then don't add an underscore
                if ((prevCharLowerCase || nextCharLowerCase) && (i > 0)) {
                    sb.append('_');
                }
                sb.append(ch);
            } else {
                sb.append(Character.toUpperCase(ch));
            }
        }

        return sb.toString();
    }

    /**
     * Fashion an instance identifier from the given string.
     * @param string
     * @return   A valid java instance identifier.  Camel case.
     */
    public static String toJavaInstanceIdentifier(String string) {
        StringBuilder sb = new StringBuilder(string);
        removeInvalidCharacters(sb);
        sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
        return sb.toString();
    }

    private static void removeInvalidCharacters(StringBuilder sb) {
        int i = 0;
        boolean whiteSpaceOrUnderscore = false;
        while (i < sb.length()) {
            if (i == 0) {
                if (Character.isJavaIdentifierStart(sb.charAt(0)) && (sb.charAt(0) != '_')) {
                    i++;
                } else {
                    sb.deleteCharAt(0);
                }
            } else {
                if (Character.isJavaIdentifierPart(sb.charAt(i)) && (sb.charAt(i) != '_')) {
                    if (whiteSpaceOrUnderscore) {
                        sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
                    }
                    whiteSpaceOrUnderscore = false;
                    i++;
                } else {
                    if (Character.isWhitespace(sb.charAt(i)) || (sb.charAt(i) == '_')) {
                        whiteSpaceOrUnderscore = true;
                    }
                    sb.deleteCharAt(i);
                }
            }
        }
    }
}

Related

  1. toJavascriptArray(String[][] Vals)
  2. toJavaScriptEncode(String string)
  3. toJavaScriptSafeString(String content)
  4. toJavaSecs(long secs)
  5. toJavaSourceType(String type)
  6. toJavaString(final String s)
  7. toJavaString(String buf)
  8. toJavaString(String text)
  9. toJavaTime(double time)