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:org.exoplatform.ks.common.Utils.java

/**
 * Encode special character to html number. Ex: '/' --> / 
 * @param String s, the string input//from   w  ww .  ja  v  a 2s .  c o  m
 * @param String charIgnore, the string content ignore some special character can not encode.
 * @param boolean isTitle, the boolean for check convert is title or not.
 * @return String 
 */
public static String encodeSpecialCharToHTMLnumber(String s, String charIgnore, boolean isTitle) {
    if (isEmpty(s)) {
        return EMPTY_STR;
    }
    int i = 0;
    /*
     *  The distance code number content special character.
     *  Ex: from ' '(32) to '0'(48): ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/'
     *  See: http://www.ascii.cl/htmlcodes.htm
    */
    int[] charCodes = new int[] { 48, 32, 65, 57, 97, 90, 127, 122, 39 };// '0', ' ', 'A', '9', 'a', 'Z', '~', 'z', '\''
    String apos = "'", str1 = "&#", str2 = "<", str3 = ">";
    StringBuilder builder = new StringBuilder();
    while (i < s.length()) {
        char c = s.charAt(i);
        if (charIgnore.indexOf(String.valueOf(c)) >= 0) {
            builder.append(c);
        } else {
            int t = s.codePointAt(i);
            if (t == charCodes[8]) {
                builder.append(apos);
            } else if (t < charCodes[0] && t > charCodes[1] || t < charCodes[2] && t > charCodes[3]
                    || t < charCodes[4] && t > charCodes[5] || t < charCodes[6] && t > charCodes[7]) {
                if (isTitle && (t == 60 || t == 62)) {
                    if (t == 60) {
                        builder.append(str2);
                    } else if (t == 62) {
                        builder.append(str3);
                    }
                } else {
                    builder.append(str1).append(t).append(SEMICOLON);
                }
            } else {
                builder.append(c);
            }
        }
        ++i;
    }
    return builder.toString();
}

From source file:org.pentaho.reporting.libraries.xmlns.writer.XmlWriterSupport.java

private static void writeTextNormalized(final Writer writer, final String s, final CharsetEncoder encoder,
        final boolean transformNewLine) throws IOException {

    if (s == null) {
        return;//w  w  w.  j av a2s  .  co m
    }

    final StringBuilder strB = new StringBuilder(s.length());
    for (int offset = 0; offset < s.length();) {
        final int cp = s.codePointAt(offset);

        switch (cp) {
        case 9: // \t
            strB.appendCodePoint(cp);
            break;
        case 10: // \n
            if (transformNewLine) {
                strB.append("&#10;");
                break;
            }
            strB.appendCodePoint(cp);
            break;
        case 13: // \r
            if (transformNewLine) {
                strB.append("&#13;");
                break;
            }
            strB.appendCodePoint(cp);
            break;
        case 60: // <
            strB.append("&lt;");
            break;
        case 62: // >
            strB.append("&gt;");
            break;
        case 34: // "
            strB.append("&quot;");
            break;
        case 38: // &
            strB.append("&amp;");
            break;
        case 39: // '
            strB.append("&apos;");
            break;
        default:
            if (cp >= 0x20) {
                final String cpStr = new String(new int[] { cp }, 0, 1);
                if ((encoder != null) && !encoder.canEncode(cpStr)) {
                    strB.append("&#x" + Integer.toHexString(cp));
                } else {
                    strB.appendCodePoint(cp);
                }
            }
        }

        offset += Character.charCount(cp);
    }

    writer.write(strB.toString());
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static int findEndOfHashtag(String string, int fromIndex) {
    final int length = string.length();
    for (int i = fromIndex + 1; i < length;) {
        int codepoint = string.codePointAt(i);
        if (Character.isWhitespace(codepoint)) {
            return i;
        } else if (codepoint == '#') {
            return -1;
        }/*from ww w. jav  a2 s  .  co  m*/
        i += Character.charCount(codepoint);
    }
    return length;
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

private static int findEndOfMention(String string, int fromIndex) {
    int atCount = 0;
    final int length = string.length();
    for (int i = fromIndex + 1; i < length;) {
        int codepoint = string.codePointAt(i);
        if (Character.isWhitespace(codepoint)) {
            return i;
        } else if (codepoint == '@') {
            atCount += 1;//w ww .  j av a  2 s .c om
            if (atCount >= 2) {
                return -1;
            }
        }
        i += Character.charCount(codepoint);
    }
    return length;
}

From source file:nz.ac.otago.psyanlab.common.designer.program.operand.RenameOperandDialogueFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Bundle args = getArguments();//from  w  ww  . j  a va2 s.  c o  m
    if (args != null) {
        mOperandId = args.getLong(ARG_OPERAND_ID, -1);
    }

    if (mOperandId == -1) {
        throw new RuntimeException("Invalid operand id.");
    }

    mOperand = mCallbacks.getOperand(mOperandId);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.dialogue_rename_variable, null);
    mName = (EditText) view.findViewById(R.id.name);
    mName.setText(mOperand.getName());

    // Thanks to serwus <http://stackoverflow.com/users/1598308/serwus>,
    // who posted at <http://stackoverflow.com/a/20325852>. Modified to
    // support unicode codepoints and validating first character of input.
    InputFilter filter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                int dend) {
            boolean keepOriginal = true;
            StringBuilder sb = new StringBuilder(end - start);

            int offset = 0;
            String s = source.toString();

            while (offset < s.length()) {
                final int codePoint = s.codePointAt(offset);
                if ((offset == 0 && isAllowedAsFirst(codePoint)) || (offset > 0 && isAllowed(codePoint))) {
                    sb.appendCodePoint(codePoint);
                } else {
                    keepOriginal = false;
                }
                offset += Character.charCount(codePoint);
            }

            if (keepOriginal)
                return null;
            else {
                if (source instanceof Spanned) {
                    SpannableString sp = new SpannableString(sb);
                    TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
                    return sp;
                } else {
                    return sb;
                }
            }
        }

        private boolean isAllowed(int codePoint) {
            return Character.isLetterOrDigit(codePoint);
        }

        private boolean isAllowedAsFirst(int codePoint) {
            return Character.isLetter(codePoint);
        }
    };

    mName.setFilters(new InputFilter[] { filter });

    // Build dialogue.
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getString(R.string.title_rename_variable, mOperand.getName())).setView(view)
            .setPositiveButton(R.string.action_rename, mPositiveListener)
            .setNegativeButton(R.string.action_cancel, mNegativeListener);

    // Create the AlertDialog object and return it
    Dialog dialog = builder.create();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    return dialog;
}

From source file:com.google.android.apps.chrometophone.C2DMReceiver.java

private String parseTelephoneNumber(String sel) {
    if (sel == null || sel.length() == 0)
        return null;

    // Hack: Remove trailing left-to-right mark (Google Maps adds this)
    if (sel.codePointAt(sel.length() - 1) == 8206) {
        sel = sel.substring(0, sel.length() - 1);
    }//from  www. j av a 2 s.co  m

    String number = null;
    if (sel.matches("([Tt]el[:]?)?\\s?[+]?(\\(?[0-9|\\s|\\-|\\.]\\)?)+")) {
        String elements[] = sel.split("([Tt]el[:]?)");
        number = elements.length > 1 ? elements[1] : elements[0];
        number = number.replace(" ", "");

        // Remove option (0) in international numbers, e.g. +44 (0)20 ...
        if (number.matches("\\+[0-9]{2,3}\\(0\\).*")) {
            int openBracket = number.indexOf('(');
            int closeBracket = number.indexOf(')');
            number = number.substring(0, openBracket) + number.substring(closeBracket + 1);
        }
    }
    return number;
}

From source file:net.jimj.automaton.commands.QuoteCommand.java

protected boolean isMetaWord(String word) {
    if (StringUtils.isBlank(word)) {
        return false;
    }//from ww w .j av  a2  s  .c o m
    return !(Character.isAlphabetic(word.codePointAt(0)) || Character.isDigit(word.codePointAt(0)));
}

From source file:org.alfresco.textgen.TextGenerator.java

private String getSingleWord(ArrayList<String> choice, Random random) {
    String candidate;
    NEXT: for (int i = 0; i < 100000; i++) {
        candidate = choice.get(random.nextInt(choice.size()));
        for (int j = 0; j < candidate.length(); j++) {
            int cp = candidate.codePointAt(j);
            if (!Character.isAlphabetic(cp) && !Character.isDigit(cp)) {
                continue NEXT;
            }/*w  ww.  j  ava 2s  .  c o  m*/
        }
        return candidate;
    }
    return "";
}

From source file:org.maodian.flyingcat.xmpp.codec.SASLCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    String mechanism = xmlsr.getAttributeValue("", "mechanism");
    if (StringUtils.equals("PLAIN", mechanism)) {
        String base64Data = null;
        try {/*from w  ww  . j ava2s  . c o  m*/
            base64Data = xmlsr.getElementText();
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }

        if (!Base64.isBase64(base64Data)) {
            throw new XmppException(SASLError.INCORRECT_ENCODING);
        }

        byte[] value = Base64.decodeBase64(base64Data);
        String text = new String(value, StandardCharsets.UTF_8);

        // apply PLAIN SASL mechanism whose rfc locates at http://tools.ietf.org/html/rfc4616
        int[] nullPosition = { -1, -1 };
        int nullIndex = 0;
        for (int i = 0; i < text.length(); ++i) {
            if (text.codePointAt(i) == 0) {
                // a malicious base64 value may contain more than two null character
                if (nullIndex > 1) {
                    throw new XmppException(SASLError.MALFORMED_REQUEST);
                }
                nullPosition[nullIndex++] = i;
            }
        }

        if (nullPosition[0] == -1 || nullPosition[1] == -1) {
            throw new XmppException("The format is invalid", SASLError.MALFORMED_REQUEST);
        }
        String authzid = StringUtils.substring(text, 0, nullPosition[0]);
        String authcid = StringUtils.substring(text, nullPosition[0] + 1, nullPosition[1]);
        String password = StringUtils.substring(text, nullPosition[1] + 1);

        if (authzid.getBytes(StandardCharsets.UTF_8).length > 255
                || authcid.getBytes(StandardCharsets.UTF_8).length > 255
                || password.getBytes(StandardCharsets.UTF_8).length > 255) {
            throw new XmppException(
                    "authorization id, authentication id and password should be equal or less than 255 bytes",
                    SASLError.MALFORMED_REQUEST);
        }
        return new Auth(authzid, authcid, password);
    } else {
        throw new XmppException(SASLError.INVALID_MECHANISM).set("mechanism", mechanism);
    }
}

From source file:org.apache.pdfbox.pdmodel.font.PDCIDFontType0.java

/**
 * Returns the name of the glyph with the given character code. This is done by looking up the
 * code in the parent font's ToUnicode map and generating a glyph name from that.
 *//*from  w ww . j a  v a  2 s .c  om*/
private String getGlyphName(int code) throws IOException {
    String unicodes = parent.toUnicode(code);
    if (unicodes == null) {
        return ".notdef";
    }
    return getUniNameOfCodePoint(unicodes.codePointAt(0));
}