Example usage for java.text StringCharacterIterator current

List of usage examples for java.text StringCharacterIterator current

Introduction

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

Prototype

public char current() 

Source Link

Document

Implements CharacterIterator.current() for String.

Usage

From source file:org.rhq.enterprise.gui.legacy.action.resource.common.monitor.visibility.ViewChartAction.java

/**
 * Replace characters having special meaning <em>inside</em> HTML tags with
 * their escaped equivalents, using character entities such as
 * <tt>'&amp;'</tt>./*from ww w .  jav  a  2 s .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.)
 * 
 * @param aTagFragment
 *           some HTML to be escaped
 * @return escaped HTML
 */
private static String forHTMLTag(String aTagFragment) {
    final StringBuffer result = new StringBuffer();

    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    for (char character = iterator.current(); character != CharacterIterator.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: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  va  2 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:org.wso2.carbon.ui.taglibs.Breadcrumb.java

/**
 * replaces backslash with forward slash
 * @param str/*from   ww  w . jav a 2 s.c om*/
 * @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:uk.ac.ebi.intact.dataexchange.psimi.xml.converter.util.PsiConverterUtils.java

public static String forXML(String aText) {
    if (aText == null) {
        return null;
    }//from   w  ww. ja  v a 2 s.c o m

    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 if (!XMLChar.isInvalid(character)) {
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}