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.hyperic.hq.ui.util.SaveChartToDashboardUtil.java

/**
 * Replace characters having special meaning <em>inside</em> HTML tags
 * with their escaped equivalents, using character entities such as
 * <tt>'&amp;'</tt>.//from w  w  w .j a va2s .c o  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.)
 */
private static String forHTMLTag(String aTagFragment) {
    final StringBuffer result = new StringBuffer();

    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    for (char character = iterator.current(); character != StringCharacterIterator.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:hudson.plugins.dimensionsscm.DimensionsChangeLogWriter.java

private static String escapeXML(String inTxt) {
    if (inTxt == null || inTxt.length() == 0)
        return inTxt;

    final StringBuilder outTxt = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(inTxt);
    char character = iterator.current();

    // Scan through strings and escape as necessary...
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            outTxt.append("&lt;");
        } else if (character == '>') {
            outTxt.append("&gt;");
        } else if (character == '\"') {
            outTxt.append("&quot;");
        } else if (character == '\'') {
            outTxt.append("&#039;");
        } else if (character == '&') {
            outTxt.append("&amp;");
        } else {/* w w w  .j av a2  s. co m*/
            outTxt.append(character);
        }
        character = iterator.next();
    }
    return outTxt.toString();
}

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   www. 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.squale.welcom.outils.Util.java

public static String formatJavaScript(String st) {
    if (st == null) {
        return "";
    }//from   w ww  .j a  v  a 2 s.co m

    st = st.replaceAll("\r\n", "\\n");
    final StringBuffer sb = new StringBuffer();
    final StringCharacterIterator iter = new StringCharacterIterator(st);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '\t':
            sb.append("\\t");

            break;

        case '\n':
            sb.append("\\n");

            break;

        case '\'':
            sb.append("\\&#39;");

            break;

        case '\"':
            sb.append("\\&quot;");

            break;

        case '&':
            sb.append("&amp;");

            break;

        case '>':
            sb.append("&gt;");

            break;

        case '<':
            sb.append("&lt;");

            break;

        default:
            sb.append(c);

            break;
        }
    }

    return sb.toString();
}

From source file:Base64.java

public static byte[] decodeUU(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();/*from   ww w .  ja v  a2s .co 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 (ch <= ' ' || ch >= '`')
            continue;
        while (true) {
            int v = (int) (it.next() - ' ');
            if (v >= 0 && v < 64) {
                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:hudson.plugins.dimensionsscm.DimensionsChangeLogWriter.java

private static String escapeHTML(String inTxt) {
    if (inTxt == null || inTxt.length() == 0)
        return inTxt;

    final StringBuilder outTxt = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(inTxt);
    char character = iterator.current();

    // Scan through strings and escape as necessary...
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            outTxt.append("&lt;");
        } else if (character == '>') {
            outTxt.append("&gt;");
        } else if (character == '\"') {
            outTxt.append("&quot;");
        } else if (character == '\'') {
            outTxt.append("&#039;");
        } else if (character == '&') {
            outTxt.append("&amp;");
        } else if (character == ' ') {
            outTxt.append("&nbsp;");
        } else {//from w  w w .ja  v a 2 s . com
            outTxt.append(character);
        }
        character = iterator.next();
    }
    return outTxt.toString();
}

From source file:org.squale.welcom.outils.Util.java

public static String formatEmailList(final String st) {
    final StringBuffer sb = new StringBuffer();
    final StringCharacterIterator iter = new StringCharacterIterator(st);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c != ' ') {
            sb.append(c);//from w ww . ja va 2  s .  com
        }
    }

    return sb.toString();
}

From source file:org.wso2.carbon.ui.taglibs.Breadcrumb.java

/**
 * replaces backslash with forward slash
 * @param str/*from   w  w w.j  ava 2  s . co m*/
 * @return
 */
private static String replaceBacklash(String str) {
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(str);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '\\') {
            result.append("/");
        } else {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:ar.com.tadp.xml.rinzo.core.model.XMLNode.java

/**
 * Devuelve el String sobre el que est posicionado el cursor
 *///from ww w.  j  av  a 2s  .  c o m
public String getStringAt(int offset) {
    int relativeOffset = offset - this.offset;
    int start = 0, end = 0;
    String content = this.getContent();
    StringCharacterIterator iter = new StringCharacterIterator(content);
    char c;

    for (c = iter.setIndex(relativeOffset); c != CharacterIterator.DONE
            && this.isFullIdentifierPart(c); c = iter.previous()) {
    }
    start = this.isFullIdentifierPart(iter.current()) ? iter.getIndex() : iter.getIndex() + 1;

    for (c = iter.setIndex(relativeOffset); c != CharacterIterator.DONE
            && this.isFullIdentifierPart(c); c = iter.next()) {
    }
    end = iter.getIndex();

    return (start <= end) ? content.substring(start, end) : "";
}

From source file:XmlCharacters.java

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