Example usage for java.text CharacterIterator DONE

List of usage examples for java.text CharacterIterator DONE

Introduction

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

Prototype

char DONE

To view the source code for java.text CharacterIterator DONE.

Click Source Link

Document

Constant that is returned when the iterator has reached either the end or the beginning of the text.

Usage

From source file:com.googlecode.jsonplugin.JSONWriter.java

/**
 * escape characters//from w w w  .j a v  a  2s  . c om
 */
private void string(Object obj) {
    this.add('"');

    CharacterIterator it = new StringCharacterIterator(obj.toString());

    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"') {
            this.add("\\\"");
        } else if (c == '\\') {
            this.add("\\\\");
        } else if (c == '/') {
            this.add("\\/");
        } else if (c == '\b') {
            this.add("\\b");
        } else if (c == '\f') {
            this.add("\\f");
        } else if (c == '\n') {
            this.add("\\n");
        } else if (c == '\r') {
            this.add("\\r");
        } else if (c == '\t') {
            this.add("\\t");
        } else if (Character.isISOControl(c)) {
            this.unicode(c);
        } else {
            this.add(c);
        }
    }

    this.add('"');
}

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  ww.  j a  v  a  2 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:Unsigned.java

/**
 * Parse a binary number, skipping leading whitespace. Does not throw an
 * exception; if no object can be parsed, index is unchanged!
 * //  w w w  .  j a  va2  s. com
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The binary number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    boolean skipWhitespace = true;
    StringBuffer buffer = new StringBuffer();

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        skipWhitespace = false;

        if ((c == '1') || (c == '0')) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        return (null);
    }

    // convert binary to long
    if (buffer.length() > 64) {
        // larger than a long, error
        return (null);
    }

    long result = 0;
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }
    status.setIndex(iter.getIndex());
    return (new Long(result));
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * See:  http://www.javapractices.com/topic/TopicAction.do?Id=96
 *//*from w  ww  . j a va 2 s. c  om*/
public static String escapeHtml(String html) {
    final StringBuilder escapedHtml = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(html);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            escapedHtml.append("&lt;");
        } else if (character == '>') {
            escapedHtml.append("&gt;");
        } else if (character == '\"') {
            escapedHtml.append("&quot;");
        } else if (character == '\'') {
            escapedHtml.append("&#039;");
        } else if (character == '&') {
            escapedHtml.append("&amp;");
        } else {
            //the char is not a special one
            //add it to the result as is
            escapedHtml.append(character);
        }
        character = iterator.next();
    }
    return escapedHtml.toString();
}

From source file:org.rhq.enterprise.gui.legacy.action.resource.common.monitor.visibility.ViewChartAction.java

/**
 * Replace characters having special meaning <em>inside</em> HTML tags with
 * their escaped equivalents, using character entities such as
 * <tt>'&amp;'</tt>./*from  ww  w . j av a 2  s.co m*/
 * <P>
 * The escaped characters are :
 * <ul>
 * <li><
 * <li>>
 * <li>"
 * <li>'
 * <li>\
 * <li>&
 * </ul>
 * <P>
 * This method ensures that arbitrary text appearing inside a tag does not
 * "confuse" the tag. For example, <tt>HREF='Blah.do?Page=1&Sort=ASC'</tt>
 * does not comply with strict HTML because of the ampersand, and should be
 * changed to <tt>HREF='Blah.do?Page=1&amp;Sort=ASC'</tt>. This is
 * commonly seen in building query strings. (In JSTL, the c:url tag performs
 * this task automatically.)
 * 
 * @param aTagFragment
 *           some HTML to be escaped
 * @return escaped HTML
 */
private static String forHTMLTag(String aTagFragment) {
    final StringBuffer result = new StringBuffer();

    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    for (char character = iterator.current(); character != CharacterIterator.DONE; character = iterator
            .next()) {
        switch (character) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        case '\"':
            result.append("&quot;");
            break;
        case '\'':
            result.append("&#039;");
            break;
        case '\\':
            result.append("&#092;");
            break;
        case '&':
            result.append("&amp;");
            break;
        case '|':
            result.append("&#124;");
            break;
        case ',':
            result.append("&#44;");
            break;
        default:
            // the char is not a special one add it to the result as is
            result.append(character);
            break;
        }
    }
    return result.toString();
}

From source file:org.apache.arrow.vector.util.Text.java

/**
 * For the given string, returns the number of UTF-8 bytes required to encode the string.
 *
 * @param string//from www  .  j  a  v a  2  s  .  c om
 *          text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                iter.previous(); // rewind one
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}

From source file:stg.utils.RandomStringGenerator.java

/**
 * Validates the given format./*from w  w w.ja  va  2  s. c o m*/
 * Valid characters are :
 * <li>a<dt>For lower case alphabets.
 * <li>A<dt>For upper case alphabets.
 * <li>9<dt>For numbers.
 * <li>#<dt>For special characters.
 * The format characters can be repeated in whatever sequence. 
 * The random string generated will have the same length as that of the format.
 * 
 * @param format
 */
private void validate(String format) {
    CharacterIterator iter = new StringCharacterIterator(format);
    char c = iter.first();
    boolean constantStarted = false;
    while (c != CharacterIterator.DONE) {
        switch (c) {
        case ESCAPE:
            c = iter.next();
            if (c == CharacterIterator.DONE) {
                throw new IllegalArgumentException(
                        "Invalid format! Escape character found without any associated character that was to be escaped.");
            }
            break;
        case LOWER_CASE:
            break;
        case UPPER_CASE:
            break;
        case NUMBER:
            break;
        case SPECIAL:
            break;
        case START_CONSTANT:
            if (!constantStarted)
                constantStarted = true;
            break;
        case END_CONSTANT:
            if (constantStarted)
                constantStarted = false;
            break;
        default:
            if (!constantStarted)
                throw new IllegalArgumentException("Invalid format character found '" + c + "'");
        }
        c = iter.next();
    }
}

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();/*from www  .  ja  v a  2s .  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();
}

From source file:de.innovationgate.utils.WGUtils.java

/**
 * Clears out "internal strings" from a text that contains some kind of
 * program code. I.e. if the text itself contains string delimiter
 * characters, like " or ', the contents between these characters is
 * regarded a string. Its contents will be cleared in the text version that
 * is returned by this method. This is useful to prepare a text for an
 * operation, that may not react on the contents of strings inside it. This
 * method regards the character \ as an escape sign for string delimiters.
 * So delimiter characters that are prefixed by a \ will be ignored.
 * /*from  w  ww .  java  2  s . com*/
 * @param colString
 *            The text
 * @param stringDelimiter
 *            The character that introduces and closes strings inside the
 *            text
 * @param replaceChar
 *            The character that is used to clear out strings.
 * @return The text with cleared out internal strings
 */
public static String clearStrings(String colString, char stringDelimiter, char replaceChar) {

    CharacterIterator it = new StringCharacterIterator(colString);
    StringBuffer out = new StringBuffer();
    boolean inAString = false;
    char prevChar = ' ';
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        // Look for string introducor
        if (c == stringDelimiter && prevChar != '\\') {
            inAString = !inAString;
            out.append(stringDelimiter);
        } else if (inAString) {
            out.append(replaceChar);
        } else {
            out.append(c);
        }
        prevChar = c;
    }
    return out.toString();

}

From source file:XmlNameEncoder.java

/**
 * {@inheritDoc}/*from w  w  w. j av a2s  .  c  o m*/
 * 
 * @see org.jboss.dna.common.text.TextDecoder#decode(java.lang.String)
 */
public String decode(String encodedText) {
    if (encodedText == null)
        return null;
    if (encodedText.length() < 7) {
        // Not big enough to have an encoded sequence
        return encodedText;
    }
    StringBuilder sb = new StringBuilder();
    char[] digits = new char[4];
    CharacterIterator iter = new StringCharacterIterator(encodedText);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '_') {
            // Read the next character, if there is one ...
            char next = iter.next();
            if (next == CharacterIterator.DONE) {
                sb.append(c);
                break;
            }
            // If the next character is not 'x', then these are just regular characters ...
            if (next != 'x') {
                sb.append(c).append(next);
                continue;
            }
            // Read the next 4 characters (digits) and another '_' character ...
            digits[0] = iter.next();
            if (digits[0] == CharacterIterator.DONE) {
                sb.append(c).append(next);
                break;
            }
            digits[1] = iter.next();
            if (digits[1] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 1);
                break;
            }
            digits[2] = iter.next();
            if (digits[2] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 2);
                break;
            }
            digits[3] = iter.next();
            if (digits[3] == CharacterIterator.DONE) {
                sb.append(c).append(next).append(digits, 0, 3);
                break;
            }
            char underscore = iter.next();
            if (underscore != '_') { // includes DONE
                sb.append(c).append(next).append(digits, 0, 4);
                if (underscore == CharacterIterator.DONE)
                    break;
                sb.append(underscore);
                continue;
            }
            // We've read all 4 digits, including the trailing '_'
            // Now parse into the resulting character
            try {
                sb.appendCodePoint(Integer.parseInt(new String(digits), 16));
            } catch (NumberFormatException e) {
                // code was not hexadecimal, so just write out the characters as is ...
                sb.append(c).append(next).append(digits).append(underscore);
            }
        } else {
            // Just append other characters ...
            sb.append(c);
        }
    }
    return sb.toString();
}