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: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.
 * /* ww  w.  j a va2 s  . co m*/
 * @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 w  w  .  ja v a  2 s  .  c  o  m
 * @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:ar.com.tadp.xml.rinzo.core.model.XMLNode.java

/**
 * Devuelve el String sobre el que est posicionado el cursor
 *///from w  w w  .  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) : "";
}

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 ww w .  java 2s. c o  m*/
 * <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.//from   w w  w.  jav  a  2  s .  c  o  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:com.topsec.tsm.sim.report.util.ReportUiUtil.java

/**
 * sanitize HTML?TAG? "&" --> "&amp" "<" --> "&lt" ">" --> "&gt" "\""
 * --> "&quot" "\r\n" --> "<br>" &amp; &#39;
 * //from  w  w  w  .j  av a 2s .co m
 * @param str
 *            strin?
 * @return String ??
 * @version 20040810
 */

public static String sanitize(String s) {
    if (s == null)
        return null;
    StringBuffer stringbuffer = new StringBuffer();
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) {
        String s1 = (String) sanitizeTable.get(new Character(c));
        if (s1 != null)
            stringbuffer.append(s1);
        else
            stringbuffer.append(c);
    }

    return stringbuffer.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);/*from   w ww  .ja  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
}

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 www . j  a  v a2s .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>//from  www .j  a  v  a  2 s .com
 *
 * @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/* ww  w  .ja 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();
}