Java Code Examples for java.text.ParsePosition#getErrorIndex()

The following examples show how to use java.text.ParsePosition#getErrorIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCKLocalizedFieldParser.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="LocalWeekMonthYearPatterns")
public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        assertEquals(parsed.isSupported(YEAR_OF_ERA), true);
        assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true);
        assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) ||
                parsed.isSupported(WeekFields.of(locale).weekOfYear()), true);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern);
    }
}
 
Example 2
Source File: DateTimeFormatter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: DateTimeFormatter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: TestReducedParser.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
    ParsePosition ppos = new ParsePosition(0);
    builder = new DateTimeFormatterBuilder();
    setStrict(strict);
    builder.appendPattern(pattern);
    DateTimeFormatter dtf = builder.toFormatter();

    TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
    assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
        assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
        assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
    }
}
 
Example 5
Source File: TCKLocalizedFieldParser.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="LocalWeekBasedYearPatterns")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        WeekFields weekDef = WeekFields.of(locale);
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef);
    }
}
 
Example 6
Source File: AbstractNumberFormatter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 7
Source File: StdDateFormat.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Date parse(String dateStr) throws ParseException
{
    dateStr = dateStr.trim();
    ParsePosition pos = new ParsePosition(0);
    Date dt = _parseDate(dateStr, pos);
    if (dt != null) {
        return dt;
    }
    StringBuilder sb = new StringBuilder();
    for (String f : ALL_FORMATS) {
        if (sb.length() > 0) {
            sb.append("\", \"");
        } else {
            sb.append('"');
        }
        sb.append(f);
    }
    sb.append('"');
    throw new ParseException
        (String.format("Cannot parse date \"%s\": not compatible with any of standard forms (%s)",
                       dateStr, sb.toString()), pos.getErrorIndex());
}
 
Example 8
Source File: AbstractFormatValidator.java    From data-binding-validator with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Parse the value with the specified <code>Format</code>.</p>
 *
 * @param value The value to be parsed.
 * @param formatter The Format to parse the value with.
 * @return The parsed value if valid or <code>null</code> if invalid.
 */
protected Object parse(String value, Format formatter) {

    ParsePosition pos = new ParsePosition(0);
    Object parsedValue = formatter.parseObject(value, pos);
    if (pos.getErrorIndex() > -1) {
        return null;
    }

    if (isStrict() && pos.getIndex() < value.length()) {
        return null;
    }

    if (parsedValue != null) {
        parsedValue = processParsedValue(parsedValue, formatter);
    }

    return parsedValue;

}
 
Example 9
Source File: DateTimeFormatter.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source File: TCKLocalizedFieldParser.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="LocalWeekBasedYearPatterns")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        WeekFields weekDef = WeekFields.of(locale);
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef);
    }
}
 
Example 11
Source File: AbstractNumberFormatter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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;
}
 
Example 12
Source File: TestNumberParser.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="parseDigitsLenient")
public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
    setStrict(false);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
    if (pos.getErrorIndex() != -1) {
        assertEquals(pos.getErrorIndex(), parseLen);
    } else {
        assertEquals(pos.getIndex(), parseLen);
        assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
 
Example 13
Source File: TestReducedParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseLenientSensitive")
public void test_parseStrict_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
                             Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
 
Example 14
Source File: TestReducedParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseAll")
public void test_parseAllStrict(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}
 
Example 15
Source File: TestReducedParser.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseAll")
public void test_parseAllStrict(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}
 
Example 16
Source File: TestReducedParser.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseAll")
public void test_parseAllLenient(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}
 
Example 17
Source File: TestReducedParser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseLenientSensitive")
public void test_parseStrict(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
    Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
 
Example 18
Source File: TestReducedParser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="ParseLenientSensitive")
public void test_parseLenient_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
                              Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), lenient.parseLen, "error case parse position");
        assertEquals(parsed, lenient.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), lenient.parseLen, "parse position");
        assertParsed(parsed, YEAR, lenient.parseVal != null ? (long) lenient.parseVal : null);
    }
}
 
Example 19
Source File: TestNumberParser.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="parseDigitsLenient")
public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
    setStrict(false);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
    if (pos.getErrorIndex() != -1) {
        assertEquals(pos.getErrorIndex(), parseLen);
    } else {
        assertEquals(pos.getIndex(), parseLen);
        assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
 
Example 20
Source File: TestNumberParser.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="parseSignsLenient")
public void test_parseSignsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
    setStrict(false);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
    if (pos.getErrorIndex() != -1) {
        assertEquals(pos.getErrorIndex(), parseLen);
    } else {
        assertEquals(pos.getIndex(), parseLen);
        assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}