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

/**
 * Parse a hex number into a Number object. Hexadecimal numbers may be
 * indicated with a leading character designation of '0x'. If up to 1 byte
 * is parsed, returns a Byte. If more than 1 and up to 2 bytes are parsed,
 * return a Short. If more than 2 and up to 4 bytes are parsed, return an
 * Integer. If more than 4 and up to 8 bytes are parsed, return a Long.
 * //from   ww w.  j a v a2 s  . com
 * @param text
 *            a hexadecimal 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, nibbles;

    // 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;
        }
        break;
    }

    // skip a leading hex designation of the characters '0x'
    if (text.regionMatches(iter.getIndex(), "0x", 0, 2)) {
        parsePosition.setIndex(iter.getIndex() + 2);
    } else {
        parsePosition.setIndex(iter.getIndex());
    }

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

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

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

From source file:HexFormat.java

/**
 * Parse a hexadecimal number, skipping leading whitespace. Does not throw
 * an exception; if no object can be parsed, index is unchanged! Hexadecimal
 * numbers may be indicated with a leading character designation of '0x'.
 * //from  w ww . ja v a  2  s  .com
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The hexadecimal number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    StringBuffer buffer = new StringBuffer();
    char c, c2;
    long result;

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

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

    if (c == CharacterIterator.DONE) {
        return (null);
    }

    if (c == '0') {
        c2 = iter.next();

        if (c2 == CharacterIterator.DONE) {
            return (null);
        }

        if (c2 == 'x') {
            // has a leading '0x' designation, so skip over it
        } else {
            // replace the two characters
            iter.previous();
            iter.previous();
        }
    } else {
        // skip back one character
        iter.previous();
    }

    // gather valid hex digits
    for (c = iter.next(); c != CharacterIterator.DONE; c = iter.next()) {
        if (hexDigits.indexOf(c) != -1) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        // no valid hex digits
        return (null);
    }

    // convert hex to long
    if (buffer.length() > 16) {
        // larger than a long, error
        // with a buffer full of nibbles, the maximum nibbles in a
        // 64 bit number is 16 nibbles
        return (null);
    }

    // parse number
    try {
        result = Long.parseLong(buffer.toString(), 16);
    } catch (NumberFormatException e) {
        // unable to parse number
        return (null);
    }

    status.setIndex(iter.getIndex());
    return (new Long(result));
}

From source file:org.activiti.designer.kickstart.eclipse.sync.SyncUtil.java

protected static String backlashReplace(String myStr) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(myStr);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '\\') {
            result.append("/");
        } else {/*from   w  ww .j a  va  2  s  .  c  o m*/
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:org.architecturerules.domain.SourceDirectory.java

/**
 * <p>Replaces inappropriate backslash with the appropriate slash based on the operating system's requirements</p>
 *
 * <p>For example, on a Windows system, <tt>src/main/resources</tt> becomes <tt>src\\main\\resource</tt></p>
 *
 * <p>TODO: this may be able to be replaced with String.replaceAll, but I couldn't get the regex just right</p>
 *
 * <p>This todo/issue is open at <a href="http://code.google.com/p/architecturerules/issues/detail?id=29">issue
 * 29</a></p>/*w  ww .  j  a va  2 s .c o  m*/
 *
 * @param path String the path to fix
 * @return String the fixed path
 */
String replaceBackslashForOS(final String path) {

    final StringBuffer result = new StringBuffer();

    final StringCharacterIterator iterator = new StringCharacterIterator(path);

    char character = iterator.current();

    final char goal = File.separator.toCharArray()[0];
    final char target = ((goal == '\\') ? '/' : '\\');

    while (character != CharacterIterator.DONE) {

        result.append((character == target) ? goal : character);
        character = iterator.next();
    }

    return result.toString();
}

From source file:org.fao.geonet.kernel.search.LuceneSearcher.java

/**
 * Unused at the moment - but might be useful later.
 * @param aText/*from  w  ww  .j  a  v  a 2 s .  c om*/
 * @param excludes
 * @return
 */
public static String escapeLuceneChars(String aText, String excludes) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '\\' && !excludes.contains("\\")) {
            result.append("\\");
        } else if (character == '!' && !excludes.contains("!")) {
            result.append("\\");
        } else if (character == '(' && !excludes.contains("(")) {
            result.append("\\");
        } else if (character == ')' && !excludes.contains(")")) {
            result.append("\\");
        } else if (character == '*' && !excludes.contains("*")) {
            result.append("\\");
        } else if (character == '+' && !excludes.contains("+")) {
            result.append("\\");
        } else if (character == '-' && !excludes.contains("-")) {
            result.append("\\");
        } else if (character == ':' && !excludes.contains(":")) {
            result.append("\\");
        } else if (character == '?' && !excludes.contains("?")) {
            result.append("\\");
        } else if (character == '[' && !excludes.contains("[")) {
            result.append("\\");
        } else if (character == ']' && !excludes.contains("]")) {
            result.append("\\");
        } else if (character == '^' && !excludes.contains("^")) {
            result.append("\\");
        } else if (character == '{' && !excludes.contains("{")) {
            result.append("\\");
        } else if (character == '}' && !excludes.contains("}")) {
            result.append("\\");
        }
        result.append(character);
        character = iterator.next();
    }
    if (Log.isDebugEnabled(Geonet.SEARCH_ENGINE))
        Log.debug(Geonet.SEARCH_ENGINE, "Escaped: " + result.toString());
    return result.toString();
}

From source file:org.getobjects.eoaccess.EOAdaptor.java

public boolean escapeIntoBuffer(final StringBuilder _sb, final String _value, final char _quoteChar) {
    if (_value == null)
        return false;

    StringCharacterIterator localParser = new StringCharacterIterator(_value);

    for (char c = localParser.current(); c != CharacterIterator.DONE; c = localParser.next()) {
        if (c == _quoteChar) {
            // TBD: buggy? Just quotes single-quotes?
            _sb.append('\'');
            _sb.append('\'');
        } else if (c == '\\') { // escape backslash with double-backslash (why?)
            _sb.append('\\');
            _sb.append('\\');
        } else//from  www .  jav  a 2s .c  o  m
            _sb.append(c);
    }

    return true;
}

From source file:org.getobjects.eoaccess.EOSQLExpression.java

/**
 * This function escapes single quotes and backslashes with itself. Eg:<pre>
 *   Hello 'World'//from  ww  w.j a  v a  2  s .  c om
 *   Hello ''World''</pre>
 * @param _value - String to escape
 * @return escaped String
 */
public static String escapeSQLString(final String _value) {
    if (_value == null)
        return null;
    if (_value.length() == 0)
        return "";

    final StringBuilder buffer = new StringBuilder(_value.length() + 8);
    final StringCharacterIterator localParser = new StringCharacterIterator(_value);

    // slow
    for (char c = localParser.current(); c != CharacterIterator.DONE; c = localParser.next()) {
        if (c == '\'') { // replace ' with ''
            buffer.append('\'');
            buffer.append('\'');
        } else if (c == '\\') { // replace \ with \\
            buffer.append('\\');
            buffer.append('\\');
        } else
            buffer.append(c);
    }
    return buffer.toString();
}

From source file:org.hyperic.hq.ui.action.resource.common.monitor.visibility.IndicatorChartsAction.java

/**
 * Find characters having special meaning <em>inside</em> HTML tags and
 * URLs.//  w w w . j a  v a2 s .  c om
 * 
 * <P>
 * The special characters are :
 * <ul>
 * <li><
 * <li>>
 * <li>"
 * <li>'
 * <li>\
 * <li>&
 * <li>|
 * <li>?
 * </ul>
 * 
 * <P>
 */
private int indexOfSpecialChars(String aTagFragment) {
    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    int i = 0;
    for (char character = iterator.current(); character != StringCharacterIterator.DONE; character = iterator
            .next(), i++) {
        switch (character) {
        case '<':
        case '>':
        case '\"':
        case '\'':
        case '\\':
        case '&':
        case '|':
        case '?':
            return i;
        default:
            break;
        }
    }
    return -1;
}

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 v  a  2s.  c om*/
 * 
 * <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:org.rhq.enterprise.gui.legacy.action.resource.common.monitor.visibility.IndicatorChartsAction.java

/**
 * Find characters having special meaning <em>inside</em> HTML tags and URLs.
 * <p/>//from  w  ww.  j a  v  a2s .co  m
 * <p/>
 * The special characters are : <ul> <li>< <li>> <li>" <li>' <li>\ <li>& <li>| <li>? </ul>
 * <p/>
 * <p/>
 */
private static int indexOfSpecialChars(String aTagFragment) {
    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    int i = 0;
    for (char character = iterator.current(); character != CharacterIterator.DONE; character = iterator
            .next(), i++) {
        switch (character) {
        case '<':
        case '>':
        case '\"':
        case '\'':
        case '\\':
        case '&':
        case '|':
        case '?':
            return i;
        default:
            break;
        }
    }
    return -1;
}