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.netspective.commons.text.TextUtils.java

/**
 * Given a text string, return a string that would be suitable for that string to be used
 * as a Java constant (public static final XXX). The rule is to basically take every letter
 * or digit and return it in uppercase and every non-letter or non-digit as an underscore.
 * This trims all non-letter/digit characters from the beginning of the string.
 *//*from   w w w  .  ja va 2  s . c o  m*/
public String xmlTextToJavaConstantTrimmed(String xml) {
    if (xml == null || xml.length() == 0)
        return xml;

    boolean stringStarted = false;
    StringBuffer constant = new StringBuffer();
    for (int i = 0; i < xml.length(); i++) {
        char ch = xml.charAt(i);
        if (Character.isJavaIdentifierPart(ch)) {
            stringStarted = true;
            constant.append(Character.toUpperCase(ch));
        } else if (stringStarted)
            constant.append('_');
    }
    return constant.toString();
}

From source file:org.dhatim.edisax.util.EDIUtils.java

public static String encodeJavaIdentifier(String identifier) {
    StringBuilder result = new StringBuilder();
    int len = identifier.length();
    boolean matchPrevious = false;
    char currentChar;

    for (int i = 0; i < len; i++) {
        currentChar = identifier.charAt(i);

        if (i == 0 && !Character.isJavaIdentifierStart(currentChar)) {
            result.append('_');
        }/*from www .  j  av  a  2  s. c o  m*/
        if (!Character.isJavaIdentifierPart(currentChar)) {
            matchPrevious = true;
            continue;
        }
        if (matchPrevious) {
            currentChar = Character.toUpperCase(currentChar);
            matchPrevious = false;
        }
        result.append(currentChar);
    }
    return result.toString();
}

From source file:com.agloco.util.StringUtil.java

public static String replace(String template, String placeholder, String replacement, boolean wholeWords) {
    int loc = template.indexOf(placeholder);
    if (loc < 0) {
        return template;
    } else {/* w w w  . j  av a2 s .co m*/
        final boolean actuallyReplace = !wholeWords || loc + placeholder.length() == template.length()
                || !Character.isJavaIdentifierPart(template.charAt(loc + placeholder.length()));
        String actualReplacement = actuallyReplace ? replacement : placeholder;
        return new StringBuffer(template.substring(0, loc)).append(actualReplacement).append(
                replace(template.substring(loc + placeholder.length()), placeholder, replacement, wholeWords))
                .toString();
    }
}

From source file:ch.randelshofer.cubetwister.HTMLExporter.java

/**
 * Converts a String, so that it only contains lower case ASCII characters.
 *///from   w  w w. j av a 2  s.c  om
private String toID(String str) {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < str.length(); i++) {
        char ch = Character.toLowerCase(str.charAt(i));
        if (Character.isJavaIdentifierPart(ch)) {
            buf.append(ch);
        } else if (Character.isWhitespace(ch)) {
            buf.append('_');
        }
    }
    return buf.toString();
}

From source file:org.gcaldaemon.core.ldap.ContactLoader.java

private final String saveVCard(GmailContact contact, String rev) {
    String name = contact.email.toLowerCase();
    if (name.length() == 0) {
        name = contact.name.toLowerCase();
    }/* w  w  w.j  a  va2 s .  co m*/
    name = name.trim();
    if (name.length() == 0 || name.indexOf('=') != -1) {
        return VCARD_EXTENSION;
    }
    char[] chars = name.toCharArray();
    QuickWriter writer = new QuickWriter(chars.length);
    boolean writeMinus = true;
    char c;
    int i;
    for (i = 0; i < chars.length; i++) {
        c = chars[i];
        if (c != '_' && Character.isJavaIdentifierPart(c)) {
            writer.write(c);
            writeMinus = true;
            continue;
        }
        if (c == ',') {
            break;
        }
        if (writeMinus) {
            writer.write('-');
            writeMinus = false;
        }
    }
    name = writer.toString() + VCARD_EXTENSION;
    File file = new File(vcardDirectory, name);
    FileOutputStream out = null;
    try {
        writer = new QuickWriter(500);
        String encoding = StringUtils.UTF_8;
        String displayName = contact.name;
        if (displayName.length() == 0) {
            return VCARD_EXTENSION;
        }
        String firstName = null;
        String lastName = null;
        i = displayName.indexOf(' ');
        if (i != -1) {
            firstName = displayName.substring(0, i);
            lastName = displayName.substring(i + 1);
        }

        // Write vCard
        writer.write("BEGIN:VCARD\r\n");
        if (vcardVersion.charAt(0) == '3') {
            writer.write("VERSION:");
            writer.write(vcardVersion);
            writer.write("\r\nPRODID:");
            writer.write(Configurator.VERSION);
            writer.write("\r\n");
        } else {
            writer.write("VERSION:2.1\r\n");
        }
        switch (vcardEncoding) {
        case VCARD_UTF8_ENCODING:

            // Pure UTF8 vCard format
            writer.write("X-LOTUS-CHARSET:UTF-8\r\n");
            writer.write("FN;CHARSET=UTF-8:");
            writer.write(displayName);
            if (firstName != null) {

                // Name
                writer.write("\r\nN;CHARSET=UTF-8:");
                writer.write(firstName);
                writer.write(';');
                writer.write(lastName);
                writer.write(";;;");
            }
            if (contact.notes.length() != 0) {

                // Notes
                writer.write("\r\nNOTE;CHARSET=UTF-8:");
                writer.write(contact.notes);
            }
            if (contact.address.length() != 0) {

                // Address
                if (vcardVersion.charAt(0) == '3') {
                    writer.write("\r\nADR;TYPE=HOME;CHARSET=UTF-8:");
                } else {
                    writer.write("\r\nADR;HOME;CHARSET=UTF-8:");
                }
                writer.write(contact.address);
            }
            break;
        case VCARD_NATIVE_ENCODING:

            // Native vCard format
            encoding = NATIVE_CHARSET;
            writer.write("X-LOTUS-CHARSET:");
            writer.write(NATIVE_CHARSET);
            writer.write("\r\nFN:");
            writer.write(displayName);
            i = displayName.indexOf(' ');
            if (firstName != null) {

                // Name
                writer.write("\r\nN:");
                writer.write(firstName);
                writer.write(';');
                writer.write(lastName);
            }
            if (contact.notes.length() != 0) {

                // Notes
                writer.write("\r\nNOTE:");
                writer.write(contact.notes);
            }
            if (contact.address.length() != 0) {

                // Address
                if (vcardVersion.charAt(0) == '3') {
                    writer.write("\r\nADR;TYPE=HOME:");
                } else {
                    writer.write("\r\nADR;HOME:");
                }
                writer.write(contact.address);
            }
            break;
        default:

            // Quoted-printable vCard format
            encoding = StringUtils.US_ASCII;
            writer.write("X-LOTUS-CHARSET:UTF-8\r\n");
            writer.write("FN;QUOTED-PRINTABLE:");
            writer.write(encodeQuotedPrintable(displayName));
            i = displayName.indexOf(' ');
            if (firstName != null) {

                // Name
                writer.write("\r\nN;QUOTED-PRINTABLE:");
                writer.write(encodeQuotedPrintable(firstName));
                writer.write(';');
                writer.write(encodeQuotedPrintable(lastName));
            }
            if (contact.notes.length() != 0) {

                // Notes
                writer.write("\r\nNOTE;QUOTED-PRINTABLE:");
                writer.write(encodeQuotedPrintable(contact.notes));
            }
            if (contact.address.length() != 0) {

                // Address
                if (vcardVersion.charAt(0) == '3') {
                    writer.write("\r\nADR;TYPE=HOME;QUOTED-PRINTABLE:");
                } else {
                    writer.write("\r\nADR;HOME;QUOTED-PRINTABLE:");
                }
                writer.write(encodeQuotedPrintable(contact.address));
            }
        }
        if (contact.email.length() != 0) {

            // Default email
            if (vcardVersion.charAt(0) == '3') {
                writer.write("\r\nEMAIL;TYPE=PREF;TYPE=INTERNET:");
            } else {
                writer.write("\r\nEMAIL;PREF;INTERNET:");
            }
            writer.write(contact.email);
        }
        if (contact.mail.length() != 0) {

            // Additional email
            if (vcardVersion.charAt(0) == '3') {
                writer.write("\r\nEMAIL;TYPE=INTERNET:");
            } else {
                writer.write("\r\nEMAIL;INTERNET:");
            }
            writer.write(contact.mail);
        }
        if (contact.phone.length() != 0) {

            // Phone number
            if (vcardVersion.charAt(0) == '3') {
                writer.write("\r\nTEL;TYPE=HOME:");
            } else {
                writer.write("\r\nTEL;HOME:");
            }
            writer.write(contact.phone);
        }
        writer.write("\r\nREV:");
        writer.write(rev);
        writer.write("\r\nEND:VCARD\r\n");
        byte[] bytes;
        if (encoding.equals(StringUtils.US_ASCII)) {
            bytes = writer.getBytes();
        } else {
            bytes = StringUtils.encodeString(writer.toString(), encoding);
        }
        for (int retries = 0;; retries++) {
            try {
                out = new FileOutputStream(file);
                out.write(bytes);
                out.flush();
                out.close();
                out = null;
                break;
            } catch (Exception lockedError) {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception ignored) {
                    }
                    out = null;
                }
                if (retries == 5) {
                    throw lockedError;
                }
                Thread.sleep(500);
            }
        }
    } catch (Exception ioError) {
        log.warn(ioError);
        if (file != null) {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception ignored) {
                }
            }
            file.delete();
        }
    }
    return name;
}

From source file:net.sf.jabref.logic.layout.LayoutEntry.java

public static List<List<String>> parseMethodsCalls(String calls) {

    List<List<String>> result = new ArrayList<>();

    char[] c = calls.toCharArray();

    int i = 0;//from  w  w  w  .  ja v  a2  s . c om

    while (i < c.length) {

        int start = i;
        if (Character.isJavaIdentifierStart(c[i])) {
            i++;
            while ((i < c.length) && (Character.isJavaIdentifierPart(c[i]) || (c[i] == '.'))) {
                i++;
            }
            if ((i < c.length) && (c[i] == '(')) {

                String method = calls.substring(start, i);

                // Skip the brace
                i++;

                if (i < c.length) {
                    if (c[i] == '"') {
                        // Parameter is in format "xxx"

                        // Skip "
                        i++;

                        int startParam = i;
                        i++;
                        boolean escaped = false;
                        while (((i + 1) < c.length) && !(!escaped && (c[i] == '"') && (c[i + 1] == ')'))) {
                            if (c[i] == '\\') {
                                escaped = !escaped;
                            } else {
                                escaped = false;
                            }
                            i++;

                        }

                        String param = calls.substring(startParam, i);

                        result.add(Arrays.asList(method, param));
                    } else {
                        // Parameter is in format xxx

                        int startParam = i;

                        while ((i < c.length) && (c[i] != ')')) {
                            i++;
                        }

                        String param = calls.substring(startParam, i);

                        result.add(Arrays.asList(method, param));

                    }
                } else {
                    // Incorrectly terminated open brace
                    result.add(Collections.singletonList(method));
                }
            } else {
                String method = calls.substring(start, i);
                result.add(Collections.singletonList(method));
            }
        }
        i++;
    }

    return result;
}

From source file:org.lockss.util.StringUtil.java

public static String sanitizeToIdentifier(String name) {
    StringBuilder sb = new StringBuilder();
    for (int ix = 0; ix < name.length(); ix++) {
        char ch = name.charAt(ix);
        if (Character.isJavaIdentifierPart(ch)) {
            sb.append(ch);//from www.ja v a2  s.  c o  m
        }
    }
    return sb.toString();
}

From source file:com.helpinput.core.Utils.java

public static int replaceWholeWord(final StringBuilder sb, final int begin, final String source,
        final String oldStr, final String newStr) {
    int lastb = begin;
    int olen = oldStr.length();
    int sLen = source.length();

    int idx = source.indexOf(oldStr, lastb);

    if (idx > -1) {
        boolean needAppend = false;
        sb.append(source, lastb, idx);//from w w  w .  j  a  v  a 2  s. c  o m
        lastb = idx + olen;

        if ((idx == 0 || !Character.isJavaIdentifierPart(source.charAt(idx - 1)))) {
            if (idx + olen >= sLen || !Character.isJavaIdentifierPart(source.charAt(idx + olen))) {
                needAppend = true;
                sb.append(newStr);
            }
        }

        if (!needAppend)
            sb.append(source, idx, idx + olen);
        lastb = replaceWholeWord(sb, lastb, source, oldStr, newStr);
    } else
        sb.append(source, lastb, sLen);

    return lastb;
}

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

/**
 * isJavaId/*w w  w .  ja va  2 s  . c om*/
 * Returns true if the name is a valid java identifier.
 * @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:net.bioclipse.ds.sdk.pdewizard.DSTemplate.java

protected String getFormattedPackageName(String id) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < id.length(); i++) {
        char ch = id.charAt(i);
        if (buffer.length() == 0) {
            if (Character.isJavaIdentifierStart(ch))
                buffer.append(Character.toLowerCase(ch));
        } else {/*from   w w  w.ja va 2 s.  co m*/
            if (Character.isJavaIdentifierPart(ch) || ch == '.')
                buffer.append(ch);
        }
    }
    return buffer.toString().toLowerCase(Locale.ENGLISH);
}