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:com.kstenschke.shifter.utils.UtilsTextual.java

/**
 * @param   text      Text to be analyzed
 * @param   offset      Character offset in text, intersecting the word dealing with
 * @return  int         Starting position offset of word at given offset in given CharSequence
 *//*  w  w w  .  j  a  v a 2  s  . c o  m*/
public static int getStartOfWordAtOffset(CharSequence text, int offset) {
    if (text.length() == 0)
        return 0;

    if (offset > 0 && !Character.isJavaIdentifierPart(text.charAt(offset))
            && Character.isJavaIdentifierPart(text.charAt(offset - 1))) {
        offset--;
    }

    if (Character.isJavaIdentifierPart(text.charAt(offset))) {
        int start = offset;

        while (start > 0 && Character.isJavaIdentifierPart(text.charAt(start - 1))) {
            start--;
        }

        return start;
    }

    return 0;
}

From source file:org.jboss.dashboard.factory.Component.java

/**
 * Determine if the property name is a valid property identifier
 *
 * @param propertyName Property name to evaluate
 * @return true  if the property name is a valid property identifier
 *//* w  w  w  .j  ava  2  s  .  c o m*/
protected boolean isValidPropertyName(String propertyName) {
    if (propertyName == null || propertyName.length() == 0)
        return false;
    if (propertyName.endsWith("-") || propertyName.endsWith("+"))
        propertyName = propertyName.substring(0, propertyName.length() - 1);
    if (propertyName.length() == 0)
        return false;
    for (int i = 0; i < propertyName.length(); i++) {
        char c = propertyName.charAt(i);
        if ('.' == c)
            break; //After this, it is a JXPath
        if (!Character.isJavaIdentifierPart(c))
            return false;
    }
    return true;
}

From source file:org.apache.sqoop.orm.ClassWriter.java

/**
 * Attempt to coerce a candidate name that is not valid Java or Avro into validity based on a prefix.
 *
 * This does not ensure that the result will be Java or Avro compatible if the prefix is not a valid Java or Avro
 * character./*from  ww  w .j  ava  2  s. co  m*/
 *
 * @param candidate A string we want to use as an identifier
 * @param prefix A string which is added as a prefix
 * @return A string naming an identifier which compiles and is
 *   similar to the candidate.
 */
public static String toIdentifier(String candidate, String prefix) {
    StringBuilder sb = new StringBuilder();
    // boolean first = true;
    //sb.append("_");
    boolean first = true;
    for (char c : candidate.toCharArray()) {
        if (Character.isJavaIdentifierStart(c) && first) {
            // Ok for this to be the first character of the identifier.
            sb.append(c);
            first = false;
        } else if (Character.isJavaIdentifierPart(c) && !first) {
            // Ok for this character to be in the output identifier.
            sb.append(c);
        } else {
            // We have a character in the original that can't be
            // part of this identifier we're building.
            // If it's just not allowed to be the first char, add a leading '_'.
            // If we have a reasonable translation (e.g., '-' -> '_'), do that.
            // Otherwise, drop it.
            if (first && Character.isJavaIdentifierPart(c) && !Character.isJavaIdentifierStart(c)) {
                sb.append(prefix);
                sb.append(c);
                first = false;
            } else {
                // Try to map this to a different character or string.
                // If we can't just give up.
                String translated = getIdentifierStrForChar(c);
                if (null != translated) {
                    sb.append(translated);
                    first = false;
                }
            }
        }
    }
    return sb.toString();
}

From source file:com.wavemaker.commons.util.StringUtils.java

public static String toJavaIdentifier(String s, CharSequence prefixReplacementChar, char replacementChar,
        boolean checkKeyword) {

    if (ObjectUtils.isNullOrEmpty(s)) {
        throw new IllegalArgumentException("input cannot be null or empty");
    }/*from w  ww .j a va 2  s  .  c o  m*/

    String unquoted = unquote(s);
    if (!unquoted.isEmpty()) {
        s = unquoted;
    }

    // although '$' is ok, it causes issues with type generation
    // because of inner class confusion
    s = s.replace("$", "");

    if (s.isEmpty()) {
        s = "" + replacementChar;
    }

    StringBuilder rtn = new StringBuilder();

    if ((checkKeyword && (JAVA_KEYWORDS.contains(s.toLowerCase()) || HQL_KEYWORDS.contains(s.toLowerCase())))
            || !Character.isJavaIdentifierStart(s.charAt(0))) {
        rtn.append(prefixReplacementChar);
    }

    if (s.length() == 1) {
        if (rtn.length() > 0) {
            return rtn.toString();
        } else {
            return s;
        }
    }

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (!Character.isJavaIdentifierPart(c)) {
            c = replacementChar;
        }
        rtn.append(c);
    }

    return rtn.toString();
}

From source file:org.matonto.rdf.orm.generate.SourceGenerator.java

/**
 * Simple method to strip whitespaces from the name. It will also ensure it
 * is a valid class or field name.//from  w w w. j  a va2s  .com
 *
 * @param input The input string
 * @return The stripped and cleaned output name
 */
protected static String stripWhiteSpace(final String input) {
    StringBuilder builder = new StringBuilder();
    boolean lastIsWhiteSpace = false;
    boolean first = true;
    for (char c : input.toCharArray()) {
        if (first && !Character.isJavaIdentifierStart(c) && Character.isJavaIdentifierPart(c)) {
            builder.append("_");
            builder.append(c);
            first = false;
        } else if (Character.isWhitespace(c)) {
            lastIsWhiteSpace = true;
        } else if (Character.isJavaIdentifierPart(c)) {
            builder.append(lastIsWhiteSpace ? StringUtils.capitalize(c + "") : c);
            lastIsWhiteSpace = false;
            first = false;
        }
    }
    return builder.toString();
}

From source file:netbeanstypescript.TSCodeCompletion.java

@Override
public String getPrefix(ParserResult info, int caretOffset, boolean upToOffset) {
    CharSequence seq = info.getSnapshot().getText();
    int i = caretOffset, j = i;
    while (i > 0 && Character.isJavaIdentifierPart(seq.charAt(i - 1))) {
        i--;/*  w w  w .ja va  2 s  . c  om*/
    }
    while (!upToOffset && j < seq.length() && Character.isJavaIdentifierPart(seq.charAt(j))) {
        j++;
    }
    return seq.subSequence(i, j).toString();
}

From source file:org.pentaho.reporting.designer.core.settings.prefs.BinaryPreferences.java

/**
 * Encodes the given configuration path. All non-ascii characters get replaced by an escape sequence.
 *
 * @param path the path.//from   ww  w  . j a v  a  2  s  .c  om
 * @return the translated path.
 */
private static String encodePath(final String path) {
    final char[] data = path.toCharArray();
    final StringBuffer encoded = new StringBuffer(path.length());
    for (int i = 0; i < data.length; i++) {
        if (data[i] == '$') {
            // double quote
            encoded.append('$');
            encoded.append('$');
        } else if (Character.isJavaIdentifierPart(data[i]) == false) {
            // padded hex string
            encoded.append('$');
            final String hex = Integer.toHexString(data[i]);
            for (int x = hex.length(); x < 4; x++) {
                encoded.append('0');
            }
            encoded.append(hex);
        } else {
            encoded.append(data[i]);
        }
    }
    return encoded.toString();
}

From source file:org.jactr.eclipse.ui.editor.assist.ACTRContentAssistProposer.java

private IRegion getPrefixRegion(ITextViewer viewer, int offset) throws BadLocationException {
    IDocument doc = viewer.getDocument();
    if (doc == null || offset > doc.getLength())
        return null;

    int length = 0;
    char current = 0;
    while (--offset >= 0 && (current = doc.getChar(offset)) != 0
            && (Character.isJavaIdentifierPart(current) || current == '-' || current == '='))
        length++;// ww w .  j  av  a2  s . c o  m

    return new Region(offset + 1, length);
}

From source file:org.apache.airavata.common.utils.StringUtil.java

/**
 * Converts a specified string to a Java identifier.
 * /*  w w  w . j a va  2  s .co  m*/
 * @param name
 * @return the Java identifier
 */
public static String convertToJavaIdentifier(String name) {

    final char REPLACE_CHAR = '_';

    if (name == null || name.length() == 0) {
        return "" + REPLACE_CHAR;
    }

    StringBuilder buf = new StringBuilder();

    char c = name.charAt(0);
    if (!Character.isJavaIdentifierStart(c)) {
        // Add _ at the beggining instead of replacing it to _. This is
        // more readable if the name is like 3D_Model.
        buf.append(REPLACE_CHAR);
    }

    for (int i = 0; i < name.length(); i++) {
        c = name.charAt(i);
        if (Character.isJavaIdentifierPart(c)) {
            buf.append(c);
        } else {
            buf.append(REPLACE_CHAR);
        }
    }

    return buf.toString();
}