Example usage for java.text CharacterIterator first

List of usage examples for java.text CharacterIterator first

Introduction

In this page you can find the example usage for java.text CharacterIterator first.

Prototype

public char first();

Source Link

Document

Sets the position to getBeginIndex() and returns the character at that position.

Usage

From source file:XmlValueEncoder.java

/**
 * {@inheritDoc}/*from w  w  w.j  a va  2s  .  com*/
 *
 * @see org.jboss.dna.common.text.TextEncoder#encode(java.lang.String)
 */
public String encode(String text) {
    if (text == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '&':
            sb.append("&");
            break;
        case '"':
            sb.append(""");
            break;
        case '<':
            sb.append("&lt;");
            break;
        case '>':
            sb.append("&gt;");
            break;
        case '\'':
            sb.append("&#039;");
            break;
        default:
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:UrlEncoder.java

/**
 * {@inheritDoc}/* w ww  . j  a  v  a2s .c o m*/
 */
public String encode(String text) {
    if (text == null)
        return null;
    if (text.length() == 0)
        return text;
    final BitSet safeChars = isSlashEncoded() ? RFC2396_UNRESERVED_CHARACTERS
            : RFC2396_UNRESERVED_WITH_SLASH_CHARACTERS;
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (safeChars.get(c)) {
            // Safe character, so just pass through ...
            result.append(c);
        } else {
            // The character is not a safe character, and must be escaped ...
            result.append(ESCAPE_CHARACTER);
            result.append(Character.toLowerCase(Character.forDigit(c / 16, 16)));
            result.append(Character.toLowerCase(Character.forDigit(c % 16, 16)));
        }
    }
    return result.toString();
}

From source file:XmlValueEncoder.java

/**
 * {@inheritDoc}/*from  w  ww .j a  v a2  s .  c  om*/
 *
 * @see org.jboss.dna.common.text.TextDecoder#decode(java.lang.String)
 */
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '&') {
            int index = iter.getIndex();

            do {
                c = iter.next();
            } while (c != CharacterIterator.DONE && c != ';');

            // We found a closing semicolon
            if (c == ';') {
                String s = encodedText.substring(index + 1, iter.getIndex());

                if (SPECIAL_ENTITIES.containsKey(s)) {
                    sb.append(SPECIAL_ENTITIES.get(s));
                    continue;

                }

                if (s.length() > 0 && s.charAt(0) == '#') {
                    try {
                        sb.append((char) Short.parseShort(s.substring(1, s.length())));
                        continue;
                    } catch (NumberFormatException nfe) {
                        // This is possible in malformed encodings, but let it fall through
                    }
                }
            }

            // Malformed encoding, restore state and pass poorly encoded data back
            c = '&';
            iter.setIndex(index);
        }

        sb.append(c);

    }
    return sb.toString();
}

From source file:UrlEncoder.java

/**
 * {@inheritDoc}// w  w w .j a v a  2s.c  o m
 */
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() == 0)
        return encodedText;
    final StringBuilder result = new StringBuilder();
    final CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == ESCAPE_CHARACTER) {
            boolean foundEscapedCharacter = false;
            // Found the first character in a potential escape sequence, so grab the next two characters ...
            char hexChar1 = iter.next();
            char hexChar2 = hexChar1 != CharacterIterator.DONE ? iter.next() : CharacterIterator.DONE;
            if (hexChar2 != CharacterIterator.DONE) {
                // We found two more characters, but ensure they form a valid hexadecimal number ...
                int hexNum1 = Character.digit(hexChar1, 16);
                int hexNum2 = Character.digit(hexChar2, 16);
                if (hexNum1 > -1 && hexNum2 > -1) {
                    foundEscapedCharacter = true;
                    result.append((char) (hexNum1 * 16 + hexNum2));
                }
            }
            if (!foundEscapedCharacter) {
                result.append(c);
                if (hexChar1 != CharacterIterator.DONE)
                    result.append(hexChar1);
                if (hexChar2 != CharacterIterator.DONE)
                    result.append(hexChar2);
            }
        } else {
            result.append(c);
        }
    }
    return result.toString();
}

From source file:org.intermine.common.swing.text.RestrictedInputDocument.java

/**
 * Insert the given text into this document, as long as it leaves the document valid.
 * //from ww  w .  ja v  a2  s. c  om
 * @param offset The starting offset &gt;= 0.
 * @param str The string to insert; does nothing with null/empty strings.
 * @param attr The attributes for the inserted content.
 * 
 * @throws BadLocationException if the given insert position is not a valid
 * position within the document.
 *   
 * @see javax.swing.text.Document#insertString
 */
@Override
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
    if (str == null) {
        return;
    }

    if (limit < 0 || getLength() + str.length() <= limit) {
        if (convertArray == null || convertArray.length < str.length()) {
            convertArray = new char[str.length()];
        }

        CharacterIterator iter = new StringCharacterIterator(str);
        char c = iter.first();
        int index;
        for (index = 0; c != CharacterIterator.DONE; c = iter.next()) {
            if (!overrideChecks && !StringUtils.contains(allowableCharacters, c)) {

                // At this point, c is invalid. See if a case change remedies this.

                if (caseConvert && Character.isLetter(c)) {
                    if (Character.isLowerCase(c)) {
                        c = Character.toUpperCase(c);
                    } else if (Character.isUpperCase(c)) {
                        c = Character.toLowerCase(c);
                    }
                    if (!StringUtils.contains(allowableCharacters, c)) {
                        // Don't insert but otherwise ignore.
                        return;
                    }
                } else {
                    return;
                }
            }
            convertArray[index++] = c;

        }
        super.insertString(offset, new String(convertArray, 0, index), attr);
    }
}

From source file:com.websystique.springmvc.youtube_api.Function.java

public String replace_all(String token) {
    StringBuilder s = new StringBuilder(token.length());
    CharacterIterator it = new StringCharacterIterator(token);
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        switch (ch) {
        case '?':
            s.append("?####");
            break;
        case '.':
            s.append(".####");
            break;
        case ';':
            s.append(";####");
            break;
        default:/*from w  w w.  ja  v  a2 s. c  om*/
            s.append(ch);
            break;
        }
    }

    token = s.toString();
    return token;
}

From source file:Base64.java

public static byte[] decodeBase64(String s) {
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0, j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '='; ch = it.next()) {
        int v = b64d(ch);
        if (v >= 0) {
            i = (i << 6) | v;/*w w w  .j  av a2  s.  c  o m*/
            j++;
            if (j >= 4) {
                out.write(i >> 16);
                out.write(i >> 8);
                out.write(i);
                i = 0;
                j = 0;
            }
        }
    }
    switch (j) {
    case 3:
        out.write(i >> 10);
        out.write(i >> 2);
        break;
    case 2:
        out.write(i >> 4);
        break;
    }
    return out.toByteArray();
}

From source file:Base64.java

public static byte[] decodeUU(String s) {
    s = s.replaceAll("\r\n|\r|\n|\u2028|\u2029", "\n").trim();
    if (s.startsWith("begin ") && s.endsWith("\nend")) {
        int o = s.indexOf('\n');
        int e = s.length() - 4;
        s = s.substring(o, e).trim();/*from  ww  w .  j a v a2  s.c  o  m*/
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0, j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '`'; ch = it.next()) {
        if (ch <= ' ' || ch >= '`')
            continue;
        while (true) {
            int v = (int) (it.next() - ' ');
            if (v >= 0 && v < 64) {
                i = (i << 6) | v;
                j++;
                if (j >= 4) {
                    out.write(i >> 16);
                    out.write(i >> 8);
                    out.write(i);
                    i = 0;
                    j = 0;
                }
            } else {
                break;
            }
        }
    }
    switch (j) {
    case 3:
        out.write(i >> 10);
        out.write(i >> 2);
        break;
    case 2:
        out.write(i >> 4);
        break;
    }
    return out.toByteArray();
}

From source file:Base64.java

public static byte[] decodeXX(String s) {
    s = s.replaceAll("\r\n|\r|\n|\u2028|\u2029", "\n").trim();
    if (s.startsWith("begin ") && s.endsWith("\nend")) {
        int o = s.indexOf('\n');
        int e = s.length() - 4;
        s = s.substring(o, e).trim();// w  w w . jav a2  s .  c o m
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0, j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '+'; ch = it.next()) {
        if (xxd(ch) < 0)
            continue;
        while (true) {
            int v = xxd(it.next());
            if (v >= 0) {
                i = (i << 6) | v;
                j++;
                if (j >= 4) {
                    out.write(i >> 16);
                    out.write(i >> 8);
                    out.write(i);
                    i = 0;
                    j = 0;
                }
            } else {
                break;
            }
        }
    }
    switch (j) {
    case 3:
        out.write(i >> 10);
        out.write(i >> 2);
        break;
    case 2:
        out.write(i >> 4);
        break;
    }
    return out.toByteArray();
}

From source file:Base64.java

public static byte[] decodeBinHex(String s) {
    // phase 1 - base64 encoding
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream iout = new ByteArrayOutputStream();
    int i = 0, j = 0;
    char ch = it.first();
    if (ch == ':')
        ch = it.next();//w  ww  .j  ava2 s  . c om
    while (ch != CharacterIterator.DONE && ch != ':') {
        int v = hqxd(ch);
        if (v >= 0) {
            i = (i << 6) | v;
            j++;
            if (j >= 4) {
                iout.write(i >> 16);
                iout.write(i >> 8);
                iout.write(i);
                i = 0;
                j = 0;
            }
        }
        ch = it.next();
    }
    switch (j) {
    case 3:
        iout.write(i >> 10);
        iout.write(i >> 2);
        break;
    case 2:
        iout.write(i >> 4);
        break;
    }
    byte[] b = iout.toByteArray();
    // phase 2 - RLE
    int off = 0;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte last = 0;
    while (off < b.length) {
        byte v = b[off++];
        if (v == (byte) 0x90 && off < b.length) {
            int r = b[off++] & 0xFF;
            if (r == 0) {
                out.write(last = (byte) 0x90);
            } else {
                r--;
                while (r-- > 0) {
                    out.write(last);
                }
            }
        } else {
            out.write(last = v);
        }
    }
    return out.toByteArray();
}