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.nextep.designer.sqlgen.ui.editors.sql.SQLCompletionProcessor.java

/**
 * Retrieves the prefix from the given offset
 * //from w  w  w . ja va2 s  .  co  m
 * @param viewer text viewer
 * @param offset offset at which the content assist has been triggerred
 * @return the string prefix
 * @throws BadLocationException
 */
private int getPrefixStart(ITextViewer viewer, int offset) {
    IDocument doc = viewer.getDocument();
    if (doc == null || offset > doc.getLength())
        return -1;

    int length = 0;
    try {
        while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset)))
            length++;

        return offset + 1;
    } catch (BadLocationException e) {
        LOGGER.debug("Error while retrieving completion prefix: BadLocation.");
        return -1;
    }

}

From source file:org.apache.axis.utils.JavaUtils.java

/**
 * Map an XML name to a Java identifier per
 * the mapping rules of JSR 101 (in version 1.0 this is
 * "Chapter 20: Appendix: Mapping of XML Names"
 * /* w w  w. j a v  a2 s . co  m*/
 * @param name is the xml name
 * @return the java name per JSR 101 specification
 */
public static String xmlNameToJava(String name) {
    // protect ourselves from garbage
    if (name == null || name.equals(""))
        return name;

    char[] nameArray = name.toCharArray();
    int nameLen = name.length();
    StringBuffer result = new StringBuffer(nameLen);
    boolean wordStart = false;

    // The mapping indicates to convert first character.
    int i = 0;
    while (i < nameLen && (isPunctuation(nameArray[i]) || !Character.isJavaIdentifierStart(nameArray[i]))) {
        i++;
    }
    if (i < nameLen) {
        // Decapitalization code used to be here, but we use the
        // Introspector function now after we filter out all bad chars.

        result.append(nameArray[i]);
        //wordStart = !Character.isLetter(nameArray[i]);
        wordStart = !Character.isLetter(nameArray[i]) && nameArray[i] != "_".charAt(0);
    } else {
        // The identifier cannot be mapped strictly according to
        // JSR 101
        if (Character.isJavaIdentifierPart(nameArray[0])) {
            result.append("_" + nameArray[0]);
        } else {
            // The XML identifier does not contain any characters
            // we can map to Java.  Using the length of the string
            // will make it somewhat unique.
            result.append("_" + nameArray.length);
        }
    }

    // The mapping indicates to skip over
    // all characters that are not letters or
    // digits.  The first letter/digit
    // following a skipped character is
    // upper-cased.
    for (++i; i < nameLen; ++i) {
        char c = nameArray[i];

        // if this is a bad char, skip it and remember to capitalize next
        // good character we encounter
        if (isPunctuation(c) || !Character.isJavaIdentifierPart(c)) {
            wordStart = true;
            continue;
        }
        if (wordStart && Character.isLowerCase(c)) {
            result.append(Character.toUpperCase(c));
        } else {
            result.append(c);
        }
        // If c is not a character, but is a legal Java
        // identifier character, capitalize the next character.
        // For example:  "22hi" becomes "22Hi"
        //wordStart = !Character.isLetter(c);
        wordStart = !Character.isLetter(c) && c != "_".charAt(0);
    }

    // covert back to a String
    String newName = result.toString();

    // Follow JavaBean rules, but we need to check if the first 
    // letter is uppercase first
    if (Character.isUpperCase(newName.charAt(0)))
        newName = Introspector.decapitalize(newName);

    // check for Java keywords
    if (isJavaKeyword(newName))
        newName = makeNonJavaKeyword(newName);

    return newName;
}

From source file:org.gvnix.service.roo.addon.addon.util.WsdlParserUtils.java

/**
 * Returns true if the name is a valid java identifier.
 * //from w w  w .ja va 2s  .  c om
 * @param id to check
 * @return boolean true/false
 **/
public static boolean isJavaId(String id) {

    if (id == null || id.equals("") || isJavaKeyword(id))
        return false;
    if (!Character.isJavaIdentifierStart(id.charAt(0)))
        return false;
    for (int i = 1; i < id.length(); i++)
        if (!Character.isJavaIdentifierPart(id.charAt(i)))
            return false;

    return true;
}

From source file:org.opencms.util.CmsStringUtil.java

/**
 * Checks if the given class name is a valid Java class name.<p>
 * //  w  w w . jav a2 s.  c o  m
 * @param className the name to check
 * 
 * @return true if the given class name is a valid Java class name
 */
public static boolean isValidJavaClassName(String className) {

    if (CmsStringUtil.isEmpty(className)) {
        return false;
    }
    int length = className.length();
    boolean nodot = true;
    for (int i = 0; i < length; i++) {
        char ch = className.charAt(i);
        if (nodot) {
            if (ch == '.') {
                return false;
            } else if (Character.isJavaIdentifierStart(ch)) {
                nodot = false;
            } else {
                return false;
            }
        } else {
            if (ch == '.') {
                nodot = true;
            } else if (Character.isJavaIdentifierPart(ch)) {
                nodot = false;
            } else {
                return false;
            }
        }
    }
    return true;
}

From source file:com.nextep.designer.sqlgen.ui.editors.sql.SQLCompletionProcessor.java

/**
 * Retrieves the last word immediately before the specified offset in the text viewer. Any space
 * will be ignored./*from ww  w  . ja v a  2 s  .co m*/
 * 
 * @param viewer viewer to look into
 * @param offset offset to start the search from.
 * @return the last word.
 */
private String getLastWord(ITextViewer viewer, int offset) {
    IDocument doc = viewer.getDocument();
    if (doc == null || offset > doc.getLength())
        return ""; //$NON-NLS-1$

    int length = 0;
    try {
        boolean wordStarted = false;
        while (--offset >= 0
                && (!wordStarted || wordStarted && Character.isJavaIdentifierPart(doc.getChar(offset)))) {
            // We continue on spaces
            if (' ' == doc.getChar(offset)) {
                continue;
            } else if (',' == doc.getChar(offset)) {
                int lastWordStart = getPrefixStart(viewer, offset - 1);
                return getLastWord(viewer, lastWordStart);
            } else if (!wordStarted) {
                wordStarted = true;
            }
            length++;
        }

        return doc.get(offset + 1, length);
    } catch (BadLocationException e) {
        LOGGER.debug("Error while retrieving completion prefix: BadLocation.");
        return ""; //$NON-NLS-1$
    }
}

From source file:org.gvnix.service.roo.addon.addon.util.WsdlParserUtils.java

/**
 * Map an XML name to a Java identifier per the mapping rules of JSR 101 (in
 * version 1.0 this is "Chapter 20: Appendix: Mapping of XML Names"
 * //from   ww  w  .  ja v  a  2 s .  com
 * @param name is the xml name
 * @return the java name per JSR 101 specification
 */
public static String xmlNameToJava(String name) {

    // protect ourselves from garbage
    if (name == null || name.equals(""))
        return name;

    char[] nameArray = name.toCharArray();
    int nameLen = name.length();
    StringBuffer result = new StringBuffer(nameLen);
    boolean wordStart = false;

    // The mapping indicates to convert first character.
    int i = 0;
    while (i < nameLen && (isPunctuation(nameArray[i]) || !Character.isJavaIdentifierStart(nameArray[i]))) {
        i++;
    }
    if (i < nameLen) {
        // Decapitalization code used to be here, but we use the
        // Introspector function now after we filter out all bad chars.

        result.append(nameArray[i]);
        // wordStart = !Character.isLetter(nameArray[i]);
        wordStart = !Character.isLetter(nameArray[i]) && nameArray[i] != "_".charAt(0);
    } else {
        // The identifier cannot be mapped strictly according to
        // JSR 101
        if (Character.isJavaIdentifierPart(nameArray[0])) {
            result.append("_" + nameArray[0]);
        } else {
            // The XML identifier does not contain any characters
            // we can map to Java. Using the length of the string
            // will make it somewhat unique.
            result.append("_" + nameArray.length);
        }
    }

    // The mapping indicates to skip over
    // all characters that are not letters or
    // digits. The first letter/digit
    // following a skipped character is
    // upper-cased.
    for (++i; i < nameLen; ++i) {
        char c = nameArray[i];

        // if this is a bad char, skip it and remember to capitalize next
        // good character we encounter
        if (isPunctuation(c) || !Character.isJavaIdentifierPart(c)) {
            wordStart = true;
            continue;
        }
        if (wordStart && Character.isLowerCase(c)) {
            result.append(Character.toUpperCase(c));
        } else {
            result.append(c);
        }
        // If c is not a character, but is a legal Java
        // identifier character, capitalize the next character.
        // For example: "22hi" becomes "22Hi"
        // wordStart = !Character.isLetter(c);
        wordStart = !Character.isLetter(c) && c != "_".charAt(0);
    }

    // covert back to a String
    String newName = result.toString();

    // Follow JavaBean rules, but we need to check if the first
    // letter is uppercase first
    if (Character.isUpperCase(newName.charAt(0)))
        newName = Introspector.decapitalize(newName);

    // check for Java keywords
    if (isJavaKeyword(newName))
        newName = makeNonJavaKeyword(newName);

    return newName;
}

From source file:com.amazon.carbonado.repo.jdbc.JDBCStorableIntrospector.java

/**
 * Generates aliases for the given name, converting camel case form into
 * various underscore forms./*from w w  w  . ja va  2  s .c  om*/
 */
static String[] generateAliases(String base) {
    int length = base.length();
    if (length <= 1) {
        return new String[] { base.toUpperCase(), base.toLowerCase() };
    }

    ArrayList<String> aliases = new ArrayList<String>(4);

    StringBuilder buf = new StringBuilder();

    int i;
    for (i = 0; i < length;) {
        char c = base.charAt(i++);
        if (c == '_' || !Character.isJavaIdentifierPart(c)) {
            // Keep scanning for first letter.
            buf.append(c);
        } else {
            buf.append(Character.toUpperCase(c));
            break;
        }
    }

    boolean canSeparate = false;
    boolean appendedIdentifierPart = false;

    for (; i < length; i++) {
        char c = base.charAt(i);
        if (c == '_' || !Character.isJavaIdentifierPart(c)) {
            canSeparate = false;
            appendedIdentifierPart = false;
        } else if (Character.isLowerCase(c)) {
            canSeparate = true;
            appendedIdentifierPart = true;
        } else {
            if (appendedIdentifierPart && i + 1 < length && Character.isLowerCase(base.charAt(i + 1))) {
                canSeparate = true;
            }
            if (canSeparate) {
                buf.append('_');
            }
            canSeparate = false;
            appendedIdentifierPart = true;
        }
        buf.append(c);
    }

    String derived = buf.toString();

    addToSet(aliases, derived.toUpperCase());
    addToSet(aliases, derived.toLowerCase());
    addToSet(aliases, derived);
    addToSet(aliases, base.toUpperCase());
    addToSet(aliases, base.toLowerCase());
    addToSet(aliases, base);

    return aliases.toArray(new String[aliases.size()]);
}

From source file:org.apache.jasper.compiler.JspUtil.java

/**
 * Converts the given identifier to a legal Java identifier
 *
 * @param identifier Identifier to convert
 *
 * @return Legal Java identifier corresponding to the given identifier
 *///w w  w .j a v a2 s  . c om
public static final String makeJavaIdentifier(String identifier) {
    StringBuffer modifiedIdentifier = new StringBuffer(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.rdkit.knime.wizards.RDKitNodesWizardsPage.java

/**
 * Validates the page, e.g. checks whether the text fields contain valid
 * values./*  w ww .j a  v a  2s  .com*/
 *
 * @return Returns true, if all information on page is correct. False otherwise.
 */
protected boolean validatePage() {
    // Check existing project setting
    if (getProjectName().trim().equals("")) { //$NON-NLS-1$
        setErrorMessage(null);
        setMessage("Please select an existing project.");
        return false;
    }

    // Check the node name
    String nodeName = m_textNodeName.getText();
    if (nodeName.trim().isEmpty()) {
        setErrorMessage(null);
        setMessage("Please provide a valid node name.");
        return false;
    }
    if ((!Character.isLetter(nodeName.charAt(0))) || (nodeName.charAt(0) != nodeName.toUpperCase().charAt(0))) {
        setErrorMessage("The node name must start with an uppercase letter.");
        return false;
    }

    String strClassName = getNodeClassName();
    for (int i = 0; i < strClassName.length(); i++) {
        char c = strClassName.charAt(i);
        if (!(i == 0 && Character.isJavaIdentifierStart(c)) && !(i > 0 && Character.isJavaIdentifierPart(c))) {
            setErrorMessage("The class name '" + strClassName + "' is invalid.");
            return false;
        }
    }

    // Check package name
    String basePackage = m_textBasePackage.getText();
    if (basePackage.length() == 0) {
        setErrorMessage(null);
        setMessage("Please provide a package name.");
        return false;
    }
    for (int i = 0; i < basePackage.length(); i++) {
        char c = basePackage.charAt(i);
        if (!(Character.isLowerCase(c) || Character.isDigit(c) || c == '.' || c == '_')) {
            setErrorMessage("The package name '" + basePackage + "' is invalid.");
            return false;
        }
    }

    // Check for existing classes (naming conflict?)
    IProject project = RDKitNodesWizards.getProjectForName(getProjectName());
    String path = "src/" + m_textBasePackage.getText().trim().replace('.', '/') + "/" + nodeName;
    IFile file = project.getFile(new Path(path + "NodeModel.java"));
    if (file.exists()) {
        setErrorMessage("A node with the given name exists already. Please provide another name or package.");
        return false;
    }

    // Check percentages for pre- and post processing
    try {
        double dPrePerc = getPreProcessingPercentage();
        double dPostPerc = getPostProcessingPercentage();

        if (dPrePerc + dPostPerc > 1.0d) {
            setErrorMessage("The total of pre and post processing activities cannot be greater than 100%.");
            return false;
        }
    } catch (NumberFormatException exc) {
        setErrorMessage("Bad number format: " + exc.getMessage());
        return false;
    }

    // Everything is ok so far
    setErrorMessage(null);
    setMessage(null);
    return true;
}

From source file:org.python.pydev.core.docutils.StringUtils.java

/**
 * Tests whether each character in the given
 * string is a letter.//w  w  w .ja v  a  2s. c o  m
 *
 * @param str
 * @return <code>true</code> if the given string is a word
 */
public static boolean isWord(final String str) {
    int len = str.length();
    if (str == null || len == 0)
        return false;

    for (int i = 0; i < len; i++) {
        if (!Character.isJavaIdentifierPart(str.charAt(i)))
            return false;
    }
    return true;
}