Example usage for java.lang Character isJavaIdentifierStart

List of usage examples for java.lang Character isJavaIdentifierStart

Introduction

In this page you can find the example usage for java.lang Character isJavaIdentifierStart.

Prototype

public static boolean isJavaIdentifierStart(int codePoint) 

Source Link

Document

Determines if the character (Unicode code point) is permissible as the first character in a Java identifier.

Usage

From source file:com.netspective.commons.text.TextUtils.java

/**
 * Given a text string, return a string that would be suitable for that string to be used
 * as a Java identifier (as a variable or method name). Depending upon whether ucaseInitial
 * is set, the string starts out with a lowercase or uppercase letter. Then, the rule is
 * to convert all periods into underscores and title case any words separated by
 * underscores. This has the effect of removing all underscores and creating mixed case
 * words. For example, Person_Address becomes personAddress or PersonAddress depending upon
 * whether ucaseInitial is set to true or false. Person.Address would become Person_Address.
 *///w w w  . j a v a2s  .  c  om
public String xmlTextToJavaIdentifier(String xml, boolean ucaseInitial) {
    if (xml == null || xml.length() == 0)
        return xml;

    String translated = (String) JAVA_RESERVED_WORD_TRANSLATION_MAP.get(xml.toString().toLowerCase());
    if (translated != null)
        xml = translated;

    StringBuffer identifier = new StringBuffer();
    char ch = xml.charAt(0);
    if (Character.isJavaIdentifierStart(ch))
        identifier.append(ucaseInitial ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
    else {
        identifier.append('_');
        if (Character.isJavaIdentifierPart(ch))
            identifier.append(ucaseInitial ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
    }

    boolean uCase = false;
    for (int i = 1; i < xml.length(); i++) {
        ch = xml.charAt(i);
        if (ch == '.') {
            identifier.append('_');
        } else if (ch != '_' && Character.isJavaIdentifierPart(ch)) {
            identifier.append(Character.isUpperCase(ch) ? ch
                    : (uCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch)));
            uCase = false;
        } else
            uCase = true;
    }
    return identifier.toString();
}

From source file:com.puppycrawl.tools.checkstyle.utils.CommonUtil.java

/**
 * Checks whether the given string is a valid identifier.
 * @param str A string to check./*  www.  j a  v a  2  s  . c o m*/
 * @return true when the given string contains valid identifier.
 */
public static boolean isIdentifier(String str) {
    boolean isIdentifier = !str.isEmpty();

    for (int i = 0; isIdentifier && i < str.length(); i++) {
        if (i == 0) {
            isIdentifier = Character.isJavaIdentifierStart(str.charAt(0));
        } else {
            isIdentifier = Character.isJavaIdentifierPart(str.charAt(i));
        }
    }

    return isIdentifier;
}

From source file:org.apache.sqoop.model.ConfigUtils.java

private static void checkForValidConfigName(Set<String> existingConfigNames, String customConfigName) {
    // uniqueness across fields check
    if (existingConfigNames.contains(customConfigName)) {
        throw new SqoopException(ModelError.MODEL_012, "Issue with field config name " + customConfigName);
    }/* w ww.  j  av a 2 s  .co  m*/

    if (!Character.isJavaIdentifierStart(customConfigName.toCharArray()[0])) {
        throw new SqoopException(ModelError.MODEL_013, "Issue with field config name " + customConfigName);
    }
    for (Character c : customConfigName.toCharArray()) {
        if (Character.isJavaIdentifierPart(c))
            continue;
        throw new SqoopException(ModelError.MODEL_013, "Issue with field config name " + customConfigName);
    }

    if (customConfigName.length() > 30) {
        throw new SqoopException(ModelError.MODEL_014, "Issue with field config name " + customConfigName);

    }
}

From source file:org.dhatim.edisax.util.EDIUtils.java

public static String encodeJavaIdentifier(String identifier) {
    StringBuilder result = new StringBuilder();
    int len = identifier.length();
    boolean matchPrevious = false;
    char currentChar;

    for (int i = 0; i < len; i++) {
        currentChar = identifier.charAt(i);

        if (i == 0 && !Character.isJavaIdentifierStart(currentChar)) {
            result.append('_');
        }/* w ww . j a va2 s.c  om*/
        if (!Character.isJavaIdentifierPart(currentChar)) {
            matchPrevious = true;
            continue;
        }
        if (matchPrevious) {
            currentChar = Character.toUpperCase(currentChar);
            matchPrevious = false;
        }
        result.append(currentChar);
    }
    return result.toString();
}

From source file:net.sf.jabref.logic.layout.LayoutEntry.java

public static List<List<String>> parseMethodsCalls(String calls) {

    List<List<String>> result = new ArrayList<>();

    char[] c = calls.toCharArray();

    int i = 0;/*from   w ww  .j a va2  s. co m*/

    while (i < c.length) {

        int start = i;
        if (Character.isJavaIdentifierStart(c[i])) {
            i++;
            while ((i < c.length) && (Character.isJavaIdentifierPart(c[i]) || (c[i] == '.'))) {
                i++;
            }
            if ((i < c.length) && (c[i] == '(')) {

                String method = calls.substring(start, i);

                // Skip the brace
                i++;

                if (i < c.length) {
                    if (c[i] == '"') {
                        // Parameter is in format "xxx"

                        // Skip "
                        i++;

                        int startParam = i;
                        i++;
                        boolean escaped = false;
                        while (((i + 1) < c.length) && !(!escaped && (c[i] == '"') && (c[i + 1] == ')'))) {
                            if (c[i] == '\\') {
                                escaped = !escaped;
                            } else {
                                escaped = false;
                            }
                            i++;

                        }

                        String param = calls.substring(startParam, i);

                        result.add(Arrays.asList(method, param));
                    } else {
                        // Parameter is in format xxx

                        int startParam = i;

                        while ((i < c.length) && (c[i] != ')')) {
                            i++;
                        }

                        String param = calls.substring(startParam, i);

                        result.add(Arrays.asList(method, param));

                    }
                } else {
                    // Incorrectly terminated open brace
                    result.add(Collections.singletonList(method));
                }
            } else {
                String method = calls.substring(start, i);
                result.add(Collections.singletonList(method));
            }
        }
        i++;
    }

    return result;
}

From source file:org.jboss.tools.jst.web.ui.palette.html.wizard.AbstractNewHTMLWidgetWizardPage.java

static void addRangesInJavaScript(String text, int startOffset, int endOffset,
        ArrayList<StyleRange> regionList) {
    boolean inLineComments = false;
    boolean inBlockComments = false;
    char quota = '\0';
    boolean word = false;
    StringBuilder name = new StringBuilder();
    int offset = -1;
    for (int i = startOffset; i < endOffset; i++) {
        char ch = text.charAt(i);
        if (inBlockComments) {
            if (ch == '*' && i + 1 < endOffset && text.charAt(i + 1) == '/') {
                addRange(offset, i + 2 - offset, commentColor, false, regionList);
                inBlockComments = false;
                i++;/*from ww w .  j  ava2s . co m*/
            }
        } else if (inLineComments) {
            if (ch == '\n' || ch == '\r') {
                addRange(offset, i - offset, commentColor, false, regionList);
                inLineComments = false;
            }
        } else if (quota != '\0') {
            if (ch == quota) {
                quota = '\0';
                addRange(offset, i + 1 - offset, valueColor, false, regionList);
            }
        } else if (ch == '\'' || ch == '"') {
            quota = ch;
            offset = i;
        } else {
            if (Character.isLetterOrDigit(ch) || ch == '_') {
                if (word) {
                    name.append(ch);
                } else if (Character.isJavaIdentifierStart(ch)) {
                    word = true;
                    name.append(ch);
                    offset = i;
                }
            } else {
                if (word && name.length() > 0) {
                    if (keywords.contains(name.toString())) {
                        addRange(offset, name.length(), keywordColor, false, true, regionList);
                    }
                    name.setLength(0);
                }
                word = false;
            }
            if (ch == '/' && i + 1 < endOffset) {
                char ch1 = text.charAt(i + 1);
                if (ch1 == '/') {
                    inLineComments = true;
                    offset = i;
                } else if (ch1 == '*') {
                    inBlockComments = true;
                    offset = i;
                    i++;
                }
            }
        }
    }
}

From source file:org.apache.axis.utils.JavaUtils.java

/**
 * isJavaId// w w  w  . j av a  2s  .com
 * Returns true if the name is a valid java identifier.
 * @param id to check
 * @return boolean true/false
 **/
public static boolean isJavaId(String id) {
    if (id == null || id.equals("") || isJavaKeyword(id))
        return false;
    if (!Character.isJavaIdentifierStart(id.charAt(0)))
        return false;
    for (int i = 1; i < id.length(); i++)
        if (!Character.isJavaIdentifierPart(id.charAt(i)))
            return false;
    return true;
}

From source file:net.bioclipse.ds.sdk.pdewizard.DSTemplate.java

protected String getFormattedPackageName(String id) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < id.length(); i++) {
        char ch = id.charAt(i);
        if (buffer.length() == 0) {
            if (Character.isJavaIdentifierStart(ch))
                buffer.append(Character.toLowerCase(ch));
        } else {//w  w w.  j  av a 2 s .com
            if (Character.isJavaIdentifierPart(ch) || ch == '.')
                buffer.append(ch);
        }
    }
    return buffer.toString().toLowerCase(Locale.ENGLISH);
}

From source file:com.abstratt.mdd.internal.frontend.textuml.TextUMLFormatter.java

private static boolean isPunctuation(String text) {
    return !Character.isJavaIdentifierStart(text.charAt(0)) && text.charAt(0) != '\\';
}

From source file:com.servoy.j2db.util.Utils.java

public static String stringReplace(String org, String source, String destination, int replaceOccurence,
        boolean mustExact, boolean caseInsensitiveSearch) {
    if (org == null)
        return null;
    if (source == null || source.length() == 0)
        return org;
    String searchOrg = org;//from   w  w w  .j  a va  2s . c o m
    if (caseInsensitiveSearch) {
        searchOrg = org.toLowerCase();
        source = source.toLowerCase();
    }
    int index = searchOrg.indexOf(source);
    if (index != -1) {
        int occurence = 0;
        StringBuilder sb = new StringBuilder();
        int startIndex = 0;
        boolean isExact = true;
        while (index != -1) {
            if (mustExact) {
                //check left
                if (index > 0) {
                    isExact = !Character.isJavaIdentifierStart(org.charAt(index - 1));
                }
                //check right
                if ((index + source.length() < org.length()) && isExact) {
                    isExact = !Character.isJavaIdentifierStart(org.charAt(index + source.length()));
                }
            }
            sb.append(org.substring(startIndex, index));
            if (mustExact) {
                if (isExact) {
                    sb.append(destination);
                } else {
                    sb.append(source);
                }
            } else {
                if (replaceOccurence < 0 || replaceOccurence == occurence) {
                    sb.append(destination);
                } else {
                    sb.append(source);
                }
            }

            startIndex = index + source.length();
            index = searchOrg.indexOf(source, startIndex);
            occurence++;
        }
        sb.append(org.substring(startIndex, org.length()));//add tail
        return sb.toString();
    } else {
        return org;
    }
}