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:UrlEncoder.java

/**
 * {@inheritDoc}//from  w w w.j a  v  a2  s  .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:Main.java

/**
 * Serialises an <code>AttributedString</code> object.
 *
 * @param as  the attributed string object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*from  w  w  w .  j av a  2 s  . co m*/
public static void writeAttributedString(AttributedString as, ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (as != null) {
        stream.writeBoolean(false);
        AttributedCharacterIterator aci = as.getIterator();
        // build a plain string from aci
        // then write the string
        StringBuffer plainStr = new StringBuffer();
        char current = aci.first();
        while (current != CharacterIterator.DONE) {
            plainStr = plainStr.append(current);
            current = aci.next();
        }
        stream.writeObject(plainStr.toString());

        // then write the attributes and limits for each run
        current = aci.first();
        int begin = aci.getBeginIndex();
        while (current != CharacterIterator.DONE) {
            // write the current character - when the reader sees that this
            // is not CharacterIterator.DONE, it will know to read the
            // run limits and attributes
            stream.writeChar(current);

            // now write the limit, adjusted as if beginIndex is zero
            int limit = aci.getRunLimit();
            stream.writeInt(limit - begin);

            // now write the attribute set
            Map atts = new HashMap(aci.getAttributes());
            stream.writeObject(atts);
            current = aci.setIndex(limit);
        }
        // write a character that signals to the reader that all runs
        // are done...
        stream.writeChar(CharacterIterator.DONE);
    } else {
        // write a flag that indicates a null
        stream.writeBoolean(true);
    }

}

From source file:com.entertailion.android.slideshow.utils.Utils.java

/**
 * Escape XML entities//from  ww  w .  j av  a 2s  . c o m
 * 
 * @param aText
 * @return
 */
public static final String escapeXML(String aText) {
    if (null == aText) {
        return "";
    }
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {
            // the char is not a special one
            // add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:com.jwebmp.core.utilities.EscapeChars.java

/**
 * Replace characters having special meaning in regular expressions with their escaped equivalents, preceded by a '\' character.
 *
 * @param aRegexFragment/*  ww  w  . ja  v a  2  s . c  o m*/
 *
 * @return
 */
public static String forRegex(String aRegexFragment) {
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (EscapeChars.EscapeRegexCharset.contains(character)) {
            result.append('\\' + character);
        } else {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

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.
 *///from   w  w w. j a va2 s  .c om
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:stg.utils.StringUtils.java

/**
 * Returns the count of tokens in the given string separated by a space.
 * @param text string//from   w w  w . j av a2  s.  co  m
 * @return int count
 */
public static int countTokens(String text) {
    return countTokens(text, ' ', CharacterIterator.DONE);
}

From source file:EscapeHTML.java

/**
 * Return <tt>aText</tt> with all start-of-tag and end-of-tag characters
 * replaced by their escaped equivalents.
 *
 * <P>If user input may contain tags which must be disabled, then call
 * this method, not {@link #forHTMLTag}. This method is used for text appearing
 * <em>outside</em> of a tag, while {@link #forHTMLTag} is used for text appearing
 * <em>inside</em> an HTML tag.
 *
 * <P>It is not uncommon to see text on a web page presented erroneously, because
 * <em>all</em> special characters are escaped (as in {@link #forHTMLTag}), instead of 
 * just the start-of-tag and end-of-tag characters. In
 * particular, the ampersand character is often escaped not once but <em>twice</em> :
 * once when the original input occurs, and then a second time when the same item is
 * retrieved from the database. This occurs because the ampersand is the only escaped
 * character which appears in a character entity.
 *///  w w w .ja  v a  2s .c o m
public String escapeDisableTags(String aText) {

    final StringBuffer result = new StringBuffer();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else {
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:org.talend.dataprep.transformation.actions.math.ExtractNumber.java

/**
 * @param value the value to parse.//from  ww  w  .j a va 2 s  .  c om
 * @param defaultValue the value to return when no number can be extracted
 * @return the number extracted out of the given value.
 */
protected static String extractNumber(String value, String defaultValue) {

    // easy case
    if (StringUtils.isEmpty(value)) {
        return defaultValue;
    }

    // Test if the input value is a valid number before removing any characters:
    if (NumericHelper.isBigDecimal(value)) {
        // If yes (no exception thrown), return the value as it, no change required:
        return String.valueOf(BigDecimalParser.toBigDecimal(value));
    }

    StringCharacterIterator iter = new StringCharacterIterator(value);

    MetricPrefix metricPrefixBefore = null, metricPrefixAfter = null;

    boolean numberFound = false;

    // we build a new value including only number or separator as , or .
    StringBuilder reducedValue = new StringBuilder(value.length());

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        // we remove all non numeric characters but keep separators
        if (NumberUtils.isNumber(String.valueOf(c)) || SEPARATORS.contains(c)) {
            reducedValue.append(c);
            numberFound = true;
        } else {
            // we take the first metric prefix found before and after a number found
            if (metricPrefixBefore == null) {
                MetricPrefix found = METRICPREFIXES.get(String.valueOf(c));
                if (found != null && !numberFound) {
                    metricPrefixBefore = found;
                }
            }
            if (metricPrefixAfter == null) {
                MetricPrefix found = METRICPREFIXES.get(String.valueOf(c));
                if (found != null && numberFound) {
                    metricPrefixAfter = found;
                }
            }

        }
    }

    if (!NumericHelper.isBigDecimal(reducedValue.toString())) {
        return defaultValue;
    }
    BigDecimal bigDecimal = BigDecimalParser.toBigDecimal(reducedValue.toString());

    if (metricPrefixBefore != null || metricPrefixAfter != null) {
        // the metrix found after use first
        MetricPrefix metricPrefix = metricPrefixAfter != null ? metricPrefixAfter : metricPrefixBefore;
        bigDecimal = bigDecimal.multiply(metricPrefix.getMultiply());
    }

    DecimalFormat decimalFormat = new DecimalFormat("0.#");
    decimalFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS_DISPLAY);
    return decimalFormat.format(bigDecimal.stripTrailingZeros());
}

From source file:UrlEncoder.java

/**
 * {@inheritDoc}/*from  ww  w.  ja va2 s.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:stg.utils.StringUtils.java

/**
 * Returns the count of tokens in the given string separated by the separator char.
 * /*from ww  w  .ja v a  2  s  . c  om*/
 * Return zero if the string is null and 1 if empty.
 * 
 * @param text
 * @param separatorChar
 * @return int count
 */
public static int countTokens(String text, char separatorChar) {
    return countTokens(text, separatorChar, CharacterIterator.DONE);
}