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:Main.java

/**
 * Returns a valid Java name from an XML Name.
 *
 * @param name/*from   ww w  . ja va  2  s  .co m*/
 * @param isUpperCase
 * @return a valid Java name from an XML Name
 */
public static String getJavaNameFromXMLName(String name, boolean isUpperCase) {
    List<String> parsedName = parseName(name, '_');
    StringBuilder result = new StringBuilder(64 * parsedName.size());
    for (String nameComponent : parsedName) {
        if (nameComponent.length() > 0) {
            if (result.length() > 0 || isUpperCase) {
                result.append(Character.toUpperCase(nameComponent.charAt(0)));
                result.append(nameComponent.substring(1));
            } else {
                result.append(nameComponent);
            }
        }
    }

    if (result.length() == 0) {
        return "_";
    }
    if (Character.isJavaIdentifierStart(result.charAt(0))) {
        return isUpperCase ? result.toString() : decapitalizeName(result.toString());
    }
    return "_" + result;
}

From source file:Main.java

/**
 * Check whether the given String is a valid identifier according
 * to the Java Language specifications./*from  w ww.ja v  a  2 s .  c  om*/
 *
 * See The Java Language Specification Second Edition, Section 3.8
 * for the definition of what is a valid identifier.
 *
 * @param s String to check
 *
 * @return <code>true</code> if the given String is a valid Java
 *         identifier, <code>false</code> otherwise.
 */
public final static boolean isValidJavaIdentifier(String s) {
    // an empty or null string cannot be a valid identifier
    if (s == null || s.length() == 0) {
        return false;
    }

    char[] c = s.toCharArray();
    if (!Character.isJavaIdentifierStart(c[0])) {
        return false;
    }

    for (int i = 1; i < c.length; i++) {
        if (!Character.isJavaIdentifierPart(c[i])) {
            return false;
        }
    }

    return true;
}

From source file:Strings.java

public static boolean isJavaIdentifier(String id) {
    if (id == null) {
        return false;
    }//from   w w  w.  j  a  v  a2 s  .c  om
    int len = id.length();
    if (len == 0) {
        return false;
    }
    if (!Character.isJavaIdentifierStart(id.charAt(0))) {
        return false;
    }
    for (int i = 1; i < len; i++) {
        if (!Character.isJavaIdentifierPart(id.charAt(i))) {
            return false;
        }
    }
    return true;
}

From source file:org.opensingular.lib.commons.base.SingularUtil.java

public static String convertToJavaIdentity(String original, boolean firstCharacterUpperCase,
        boolean normalize) {
    String normalized = normalize ? normalize(original) : original;
    StringBuilder sb = new StringBuilder(normalized.length());
    boolean nextUpper = false;
    for (char c : normalized.toCharArray()) {
        if (sb.length() == 0) {
            if (Character.isJavaIdentifierStart(c)) {
                c = firstCharacterUpperCase ? Character.toUpperCase(c) : Character.toLowerCase(c);
                sb.append(c);/*from  ww  w  . j  a  v a2s  . c  om*/
            }
        } else if (Character.isJavaIdentifierPart(c)) {
            if (nextUpper) {
                c = Character.toUpperCase(c);
                nextUpper = false;
            }
            sb.append(c);
        } else if (Character.isWhitespace(c)) {
            nextUpper = true;
        }
    }
    return sb.toString();
}

From source file:Utils.java

/**
 * Determine whether the supplied string represents a well-formed fully-qualified Java classname. This utility method enforces
 * no conventions (e.g., packages are all lowercase) nor checks whether the class is available on the classpath.
 * // w  w w  .  j  a  va 2s .  c  om
 * @param classname
 * @return true if the string is a fully-qualified class name
 */
public static boolean isFullyQualifiedClassname(String classname) {
    if (classname == null)
        return false;
    String[] parts = classname.split("[\\.]");
    if (parts.length == 0)
        return false;
    for (String part : parts) {
        CharacterIterator iter = new StringCharacterIterator(part);
        // Check first character (there should at least be one character for each part) ...
        char c = iter.first();
        if (c == CharacterIterator.DONE)
            return false;
        if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c))
            return false;
        c = iter.next();
        // Check the remaining characters, if there are any ...
        while (c != CharacterIterator.DONE) {
            if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c))
                return false;
            c = iter.next();
        }
    }
    return true;
}

From source file:de.micromata.tpsb.doc.parser.TypeUtils.java

License:asdf

public static boolean isValidJavaIdentifier(String s, boolean withDots) {
    if (StringUtils.isEmpty(s) == true) {
        return false;
    }//from ww  w  .  jav  a 2 s  .co  m
    if (Character.isJavaIdentifierStart(s.charAt(0)) == false) {
        return false;
    }
    for (int i = 1; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (Character.isJavaIdentifierPart(c) == false && (withDots == true && c == '.') == false) {
            return false;
        }
    }
    return true;
}

From source file:com.github.tomregan.utilities.StringUtilities.java

boolean isValidJavaIdentifier(final String identifier) {
    return Character.isJavaIdentifierStart(identifier.charAt(0))
            && isValidJavaIdentifier(identifier.toCharArray());
}

From source file:com.phoenixnap.oss.ramlapisync.naming.NamingHelper.java

/**
 * Utility method to check if a string can be used as a valid class name
 * //from  w  ww.ja  v a 2 s  .  c  o  m
 * @param input String to check
 * @return true if valid
 */
public static boolean isValidJavaClassName(String input) {
    if (!StringUtils.hasText(input)) {
        return false;
    }
    if (!Character.isJavaIdentifierStart(input.charAt(0))) {
        return false;
    }
    if (input.length() > 1) {
        for (int i = 1; i < input.length(); i++) {
            if (!Character.isJavaIdentifierPart(input.charAt(i))) {
                return false;
            }
        }
    }
    return true;
}

From source file:org.eclipse.tycho.extras.docbundle.PackageNameMatcher.java

private static Pattern buildPattern(final String spec) {
    if (StringUtils.isEmpty(spec)) {
        throw new IllegalArgumentException("empty package name");
    }/*from   w w w  . jav a2 s .c om*/

    final StringBuilder regex = new StringBuilder();

    for (int idx = 0; idx < spec.length(); idx++) {
        char ch = spec.charAt(idx);
        if (ch == '*') {
            regex.append(".*");

        } else if (ch == '.') {
            regex.append("\\.");

        } else if (idx == 0 && !Character.isJavaIdentifierStart(ch)) {
            throw new IllegalArgumentException("invalid package name: " + spec);

        } else if (!Character.isJavaIdentifierPart(ch)) {
            throw new IllegalArgumentException("invalid package name: " + spec);

        } else {
            regex.append(ch);
        }
    }

    try {
        return Pattern.compile(regex.toString());
    } catch (PatternSyntaxException e) {
        throw new IllegalArgumentException("invalid package specification: " + spec, e);
    }
}

From source file:org.gradle.groovy.scripts.AbstractUriScriptSource.java

/**
 * Returns the class name for use for this script source.  The name is intended to be unique to support mapping
 * class names to source files even if many sources have the same file name (e.g. build.gradle).
 *///from   w  ww. jav a  2 s  . c  o m
public String getClassName() {
    if (className == null) {
        URI sourceUri = getResource().getURI();
        String name = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(sourceUri.toString(), "/"),
                ".");
        StringBuilder className = new StringBuilder(name.length());
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            if (Character.isJavaIdentifierPart(ch)) {
                className.append(ch);
            } else {
                className.append('_');
            }
        }
        if (!Character.isJavaIdentifierStart(className.charAt(0))) {
            className.insert(0, '_');
        }
        className.setLength(Math.min(className.length(), 30));
        className.append('_');
        String path = sourceUri.toString();
        className.append(HashUtil.createCompactMD5(path));

        this.className = className.toString();
    }

    return className;
}