Example usage for java.text StringCharacterIterator next

List of usage examples for java.text StringCharacterIterator next

Introduction

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

Prototype

public char next() 

Source Link

Document

Implements CharacterIterator.next() for String.

Usage

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 a va 2  s.co 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 {//from  w  ww. ja  va2 s . co  m
            outTxt.append(character);
        }
        character = iterator.next();
    }
    return outTxt.toString();
}

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

/**
 * Escape XML entities/*from  ww  w.  ja  v  a2s.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:edu.umd.cfar.lamp.viper.util.StringHelp.java

/**
 * Convert the plain text string into something that
 * is a valid HTML/XML text string (i.e. escaping angle brackets,
 * etc.).//from   ww w.jav  a  2s  .c om
 * I owe a lot to the apache project for their source code
 * was an assistance. It is annoying that they don't have
 * this as a public method somewhere, though.
 * @param str the string to escape 
 * @return the string, with some characters converted to XML character entity references
 */
public static String webify(String str) {
    StringBuffer buff = new StringBuffer();
    StringCharacterIterator iter = new StringCharacterIterator(str);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '<':
            buff.append("&lt;");
            break;

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

        case '"':
            buff.append("&quot;");
            break;

        case '\'':
            buff.append("&apos;");
            break;

        case '&':
            buff.append("&amp;");
            break;

        default:
            if ((c >= ' ' && c < 0xF7) || c == '\n' || c == '\r' || c == '\t') {
                buff.append(c);
            } else {
                buff.append("&#x").append(Integer.toHexString(c)).append(';');
            }
        }
    }
    return buff.toString();
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * See:  http://www.javapractices.com/topic/TopicAction.do?Id=96
 *//* w w  w  .j  av a2 s .  co  m*/
public static String escapeHtml(String html) {
    final StringBuilder escapedHtml = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(html);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            escapedHtml.append("&lt;");
        } else if (character == '>') {
            escapedHtml.append("&gt;");
        } else if (character == '\"') {
            escapedHtml.append("&quot;");
        } else if (character == '\'') {
            escapedHtml.append("&#039;");
        } else if (character == '&') {
            escapedHtml.append("&amp;");
        } else {
            //the char is not a special one
            //add it to the result as is
            escapedHtml.append(character);
        }
        character = iterator.next();
    }
    return escapedHtml.toString();
}

From source file:expect4j.ExpectEmulation.java

public static String escape(final String value) {
    String raw = value;/*from   www . j  a  va 2 s  .c  o  m*/
    boolean isString = false;

    if (value.indexOf('"') == 0 && value.lastIndexOf('"') == value.length() - 1) {
        isString = true;
        raw = value.substring(1, value.length() - 1);
    }

    final StringBuffer result = new StringBuffer();

    StringCharacterIterator iterator = new StringCharacterIterator(raw);
    char character = iterator.current();
    while (character != StringCharacterIterator.DONE) {
        /*
         * All literals need to have backslashes doubled.
         * &;`'"|*?~<>^()[]{}$\
         */
        switch (character) {
        case '&':
        case ';':
        case '\'':
        case '"':
        case '|':
        case '*':
        case '?':
        case '~':
        case '<':
        case '>':
        case '^':
        case '(':
        case ')':
        case '[':
        case ']':
        case '{':
        case '}':
        case '$':
        case '\\':
            result.append("\\");
        default:
            result.append(character);
        }
        character = iterator.next();
    }
    String clean = result.toString();
    if (isString)
        clean = '"' + clean + '"';

    return clean;
}

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.jav a 2 s. 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:edu.sdsc.nbcr.opal.OpalClient.java

/**
 *  This function is used to escape illegal character in html
 *///from  w  w  w.  j ava2  s. c o  m
public static String forXML(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:Unsigned.java

/**
 * Parse a binary number, skipping leading whitespace. Does not throw an
 * exception; if no object can be parsed, index is unchanged!
 * /*from  w  w  w  . j  av  a2  s. c  o m*/
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The binary number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    boolean skipWhitespace = true;
    StringBuffer buffer = new StringBuffer();

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        skipWhitespace = false;

        if ((c == '1') || (c == '0')) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        return (null);
    }

    // convert binary to long
    if (buffer.length() > 64) {
        // larger than a long, error
        return (null);
    }

    long result = 0;
    buffer.reverse();
    int length = buffer.length();
    for (int i = 0; i < length; i++) {
        result += (buffer.charAt(i) == '1') ? 1 << i : 0;
    }
    status.setIndex(iter.getIndex());
    return (new Long(result));
}

From source file:Unsigned.java

/**
 * Parse a binary number into a Number object. If up to 8 bits are parsed,
 * returns a Byte. If more than 8 and up to 16 bits are parsed, return a
 * Short. If more than 16 and up to 32 bits are parsed, return an Integer.
 * If more than 32 and up to 64 bits are parsed, return a Long.
 * //  w  w  w  .  j  a v a 2s .c  o  m
 * @param text
 *            a binary number
 * @param parsePosition
 *            position to start parsing from
 * @return return an integer form of Number object if parse is successful;
 *         <CODE>null</CODE> otherwise
 * 
 * @since 1.0
 */
public Number parse(String text, ParsePosition parsePosition) {
    boolean skipWhitespace = true;
    int startIndex, bits;

    // remove whitespace
    StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex());
    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
    }
    parsePosition.setIndex(iter.getIndex());

    startIndex = parsePosition.getIndex();
    Number result = (Number) parseObject(text, parsePosition);

    if (result == null) {
        return (result);
    }

    bits = parsePosition.getIndex() - startIndex;
    if (bits <= 8) {
        result = new Byte(result.byteValue());
    } else if (bits <= 16) {
        result = new Short(result.shortValue());
    } else if (bits <= 32) {
        result = new Integer(result.intValue());
    } else if (bits <= 64) {
        result = new Long(result.longValue());
    }
    return (result);
}