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:com.tripit.auth.OAuthCredential.java

private static void jsonEncodeString(String string, StringBuilder sb) {
    CharacterIterator ci = new StringCharacterIterator(string);
    sb.append('"');
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        switch (c) {
        case '\"':
            sb.append("\\\"");
            break;
        case '\\':
            sb.append("\\\\");
            break;
        case '\b':
            sb.append("\\b");
            break;
        case '\f':
            sb.append("\\f");
            break;
        case '\n':
            sb.append("\\n");
            break;
        case '\r':
            sb.append("\\r");
            break;
        case '\t':
            sb.append("\\t");
            break;
        default://w  w w  .  ja va 2 s .  c o  m
            sb.append(c);
            break;
        }
    }
    sb.append('"');
}

From source file:XmlCharacters.java

/**
 * Determine if the supplied name is a valid XML Name.
 * //from  w  ww. j a  v a 2s  .c o  m
 * @param name the string being checked
 * @return true if the supplied name is indeed a valid XML Name, or false otherwise
 */
public static boolean isValidName(String name) {
    if (name == null || name.length() == 0)
        return false;
    CharacterIterator iter = new StringCharacterIterator(name);
    char c = iter.first();
    if (!isValidNameStart(c))
        return false;
    while (c != CharacterIterator.DONE) {
        if (!isValidName(c))
            return false;
        c = iter.next();
    }
    return true;
}

From source file:XmlCharacters.java

/**
 * Determine if the supplied name is a valid XML NCName.
 * /*ww  w . j  av a2s  .  com*/
 * @param name the string being checked
 * @return true if the supplied name is indeed a valid XML NCName, or false otherwise
 */
public static boolean isValidNcName(String name) {
    if (name == null || name.length() == 0)
        return false;
    CharacterIterator iter = new StringCharacterIterator(name);
    char c = iter.first();
    if (!isValidNcNameStart(c))
        return false;
    while (c != CharacterIterator.DONE) {
        if (!isValidNcName(c))
            return false;
        c = iter.next();
    }
    return true;
}

From source file:org.ppwcode.vernacular.l10n_III.I18nExceptionHelpers.java

protected static String processTemplate(String template, Object context, Locale locale, List<Object> objects)
        throws I18nException {
    StringBuffer result = new StringBuffer(1024);
    CharacterIterator iterator = new StringCharacterIterator(template);
    char token = iterator.first();
    try {//from   w  ww.j a v a 2  s  .  co  m
        while (token != CharacterIterator.DONE) {
            if (token != '{') {
                result.append(token);
            } else {
                result.append("{");
                result.append(processTemplatePattern(context, locale, objects, iterator));
                result.append("}");
            }
            token = iterator.next();
        }
    } catch (I18nException exc) {
        throw new I18nTemplateException("Error processing template", template, context, locale, exc);
    }
    return result.toString();
}

From source file:org.fracturedatlas.athena.audit.manager.AuditManager.java

static Set<String> parseValues(String valueString) {
    HashSet<String> values = new HashSet<String>();
    valueString = StringUtils.trimToEmpty(valueString);
    valueString = StringUtils.strip(valueString, "()");
    valueString = StringUtils.trimToEmpty(valueString);
    CharacterIterator it = new StringCharacterIterator(valueString);
    boolean inString = false;
    int begin = 0;
    int end = 0;//from   ww w. j av a2s.  c o  m
    int numValues = 0;
    StringBuilder sb = new StringBuilder();
    // Iterate over the characters in the forward direction
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        if (ch == '\"') {
            inString = true;
            ch = it.next();
            sb = new StringBuilder();
            for (; ch != CharacterIterator.DONE; ch = it.next()) {
                if (ch == '\\') {
                    // skip any " in a string
                    sb.append(ch);
                    ch = it.next();
                } else if (ch == '\"') {
                    break;
                }
                sb.append(ch);
            }
            inString = false;
            values.add(StringUtils.trimToEmpty(sb.toString()));
        } else if (ch == ',') {
            // new value
        } else if (" \t\n\r".indexOf(ch) > -1) {
            //skip whitespace
        } else {
            // not a comma, whitespace or a string start
            sb = new StringBuilder();
            for (; ch != CharacterIterator.DONE; ch = it.next()) {
                if (ch == ',') {
                    break;
                }
                sb.append(ch);
            }
            inString = false;
            values.add(StringUtils.trimToEmpty(sb.toString()));

        }
    }
    return values;
}

From source file:org.fracturedatlas.athena.search.AthenaSearch.java

public static Set<String> parseValues(String valueString) {
    HashSet<String> values = new HashSet<String>();
    valueString = StringUtils.trimToEmpty(valueString);
    valueString = StringUtils.strip(valueString, "()");
    valueString = StringUtils.trimToEmpty(valueString);
    CharacterIterator it = new StringCharacterIterator(valueString);
    boolean inString = false;
    int begin = 0;
    int end = 0;//from  w  ww . j  a  v  a2 s .  c o  m
    int numValues = 0;
    StringBuilder sb = new StringBuilder();
    // Iterate over the characters in the forward direction
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        if (ch == '\"') {
            inString = true;
            ch = it.next();
            sb = new StringBuilder();
            for (; ch != CharacterIterator.DONE; ch = it.next()) {
                if (ch == '\\') {
                    // skip any " in a string
                    sb.append(ch);
                    ch = it.next();
                } else if (ch == '\"') {
                    break;
                }
                sb.append(ch);
            }
            inString = false;
            values.add(StringUtils.trimToEmpty(sb.toString()));
        } else if (ch == ',') {
            // new value
        } else if (" \t\n\r".indexOf(ch) > -1) {
            //skip whitespace
        } else {
            // not a comma, whitespace or a string start
            sb = new StringBuilder();
            for (; ch != CharacterIterator.DONE; ch = it.next()) {
                if (ch == ',') {
                    break;
                }
                sb.append(ch);
            }
            inString = false;
            values.add(StringUtils.trimToEmpty(sb.toString()));

        }
    }

    return values;
}

From source file:org.fracturedatlas.athena.web.manager.RecordManager.java

static Set<String> parseValues(String valueString) {
    HashSet<String> values = new HashSet<String>();
    valueString = StringUtils.trimToEmpty(valueString);
    valueString = StringUtils.strip(valueString, "()");
    valueString = StringUtils.trimToEmpty(valueString);
    CharacterIterator it = new StringCharacterIterator(valueString);
    boolean inString = false;
    int begin = 0;
    int end = 0;/* ww  w .  j a  v a 2 s .com*/
    int numValues = 0;
    StringBuilder sb = new StringBuilder();
    // Iterate over the characters in the forward direction
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        if (ch == '\"') {
            inString = true;
            ch = it.next();
            sb = new StringBuilder();
            for (; ch != CharacterIterator.DONE; ch = it.next()) {
                if (ch == '\\') {
                    // skip any " in a string
                    sb.append(ch);
                    ch = it.next();
                } else if (ch == '\"') {
                    break;
                }
                sb.append(ch);
            }
            inString = false;
            values.add(StringUtils.trimToEmpty(sb.toString()));
        } else if (ch == ',') {
            // new value
        } else if (" \t\n\r".indexOf(ch) > -1) {
            //skip whitespace
        } else {
            // not a comma, whitespace or a string start
            sb = new StringBuilder();
            for (; ch != CharacterIterator.DONE; ch = it.next()) {
                if (ch == ',') {
                    break;
                }
                sb.append(ch);
            }
            inString = false;
            values.add(StringUtils.trimToEmpty(sb.toString()));

        }
    }

    return values;
}

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   w  w w.jav a 2s  . co  m*/
 *          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:com.itude.mobile.android.util.StringUtil.java

/**
 * Strip a {@link String} by removing HTML elements
 * /*w  ww  .j a v a2 s.co m*/
 * @param textToStrip {@link String} to strip
 * @return stripped {@link String}
 */
public static String stripHTMLTags(String textToStrip) {

    StringBuffer returnText = new StringBuffer(textToStrip.length());

    CharacterIterator iterator = new StringCharacterIterator(textToStrip);

    boolean finished = true;
    boolean started = false;
    for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
        if (ch == '<') {
            started = true;
        } else if (ch == '>') {
            started = false;
            finished = true;
        } else if (finished && !started) {
            returnText.append(ch);
        }

    }

    return returnText.toString().trim();
}

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);// w  w  w.  j a v  a  2 s. 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;
}