/** * Parses <code>source</code> until a non-whitespace character is found. * * @param source the string to parse * @param pos input/ouput parsing parameter. * @return the first non-whitespace character. */ public static char parseNextCharacter(String source, ParsePosition pos) { int index = pos.getIndex(); int n = source.length(); char ret = 0; if (index < n) { char c; do { c = source.charAt(index++); } while (Character.isWhitespace(c) && index < n); pos.setIndex(index); if (index < n) { ret = c; } } return ret; }
/** * Parses <code>source</code> for special double values. These values * include Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY. * * @param source the string to parse * @param value the special value to parse. * @param pos input/ouput parsing parameter. * @return the special number. */ private static Number parseNumber(String source, double value, ParsePosition pos) { Number ret = null; StringBuilder sb = new StringBuilder(); sb.append('('); sb.append(value); sb.append(')'); int n = sb.length(); int startIndex = pos.getIndex(); int endIndex = startIndex + n; if (endIndex < source.length()) { if (source.substring(startIndex, endIndex).compareTo(sb.toString()) == 0) { ret = Double.valueOf(value); pos.setIndex(endIndex); } } return ret; }
/** * Parse <code>source</code> for an expected fixed string. * @param source the string to parse * @param expected expected string * @param pos input/ouput parsing parameter. * @return true if the expected string was there */ public static bool parseFixedstring(String source, String expected, ParsePosition pos) { int startIndex = pos.getIndex(); int endIndex = startIndex + expected.length(); if ((startIndex >= source.length()) || (endIndex > source.length()) || (source.substring(startIndex, endIndex).compareTo(expected) != 0)) { // set index back to start, error index should be the start index pos.setIndex(startIndex); pos.setErrorIndex(startIndex); return false; } // the string was here pos.setIndex(endIndex); return true; }
/** * Parses <code>source</code> until a non-whitespace character is found. * * @param source the string to parse * @param pos input/ouput parsing parameter. On output, <code>pos</code> * holds the index of the next non-whitespace character. */ public static void parseAndIgnoreWhitespace(String source, ParsePosition pos) { parseNextCharacter(source, pos); pos.setIndex(pos.getIndex() - 1); }
/** * Parses <code>source</code> for a number. This method can parse normal, * numeric values as well as special values. These special values include * Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY. * * @param source the string to parse * @param format the number format used to parse normal, numeric values. * @param pos input/ouput parsing parameter. * @return the parsed number. */ public static Number parseNumber(String source, NumberFormat format, ParsePosition pos) { int startIndex = pos.getIndex(); Number number = format.parse(source, pos); int endIndex = pos.getIndex(); // check for error parsing number if (startIndex == endIndex) { // try parsing special numbers double[] special = { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }; for (int i = 0; i < special.length; ++i) { number = parseNumber(source, special[i], pos); if (number != null) { break; } } } return number; }