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: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"
 * //ww  w.ja  v a 2s . 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.
 * /*www.  j  a  v  a2 s. c  o m*/
 * @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>
 * //from w ww . j  av a 2 s.c  om
 * @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: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 w w w .  ja v a  2  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.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
 *///from   www .ja  va2 s .  c  o  m
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./*from w w  w . ja v a2 s .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.apache.camel.util.ObjectHelper.java

/**
 * Returns true if the given name is a valid java identifier
 *///from w w  w.j a va2 s .c  om
public static boolean isJavaIdentifier(String name) {
    if (name == null) {
        return false;
    }
    int size = name.length();
    if (size < 1) {
        return false;
    }
    if (Character.isJavaIdentifierStart(name.charAt(0))) {
        for (int i = 1; i < size; i++) {
            if (!Character.isJavaIdentifierPart(name.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    return false;
}

From source file:org.rdkit.knime.wizards.RDKitNodesWizardsPage.java

/**
 * Returns the specified node name to be used as class name (stump without any spaces).
 *
 * @param bPrependRdkit Set to true to prepend "RDKit ".
 *
 * @return Specified node name. Empty string, if undefined.
 *//*w  w w  .  j a v a2 s . com*/
private String getNodeClassName(final boolean bPrependRdkit) {
    String strEnteredName = getNodeName().trim();
    StringBuilder sbClassName = new StringBuilder();
    boolean bMakeUpperCase = true;

    for (char ch : strEnteredName.toCharArray()) {
        if (sbClassName.length() == 0 && Character.isJavaIdentifierStart(ch)) {
            // Make the first character upper case anyway
            sbClassName.append(Character.toUpperCase(ch));
            bMakeUpperCase = false;
        } else if (sbClassName.length() > 0 && Character.isJavaIdentifierPart(ch)) {
            // Make a character upper case only, if a character was found before that was eliminated
            if (bMakeUpperCase) {
                sbClassName.append(Character.toUpperCase(ch));
                bMakeUpperCase = false;
            } else {
                sbClassName.append(ch);
            }
        } else {
            // Eliminate and make next character upper case
            bMakeUpperCase = true;
        }
    }

    String strClassName = sbClassName.toString();

    // Prepend RDKit to the class identifier
    if (bPrependRdkit && !strClassName.toLowerCase().startsWith("rdkit")) {
        strClassName = "RDKit" + strClassName;
    }

    return strClassName;
}

From source file:org.opentravel.schemacompiler.repository.RepositoryFileManager.java

/**
 * Returns the given URI path component as a legal folder name for the OTA2.0 repository.
 * //from   w  w w. ja  v a  2s .  com
 * @param uriComponent
 *            the URI component string to convert
 * @return String
 */
protected String toFolderName(String uriComponent) {
    if ((uriComponent == null) || (uriComponent.length() == 0)) {
        return null;

    }
    String folderName = uriComponent.toLowerCase();

    if (!Character.isJavaIdentifierStart(folderName.charAt(0))) {
        folderName = "_" + folderName;
    }
    return folderName;
}

From source file:org.jboss.dashboard.commons.text.StringUtil.java

/**
 * Converts the given string to a Java valid identifier.
 *
 * @param str string to process//ww w .  j a  v  a 2  s .c om
 * @return A java based identifier.
 */
public static String toJavaIdentifier(String str) {
    if (str == null || str.trim().equals(""))
        return null;
    StringBuffer buf = new StringBuffer(str);
    int bufIdx = 0;
    while (bufIdx < buf.length()) {
        char c = buf.charAt(bufIdx);

        // Replace tilded by non-tilded chars.
        int tilded = TILDED_CHARS.indexOf(c);
        if (tilded != -1 && tilded < NON_TILDED_CHARS.length()) {
            buf.deleteCharAt(bufIdx);
            c = NON_TILDED_CHARS.charAt(tilded);
            buf.insert(bufIdx++, c);
            continue;
        }
        // Discard special chars and non-valid java identifiers.
        int special = SPECIAL_CHARS.indexOf(c);
        if (special != -1 || !Character.isJavaIdentifierPart(c)) {
            buf.deleteCharAt(bufIdx);
            continue;
        }
        // Adjust buffer index.
        bufIdx++;
    }
    if (buf.length() == 0)
        return "";
    while (buf.length() > 0 && !Character.isJavaIdentifierStart(buf.charAt(0)))
        buf.deleteCharAt(0);

    // Avoid reserved java keywords.
    String javaId = buf.toString();
    if (isJavaKeyword(javaId)) {
        if (javaId.equals("class"))
            javaId = "clazz";
        else
            javaId = '_' + javaId;
    }
    return javaId;
}