Example usage for java.lang Character isLetterOrDigit

List of usage examples for java.lang Character isLetterOrDigit

Introduction

In this page you can find the example usage for java.lang Character isLetterOrDigit.

Prototype

public static boolean isLetterOrDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a letter or digit.

Usage

From source file:net.sf.jabref.gui.MainTableSelectionListener.java

/**
 * Receive key event on the main table. If the key is a letter or a digit,
 * we should select the first entry in the table which starts with the given
 * letter in the column by which the table is sorted.
 * @param e The KeyEvent//from  w ww .ja va  2s.  c o m
 */
@Override
public void keyTyped(KeyEvent e) {
    if ((!e.isActionKey()) && Character.isLetterOrDigit(e.getKeyChar())
    //&& !e.isControlDown() && !e.isAltDown() && !e.isMetaDown()) {
            && (e.getModifiers() == 0)) {
        long time = System.currentTimeMillis();
        long QUICK_JUMP_TIMEOUT = 2000;
        if ((time - lastPressedTime) > QUICK_JUMP_TIMEOUT) {
            lastPressedCount = 0; // Reset last pressed character
        }
        // Update timestamp:
        lastPressedTime = time;
        // Add the new char to the search array:
        int c = e.getKeyChar();
        if (lastPressedCount < lastPressed.length) {
            lastPressed[lastPressedCount] = c;
            lastPressedCount++;
        }

        int sortingColumn = table.getSortingColumn(0);
        if (sortingColumn == -1) {
            return; // No sorting? TODO: look up by author, etc.?
        }
        // TODO: the following lookup should be done by a faster algorithm,
        // such as binary search. But the table may not be sorted properly,
        // due to marked entries, search etc., which rules out the binary search.
        int startRow = 0;
        /*if ((c == lastPressed) && (lastQuickJumpRow >= 0)) {
        if (lastQuickJumpRow < table.getRowCount()-1)
            startRow = lastQuickJumpRow+1;
        }*/

        boolean done = false;
        while (!done) {
            for (int i = startRow; i < table.getRowCount(); i++) {
                Object o = table.getValueAt(i, sortingColumn);
                if (o == null) {
                    continue;
                }
                String s = o.toString().toLowerCase();
                if (s.length() >= lastPressedCount) {
                    for (int j = 0; j < lastPressedCount; j++) {
                        if (s.charAt(j) != lastPressed[j]) {
                            break; // Escape the loop immediately when we find a mismatch
                        } else if (j == (lastPressedCount - 1)) {
                            // We found a match:
                            table.setRowSelectionInterval(i, i);
                            table.ensureVisible(i);
                            return;
                        }
                    }
                    //if ((s.length() >= 1) && (s.charAt(0) == c)) {
                    //}
                }
            }
            // Finished, no result. If we didn't start at the beginning of
            // the table, try that. Otherwise, exit the while loop.
            if (startRow > 0) {
                startRow = 0;
            } else {
                done = true;
            }

        }

    } else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
        lastPressedCount = 0;

    }
}

From source file:org.jboss.tools.jst.web.ui.palette.html.wizard.AbstractNewHTMLWidgetWizardPage.java

/**
 * Creates elementary coloring of xml.//  w ww .  j av a  2 s  . com
 * @param text
 * @return
 */
public static StyleRange[] getRanges(String text) {
    ArrayList<StyleRange> regionList = new ArrayList<StyleRange>();
    char quota = '\0';
    boolean inTag = false;
    boolean scriptDetected = false;
    int offset = 0;
    StringBuilder name = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        char ch = text.charAt(i);
        if (inTag && (quota != '\0' && quota == ch)) {
            quota = '\0';
            addRange(offset, 1, valueColor, false, regionList);
            if (i - offset > 1) {
                addRange(offset + 1, i - offset - 1, valueColor, true, regionList);
            }
            addRange(i, 1, valueColor, false, regionList);
        } else if (inTag && quota == '\0' && (ch == '"' || ch == '\'')) {
            quota = ch;
            offset = i;
        } else if (ch == '>' && quota == '\0') {
            if (inTag && name.toString().equalsIgnoreCase(SCRIPT_OPEN)) {
                scriptDetected = true;
            }
            if (scriptDetected && text.charAt(i - 1) == '/') {
                scriptDetected = false;
            }
            inTag = false;
            if (name.length() > 0) {
                addRange(offset, name.length(), tagColor, false, regionList);
                name.setLength(0);
            }
            addRange(i, 1, tagColor, false, regionList);
            if (scriptDetected) {
                int j = text.toLowerCase().indexOf(SCRIPT_CLOSE, i + 1);
                if (j < 0)
                    j = text.length();
                addRangesInJavaScript(text, i + 1, j, regionList);
                if (j < text.length()) {
                    addRange(j, SCRIPT_CLOSE.length(), tagColor, false, regionList);
                    i = j + SCRIPT_CLOSE.length() - 1;
                } else {
                    i = text.length();
                }
                scriptDetected = false;
            }
        } else if (ch == '<' && quota == '\0') {
            inTag = true;
            name.setLength(0);
            name.append(ch);
            offset = i;
        } else if (quota == '\0' && inTag && (Character.isLetterOrDigit(ch) || ch == '-' || ch == '/')) {
            if (name.length() == 0) {
                offset = i;
            }
            name.append(ch);
        } else if (name.length() > 0) {
            Color c = name.charAt(0) == '<' ? tagColor : attrColor;
            addRange(offset, name.length(), c, false, regionList);
            if (inTag && name.toString().equalsIgnoreCase(SCRIPT_OPEN)) {
                scriptDetected = true;
            }
            name.setLength(0);
        }

    }
    return (StyleRange[]) regionList.toArray(new StyleRange[0]);
}

From source file:com.cohort.util.String2.java

/**
 * This goes beyond equalsIgnoreCase by looking after punctuation removed.
 *
 * @param s1/*w  w  w  . ja v a  2  s.  co  m*/
 * @param s2
 * @return true if find is loosely in s. Return false if s or find !isSomething.
 */
public static boolean looselyEquals(String s1, String s2) {
    if (s1 == null || s2 == null)
        return false;

    int s1Length = s1.length();
    StringBuilder s1sb = new StringBuilder();
    for (int i = 0; i < s1Length; i++) {
        char ch = s1.charAt(i);
        if (Character.isLetterOrDigit(ch))
            s1sb.append(Character.toLowerCase(ch));
    }
    if (s1sb.length() == 0)
        return false;

    int s2Length = s2.length();
    StringBuilder s2sb = new StringBuilder();
    for (int i = 0; i < s2Length; i++) {
        char ch = s2.charAt(i);
        if (Character.isLetterOrDigit(ch))
            s2sb.append(Character.toLowerCase(ch));
    }
    if (s2sb.length() == 0)
        return false;

    return s1sb.toString().equals(s2sb.toString());
}

From source file:com.netspective.commons.text.TextUtils.java

/**
 * Given a text string, return a string that would be suitable for an XML element name. For example,
 * when given Person_Address it would return person-address. The rule is to basically take every letter
 * or digit and return it in lowercase and every non-letter or non-digit as a dash.
 *///  w w w . j a  v a  2  s.c  o  m
public String xmlTextToNodeName(String xml) {
    if (xml == null || xml.length() == 0)
        return xml;

    StringBuffer constant = new StringBuffer();
    for (int i = 0; i < xml.length(); i++) {
        char ch = xml.charAt(i);
        constant.append(Character.isLetterOrDigit(ch) ? Character.toLowerCase(ch) : '-');
    }
    return constant.toString();
}

From source file:org.loklak.geo.GeoNames.java

/**
 * Split the text into word tokens. The tokens are lower-cased. To maintain the original spelling
 * of the word without lowercasing them, the original word is attached too.
 * @param text/*from w  w  w  .  j a  v  a 2 s.  c o  m*/
 * @return a List of Map.Entry objects where the key is the lower-cased word token and the value is the original word
 */
public static ArrayList<Map.Entry<String, String>> split(final String text) {
    ArrayList<Map.Entry<String, String>> a = new ArrayList<>(1 + text.length() / 4);
    final StringBuilder o = new StringBuilder();
    final StringBuilder l = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        final char c = text.charAt(i);
        if (Character.isLetterOrDigit(c)) {
            o.append(c);
            l.append(Character.toLowerCase(c));
            continue;
        }
        // if it is not letter or digit, we split it.
        if (o.length() > 0) {
            a.add(new AbstractMap.SimpleEntry<String, String>(l.toString(), o.toString()));
            o.setLength(0);
            l.setLength(0);
        }
    }
    if (o.length() > 0) {
        a.add(new AbstractMap.SimpleEntry<String, String>(l.toString(), o.toString()));
        o.setLength(0);
        l.setLength(0);
    }
    return a;
}

From source file:org.eclipse.ebr.maven.AboutFilesUtil.java

private String sanitizeFileName(final String name) {
    final StrBuilder result = new StrBuilder();
    for (final char c : name.toCharArray()) {
        if (Character.isLetterOrDigit(c) || (c == '+') || (c == '-') || (c == '.')) {
            result.append(c);//from w w  w  .ja v a  2  s .  c o  m
        } else {
            result.append('_');
        }
    }
    return result.toString();
}

From source file:h2o.common.spring.util.StringUtils.java

/**
 * Parse the given <code>localeString</code> value into a {@link Locale}.
 * <p>This is the inverse operation of {@link Locale#toString Locale's toString}.
 * @param localeString the locale string, following <code>Locale's</code>
 * <code>toString()</code> format ("en", "en_UK", etc);
 * also accepts spaces as separators, as an alternative to underscores
 * @return a corresponding <code>Locale</code> instance
 *//*from   w ww  .  ja  v a2 s .c o m*/
public static Locale parseLocaleString(String localeString) {
    for (int i = 0; i < localeString.length(); i++) {
        char ch = localeString.charAt(i);
        if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) {
            throw new IllegalArgumentException(
                    "Locale value \"" + localeString + "\" contains invalid characters");
        }
    }
    String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
    String language = (parts.length > 0 ? parts[0] : "");
    String country = (parts.length > 1 ? parts[1] : "");
    String variant = "";
    if (parts.length >= 2) {
        // There is definitely a variant, and it is everything after the country
        // code sans the separator between the country code and the variant.
        int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
        // Strip off any leading '_' and whitespace, what's left is the variant.
        variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
        if (variant.startsWith("_")) {
            variant = trimLeadingCharacter(variant, '_');
        }
    }
    return (language.length() > 0 ? new Locale(language, country, variant) : null);
}

From source file:com.sworddance.util.UriFactoryImpl.java

/**
 * This method encodes URI string into a percent encoded string. If a URI string
 * or part of it is already percent encoded, that part of URI string is skipped.
 * This method is implemented as per specifications in RFC 1738 (section 2).
 *
 * I also had a look at//from   www.j av  a  2  s .c o  m
 * {@link java.net.URLEncoder} does not meet out requirements
 * {@link java.net.URI} also does not meet our requirements
 *
 *
 *  TODO There is one issue in this implementation, RFC 1738 section 2.2,
 *  states that a reserved character in a URI must be encoded if it is used
 *  for a purpose other than its reserved purpose. (For example :- If a
 *  reserved character is used in the name of file in a URI, then that reserved
 *  character must be encoded). As of now in the current implementation we are
 *  not encoding any reserved characters. For us it is difficult to determine
 *  whether a reserved character is used for its reserved purpose of some other
 *  purpose.
 *
 *  One possible (but costly) solution to above limitation could be,
 *  to start encoding all the possible combinations of reserved characters
 *  in the URI one at a time and see if URI starts working.
 *
 * @param input URI string which needs to be percent encoded
 * @return percent encoded string
 * {@linkplain http://java.sun.com/javaee/6/docs/api/javax/ws/rs/core/UriBuilder.html},
 * probably URIBuilder does the same work we want to do below. But no of
 * dependencies(maven) on URI are huge. So it does not look worth the effort
 * to use URIBuilder
 * {@linkplain http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars}
 * {@linkplain http://www.ietf.org/rfc/rfc1738.txt} (section 2.2)
 */
public static String percentEncoding(String input) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if (c == '%') {
            boolean inputHasAtleastTwoMoreChars = i + 2 < input.length();
            if (inputHasAtleastTwoMoreChars) {
                String twoCharsAfterPercent = input.substring(i + 1, i + 3);
                if (isHexAfterPercent(twoCharsAfterPercent)) {
                    i = i + 2;
                    sb.append("%").append(twoCharsAfterPercent);
                    continue;
                }
            }
        }
        boolean isLetterOrDigit = Character.isLetterOrDigit(c);
        boolean isSafe = Arrays.binarySearch(safe, c) >= 0;
        boolean isReserved = Arrays.binarySearch(reserved, c) >= 0;
        if (isLetterOrDigit || isSafe || isReserved) {
            sb.append(c);
        } else if (c <= 0x007F) { // convert all other ASCII to percent encoding
            sb.append("%").append(Integer.toHexString(c));
        } else if (c <= 0x07FF) { // non-ASCII <= 0x7FF (UTF 2 byte)
            sb.append("%").append(Integer.toHexString(0xc0 | (c >> 6)));
            sb.append("%").append(Integer.toHexString(0x80 | (c & 0x3F)));
        } else { // 0x7FF < ch <= 0xFFFF (UTF 3 bytes)
            sb.append("%").append(Integer.toHexString(0xe0 | (c >> 12)));
            sb.append("%").append(Integer.toHexString(0x80 | ((c >> 6) & 0x3F)));
            sb.append("%").append(Integer.toHexString(0x80 | (c & 0x3F)));
        }
    }
    return sb.toString();
}

From source file:com.github.rvesse.airline.help.cli.bash.BashCompletionGenerator.java

private String bashize(String value) {
    StringBuilder builder = new StringBuilder();
    for (char c : value.toCharArray()) {
        if (Character.isLetterOrDigit(c) || c == '_') {
            builder.append(c);/*from   w w  w .  j a  va 2s.c o m*/
        }
    }
    return builder.toString();
}

From source file:org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemData.java

public boolean isNotEmpty(String wyzText) {

    if (wyzText != null) {
        int index = 0;
        String t = (wyzText.replaceAll("<.*?>", " ")).trim();
        while (index < t.length()) {
            char c = t.charAt(index);
            if (Character.isLetterOrDigit(c)) {
                return true;
            }//  w  w  w.ja v  a 2 s  . com
            index++;
        }
    }
    return false;
}