当前位置: 首页>>代码示例>>Java>>正文


Java ParsePosition.getIndex方法代码示例

本文整理汇总了Java中java.text.ParsePosition.getIndex方法的典型用法代码示例。如果您正苦于以下问题:Java ParsePosition.getIndex方法的具体用法?Java ParsePosition.getIndex怎么用?Java ParsePosition.getIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.text.ParsePosition的用法示例。


在下文中一共展示了ParsePosition.getIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: parseResolved0

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
 * Parses and resolves the specified text.
 * <p>
 * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed.
 *
 * @param text  the text to parse, not null
 * @param position  the position to parse from, updated with length parsed
 *  and the index of any error, null if parsing whole string
 * @return the resolved result of the parse, not null
 * @throws DateTimeParseException if the parse fails
 * @throws DateTimeException if an error occurs while resolving the date or time
 * @throws IndexOutOfBoundsException if the position is invalid
 */
private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
    ParsePosition pos = (position != null ? position : new ParsePosition(0));
    DateTimeParseContext context = parseUnresolved0(text, pos);
    if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
        String abbr;
        if (text.length() > 64) {
            abbr = text.subSequence(0, 64).toString() + "...";
        } else {
            abbr = text.toString();
        }
        if (pos.getErrorIndex() >= 0) {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " +
                    pos.getErrorIndex(), text, pos.getErrorIndex());
        } else {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
                    pos.getIndex(), text, pos.getIndex());
        }
    }
    return context.toResolved(resolverStyle, resolverFields);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:DateTimeFormatter.java

示例2: parse

import java.text.ParsePosition; //导入方法依赖的package包/类
@Override
public Number parse(String text, Locale locale) throws ParseException {
	NumberFormat format = getNumberFormat(locale);
	ParsePosition position = new ParsePosition(0);
	Number number = format.parse(text, position);
	if (position.getErrorIndex() != -1) {
		throw new ParseException(text, position.getIndex());
	}
	if (!this.lenient) {
		if (text.length() != position.getIndex()) {
			// indicates a part of the string that was not parsed
			throw new ParseException(text, position.getIndex());
		}
	}
	return number;
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:17,代码来源:AbstractNumberFormatter.java

示例3: parseDate

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
 * Parses the date value using the given date formats.
 *
 * @param dateValue the date value to parse
 * @param dateFormats the date formats to use
 * @param startDate During parsing, two digit years will be placed in the range
 * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
 * be <code>null</code>. When <code>null</code> is given as a parameter, year
 * <code>2000</code> will be used.
 *
 * @return the parsed date or null if input could not be parsed
 */
public static Date parseDate(
        final String dateValue,
        final String[] dateFormats,
        final Date startDate) {
    Args.notNull(dateValue, "Date value");
    final String[] localDateFormats = dateFormats != null ? dateFormats : DEFAULT_PATTERNS;
    final Date localStartDate = startDate != null ? startDate : DEFAULT_TWO_DIGIT_YEAR_START;
    String v = dateValue;
    // trim single quotes around date if present
    // see issue #5279
    if (v.length() > 1 && v.startsWith("'") && v.endsWith("'")) {
        v = v.substring (1, v.length() - 1);
    }

    for (final String dateFormat : localDateFormats) {
        final SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
        dateParser.set2DigitYearStart(localStartDate);
        final ParsePosition pos = new ParsePosition(0);
        final Date result = dateParser.parse(v, pos);
        if (pos.getIndex() != 0) {
            return result;
        }
    }
    return null;
}
 
开发者ID:mozilla-mobile,项目名称:FirefoxData-android,代码行数:38,代码来源:DateUtils.java

示例4: convertToNumber

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
 * Convert the value to a Number using the given valueContext
 *
 * @param value
 *            The value to convert
 * @param valueContext
 *            The locale to use for conversion
 * @return The converted value
 */
protected Result<Number> convertToNumber(String value, ValueContext valueContext) {
	if (value == null) {
		return Result.ok(null);
	}

	// Remove leading and trailing white space
	value = value.trim();

	// Parse and detect errors. If the full string was not used, it is
	// an error.
	ParsePosition parsePosition = new ParsePosition(0);
	Number parsedValue = getFormat(valueContext).parse(value, parsePosition);
	if (parsePosition.getIndex() != value.length()) {
		return Result.error(getErrorMessage());
	}

	if (parsedValue == null) {
		// Convert "" to the empty value
		return Result.ok(null);
	}

	return Result.ok(parsedValue);
}
 
开发者ID:peterl1084,项目名称:bean-grid,代码行数:33,代码来源:AbstractStringToNumberConverterBean.java

示例5: parseAsExponential

import java.text.ParsePosition; //导入方法依赖的package包/类
/** 
    * parse the input string, interpreting it as exponential format.
    * If parseIntegerOnly is set, return only the integer portion of 
    * the mantissa.  (To return the decimal portion of the mantissa, 
    * use parseAsDecimal()).
    */
   public Number parseAsExponential(String text, ParsePosition parsePosition) {
Number out = null;
int p;
int start = parsePosition.getIndex();

Number mantissa = dfmt.parse(text, parsePosition);
if (mantissa == null) return null;
if (isParseIntegerOnly()) return mantissa;

// this is a work-around for a JDK 1.1.6
p = parsePosition.getIndex()-1;
if (p >= 0 && text.charAt(p) == 'E') 
    parsePosition.setIndex(p);

Number expo = efmt.parse(text, parsePosition);
if (expo == null) {
    parsePosition.setIndex(start);
    return null;
}

double val = mantissa.doubleValue() * 
    Math.pow(10, expo.doubleValue());
if (mantissa instanceof Long && expo instanceof Long) 
    out = new Long(Math.round(val));
else 
    out = new Double(val);

return out;
   }
 
开发者ID:etomica,项目名称:etomica,代码行数:36,代码来源:ScientificFormat.java

示例6: convert

import java.text.ParsePosition; //导入方法依赖的package包/类
public Date convert( String value ) {
    ParsePosition position = new ParsePosition( 0 );

    Date date = formatter.parse( value, position );
    if ( position.getIndex() != value.length() )
        throw new ValueConversionException( message( value ) );

    return date;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:DateConverter.java

示例7: parse

import java.text.ParsePosition; //导入方法依赖的package包/类
@Override
public Number parse(String source) throws ParseException {
	ParsePosition parsePosition = new ParsePosition(0);
	Number result = parse(source, parsePosition);

	/**
	 * throw an error if a parse error has occured somewhere in the source string, not only at
	 * the beginning as in {@link NumberFormat}
	 */
	if (parsePosition.getIndex() < source.length()) {
		throw new ParseException("Unparseable number: \"" + source + "\"", parsePosition.getIndex());
	}
	return result;
}
 
开发者ID:transwarpio,项目名称:rapidminer,代码行数:15,代码来源:StrictDecimalFormat.java

示例8: seekNonWs

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
 * Consume whitespace from the current parse position.
 * 
 * @param pattern String to read
 * @param pos current position
 */
private void seekNonWs(String pattern, ParsePosition pos) {
    int len = 0;
    char[] buffer = pattern.toCharArray();
    do {
        len = StrMatcher.splitMatcher().isMatch(buffer, pos.getIndex());
        pos.setIndex(pos.getIndex() + len);
    } while (len > 0 && pos.getIndex() < pattern.length());
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:15,代码来源:ExtendedMessageFormat.java

示例9: processSet

import java.text.ParsePosition; //导入方法依赖的package包/类
private int processSet(String regex, int i, StringBuilder result, UnicodeSet temp, ParsePosition pos) {
    try {
        pos.setIndex(i);
        UnicodeSet x = temp.clear().applyPattern(regex, pos, symbolTable, 0);
        x.complement().complement(); // hack to fix toPattern
        result.append(x.toPattern(false));
        i = pos.getIndex() - 1; // allow for the loop increment
        return i;
    } catch (Exception e) {
        throw (IllegalArgumentException) new IllegalArgumentException("Error in " + regex).initCause(e);
    }
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:13,代码来源:UnicodeRegex.java

示例10: parseUnresolved0

import java.text.ParsePosition; //导入方法依赖的package包/类
private DateTimeParseContext parseUnresolved0(CharSequence text, ParsePosition position) {
    Objects.requireNonNull(text, "text");
    Objects.requireNonNull(position, "position");
    DateTimeParseContext context = new DateTimeParseContext(this);
    int pos = position.getIndex();
    pos = printerParser.parse(context, text, pos);
    if (pos < 0) {
        position.setErrorIndex(~pos);  // index not updated from input
        return null;
    }
    position.setIndex(pos);  // errorIndex not updated from input
    return context;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:DateTimeFormatter.java

示例11: parse

import java.text.ParsePosition; //导入方法依赖的package包/类
@Override
public Number parse(String text, ParsePosition parsePosition) {
    long num = 0;
    boolean sawNumber = false;
    boolean negative = false;
    int base = parsePosition.getIndex();
    int offset = 0;
    for (; base + offset < text.length(); offset++) {
        char ch = text.charAt(base + offset);
        if (offset == 0 && ch == minusSign) {
            if (positiveOnly) {
                break;
            }
            negative = true;
        } else {
            int digit = ch - digits[0];
            if (digit < 0 || 9 < digit) {
                digit = UCharacter.digit(ch);
            }
            if (digit < 0 || 9 < digit) {
                for ( digit = 0 ; digit < 10 ; digit++ ) {
                    if ( ch == digits[digit]) {
                        break;
                    }
                }
            }
            if (0 <= digit && digit <= 9 && num < PARSE_THRESHOLD) {
                sawNumber = true;
                num = num * 10 + digit;
            } else {
                break;
            }
        }
    }
    Number result = null;
    if (sawNumber) {
        num = negative ? num * (-1) : num;
        result = Long.valueOf(num);
        parsePosition.setIndex(base + offset);
    }
    return result;
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:43,代码来源:DateNumberFormat.java

示例12: scanSet

import java.text.ParsePosition; //导入方法依赖的package包/类
void scanSet() {
    UnicodeSet uset = null;
    int startPos;
    ParsePosition pos = new ParsePosition(fScanIndex);
    int i;

    startPos = fScanIndex;
    try {
        uset = new UnicodeSet(fRB.fRules, pos, fSymbolTable, UnicodeSet.IGNORE_SPACE);
    } catch (Exception e) { // TODO:  catch fewer exception types.
        // Repackage UnicodeSet errors as RBBI rule builder errors, with location info.
        error(RBBIRuleBuilder.U_BRK_MALFORMED_SET);
    }

    // Verify that the set contains at least one code point.
    //
    if (uset.isEmpty()) {
        // This set is empty.
        //  Make it an error, because it almost certainly is not what the user wanted.
        //  Also, avoids having to think about corner cases in the tree manipulation code
        //   that occurs later on.
        //  TODO:  this shouldn't be an error; it does happen.
        error(RBBIRuleBuilder.U_BRK_RULE_EMPTY_SET);
    }

    // Advance the RBBI parse postion over the UnicodeSet pattern.
    //   Don't just set fScanIndex because the line/char positions maintained
    //   for error reporting would be thrown off.
    i = pos.getIndex();
    for (;;) {
        if (fNextIndex >= i) {
            break;
        }
        nextCharLL();
    }

    RBBINode n;

    n = pushNewNode(RBBINode.setRef);
    n.fFirstPos = startPos;
    n.fLastPos = fNextIndex;
    n.fText = fRB.fRules.substring(n.fFirstPos, n.fLastPos);
    //  findSetFor() serves several purposes here:
    //     - Adopts storage for the UnicodeSet, will be responsible for deleting.
    //     - Mantains collection of all sets in use, needed later for establishing
    //          character categories for run time engine.
    //     - Eliminates mulitiple instances of the same set.
    //     - Creates a new uset node if necessary (if this isn't a duplicate.)
    findSetFor(n.fText, n, uset);
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:51,代码来源:RBBIRuleScanner.java

示例13: parse

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
 * Parses the specified string, beginning at the specified position, according
 * to this formatter's rules.  This will match the string against all of the
 * formatter's public rule sets and return the value corresponding to the longest
 * parseable substring.  This function's behavior is affected by the lenient
 * parse mode.
 * @param text The string to parse
 * @param parsePosition On entry, contains the position of the first character
 * in "text" to examine.  On exit, has been updated to contain the position
 * of the first character in "text" that wasn't consumed by the parse.
 * @return The number that corresponds to the parsed text.  This will be an
 * instance of either Long or Double, depending on whether the result has a
 * fractional part.
 * @see #setLenientParseMode
 * @stable ICU 2.0
 */
@Override
public Number parse(String text, ParsePosition parsePosition) {

    // parsePosition tells us where to start parsing.  We copy the
    // text in the string from here to the end inro a new string,
    // and create a new ParsePosition and result variable to use
    // for the duration of the parse operation
    String workingText = text.substring(parsePosition.getIndex());
    ParsePosition workingPos = new ParsePosition(0);
    Number tempResult = null;

    // keep track of the largest number of characters consumed in
    // the various trials, and the result that corresponds to it
    Number result = NFRule.ZERO;
    ParsePosition highWaterMark = new ParsePosition(workingPos.getIndex());

    // iterate over the public rule sets (beginning with the default one)
    // and try parsing the text with each of them.  Keep track of which
    // one consumes the most characters: that's the one that determines
    // the result we return
    for (int i = ruleSets.length - 1; i >= 0; i--) {
        // skip private or unparseable rule sets
        if (!ruleSets[i].isPublic() || !ruleSets[i].isParseable()) {
            continue;
        }

        // try parsing the string with the rule set.  If it gets past the
        // high-water mark, update the high-water mark and the result
        tempResult = ruleSets[i].parse(workingText, workingPos, Double.MAX_VALUE);
        if (workingPos.getIndex() > highWaterMark.getIndex()) {
            result = tempResult;
            highWaterMark.setIndex(workingPos.getIndex());
        }
        // commented out because this API on ParsePosition doesn't exist in 1.1.x
        //            if (workingPos.getErrorIndex() > highWaterMark.getErrorIndex()) {
        //                highWaterMark.setErrorIndex(workingPos.getErrorIndex());
        //            }

        // if we manage to use up all the characters in the string,
        // we don't have to try any more rule sets
        if (highWaterMark.getIndex() == workingText.length()) {
            break;
        }

        // otherwise, reset our internal parse position to the
        // beginning and try again with the next rule set
        workingPos.setIndex(0);
    }

    // add the high water mark to our original parse position and
    // return the result
    parsePosition.setIndex(parsePosition.getIndex() + highWaterMark.getIndex());
    // commented out because this API on ParsePosition doesn't exist in 1.1.x
    //        if (highWaterMark.getIndex() == 0) {
    //            parsePosition.setErrorIndex(parsePosition.getIndex() + highWaterMark.getErrorIndex());
    //        }
    return result;
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:75,代码来源:RuleBasedNumberFormat.java

示例14: doParse

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
 * If in "by digits" mode, parses the string as if it were a string
 * of individual digits; otherwise, uses the superclass function.
 * @param text The string to parse
 * @param parsePosition Ignored on entry, but updated on exit to point
 * to the first unmatched character
 * @param baseValue The partial parse result prior to entering this
 * function
 * @param upperBound Only consider rules with base values lower than
 * this when filling in the substitution
 * @param lenientParse If true, try matching the text as numerals if
 * matching as words doesn't work
 * @return If the match was successful, the current partial parse
 * result; otherwise new Long(0).  The result is either a Long or
 * a Double.
 */
public Number doParse(String text, ParsePosition parsePosition, double baseValue,
                    double upperBound, boolean lenientParse) {
    // if we're not in byDigits mode, we can just use the inherited
    // doParse()
    if (!byDigits) {
        return super.doParse(text, parsePosition, baseValue, 0, lenientParse);
    }
    else {
        // if we ARE in byDigits mode, parse the text one digit at a time
        // using this substitution's owning rule set (we do this by setting
        // upperBound to 10 when calling doParse() ) until we reach
        // nonmatching text
        String workText = text;
        ParsePosition workPos = new ParsePosition(1);
        double result;
        int digit;

        DigitList dl = new DigitList();
        while (workText.length() > 0 && workPos.getIndex() != 0) {
            workPos.setIndex(0);
            digit = ruleSet.parse(workText, workPos, 10).intValue();
            if (lenientParse && workPos.getIndex() == 0) {
                Number n = ruleSet.owner.getDecimalFormat().parse(workText, workPos);
                if (n != null) {
                    digit = n.intValue();
                }
            }

            if (workPos.getIndex() != 0) {
                dl.append('0'+digit);

                parsePosition.setIndex(parsePosition.getIndex() + workPos.getIndex());
                workText = workText.substring(workPos.getIndex());
                while (workText.length() > 0 && workText.charAt(0) == ' ') {
                    workText = workText.substring(1);
                    parsePosition.setIndex(parsePosition.getIndex() + 1);
                }
            }
        }
        result = dl.count == 0 ? 0 : dl.getDouble();

        result = composeRuleValue(result, baseValue);
        return new Double(result);
    }
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:62,代码来源:NFSubstitution.java

示例15: parseOffsetLocalizedGMT

import java.text.ParsePosition; //导入方法依赖的package包/类
/**
     * Returns offset from GMT(UTC) in milliseconds for the given localized GMT
     * offset format string. When the given string cannot be parsed, this method
     * sets the current position as the error index to <code>ParsePosition pos</code>
     * and returns 0.
     *
     * @param text the text contains a localized GMT offset string at the position.
     * @param pos the position.
     * @param isShort true if this parser to try the short format first
     * @param hasDigitOffset receiving if the parsed zone string contains offset digits.
     * @return the offset from GMT(UTC) in milliseconds for the given localized GMT
     * offset format string.
     */
    private int parseOffsetLocalizedGMT(String text, ParsePosition pos, boolean isShort, Output<Boolean> hasDigitOffset) {
        int start = pos.getIndex();
        int offset = 0;
        int[] parsedLength = {0};

        if (hasDigitOffset != null) {
            hasDigitOffset.value = false;
        }

        offset = parseOffsetLocalizedGMTPattern(text, start, isShort, parsedLength);

        // For now, parseOffsetLocalizedGMTPattern handles both long and short
        // formats, no matter isShort is true or false. This might be changed in future
        // when strict parsing is necessary, or different set of patterns are used for
        // short/long formats.
//        if (parsedLength[0] == 0) {
//            offset = parseOffsetLocalizedGMTPattern(text, start, !isShort, parsedLength);
//        }

        if (parsedLength[0] > 0) {
            if (hasDigitOffset != null) {
                hasDigitOffset.value = true;
            }
            pos.setIndex(start + parsedLength[0]);
            return offset;
        }

        // Try the default patterns
        offset = parseOffsetDefaultLocalizedGMT(text, start, parsedLength);
        if (parsedLength[0] > 0) {
            if (hasDigitOffset != null) {
                hasDigitOffset.value = true;
            }
            pos.setIndex(start + parsedLength[0]);
            return offset;
        }

        // Check if this is a GMT zero format
        if (text.regionMatches(true, start, _gmtZeroFormat, 0, _gmtZeroFormat.length())) {
            pos.setIndex(start + _gmtZeroFormat.length());
            return 0;
        }

        // Check if this is a default GMT zero format
        for (String defGMTZero : ALT_GMT_STRINGS) {
            if (text.regionMatches(true, start, defGMTZero, 0, defGMTZero.length())) {
                pos.setIndex(start + defGMTZero.length());
                return 0;
            }
        }

        // Nothing matched
        pos.setErrorIndex(start);
        return 0;
    }
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:69,代码来源:TimeZoneFormat.java


注:本文中的java.text.ParsePosition.getIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。