Example usage for java.lang Character isLetter

List of usage examples for java.lang Character isLetter

Introduction

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

Prototype

public static boolean isLetter(int codePoint) 

Source Link

Document

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

Usage

From source file:Main.java

/**
 * Tests if specified string is a lexically correct NCName.
 * /*  w ww.  j a va2 s . c  om*/
 * @param s string to be tested
 * @return <code>true</code> if test is successful; <code>false</code>
 * otherwise
 */
public static final boolean isNCName(String s) {
    int count;
    if (s == null || (count = s.length()) == 0)
        return false;

    char c = s.charAt(0);
    switch (c) {
    case '_':
        break;
    default:
        if (!Character.isLetter(c))
            return false;
    }

    for (int i = 1; i < count; ++i) {
        c = s.charAt(i);
        if (c == ':' || !isNameChar(c))
            return false;
    }

    return true;
}

From source file:Main.java

/**
 * Tests if specified string is a lexically correct Name.
 * //w  ww  .j  a v  a 2  s.co m
 * @param s string to be tested
 * @return <code>true</code> if test is successful; <code>false</code>
 *         otherwise
 */
public static final boolean isName(String s) {
    int count;
    if (s == null || (count = s.length()) == 0)
        return false;

    char c = s.charAt(0);
    switch (c) {
    case '_':
    case ':':
        break;
    default:
        if (!Character.isLetter(c))
            return false;
    }

    for (int i = 1; i < count; ++i) {
        if (!isNameChar(s.charAt(i)))
            return false;
    }

    return true;
}

From source file:Main.java

/**
 * Make sure an element/attribute name is a valid NCname. Note that the name is assumed non-qualified, i.e. no
 * namespaces (hence the Non Colon Name).
 * //  w ww .j a  v  a 2  s . c o  m
 * @param name Proposed non-qualified name for an element/attribute.
 * @return Same string, with any invalid characters replaced by the underscore '_' character.
 */
public static String makeValidNCName(String name) {
    if (name == null || name.length() <= 0)
        throw new IllegalArgumentException("Invalid NCName: null or empty not allowed!");
    if (name.equalsIgnoreCase("XML"))
        throw new IllegalArgumentException("Invalid NCName: 'XML' name is reserved!");
    StringBuffer s = new StringBuffer(name);
    char c = name.charAt(0);
    if (!Character.isLetter(c) && c != '_')
        s.setCharAt(0, '_');
    for (int i = 1; i < name.length(); i++) {
        c = name.charAt(i);
        if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_' && c != '.' && c != '-')
            s.setCharAt(i, '_');
    }
    return s.toString();
}

From source file:Main.java

/**
 * <p>//from   w  ww. j  av  a 2  s.c  om
 * Checks if the String contains only unicode letters and space (<code>' '</code>).
 * </p>
 * <p/>
 * <p>
 * <code>null</code> will return <code>false</code>. An empty String will return <code>true</code>.
 * </p>
 *
 * @param str the String to check
 * @return <code>true</code> if only contains letters and space, and is non-null
 */
public static boolean isAlphaSpace(String str) {
    if (str == null) {
        return false;
    }
    int sz = str.length();
    for (int i = 0; i < sz; i++) {
        if ((Character.isLetter(str.charAt(i)) == false) && (str.charAt(i) != ' ')) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Look at the peer ID and just grab as many readable characters to form the version
 * substring as possible.// ww  w .  j a  v  a  2  s .co m
 */
public static String extractReadableVersionSubstringFromPeerID(String peer_id) {
    for (int i = 0; i < peer_id.length(); i++) {
        char c = peer_id.charAt(i);

        // This is based on All Peers peer ID at the time of writing, e.g:
        //   AP0.70rc30->>...
        if (Character.isLetter(c))
            continue;
        if (Character.isDigit(c))
            continue;
        if (c == '.')
            continue;
        // Must be delimiter character.
        return peer_id.substring(0, i);
    }
    return peer_id;
}

From source file:Main.java

public final static String encodeAttrMarkup(String str) {
    final StringBuilder buf = new StringBuilder();
    char ch;//  www  . j av a  2 s  .  co m
    for (int i = 0; i < str.length(); i++)
        switch (ch = str.charAt(i)) {
        case '&':
            boolean isEntity = false;
            for (int j = i + 1; j < str.length(); j++) {
                if (str.charAt(j) == ';') {
                    isEntity = true;
                    break;
                }
                if (!Character.isLetter(str.charAt(j))) {
                    break;
                }
            }
            if (isEntity) {
                buf.append('&');
            } else {
                buf.append("&amp;");
            }
            break;
        case '<':
            buf.append("&lt;");
            break;
        case '>':
            buf.append("&gt;");
            break;
        case '"':
            buf.append("&quot;");
            break;
        default:
            buf.append(ch);
        }
    return buf.toString();
}

From source file:Main.java

public static final String encodeAttrMarkup(final String str) {
    final StringBuilder buf = new StringBuilder();
    char ch;/*w  w  w  . j a v a  2 s.c  o m*/
    for (int i = 0; i < str.length(); i++) {
        switch (ch = str.charAt(i)) {
        case '&':
            boolean isEntity = false;
            for (int j = i + 1; j < str.length(); j++) {
                if (str.charAt(j) == ';') {
                    isEntity = true;
                    break;
                }
                if (!Character.isLetter(str.charAt(j))) {
                    break;
                }
            }
            if (isEntity) {
                buf.append('&');
            } else {
                buf.append("&amp;");
            }
            break;
        case '<':
            buf.append("&lt;");
            break;
        case '>':
            buf.append("&gt;");
            break;
        case '"':
            buf.append("&quot;");
            break;
        default:
            buf.append(ch);
        }
    }
    return buf.toString();
}

From source file:NonNumericDocument.java

@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
    if (str == null) {
        return;//from w w w .  j  a  v  a 2 s  .com
    }
    char[] arr = str.toCharArray();
    for (int i = 0; i < arr.length; i++) {
        if (Character.isDigit(arr[i]) || !Character.isLetter(arr[i])) {
            return;
        }
    }
    super.insertString(offs, new String(str), a);
}

From source file:IPDomainValidator.java

/**
 * Returns 0 if the char array {@code part} is invalid.
 *//*from   w  w  w  .j  a v a  2  s  .  c  o  m*/
public static int tokenValidate(char[] part, int start, int len) {
    int digitCount = 0, lastHyphen = -1;
    for (int i = 0; i < len; i++) {
        char c = part[start++];
        if (Character.isDigit(c))
            digitCount++;
        else if (!Character.isLetter(c)) {
            if (c == '-') {
                if (i < len - 1) {
                    if (lastHyphen + 1 == i || lastHyphen - 1 == i) {
                        // invalid placement of hyphens (succeeding, prefix)                            
                        return INVALID;
                    }
                    lastHyphen = i;
                } else {
                    // invalid placement of hyphens (suffix)  
                    return INVALID;
                }
            } else
                return INVALID;
        }
    }
    if (digitCount == len)
        return IP;

    if (digitCount == 0)
        return lastHyphen == -1 ? PLAIN : HYPHENATED;

    return lastHyphen == -1 ? ALPHANUMERIC : MIXED;
}

From source file:com.evolveum.midpoint.util.UglyHacks.java

public static String forceXsiNsDeclaration(String originalXml) {
    int iOpeningBracket = -1;
    while (true) {
        iOpeningBracket = originalXml.indexOf('<', iOpeningBracket + 1);
        if (iOpeningBracket < 0) {
            // No element ?
            return originalXml;
        }/*from w  w w  .java2s.co  m*/
        if (Character.isLetter(originalXml.charAt(iOpeningBracket + 1))) {
            break;
        }
        // Processing instruction, skip it
    }
    int iClosingBracket = originalXml.indexOf('>', iOpeningBracket);
    if (iClosingBracket < 0) {
        // Element not closed?
        return originalXml;
    }
    String firstElement = originalXml.substring(iOpeningBracket, iClosingBracket);
    // Not perfect, but should be good enough. All this is a hack anyway
    if (firstElement.indexOf("xmlns:xsi") >= 0) {
        // Already has xsi declaration
        return originalXml;
    }
    int iEndOfElementName = iOpeningBracket;
    while (iEndOfElementName < iClosingBracket) {
        char ch = originalXml.charAt(iEndOfElementName);
        if (ch == ' ' || ch == '>') {
            break;
        }
        iEndOfElementName++;
    }
    StringBuilder sb = new StringBuilder();
    sb.append(originalXml.substring(0, iEndOfElementName));
    sb.append(" xmlns:xsi='");
    sb.append(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    sb.append("'");
    sb.append(originalXml.substring(iEndOfElementName));
    return sb.toString();
}