Example usage for java.lang Character isJavaIdentifierPart

List of usage examples for java.lang Character isJavaIdentifierPart

Introduction

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

Prototype

public static boolean isJavaIdentifierPart(int codePoint) 

Source Link

Document

Determines if the character (Unicode code point) may be part of a Java identifier as other than the first character.

Usage

From source file:Main.java

/**
 * Check whether the given String is a valid identifier according
 * to the Java Language specifications./*from  ww  w .j  a v a2 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:com.github.tomregan.utilities.StringUtilities.java

private boolean isValidJavaIdentifier(char[] characters) {
    for (char character : characters) {
        if (!Character.isJavaIdentifierPart(character)) {
            return false;
        }//from w ww  .  j  ava2  s.com
    }
    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 a 2  s. co  m
    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 w  w w  .jav  a 2 s . c  o  m*/
            }
        } 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: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 .  ja va 2s. c om
    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: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.
 * //from ww  w .  java  2  s  . c o m
 * @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: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).
 *//*w  w  w. j  a v  a2s. 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;
}

From source file:com.google.dart.tools.ui.omni.elements.TopLevelElementProvider_NEW.java

private static String getIdentifierCharacters(String str) {
    int length = str.length();
    StringBuilder buf = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        char c = str.charAt(i);
        if (Character.isJavaIdentifierPart(c)) {
            buf.append(c);//from  ww w.j  a v  a2 s  .  c  om
        }
    }
    return buf.toString();
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

/**
 * Encode a String so that is is a valid JMS header name
 *
 * @param name the String to encode//w w  w  . j  a va2s. c o m
 * @return a valid JMS header name
 */
public static String encodeHeader(String name) {
    // check against JMS 1.1 spec, sections 3.5.1 (3.8.1.1)
    boolean nonCompliant = false;

    if (StringUtils.isEmpty(name)) {
        throw new IllegalArgumentException("Header name to encode must not be null or empty");
    }

    int i = 0, length = name.length();
    while (i < length && Character.isJavaIdentifierPart(name.charAt(i))) {
        // zip through
        i++;
    }

    if (i == length) {
        // String is already valid
        return name;
    } else {
        // make a copy, fix up remaining characters
        StringBuffer sb = new StringBuffer(name);
        for (int j = i; j < length; j++) {
            if (!Character.isJavaIdentifierPart(sb.charAt(j))) {
                sb.setCharAt(j, REPLACEMENT_CHAR);
                nonCompliant = true;
            }
        }

        if (nonCompliant) {
            logger.warn(MessageFormat.format(
                    "Header: {0} is not compliant with JMS specification (sec. 3.5.1, 3.8.1.1). It will cause "
                            + "problems in your and other applications. Please update your application code to correct this. "
                            + "Mule renamed it to {1}",
                    name, sb.toString()));
        }

        return sb.toString();
    }
}

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.  j a va  2 s  .co m*/

    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);
    }
}