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

static String escape(String input) {
    if (input == null) {
        return null;
    }/*from www. ja v  a2  s  .c o  m*/
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(input);
    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 {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:Main.java

public static String escape(String input) {
    if (input == null) {
        return null;
    }/*from  ww  w .j  ava 2 s.c o m*/
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(input);
    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 {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:Main.java

public static String escapeTextForXML(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("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {//  w  w  w. j a  v  a2s  .  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:Main.java

/**
 * Escape characters for text appearing as XML data, between tags.
 * <P/>/*  www .j a v  a  2  s .  c om*/
 * The following characters are replaced with corresponding character entities :
 * '<' to '&lt';
 * '>' to '&gt';
 * '&' to '&amp;'
 * '"' to '&quot;'
 * "'" to "&#039;"
 * <P/>
 * Note that JSTL's {@code <c:out>} escapes the exact same set of characters as this method.
 */
public static String escapeCharsForXML(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("&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:Main.java

public static String escapeStringForXML(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("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {/*from  ww w .  j  a  v  a  2  s.c  om*/
            //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

/**
 * Returns opening and closing tags replaced with the HTML Equivalents replaced by their escaped equivalents.
 *
 * @param aText/*from  ww  w .jav  a  2s  .  c  om*/
 *
 * @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:com.jwebmp.core.utilities.EscapeChars.java

/**
 * Replace characters having special meaning in regular expressions with their escaped equivalents, preceded by a '\' character.
 *
 * @param aRegexFragment//from  w  w  w .  j  a  v a  2s .  co  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: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 {/*from   w ww  .j  a  v  a  2  s.c o m*/
            outTxt.append(character);
        }
        character = iterator.next();
    }
    return outTxt.toString();
}

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 {/*  w ww  . j  a v  a 2 s.c om*/
            outTxt.append(character);
        }
        character = iterator.next();
    }
    return outTxt.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);/*w  w w .j  a v a2  s . co  m*/
        } 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
}