Example usage for java.text StringCharacterIterator StringCharacterIterator

List of usage examples for java.text StringCharacterIterator StringCharacterIterator

Introduction

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

Prototype

public StringCharacterIterator(String text) 

Source Link

Document

Constructs an iterator with an initial index of 0.

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;//  w ww. 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:XmlValueEncoder.java

/**
 * {@inheritDoc}//from   ww w  .j a va  2  s . com
 *
 * @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:EscapeHTML.java

/**
 * Replace characters having special meaning <em>inside</em> HTML tags
 * with their escaped equivalents, using character entities such as <tt>'&amp;'</tt>.
 *
 * <P>The escaped characters are :
 * <ul>/*from  w w w  .  j  a  v a  2  s. c o  m*/
 * <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.)
 */
public String escapeHTMLTag(String aTagFragment) {
    final StringBuffer result = new StringBuffer();

    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);
    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("&#092;");
        } 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:gov.nasa.ensemble.dictionary.nddl.NDDLUtil.java

public static String escape(String str) {
    if (str == null || str.length() == 0)
        return "";
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(str);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '!') {
            result.append(EXCLAMATIONMARK);
        } else if (character == '\"') {
            result.append(DQUOT);/*from   w w w .  j a  va 2s.com*/
        } else if (character == '\'') {
            result.append(SQUOT);
        } else if (character == '#') {
            result.append(NUMBERSIGN);
        } else if (character == '$') {
            result.append(DOLLARSIGN);
        } else if (character == '%') {
            result.append(PERCENTAGE);
        } else if (character == '&') {
            result.append(AMPERSAND);
        } else if (character == '`') {
            result.append(APOSTROPHE);
        } else if (character == '(') {
            result.append(LPARENTHESI);
        } else if (character == ')') {
            result.append(RPARENTHESI);
        } else if (character == '*') {
            result.append(ASTERISK);
        } else if (character == '+') {
            result.append(PLUS);
        } else if (character == ',') {
            result.append(COMMA);
        } else if (character == '.') {
            result.append(DOT);
        } else if (character == ';') {
            result.append(SEMICOLON);
        } else if (character == ':') {
            result.append(COLON);
        } else if (character == '?') {
            result.append(QUESTIONMARK);
        } else if (character == '\\') {
            result.append(BACKSLASH);
        } else if (character == '/') {
            result.append(FORWARDSLASH);
        } else if (character == '>') {
            result.append(GREATERTHAN);
        } else if (character == '<') {
            result.append(LESSTHAN);
        } else if (character == '=') {
            result.append(EQUALSIGN);
        } else if (character == '@') {
            result.append(ATARROA);
        } else if (character == '[') {
            result.append(LBRACKET);
        } else if (character == ']') {
            result.append(RBRACKET);
        } else if (character == '^') {
            result.append(CARET);
        } else if (character == '{') {
            result.append(LCURLYBRACE);
        } else if (character == '}') {
            result.append(RCURLYBRACE);
        } else if (character == '|') {
            result.append(VERTICALBAR);
        } else if (character == '~') {
            result.append(TILDE);
        } else if (character == '-') {
            result.append(HYPHEN);
        } else if (character == ' ') {
            result.append(SPACE);
        } else {
            // the char is not a special one
            // add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString().intern(); // intern so '==' works
}

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   ww w.j a v  a2  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:com.jwebmp.core.utilities.EscapeChars.java

/**
 * Returns opening and closing tags replaced with the HTML Equivalents replaced by their escaped equivalents.
 *
 * @param aText//  w w  w  .  j a v a 2  s  .  co  m
 *
 * @return
 */
public static String disableTags(String aText) {
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        switch (character) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        default:
            //the char is not a special one
            //add it to the result as is
            result.append(character);
            break;
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:org.wso2.carbon.bpel.ui.BpelUIUtil.java

public static String encodeHTML(String aText) {
    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("&amp;");
            //            } else if (character == '\"') {
            //                result.append("&quot;");
            //            } else if (character == '\t') {
            //                addCharEntity(9, result);
            //            } else if (character == '!') {
            //                addCharEntity(33, result);
            //            } else if (character == '#') {
            //                addCharEntity(35, result);
            //            } else if (character == '$') {
            //                addCharEntity(36, result);
            //            } else if (character == '%') {
            //                addCharEntity(37, result);
            //            } else if (character == '\'') {
            //                addCharEntity(39, result);
            //            } else if (character == '(') {
            //                addCharEntity(40, result);
            //            } else if (character == ')') {
            //                addCharEntity(41, result);
            //            } else if (character == '*') {
            //                addCharEntity(42, result);
            //            } else if (character == '+') {
            //                addCharEntity(43, result);
            //            } else if (character == ',') {
            //                addCharEntity(44, result);
            //            } else if (character == '-') {
            //                addCharEntity(45, result);
            //            } else if (character == '.') {
            //                addCharEntity(46, result);
            //            } else if (character == '/') {
            //                addCharEntity(47, result);
            //            } else if (character == ':') {
            //                addCharEntity(58, result);
            //            } else if (character == ';') {
            //                addCharEntity(59, result);
            //            } else if (character == '=') {
            //                addCharEntity(61, result);
            //            } else if (character == '?') {
            //                addCharEntity(63, result);
            //            } else if (character == '@') {
            //                addCharEntity(64, result);
            //            } else if (character == '[') {
            //                addCharEntity(91, result);
            //            } else if (character == '\\') {
            //                addCharEntity(92, result);
            //            } else if (character == ']') {
            //                addCharEntity(93, result);
            //            } else if (character == '^') {
            //                addCharEntity(94, result);
            //            } else if (character == '_') {
            //                addCharEntity(95, result);
            //            } else if (character == '`') {
            //                addCharEntity(96, result);
            //            } else if (character == '{') {
            //                addCharEntity(123, result);
            //            } else if (character == '|') {
            //                addCharEntity(124, result);
            //            } else if (character == '}') {
            //                addCharEntity(125, result);
            //            } else if (character == '~') {
            //                addCharEntity(126, result);
        } else {/*  w  w  w. ja  va2 s  . c o m*/
            //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.woonoz.proxy.servlet.UrlRewriterImpl.java

private static String removeTrailingSlashes(final String text) {
    if (text.isEmpty()) {
        return text;
    }//w ww .j a v a2 s  .co  m
    final CharacterIterator it = new StringCharacterIterator(text);
    Character c = it.last();
    while (c.equals('/')) {
        c = it.previous();
    }
    return text.substring(0, it.getIndex() + 1);
}

From source file:stg.utils.StringUtils.java

/**
 * Joins the given tokens and forms the string by appropriately adding the sepeartorChar and the escapeChar wherever necessary.
 * //from   w  w  w .  j a v  a  2 s .c o m
 * Returns null if array is null or empty string if the array is empty.
 * 
 * @param array Tokens
 * @param separatorChar delimiter character
 * @param escapeChar escape character
 * @return String
 */
public static String join(String[] array, char separatorChar, char escapeChar) {
    if (array == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    for (String str : array) {
        StringCharacterIterator iter = new StringCharacterIterator(str);
        char c = iter.first();
        while (c != CharacterIterator.DONE) {
            if (c == separatorChar) {
                if (escapeChar != CharacterIterator.DONE) {
                    sb.append(escapeChar);
                }
            }
            sb.append(c);
            c = iter.next();
        }
        sb.append(separatorChar);
    }
    if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:UrlEncoder.java

/**
 * {@inheritDoc}//from  w ww.  ja  va  2 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();
}