Example usage for java.text CharacterIterator next

List of usage examples for java.text CharacterIterator next

Introduction

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

Prototype

public char next();

Source Link

Document

Increments the iterator's index by one and returns the character at the new index.

Usage

From source file:net.issarlk.androbunny.inkbunny.API.java

public static String extractTree(Pattern pattern, String data) {
    Matcher matches = pattern.matcher(data);
    if (matches.matches()) {
        data = matches.group(1);/*from  w w  w  . jav  a 2s .  c om*/
        StringBuilder result = new StringBuilder();
        //Extract
        int taglevel = 0;

        CharacterIterator it = new StringCharacterIterator(data);
        int mode = 0;
        int inparamname = 0;
        int inparamvalue = 0;
        int outsidetags = 0;
        int intagname = 0;
        char paramvaluechar = '"';
        HashMap<String, String> params = new HashMap<String, String>();
        StringBuilder tagname = new StringBuilder();
        StringBuilder paramname = new StringBuilder();
        StringBuilder paramvalue = new StringBuilder();
        StringBuilder text = new StringBuilder();
        for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {

            if (c == '<') {
                //start of tag
                c = it.next();
                params.clear();
                outsidetags = 0;
                //Output text
                //Log.d(TAG, repeat(' ', taglevel) + text.toString());
                result.append(text);
                if (c == '/') {
                    //Start of closing tag
                    mode = 3;
                    intagname = 1;
                    c = it.next();
                } else {
                    //Start of openning tag
                    mode = 2;
                    intagname = 1;
                }
                tagname = new StringBuilder();
            }
            if (intagname == 1) {
                if (c < 'a' || c > 'z') {
                    intagname = 0;
                } else {
                    tagname.append(c);
                }
            }
            if ((mode == 2 || mode == 3) && intagname == 0 && inparamname == 0 && inparamvalue == 0 && c >= 'a'
                    && c <= 'z') {
                inparamname = 1;
                paramname = new StringBuilder();
            }
            if (inparamname == 1) {
                if (c == '=') {
                    inparamname = 0;
                    inparamvalue = 1;
                    paramvaluechar = it.next();
                    paramvalue = new StringBuilder();
                    continue;
                } else if (c != ' ') {
                    paramname.append(c);
                }
            }
            if (inparamvalue == 1) {
                if (c == paramvaluechar) {
                    //End of value
                    inparamvalue = 0;
                    params.put(paramname.toString(), paramvalue.toString());
                } else {
                    paramvalue.append(c);
                }
            }
            if (mode == 2 && inparamvalue == 0 && c == '/') {
                c = it.next();
                if (c == '>') {
                    //tag closed at its end
                    mode = 4;
                    if (tagname.toString().equals("br")) {
                        result.append("<br/>");
                    }
                } else {
                    c = it.previous();
                }
            }
            if (c == '>') {
                if (mode == 2) {
                    //openning
                    taglevel += 1;
                    outsidetags = 1;
                    text = new StringBuilder();
                    String paramstr = "";
                    for (String key : params.keySet()) {
                        paramstr += " " + key + "=\"" + params.get(key) + "\"";
                    }
                    //Log.d(TAG, repeat(' ', taglevel - 1) + "<" + tagname.toString() + paramstr + ">");
                    result.append("<" + tagname.toString() + paramstr + ">");
                    mode = 0;
                    continue;
                } else if (mode == 3) {
                    //Closing
                    taglevel -= 1;
                    outsidetags = 1;
                    text = new StringBuilder();
                    //Log.d(TAG, repeat(' ', taglevel) + "</" + tagname.toString() + ">");
                    result.append("</" + tagname.toString() + ">\n");
                    mode = 0;
                    if (taglevel <= 0) {
                        break;
                    }
                    continue;
                } else if (mode == 4) {
                    //closed at end
                    outsidetags = 1;
                    text = new StringBuilder();
                    //Log.d(TAG, repeat(' ', taglevel) + "<" + tagname.toString() + "/>");
                    result.append("<" + tagname.toString() + "/>\n");
                    mode = 0;
                    continue;
                }
            }
            if (outsidetags == 1) {
                //Append char
                text.append(c);
            }
        }

        String res = result.toString();
        return res;
    }
    return null;
}

From source file:XmlValueEncoder.java

/**
 * {@inheritDoc}//ww w  .j  a  v  a 2s  .  c  o  m
 *
 * @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("&amp;");
            break;
        case '"':
            sb.append("&quot;");
            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}/*from  ww w  .j  av 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  w  w .  jav a  2s  .c o m
 *
 * @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}//from w w  w  .  j  a va  2 s. co 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.
 * //  ww w.  j  a v  a 2s.c o m
 * @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  ww .  j  a va2 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;//from  w ww .ja v  a2  s. co  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 a2s . 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();/*from   w  w  w. java 2s.  c om*/
    }
    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();
}