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:com.entertailion.android.slideshow.utils.Utils.java

/**
 * Escape XML entities/*from w  ww .j  av a2s .  co 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:com.itude.mobile.android.util.StringUtil.java

/**
 * See:  http://www.javapractices.com/topic/TopicAction.do?Id=96
 */// w w  w  .  ja v a2s .c om
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  w  ww .  j  a va2 s.c  om
    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:edu.sdsc.nbcr.opal.OpalClient.java

/**
 *  This function is used to escape illegal character in html
 *///  w  ww  .ja  v a  2  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: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.
 *//*from  www.j  a  va2  s . c  om*/
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: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  a2s .  c  om
 * <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:info.magnolia.cms.taglibs.util.BaseImageTag.java

/**
 * Replace any special characters that are not letters or numbers with a replacement string. The two exceptions are
 * '-' and '_', which are allowed.// w w  w .j  a  v a 2  s .co  m
 */
public String convertToSimpleString(String string) {

    final StringBuffer result = new StringBuffer();

    final StringCharacterIterator iterator = new StringCharacterIterator(string);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        int charType = Character.getType(character);
        if (charType == Character.SPACE_SEPARATOR) {
            result.append("-");
        } else if ((charType != Character.UPPERCASE_LETTER) && (charType != Character.LOWERCASE_LETTER)
                && (charType != Character.DECIMAL_DIGIT_NUMBER) && (charType != Character.CONNECTOR_PUNCTUATION)
                && (charType != Character.DASH_PUNCTUATION)) {
            result.append("u" + (int) character);

        } 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  ww. j  a  va 2s .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 ww .  j  av  a  2 s  .co  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);
}

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

/**
 * Devuelve el String sobre el que est posicionado el cursor
 */// w  ww  .  j  av  a  2 s.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) : "";
}