Search in sources :

Example 6 with ParserException

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);
    }
}
Also used : ParserException(javax.measure.format.ParserException) TokenException(tech.units.indriya.internal.format.TokenException) StringReader(java.io.StringReader) LocalUnitFormatParser(tech.units.indriya.internal.format.LocalUnitFormatParser) TokenMgrError(tech.units.indriya.internal.format.TokenMgrError)

Example 7 with ParserException

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());
        }
    });
}
Also used : ParserException(javax.measure.format.ParserException) UnitFormat(javax.measure.format.UnitFormat) LocalUnitFormat(tech.units.indriya.format.LocalUnitFormat) SimpleUnitFormat(tech.units.indriya.format.SimpleUnitFormat) Test(org.junit.jupiter.api.Test)

Example 8 with ParserException

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;
}
Also used : ParserException(javax.measure.format.ParserException) Test(org.junit.Test)

Example 9 with ParserException

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;
}
Also used : ParserException(javax.measure.format.ParserException) Test(org.junit.Test)

Example 10 with ParserException

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;
}
Also used : ParserException(javax.measure.format.ParserException) ParsePosition(java.text.ParsePosition)

Aggregations

ParserException (javax.measure.format.ParserException)15 Test (org.junit.Test)5 StringReader (java.io.StringReader)3 ParsePosition (java.text.ParsePosition)2 UnitFormat (javax.measure.format.UnitFormat)2 InvalidGeodeticParameterException (org.apache.sis.referencing.factory.InvalidGeodeticParameterException)2 UnavailableFactoryException (org.apache.sis.referencing.factory.UnavailableFactoryException)2 TokenException (tec.uom.se.internal.format.TokenException)2 TokenMgrError (tec.uom.se.internal.format.TokenMgrError)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Unit (javax.measure.Unit)1 FactoryDataException (org.apache.sis.referencing.factory.FactoryDataException)1 Test (org.junit.jupiter.api.Test)1 LocalUnitFormatParser (tec.uom.se.internal.format.LocalUnitFormatParser)1 UnitFormatParser (tec.uom.se.internal.format.UnitFormatParser)1 LocalUnitFormat (tech.units.indriya.format.LocalUnitFormat)1 SimpleUnitFormat (tech.units.indriya.format.SimpleUnitFormat)1 LocalUnitFormatParser (tech.units.indriya.internal.format.LocalUnitFormatParser)1 TokenException (tech.units.indriya.internal.format.TokenException)1