Example usage for java.lang String codePointAt

List of usage examples for java.lang String codePointAt

Introduction

In this page you can find the example usage for java.lang String codePointAt.

Prototype

public int codePointAt(int index) 

Source Link

Document

Returns the character (Unicode code point) at the specified index.

Usage

From source file:Main.java

/**
 * This is useful when checking the string should be encoded into quoted-printable
 * or not, which is required by vCard 2.1.
 * See the definition of "7bit" in vCard 2.1 spec for more information.
 *///from   ww  w .ja  va 2 s.com
public static boolean containsOnlyNonCrLfPrintableAscii(String str) {
    if (TextUtils.isEmpty(str)) {
        return true;
    }

    final int length = str.length();
    final int asciiFirst = 0x20;
    final int asciiLast = 0x126;
    for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
        int c = str.codePointAt(i);
        if (c < asciiFirst || asciiLast < c || c == '\n' || c == '\r') {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * Mangle a string so that it can be represented in an XML document.
 * //from w ww  .  j  ava2s  .  c o  m
 * There are three kinds of code points in XML:
 * - Those that can be represented normally,
 * - Those that have to be escaped (for example, & must be represented 
 *     as &amp;)
 * - Those that cannot be represented at all in XML.
 *
 * The built-in SAX functions will handle the first two types for us just
 * fine.  However, sometimes we come across a code point of the third type.
 * In this case, we have to mangle the string in order to represent it at
 * all.  We also mangle backslash to avoid confusing a backslash in the
 * string with part our escape sequence.
 * 
 * The encoding used here is as follows: an illegal code point is
 * represented as '\ABCD;', where ABCD is the hexadecimal value of 
 * the code point.
 *
 * @param str     The input string.
 *
 * @return        The mangled string.
 */
public static String mangleXmlString(String str, boolean createEntityRefs) {
    final StringBuilder bld = new StringBuilder();
    final int length = str.length();
    for (int offset = 0; offset < length;) {
        final int cp = str.codePointAt(offset);
        final int len = Character.charCount(cp);
        if (codePointMustBeMangled(cp)) {
            bld.append(mangleCodePoint(cp));
        } else {
            String entityRef = null;
            if (createEntityRefs) {
                entityRef = codePointToEntityRef(cp);
            }
            if (entityRef != null) {
                bld.append(entityRef);
            } else {
                for (int i = 0; i < len; i++) {
                    bld.append(str.charAt(offset + i));
                }
            }
        }
        offset += len;
    }
    return bld.toString();
}

From source file:Main.java

public static boolean containsOnlyAlphaDigitHyphen(final Collection<String> values) {
    if (values == null) {
        return true;
    }/*from   www  .  j av a2  s .  co m*/
    final int upperAlphabetFirst = 0x41; // A
    final int upperAlphabetAfterLast = 0x5b; // [
    final int lowerAlphabetFirst = 0x61; // a
    final int lowerAlphabetAfterLast = 0x7b; // {
    final int digitFirst = 0x30; // 0
    final int digitAfterLast = 0x3A; // :
    final int hyphen = '-';
    for (final String str : values) {
        if (TextUtils.isEmpty(str)) {
            continue;
        }
        final int length = str.length();
        for (int i = 0; i < length; i = str.offsetByCodePoints(i, 1)) {
            int codepoint = str.codePointAt(i);
            if (!((lowerAlphabetFirst <= codepoint && codepoint < lowerAlphabetAfterLast)
                    || (upperAlphabetFirst <= codepoint && codepoint < upperAlphabetAfterLast)
                    || (digitFirst <= codepoint && codepoint < digitAfterLast) || (codepoint == hyphen))) {
                return false;
            }
        }
    }
    return true;
}

From source file:com.sf.springsecurityregistration1.core.services.UserService.java

public static String buildHtmlEntityCode(String input) {
    StringBuilder output = new StringBuilder(input.length() * 2);

    int len = input.length();
    int code, code1, code2, code3, code4;
    char ch;/*  w  w  w  . ja  v a2 s  .  c  o m*/

    for (int i = 0; i < len;) {
        code1 = input.codePointAt(i);
        if (code1 >> 3 == 30) {
            code2 = input.codePointAt(i + 1);
            code3 = input.codePointAt(i + 2);
            code4 = input.codePointAt(i + 3);
            code = ((code1 & 7) << 18) | ((code2 & 63) << 12) | ((code3 & 63) << 6) | (code4 & 63);
            i += 4;
            output.append("&#" + code + ";");
        } else if (code1 >> 4 == 14) {
            code2 = input.codePointAt(i + 1);
            code3 = input.codePointAt(i + 2);
            code = ((code1 & 15) << 12) | ((code2 & 63) << 6) | (code3 & 63);
            i += 3;
            output.append("&#" + code + ";");
        } else if (code1 >> 5 == 6) {
            code2 = input.codePointAt(i + 1);
            code = ((code1 & 31) << 6) | (code2 & 63);
            i += 2;
            output.append("&#" + code + ";");
        } else {
            code = code1;
            i += 1;

            ch = (char) code;
            if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9') {
                output.append(ch);
            } else {
                output.append("&#" + code + ";");
            }
        }
    }

    return output.toString();
}

From source file:com.screenslicer.common.HtmlCoder.java

public static String encode(String string) {
    try {/*w w  w .ja  v  a 2 s  .co m*/
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < string.length();) {
            int codePoint = string.codePointAt(i);
            String symbol = codePointToSymbol(codePoint);
            char[] chars = symbol.toCharArray();
            if (chars.length == 1 && CharUtils.isAscii(chars[0])) {
                builder.append(StringEscapeUtils.escapeHtml4(Character.toString(chars[0])));
            } else {
                builder.append(symbol);
            }
            i += chars.length;
        }
        return builder.toString();
    } catch (Throwable t) {
        Log.exception(t);
        return string;
    }
}

From source file:org.dkpro.tc.api.features.util.FeatureUtil.java

public static String escapeFeatureName(String name) {

    // TODO Issue 120: improve the escaping
    // the fix was necessary due to Issue 32
    // http://code.google.com/p/dkpro-tc/issues/detail?id=32
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < name.length(); i++) {
        String c = name.substring(i, i + 1);
        if (StringUtils.isAlphanumeric(c) || c.equals("_")) {
            sb.append(c);//  w  w w . j  a v  a2 s  .co m
        } else {
            sb.append("u");
            sb.append(c.codePointAt(0));
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * <p>/*from w w w. j  a  v  a  2  s .  co m*/
 * Returns true when the given String is categorized as "word" specified in vCard spec 2.1.
 * </p>
 * <p>
 * vCard 2.1 specifies:<br />
 * word = &lt;any printable 7bit us-ascii except []=:., &gt;
 * </p>
 */
public static boolean isV21Word(final String value) {
    if (TextUtils.isEmpty(value)) {
        return true;
    }
    final int asciiFirst = 0x20;
    final int asciiLast = 0x7E; // included
    final int length = value.length();
    for (int i = 0; i < length; i = value.offsetByCodePoints(i, 1)) {
        final int c = value.codePointAt(i);
        if (!(asciiFirst <= c && c <= asciiLast) || sUnAcceptableAsciiInV21WordSet.contains((char) c)) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static final String filterUCS4(String str) {
    if (TextUtils.isEmpty(str)) {
        return str;
    }//from   www  . j  a  va 2s .  c  o m

    if (str.codePointCount(0, str.length()) == str.length()) {
        return str;
    }

    StringBuilder sb = new StringBuilder();

    int index = 0;
    while (index < str.length()) {
        int codePoint = str.codePointAt(index);
        index += Character.charCount(codePoint);
        if (Character.isSupplementaryCodePoint(codePoint)) {
            continue;
        }

        sb.appendCodePoint(codePoint);
    }

    return sb.toString();
}

From source file:org.exoplatform.ks.ext.impl.ForumTransformHTML.java

public static String removeCharterStrange(String s) {
    if (s == null || s.length() <= 0)
        return "";
    int i = 0;//w ww .j av a  2  s  .co m
    StringBuilder builder = new StringBuilder();
    while (i < s.length()) {
        if (s.codePointAt(i) > 31) {
            builder.append(s.charAt(i));
        }
        ++i;
    }
    return builder.toString();
}

From source file:Main.java

/**
 * Escapes the given text such that it can be safely embedded in a string literal
 * in Java source code./*from w  w w .  j av  a2s.c o m*/
 * 
 * @param text the text to escape
 * 
 * @return the escaped text
 */
public static String escapeToJavaString(String text) {
    if (text == null) {
        return null;
    }
    String result = text.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replace("\b", "\\b")
            .replace("\f", "\\f").replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
    StringBuilder complete = new StringBuilder();
    for (int i = 0; i < result.length(); i++) {
        int codePointI = result.codePointAt(i);
        if (codePointI >= 32 && codePointI <= 127) {
            complete.append(Character.toChars(codePointI));
        } else {
            // use Unicode representation
            complete.append("\\u");
            String hex = Integer.toHexString(codePointI);
            complete.append(getRepeatingString(4 - hex.length(), '0'));
            complete.append(hex);
        }
    }
    return complete.toString();
}