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:org.apache.jetspeed.util.ValidationHelper.java

public static boolean isValidIdentifier(String folderName) {
    boolean valid = true;

    char[] chars = folderName.toCharArray();
    for (int ix = 0; ix < chars.length; ix++) {
        if (!Character.isJavaIdentifierPart(chars[ix])) {
            valid = false;/*from w w w . ja v a2 s. c o m*/
            break;
        }
    }
    return valid;
}

From source file:ca.sfu.federation.model.Expression.java

/**
 * A statement has the form of a reference if it is comprised of only 
 * alphanumeric, ('.','_','[',']') characters, and the first character is a 
 * char or the @ symbol.  No guarantee is given that the reference can be 
 * resolved./*  w w w.ja  va  2 s.c o m*/
 * @param Statement User specified statement.
 * @return True if the statement is a reference, false otherwise.
 */
private static boolean isReference(String Statement) {
    // init
    boolean result = true;
    // check if the statement is a proper reference
    char c = Statement.charAt(0);
    if (!Character.isJavaIdentifierStart(c)) {
        result = false;
    }
    if (Statement.substring(0, 1).equals("@")) {
        result = true;
    }
    // if the statement contains any operators, then it is not a reference
    int i = 0;
    while (result && i < Expression.OPERATORS.length) {
        if (Statement.contains(Expression.OPERATORS[i])) {
            result = false;
        }
        i++;
    }
    // if the statement contains punctuation other than ('@','.','_','[',']'), then it is not a reference
    if (Statement.length() > 1) {
        for (i = 1; i < Statement.length(); i++) {
            char ch = Statement.charAt(i);
            if (!Character.isJavaIdentifierPart(ch)) {
                result = false;
            }
        }
    }
    // return result
    return result;
}

From source file:me.oriley.crate.CrateGenerator.java

@NonNull
private static String sanitiseFieldName(@NonNull String fileName) {
    // JavaPoet doesn't like the dollar signs so we remove them too
    char[] charArray = fileName.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
        if (!Character.isJavaIdentifierPart(charArray[i]) || charArray[i] == '$') {
            charArray[i] = '_';
        }//from   w  w  w. jav a2s  .c om
    }

    if (!Character.isJavaIdentifierStart(charArray[0]) || charArray[0] == '$') {
        return "_" + new String(charArray);
    } else {
        return new String(charArray);
    }
}

From source file:com.oscgc.security.saml.idp.web.contoller.MetadataController.java

protected String getFileName(EntityDescriptor entityDescriptor) {
    StringBuilder fileName = new StringBuilder();
    for (Character c : entityDescriptor.getEntityID().toCharArray()) {
        if (Character.isJavaIdentifierPart(c)) {
            fileName.append(c);// www .  j  a va  2  s  .  c o  m
        }
    }
    if (fileName.length() > 0) {
        fileName.append("_sp.xml");
        return fileName.toString();
    } else {
        return "default_sp.xml";
    }
}

From source file:com.attribyte.essem.util.Util.java

/**
 * Creates a string that is a valid "Java identifier" by
 * replacing invalid characters with underscore ('_').
 * @param str The input string./*from  w w w.  ja  v a 2s.  com*/
 * @return The valid java identifier.
 */
public static String toJavaIdentifier(final String str) {

    StringBuilder buf = new StringBuilder();
    char[] chars = str.toCharArray();
    if (Character.isJavaIdentifierStart(chars[0])) {
        buf.append(chars[0]);
    } else if (Character.isJavaIdentifierPart(chars[0])) {
        buf.append("_").append(chars[0]);
    } else {
        buf.append("_");
    }

    for (int pos = 1; pos < chars.length; pos++) {
        if (Character.isJavaIdentifierPart(chars[pos])) {
            buf.append(chars[pos]);
        } else {
            buf.append("_");
        }
    }

    return buf.toString();
}

From source file:org.opencms.module.CmsModuleXmlHandler.java

/**
 * Generates a (hopefully) valid Java class name from an invalid class name.<p>
 * /*from  ww  w  .  java 2 s  . c  om*/
 * All invalid characters are replaced by an underscore "_".
 * This is for example used to make sure old (5.0) modules can still be imported,
 * by converting the name to a valid class name.<p>
 * 
 * @param className the class name to make valid
 * 
 * @return a valid Java class name from an invalid class name
 */
public static String makeValidJavaClassName(String className) {

    StringBuffer result = new StringBuffer(className.length());
    int length = className.length();
    boolean nodot = true;
    for (int i = 0; i < length; i++) {
        char ch = className.charAt(i);
        if (nodot) {
            if (ch == '.') {
                // ignore, remove
            } else if (Character.isJavaIdentifierStart(ch)) {
                nodot = false;
                result.append(ch);
            } else {
                result.append('_');
            }
        } else {
            if (ch == '.') {
                nodot = true;
                result.append(ch);
            } else if (Character.isJavaIdentifierPart(ch)) {
                nodot = false;
                result.append(ch);
            } else {
                result.append('_');
            }
        }
    }
    return result.toString();
}

From source file:bear.plugins.groovy.GroovyCodeCompleter.java

/**
 * Returning a range because rightBorder(+startPos+) can move left 1 char when placed at the space.
 * todo check if it replaces ok//from w w w . jav  a  2s  . c  o m
 * See unit test to check how it works.
 *
 * @param startPos == script.length if at the end of the script.
 */
static int[] scanForStart(String script, int startPos, final int inc) {
    int l = script.length();

    char matchingQuote = 0;

    int openedBrackets = 0;
    int closedBrackets = 0;

    int pos;

    startPos--; // ' ' or '(' are not our characters

    for (pos = startPos; pos >= 0 && pos < l; pos += inc) {

        char ch = script.charAt(pos);

        //check string mode, copied
        if (ch == '\"' || ch == '\'') {
            matchingQuote = closeOpenQuotes(matchingQuote, ch);

            continue;
        }

        if (matchingQuote != 0)
            continue;

        //normal mode
        final boolean isBracket;

        // foo2.recursiveFoo(xx()) - here the invariant is:  closed>=opened
        // outerMethod|( foo2.recursiveFoo(xx()) here it breaks
        switch (ch) {
        case ')':
            closedBrackets++;
            isBracket = true;
            break;

        case '(':
            openedBrackets++;
            isBracket = true;
            break;
        default:
            isBracket = false;
        }

        // this case: outerMethod|( foo2.recursiveFoo(xx()) here it breaks
        if (openedBrackets > closedBrackets) {
            return new int[] { firstNonSpace(script, pos + 1, 1), startPos };
        }

        if (openedBrackets < closedBrackets) {
            continue;
        }

        if (isBracket)
            continue;

        if (Character.isJavaIdentifierPart(ch) || ch == '.')
            continue;

        break;
    }

    pos -= inc;
    //        if(pos < 0) pos = 0;
    //        if(pos >= l) pos = l-1;

    return new int[] { pos, startPos };
}

From source file:org.voltdb.compiler.DDLCompiler.java

/**
 * Checks whether or not the start of the given identifier is java (and
 * thus DDL) compliant. An identifier may start with: _ [a-zA-Z] $ *
 * and contain subsequent characters including: _ [0-9a-zA-Z] $ *
 * @param identifier the identifier to check
 * @param statement the statement where the identifier is
 * @return the given identifier unmodified
 * @throws VoltCompilerException when it is not compliant
 *//*from ww  w . java  2s  . c  om*/
private String checkIdentifierWithWildcard(final String identifier, final String statement)
        throws VoltCompilerException {

    assert identifier != null && !identifier.trim().isEmpty();
    assert statement != null && !statement.trim().isEmpty();

    int loc = 0;
    do {
        if (!Character.isJavaIdentifierStart(identifier.charAt(loc)) && identifier.charAt(loc) != '*') {
            String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                    + "\" contains invalid identifier \"" + identifier + "\"";
            throw m_compiler.new VoltCompilerException(msg);
        }
        loc++;
        while (loc < identifier.length() && identifier.charAt(loc) != '.') {
            if (!Character.isJavaIdentifierPart(identifier.charAt(loc)) && identifier.charAt(loc) != '*') {
                String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                        + "\" contains invalid identifier \"" + identifier + "\"";
                throw m_compiler.new VoltCompilerException(msg);
            }
            loc++;
        }
        if (loc < identifier.length() && identifier.charAt(loc) == '.') {
            loc++;
            if (loc >= identifier.length()) {
                String msg = "Unknown indentifier in DDL: \"" + statement.substring(0, statement.length() - 1)
                        + "\" contains invalid identifier \"" + identifier + "\"";
                throw m_compiler.new VoltCompilerException(msg);
            }
        }
    } while (loc > 0 && loc < identifier.length());

    return identifier;
}

From source file:stg.utils.StringUtils.java

/**
 * A whole word is identified as a word that does not have a valid {@link Character#isJavaIdentifierPart(char)} before or after the searchStr.
 * //from ww  w.  j a v  a2 s . co  m
 * @param text in which the search and replace is needed. 
 * @param word to be searched for
 * @param newWord to be replaced with
 * @return replaced text.
 */
public static String replaceAllWholeWord(String text, String word, String newWord) {
    StringBuilder sb = new StringBuilder(text);
    int index = 0;
    while (sb.indexOf(word, index) > -1) {
        index = sb.indexOf(word, index);
        boolean notAWholeWord = false;
        if (index > 0) {
            char c = sb.charAt(index - 1);
            if (Character.isJavaIdentifierPart(c)) {
                notAWholeWord = true;
            }
            if (index + word.length() < sb.length()) {
                c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                sb.replace(index, index + word.length(), newWord);
            }
        } else if (index == 0) {
            if (index + word.length() < sb.length()) {
                char c = sb.charAt(index + word.length());
                if (Character.isJavaIdentifierPart(c)) {
                    notAWholeWord = true;
                }
            }
            if (!notAWholeWord) {
                sb.replace(0, word.length(), newWord);
            }
        }
        index++;
    }
    return sb.toString();
}

From source file:com.attribyte.essem.util.Util.java

/**
 * Determine if the character is a valid identifier.
 * @param ch The character.// w w  w  . j a  v  a 2s .  co m
 * @return Is the character a valid identifier?
 */
public static boolean isValidIdentifier(char ch) { //TODO
    switch (ch) {
    case '-':
    case '+':
    case '!':
    case '.':
    case ',':
    case '%':
    case ':':
    case '#':
    case '^':
    case '{':
    case '}':
    case '[':
    case ']':
    case '(':
    case ')':
    case '@':
    case '/':
    case '\\':
    case '~':
    case '|':
    case ' ':
        break;
    default:
        if (!Character.isJavaIdentifierPart(ch))
            return false;
    }
    return true;
}