use of javax.money.format.MonetaryParseException in project jsr354-ri-bp by JavaMoney.
the class CurrencyToken method parse.
/**
* Parses a currency from the given {@link ParseContext}. Depending on the
* current {@link CurrencyStyle} it interprets the next non empty token,
* either as
* <ul>
* <li>currency code
* <li>currency symbol
* </ul>
* Parsing of localized currency names or numeric code is not supported.
*
* @throws UnsupportedOperationException if the {@link CurrencyStyle} is configured to us currency
* names, or numeric codes for formatting.
*/
@Override
public void parse(ParseContext context) throws MonetaryParseException {
String token = context.lookupNextToken();
while (token != null) {
if (!token.trim().isEmpty()) {
break;
}
context.consume(token);
token = context.lookupNextToken();
}
if (token == null) {
throw new MonetaryException("Error parsing CurrencyUnit: token expected");
}
try {
CurrencyUnit cur;
switch(style) {
case CODE:
if (!Monetary.isCurrencyAvailable(token)) {
// Perhaps blank is missing between currency code and number...
String subCurrency = parseCurrencyCode(token);
cur = Monetary.getCurrency(subCurrency);
context.consume(subCurrency);
} else {
cur = Monetary.getCurrency(token);
context.consume(token);
}
break;
case SYMBOL:
if (token.startsWith("$")) {
throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex());
} else if (token.startsWith("€")) {
cur = Monetary.getCurrency("EUR");
context.consume("€");
} else if (token.startsWith("£")) {
cur = Monetary.getCurrency("GBP");
context.consume("£");
} else {
cur = Monetary.getCurrency(token);
context.consume(token);
}
context.setParsedCurrency(cur);
break;
case NAME:
case NUMERIC_CODE:
default:
throw new UnsupportedOperationException("Not yet implemented");
}
if (cur != null) {
context.setParsedCurrency(cur);
}
} catch (Exception e) {
throw new MonetaryException("Error parsing CurrencyUnit.", e);
}
}
use of javax.money.format.MonetaryParseException in project jsr354-ri-bp by JavaMoney.
the class ToStringMonetaryAmountFormat method parserMonetaryAmount.
private ParserMonetaryAmount parserMonetaryAmount(CharSequence text) {
String[] array = Objects.requireNonNull(text).toString().split(" ");
if (array.length != 2) {
throw new MonetaryParseException("An error happened when try to parse the Monetary Amount.", text, 0);
}
CurrencyUnit currencyUnit = Monetary.getCurrency(array[0]);
BigDecimal number = new BigDecimal(array[1]);
return new ParserMonetaryAmount(currencyUnit, number);
}
use of javax.money.format.MonetaryParseException in project jsr354-ri by JavaMoney.
the class ToStringMonetaryAmountFormat method parserMonetaryAmount.
private ParserMonetaryAmount parserMonetaryAmount(CharSequence text) {
String[] array = Objects.requireNonNull(text).toString().split(" ");
if (array.length != 2) {
throw new MonetaryParseException("An error happened when try to parse the Monetary Amount.", text, 0);
}
CurrencyUnit currencyUnit = Monetary.getCurrency(array[0]);
BigDecimal number = new BigDecimal(array[1]);
return new ParserMonetaryAmount(currencyUnit, number);
}
use of javax.money.format.MonetaryParseException in project jsr354-ri by JavaMoney.
the class CurrencyToken method parse.
/**
* Parses a currency from the given {@link ParseContext}. Depending on the
* current {@link CurrencyStyle} it interprets the next non empty token,
* either as
* <ul>
* <li>currency code
* <li>currency symbol
* </ul>
* Parsing of localized currency names or numeric code is not supported.
*
* @throws UnsupportedOperationException if the {@link CurrencyStyle} is configured to us currency
* names, or numeric codes for formatting.
*/
@Override
public void parse(ParseContext context) throws MonetaryParseException {
String token = context.lookupNextToken();
while (Objects.nonNull(token)) {
if (token.trim().isEmpty()) {
context.consume(token);
token = context.lookupNextToken();
continue;
}
break;
}
if (token == null) {
throw new MonetaryException("Error parsing CurrencyUnit: no input.");
}
try {
CurrencyUnit cur;
switch(style) {
case CODE:
if (!Monetary.isCurrencyAvailable(token)) {
// Perhaps blank is missing between currency code and number...
String subCurrency = parseCurrencyCode(token);
cur = Monetary.getCurrency(subCurrency);
context.consume(subCurrency);
} else {
cur = Monetary.getCurrency(token);
context.consume(token);
}
break;
case SYMBOL:
if (token.startsWith("$")) {
throw new MonetaryParseException("$ is not a unique currency symbol.", token, context.getErrorIndex());
} else if (token.startsWith("€")) {
cur = Monetary.getCurrency("EUR");
context.consume("€");
} else if (token.startsWith("£")) {
cur = Monetary.getCurrency("GBP");
context.consume("£");
} else {
cur = Monetary.getCurrency(token);
context.consume(token);
}
context.setParsedCurrency(cur);
break;
case NAME:
case NUMERIC_CODE:
default:
throw new UnsupportedOperationException("Not yet implemented");
}
if (Objects.nonNull(cur)) {
context.setParsedCurrency(cur);
}
} catch (Exception e) {
throw new MonetaryException("Error parsing CurrencyUnit.", e);
}
}
use of javax.money.format.MonetaryParseException in project jsr354-ri-bp by JavaMoney.
the class DefaultMonetaryAmountFormat method parse.
/**
* Fully parses the text into an instance of {@code MonetaryAmount}.
* <p>
* The parse must complete normally and parse the entire text. If the parse
* completes without reading the entire length of the text, an exception is
* thrown. If any other problem occurs during parsing, an exception is
* thrown.
*
* @param text the text to parse, not null
* @return the parsed value, never {@code null}
* @throws UnsupportedOperationException if the formatter is unable to parse
* @throws javax.money.format.MonetaryParseException if there is a problem while parsing
*/
public MonetaryAmount parse(CharSequence text) throws MonetaryParseException {
ParseContext ctx = new ParseContext(text);
try {
for (FormatToken token : this.positiveTokens) {
token.parse(ctx);
}
} catch (Exception e) {
// try parsing negative...
Logger log = Logger.getLogger(getClass().getName());
if (log.isLoggable(Level.FINEST)) {
log.log(Level.FINEST, "Failed to parse positive pattern, trying negative for: " + text, e);
}
for (FormatToken token : this.negativeTokens) {
token.parse(ctx);
}
}
CurrencyUnit unit = ctx.getParsedCurrency();
Number num = ctx.getParsedNumber();
if (unit == null) {
unit = this.amountFormatContext.get(CurrencyUnit.class);
}
if (num == null) {
throw new MonetaryParseException(text.toString(), -1);
}
MonetaryAmountFactory<?> factory = this.amountFormatContext.getParseFactory();
if (factory == null) {
factory = Monetary.getDefaultAmountFactory();
}
return factory.setCurrency(unit).setNumber(num).create();
}
Aggregations