Skip to content

Commit

Permalink
Enhance currency and price representation in MediaModule (#508)
Browse files Browse the repository at this point in the history
Closes #372
  • Loading branch information
PatrickGotthard authored Dec 29, 2021
1 parent 69135a5 commit d6dd8be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.StringTokenizer;

import org.jdom2.Element;
import org.jdom2.Namespace;
Expand Down Expand Up @@ -464,10 +465,28 @@ private void parsePrices(final Element e, final Metadata md) {
final List<Element> priceElements = e.getChildren("price", getNS());
final Price[] prices = new Price[priceElements.size()];
for (int i = 0; i < priceElements.size(); i++) {

final Element priceElement = priceElements.get(i);
prices[i] = new Price();
prices[i].setCurrency(priceElement.getAttributeValue("currency"));
prices[i].setPrice(Doubles.parse(priceElement.getAttributeValue("price")));

final String currency = priceElement.getAttributeValue("currency");
if (currency != null) {
try {
prices[i].setCurrency(Currency.getInstance(currency));
} catch (IllegalArgumentException ex) {
LOG.warn("Invalid currency", ex);
}
}

final String price = priceElement.getAttributeValue("price");
if (price != null) {
try {
prices[i].setPrice(new BigDecimal(price));
} catch (NumberFormatException ex) {
LOG.warn("Invalid price", ex);
}
}

if (priceElement.getAttributeValue("type") != null) {
prices[i].setType(Price.Type.valueOf(priceElement.getAttributeValue("type").toUpperCase()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package com.rometools.modules.mediarss.types;

import java.io.Serializable;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Currency;

/**
* Optional tag to include pricing information about a media object.
Expand All @@ -40,8 +42,8 @@ public enum Type {
}

private Type type;
private Double price;
private String currency;
private BigDecimal price;
private Currency currency;
private URL info;

/**
Expand Down Expand Up @@ -69,7 +71,7 @@ public void setType(final Type type) {
*
* @return the price
*/
public Double getPrice() {
public BigDecimal getPrice() {
return price;
}

Expand All @@ -80,7 +82,7 @@ public Double getPrice() {
*
* @param price the price
*/
public void setPrice(final Double price) {
public void setPrice(final BigDecimal price) {
this.price = price;
}

Expand All @@ -89,7 +91,7 @@ public void setPrice(final Double price) {
*
* @return ISO 4217 currency code
*/
public String getCurrency() {
public Currency getCurrency() {
return currency;
}

Expand All @@ -98,7 +100,7 @@ public String getCurrency() {
*
* @param currency ISO 4217 currency code
*/
public void setCurrency(final String currency) {
public void setCurrency(final Currency currency) {
this.currency = currency;
}

Expand Down

0 comments on commit d6dd8be

Please sign in to comment.