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.camel.util.ObjectHelper.java

/**
 * Returns true if the given name is a valid java identifier
 */// ww  w  .  ja  v  a 2s .  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 va  2s  .  c o m
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.apache.camel.util.ObjectHelper.java

/**
 * Cleans the string to pure java identifier so we can use it for loading class names.
 * <p/>//from  w w  w  . ja v a2s . c  o m
 * Especially from Spring DSL people can have \n \t or other characters that otherwise
 * would result in ClassNotFoundException
 *
 * @param name the class name
 * @return normalized classname that can be load by a class loader.
 */
public static String normalizeClassName(String name) {
    StringBuilder sb = new StringBuilder(name.length());
    for (char ch : name.toCharArray()) {
        if (ch == '.' || ch == '[' || ch == ']' || ch == '-' || Character.isJavaIdentifierPart(ch)) {
            sb.append(ch);
        }
    }
    return sb.toString();
}

From source file:co.id.app.sys.util.StringUtils.java

/**
 * Return a field name string from the given field label.
 * <p/>/*from  w w  w . ja  v a 2 s .  c om*/
 * A label of " OK do it!" is returned as "okDoIt". Any &amp;nbsp;
 * characters will also be removed.
 * <p/>
 * A label of "customerSelect" is returned as "customerSelect".
 *
 * @param label the field label or caption
 * @return a field name string from the given field label
 */
public static String toName(String label) {
    if (label == null) {
        throw new IllegalArgumentException("Null label parameter");
    }

    boolean doneFirstLetter = false;
    boolean lastCharBlank = false;
    boolean hasWhiteSpace = (label.indexOf(' ') != -1);

    HtmlStringBuffer buffer = new HtmlStringBuffer(label.length());
    for (int i = 0, size = label.length(); i < size; i++) {
        char aChar = label.charAt(i);

        if (aChar != ' ') {
            if (Character.isJavaIdentifierPart(aChar)) {
                if (lastCharBlank) {
                    if (doneFirstLetter) {
                        buffer.append(Character.toUpperCase(aChar));
                        lastCharBlank = false;
                    } else {
                        buffer.append(Character.toLowerCase(aChar));
                        lastCharBlank = false;
                        doneFirstLetter = true;
                    }
                } else {
                    if (doneFirstLetter) {
                        if (hasWhiteSpace) {
                            buffer.append(Character.toLowerCase(aChar));
                        } else {
                            buffer.append(aChar);
                        }
                    } else {
                        buffer.append(Character.toLowerCase(aChar));
                        doneFirstLetter = true;
                    }
                }
            }
        } else {
            lastCharBlank = true;
        }
    }

    return buffer.toString();
}

From source file:org.mortbay.jetty.webapp.WebAppContext.java

/**
 * Create a canonical name for a webapp tmp directory.
 * The form of the name is:/*from   w ww.j a  v  a 2 s.com*/
 *  "Jetty_"+host+"_"+port+"__"+resourceBase+"_"+context+"_"+virtualhost+base36 hashcode of whole string
 *  
 *  host and port uniquely identify the server
 *  context and virtual host uniquely identify the webapp
 * @return
 */
private String getCanonicalNameForWebAppTmpDir() {
    StringBuffer canonicalName = new StringBuffer();
    canonicalName.append("Jetty");

    //get the host and the port from the first connector 
    Connector[] connectors = getServer().getConnectors();

    //Get the host
    canonicalName.append("_");
    String host = (connectors == null || connectors[0] == null ? "" : connectors[0].getHost());
    if (host == null)
        host = "0.0.0.0";
    canonicalName.append(host.replace('.', '_'));

    //Get the port
    canonicalName.append("_");
    //try getting the real port being listened on
    int port = (connectors == null || connectors[0] == null ? 0 : connectors[0].getLocalPort());
    //if not available (eg no connectors or connector not started), 
    //try getting one that was configured.
    if (port < 0)
        port = connectors[0].getPort();
    canonicalName.append(port);

    //Resource  base
    canonicalName.append("_");
    try {
        Resource resource = super.getBaseResource();
        if (resource == null) {
            if (_war == null || _war.length() == 0)
                resource = Resource.newResource(getResourceBase());

            // Set dir or WAR
            resource = Resource.newResource(_war);
        }

        String tmp = URIUtil.decodePath(resource.getURL().getPath());
        if (tmp.endsWith("/"))
            tmp = tmp.substring(0, tmp.length() - 1);
        if (tmp.endsWith("!"))
            tmp = tmp.substring(0, tmp.length() - 1);
        //get just the last part which is the filename
        int i = tmp.lastIndexOf("/");

        canonicalName.append(tmp.substring(i + 1, tmp.length()));
    } catch (Exception e) {
        Log.warn("Can't generate resourceBase as part of webapp tmp dir name", e);
    }

    //Context name
    canonicalName.append("_");
    String contextPath = getContextPath();
    contextPath = contextPath.replace('/', '_');
    contextPath = contextPath.replace('\\', '_');
    canonicalName.append(contextPath);

    //Virtual host (if there is one)
    canonicalName.append("_");
    String[] vhosts = getVirtualHosts();
    canonicalName.append((vhosts == null || vhosts[0] == null ? "" : vhosts[0]));

    //base36 hash of the whole string for uniqueness
    String hash = Integer.toString(canonicalName.toString().hashCode(), 36);
    canonicalName.append("_");
    canonicalName.append(hash);

    // sanitize
    for (int i = 0; i < canonicalName.length(); i++) {
        char c = canonicalName.charAt(i);
        if (!Character.isJavaIdentifierPart(c))
            canonicalName.setCharAt(i, '.');
    }

    return canonicalName.toString();
}

From source file:org.openecomp.sdnc.uebclient.SdncUebCallback.java

private String escapeFilename(String str) {
    StringBuffer retval = new StringBuffer();

    for (int i = 0; i < str.length(); i++) {
        char curchar = str.charAt(i);
        if (Character.isJavaIdentifierPart(curchar)) {
            retval.append(curchar);/* w  w  w . j av a  2  s .  c om*/
        }
    }

    return (retval.toString());

}

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

/**
 * Converts the given string to a Java valid identifier.
 *
 * @param str string to process//  www . j a va  2 s.  co m
 * @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;
}

From source file:org.apache.openjpa.jdbc.meta.ReverseMappingTool.java

/**
 * Replace characters not allowed in Java names with an underscore;
 * package-private for testing.//from  w  w  w. java2s  . c om
 */
static String replaceInvalidCharacters(String str) {
    if (StringUtils.isEmpty(str))
        return str;

    StringBuilder buf = new StringBuilder(str);
    char c;
    for (int i = 0; i < buf.length(); i++) {
        c = buf.charAt(i);
        if (c == '$' || !Character.isJavaIdentifierPart(str.charAt(i)))
            buf.setCharAt(i, '_');
    }

    // strip leading and trailing underscores
    int start = 0;
    while (start < buf.length() && buf.charAt(start) == '_')
        start++;
    int end = buf.length() - 1;
    while (end >= 0 && buf.charAt(end) == '_')
        end--;

    // possible that all chars in name are invalid
    if (start > end)
        return "x";
    return buf.substring(start, end + 1);
}

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

/**
 * Checks if the specified string is a valid java identifier.
 *
 * @param str <i>(required)</i>.
 * @return Returns <code>true</code> iff the specified string is a valid java
 *         identifier./*from   www .  j av a 2s .  c  o m*/
 */
public static boolean isJavaIdentifier(String str) {
    boolean valid = (str != null) && (str.length() > 0);
    if (valid) {
        char c = str.charAt(0);
        valid = Character.isJavaIdentifierStart(c);
        for (int i = str.length(); valid && (--i >= 1);) {
            valid = Character.isJavaIdentifierPart(c);
        }
    }
    return valid && !isJavaKeyword(str);
}

From source file:jp.co.acroquest.jsonic.JSON.java

private Object parseLiteral(Context context, InputSource s, int level, boolean any)
        throws IOException, JSONException {
    int point = 0; // 0 'IdStart' 1 'IdPart' ... !'IdPart' E
    StringBuilder sb = context.getCachedBuffer();

    int n = -1;// w  w  w  .  j  a  va 2 s . c  om
    loop: while ((n = s.next()) != -1) {
        char c = (char) n;
        if (c == 0xFEFF)
            continue;

        if (c == '\\') {
            s.back();
            c = parseEscape(s);
        }

        if (point == 0 && Character.isJavaIdentifierStart(c)) {
            sb.append(c);
            point = 1;
        } else if (point == 1 && (Character.isJavaIdentifierPart(c) || c == '.')) {
            sb.append(c);
        } else {
            s.back();
            break loop;
        }
    }

    String str = sb.toString();

    if ("null".equals(str))
        return null;
    if ("true".equals(str))
        return true;
    if ("false".equals(str))
        return false;

    if (!any) {
        throw createParseException(getMessage("json.parse.UnrecognizedLiteral", str), s);
    }

    return str;
}