use of javax.measure.format.ParserException in project indriya by unitsofmeasurement.
the class LocalUnitFormat method parse.
public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws ParserException {
// Parsing reads the whole character sequence from the parse position.
int start = cursor.getIndex();
int end = csq.length();
if (end <= start) {
return AbstractUnit.ONE;
}
String source = csq.subSequence(start, end).toString().trim();
if (source.length() == 0) {
return AbstractUnit.ONE;
}
try {
LocalUnitFormatParser parser = new LocalUnitFormatParser(symbolMap, new StringReader(source));
Unit<?> result = parser.parseUnit();
cursor.setIndex(end);
return result;
} catch (TokenException e) {
if (e.currentToken != null) {
cursor.setErrorIndex(start + e.currentToken.endColumn);
} else {
cursor.setErrorIndex(start);
}
// TODO should we throw
throw new IllegalArgumentException(e);
// ParserException here,
// too?
} catch (TokenMgrError e) {
cursor.setErrorIndex(start);
throw new ParserException(e);
}
}
use of javax.measure.format.ParserException in project indriya by unitsofmeasurement.
the class UnitFormatTest method testParseLocal.
@Test
public void testParseLocal() {
final UnitFormat format = LocalUnitFormat.getInstance();
assertThrows(UnsupportedOperationException.class, () -> {
try {
Unit<?> u = format.parse("min");
assertEquals("min", u.getSymbol());
} catch (ParserException e) {
fail(e.getMessage());
}
});
}
use of javax.measure.format.ParserException in project unit-api by unitsofmeasurement.
the class QuantityFormatTest method testParserExceptionWithNullString.
@Test(expected = ParserException.class)
public void testParserExceptionWithNullString() {
ParserException pe = new ParserException(null, 0);
assertEquals(0, pe.getPosition());
assertNull(pe.getParsedString());
throw pe;
}
use of javax.measure.format.ParserException in project unit-api by unitsofmeasurement.
the class UnitFormatTest method testParserExceptionWithNullString.
@Test(expected = ParserException.class)
public void testParserExceptionWithNullString() {
ParserException pe = new ParserException(null, 0);
assertEquals(0, pe.getPosition());
assertNull(pe.getParsedString());
throw pe;
}
use of javax.measure.format.ParserException in project sis by apache.
the class UnitFormat method parse.
/**
* Parses the given text as an instance of {@code Unit}.
* If the parse completes without reading the entire length of the text, an exception is thrown.
*
* <p>The parsing is lenient: symbols can be products or quotients of units like “m∕s”,
* words like “meters per second”, or authority codes like {@code "urn:ogc:def:uom:EPSG::1026"}.
* The product operator can be either {@code '.'} (ASCII) or {@code '⋅'} (Unicode) character.
* Exponent after symbol can be decimal digits as in “m2” or a superscript as in “m²”.</p>
*
* <p>The default implementation delegates to
* <code>{@linkplain #parse(CharSequence, ParsePosition) parse}(symbols, new ParsePosition(0))</code>
* and verifies that all non-white characters have been parsed.</p>
*
* @param symbols the unit symbols or URI to parse.
* @return the unit parsed from the specified symbols.
* @throws ParserException if a problem occurred while parsing the given symbols.
*
* @see Units#valueOf(String)
*/
@Override
public Unit<?> parse(final CharSequence symbols) throws ParserException {
final ParsePosition position = new ParsePosition(0);
final Unit<?> unit = parse(symbols, position);
final int length = symbols.length();
final int unrecognized = CharSequences.skipLeadingWhitespaces(symbols, position.getIndex(), length);
if (unrecognized < length) {
throw new ParserException(Errors.format(Errors.Keys.UnexpectedCharactersAfter_2, CharSequences.trimWhitespaces(symbols, 0, unrecognized), CharSequences.trimWhitespaces(symbols, unrecognized, length)), symbols, unrecognized);
}
return unit;
}
Aggregations