Skip to content

Commit

Permalink
feat: use appropriate methods to convert the numbers in XML
Browse files Browse the repository at this point in the history
Conversion of the numbers using appropriate methods of each different type.

ING-3965
  • Loading branch information
emanuelaepure10 committed May 7, 2024
1 parent d56e083 commit dea6f93
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package eu.esdihumboldt.hale.io.gml.internal.simpletype;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
Expand All @@ -29,6 +31,11 @@
import org.apache.xmlbeans.XmlAnySimpleType;
import org.apache.xmlbeans.XmlDate;
import org.apache.xmlbeans.XmlDateTime;
import org.apache.xmlbeans.XmlDecimal;
import org.apache.xmlbeans.XmlDouble;
import org.apache.xmlbeans.XmlFloat;
import org.apache.xmlbeans.XmlInteger;
import org.apache.xmlbeans.XmlLong;
import org.apache.xmlbeans.XmlTime;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService;
Expand Down Expand Up @@ -62,6 +69,12 @@ public class SimpleTypeUtil {
TYPE_MAP.put("dateTime", XmlDateTime.class); //$NON-NLS-1$
TYPE_MAP.put("date", XmlDate.class); //$NON-NLS-1$
TYPE_MAP.put("time", XmlTime.class); //$NON-NLS-1$

TYPE_MAP.put("integer", XmlInteger.class); //$NON-NLS-1$
TYPE_MAP.put("float", XmlFloat.class); //$NON-NLS-1$
TYPE_MAP.put("double", XmlDouble.class); //$NON-NLS-1$
TYPE_MAP.put("long", XmlLong.class); //$NON-NLS-1$
TYPE_MAP.put("decimal", XmlDecimal.class); //$NON-NLS-1$
}

/**
Expand Down Expand Up @@ -119,6 +132,31 @@ else if (simpleTypeValue instanceof XmlDateTime) {
}
}

if (value instanceof BigDecimal) {
// Create a DecimalFormat object without scientific notation
DecimalFormat decimalFormat = new DecimalFormat("0.##############");
// Format the BigDecimal
return decimalFormat.format(value);
}
else
// Handle primitive types
if (value instanceof Double || value instanceof Float || value instanceof Long
|| value instanceof Integer) {
return String.valueOf(value);
}
else if (value instanceof XmlAnySimpleType) {
Number numberValue = (Number) value;
if (value instanceof XmlDouble || value instanceof XmlFloat || value instanceof XmlLong
|| value instanceof XmlInteger) {
// Return the string value of the number
return String.valueOf(numberValue);
}
else if (value instanceof XmlDecimal) {
BigDecimal decimalValue = ((XmlDecimal) value).getBigDecimalValue();
return decimalValue.stripTrailingZeros().toPlainString();
}
}

// try to convert to string
try {
String stringValue = conversionService.convert(value, String.class);
Expand Down

0 comments on commit dea6f93

Please sign in to comment.