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: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   w ww  .  j a  va  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:XmlCharacters.java

/**
 * Determine if the supplied name is a valid XML Name.
 * /* w w w .  j av  a2s .  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.
 * /*from w ww  .ja  va  2  s . c  om*/
 * @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

/**
 * Helper method for processTemplate to scan the full pattern, once the beginning of a pattern was found.
 *//*w ww .j  ava2  s.  co m*/
private static String processTemplatePattern(Object context, Locale locale, List<Object> objects,
        CharacterIterator iterator) throws I18nException {
    // previous token was "{", scan up to balanced "}"
    // scan full pattern now
    StringBuffer patternAcc = new StringBuffer(128);
    char token = ' '; // initialise with dummy value
    int balance = 1;
    while ((balance > 0) && (token != CharacterIterator.DONE)) {
        token = iterator.next();
        patternAcc.append(token);
        if (token == '{') {
            balance++;
        } else if (token == '}') {
            balance--;
        }
    }
    // done or bad template ?!?
    if (token == CharacterIterator.DONE) {
        throw new I18nTemplateException("Bad template pattern", patternAcc.toString());
    }
    // remove last "}"
    patternAcc.setLength(patternAcc.length() - 1);
    // prepare pattern, treat the "," for formatting parts
    int comma = patternAcc.indexOf(",");
    String pattern = null;
    String patternPostfix = "";
    if (comma == -1) {
        pattern = patternAcc.toString();
    } else if (comma == 0) {
        throw new I18nTemplateException("Bad template pattern", patternAcc.toString());
    } else {
        pattern = patternAcc.substring(0, comma);
        patternPostfix = patternAcc.substring(comma, patternAcc.length());
    }
    // process pattern
    String processedPattern = processPattern(pattern, context, locale, objects);
    return processedPattern + patternPostfix;
}

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 ww  w.j  ava  2  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;//from  ww  w.ja v  a  2 s .  co 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.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  w w .j ava2 s.  c  o 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:com.itude.mobile.android.util.StringUtil.java

/**
 * Strip a {@link String} by removing HTML elements
 * /*from  ww  w  .j ava 2s .c  o 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:Main.java

public static String encode(String unencodedValue) {

    CharacterIterator charIterator;
    StringBuilder encodeBuilder;/*from  ww  w.  j av a2s.c  o  m*/
    char currentChar;

    encodeBuilder = new StringBuilder();
    charIterator = new StringCharacterIterator(unencodedValue);
    while ((currentChar = charIterator.current()) != CharacterIterator.DONE) {
        switch (currentChar) {
        case '&':
            encodeBuilder.append("&amp;");
            break;
        case '<':
            encodeBuilder.append("&lt;");
            break;
        case '>':
            encodeBuilder.append("&gt;");
            break;
        default:
            encodeBuilder.append(currentChar);
        }
        charIterator.next();
    }

    return encodeBuilder.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  w  w  w .j  a va2  s .c  o  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;
}