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.caocao.util.StringUtils.java

/**
 * Returns true if s is a legal Java identifier.<p>
 * <a href="http://www.exampledepot.com/egs/java.lang/IsJavaId.html">more info.</a>
 *///from  w w  w .  ja v a  2 s  .  com
public static boolean isJavaIdentifier(String s) {
    if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) {
        return false;
    }
    for (int i = 1; i < s.length(); i++) {
        if (!Character.isJavaIdentifierPart(s.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:io.spring.initializr.metadata.InitializrConfiguration.java

private static boolean hasInvalidChar(String text) {
    if (!Character.isJavaIdentifierStart(text.charAt(0))) {
        return true;
    }//from w w  w .  jav a2 s.c  o  m
    if (text.length() > 1) {
        for (int i = 1; i < text.length(); i++) {
            if (!Character.isJavaIdentifierPart(text.charAt(i))) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.aiblockchain.api.StringUtils.java

/**
 * Returns whether the given string is a valid Java class name.
 *
 * @param string the given string//w  w  w.  j  av  a  2s  .  c  o  m
 *
 * @return whether the given string is a valid Java class name
 */
public static boolean isJavaClassName(final String string) {
    if (string == null || string.isEmpty() || !Character.isJavaIdentifierStart(string.charAt(0))
            || string.contains("..")) {
        return false;
    }
    for (int i = 1; i < string.length(); i++) {
        final char ch = string.charAt(i);
        if (!Character.isJavaIdentifierPart(ch) && ch != '.') {
            return false;
        }
    }
    final String[] lastNameParts = string.split("\\.");
    final String lastNamePart = lastNameParts[lastNameParts.length - 1];
    return Character.isUpperCase(lastNamePart.charAt(0));
}

From source file:org.eclipse.xtend.typesystem.xsd.builder.OawXSDEcoreBuilder.java

@Override
protected String validName(String name, int casing, String prefix) {
    List<String> parsedName = parseName(name, '_');
    StringBuffer result = new StringBuffer();
    for (String nameComponent : parsedName) {
        if (nameComponent.length() > 0) {
            if (casing != UNCHANGED_CASE && isUppercase(nameComponent))
                nameComponent = nameComponent.toLowerCase();
            if (result.length() > 0 || casing == UPPER_CASE) {
                result.append(Character.toUpperCase(nameComponent.charAt(0)));
                result.append(nameComponent.substring(1));
            } else {
                result.append(nameComponent);
            }/*from w  w w  .j ava2  s . c o m*/
        }
    }
    return result.length() == 0 ? prefix
            : Character.isJavaIdentifierStart(result.charAt(0))
                    ? casing == LOWER_CASE ? uncapName(result.toString()) : result.toString()
                    : prefix + result;
}

From source file:info.novatec.testit.livingdoc.ognl.OgnlResolution.java

private boolean identifierComplete(String[] parts, boolean[] dots, int index) {
    /* The identifier is complete if: - It's the last token or - The next
     * token does not start as a valid Java identifier or - A dot is to be
     * insert after identifier. */
    return index == parts.length - 1 || !Character.isJavaIdentifierStart(firstChar(parts[index + 1]))
            || insertDot(parts, dots, index);
}

From source file:org.apache.sling.scripting.sightly.impl.engine.compiled.SourceIdentifier.java

/**
 * Converts the given identifier to a legal Java identifier
 *
 * @param identifier the identifier to convert
 * @return legal Java identifier corresponding to the given identifier
 *//*  w  w w  .  ja v  a 2s.  co m*/
public static String makeJavaIdentifier(String identifier) {
    StringBuilder modifiedIdentifier = new StringBuilder(identifier.length());
    if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
        modifiedIdentifier.append('_');
    }
    for (int i = 0; i < identifier.length(); i++) {
        char ch = identifier.charAt(i);
        if (Character.isJavaIdentifierPart(ch) && ch != '_') {
            modifiedIdentifier.append(ch);
        } else if (ch == '.') {
            modifiedIdentifier.append('_');
        } else {
            modifiedIdentifier.append(mangleChar(ch));
        }
    }
    if (isJavaKeyword(modifiedIdentifier.toString())) {
        modifiedIdentifier.append('_');
    }
    return modifiedIdentifier.toString();
}

From source file:org.apache.cayenne.project.upgrade.v6.XMLDataChannelDescriptorLoader_V3_0_0_1.java

/**
 * Make sure the domain name is only made up of Java-identifier-safe
 * characters.//from  ww w.  j ava  2s  . c o  m
 */
protected String scrubDomainName(String name) {
    if (name == null || name.length() == 0) {
        return name;
    }

    StringBuilder buffer = new StringBuilder(name.length());

    for (int i = 0; i < name.length(); i++) {
        char c = name.charAt(i);
        if (i == 0 && !Character.isJavaIdentifierStart(c)) {
            buffer.append('_');
        } else if (i > 0 && !Character.isJavaIdentifierPart(c)) {
            buffer.append('_');
        } else {
            buffer.append(c);
        }
    }

    return buffer.toString();
}

From source file:org.ops4j.ramler.generator.Names.java

/**
 * Get enum field name from value/* w  w w .j a va  2s.co  m*/
 *
 * @param value
 *            string to be checked
 * @return a {@link java.lang.String} object.
 */
public static boolean canBeEnumConstantName(final String value) {
    boolean res = !value.isEmpty();
    for (int i = 0; i < value.length(); i++) {
        char c = value.charAt(i);
        if (i == 0) {
            res &= Character.isJavaIdentifierStart(c);
        } else {
            res &= Character.isJavaIdentifierPart(c);
        }
        if (!res) {
            break;
        }
    }
    return res;
}

From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.ejbql.EJBQLFieldsReader.java

/**
 * Checks if the input is a valid Java literal
 * @param literal/*from  w w  w  .  j a v  a  2s .co m*/
 * @author Gaganis Giorgos (gaganis@users.sourceforge.net) 
 */
private static boolean isValidLiteral(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;
}

From source file:info.novatec.testit.livingdoc.ognl.OgnlResolution.java

private boolean insertDot(String[] parts, boolean[] dots, int index) {
    /* A dot is to be inserted if: - It's not the last token and -
     * Indication to actually try to insert it and - The next token start as
     * a valid Java identifier. */
    return index < parts.length - 1 && dots[index]
            && Character.isJavaIdentifierStart(firstChar(parts[index + 1]));
}