Skip to content

Commit

Permalink
fix: 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 Jun 10, 2024
1 parent 4377130 commit 0c119ef
Show file tree
Hide file tree
Showing 23 changed files with 887 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:hale="eu:esdihumboldt:hale:test" xmlns:gml="http://www.opengis.net/gml/3.2" elementFormDefault="qualified" targetNamespace="eu:esdihumboldt:hale:test">

<import namespace="http://www.opengis.net/gml/3.2" schemaLocation="http://schemas.opengis.net/gml/3.2.1/gml.xsd"/>

<element name="PrimitiveTest" substitutionGroup="gml:AbstractFeature" type="hale:PrimitiveTestType" />

<complexType name="PrimitiveTestType">
<complexContent>
<extension base="gml:AbstractFeatureType">
<sequence>
<xs:element name="decimalNumber" type="xs:decimal" minOccurs="0"/>
<xs:element name="doubleNumber" type="xs:double" minOccurs="0"/>
<xs:element name="integerNumber" type="xs:int" minOccurs="0"/>
<xs:element name="longNumber" type="xs:long" minOccurs="0"/>
<xs:element name="floatNumber" type="xs:float" minOccurs="0"/>
<xs:element name="shortNumber" type="xs:short" minOccurs="0"/>

<element name="doubleSimpleNumber" type="double" minOccurs="0"/>
</sequence>
</extension>
</complexContent>
</complexType>


</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ xsi:schemaLocation="http://www.example.com shiporder.xsd">
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
<price>0E-11</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
<priceInteger>123</priceInteger>
<priceDouble>12.3</priceDouble>
<priceFloat>123.456</priceFloat>
<priceLong>1234567890123456789</priceLong>
<priceShort>12345</priceShort>
</item>
</shiporder>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="priceInteger" type="xs:int"/>
<xs:element name="priceDouble" type="xs:double"/>
<xs:element name="priceFloat" type="xs:float"/>
<xs:element name="priceLong" type="xs:long"/>
<xs:element name="priceShort" type="xs:short"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Collection;

Expand Down Expand Up @@ -160,10 +161,12 @@ public void testLoadShiporder() throws Exception {
Instance instance = it.next();
assertNotNull(instance);

Object[] orderid = instance.getProperty(new QName("orderid")); // attribute
// form
// not
// qualified
Object[] orderid = instance.getProperty(new QName("orderid"));
// attribute
// form
// not
// qualified

assertNotNull(orderid);
assertEquals(1, orderid.length);
assertEquals("889923", orderid[0]);
Expand Down Expand Up @@ -213,6 +216,11 @@ public void testLoadShiporder() throws Exception {
assertEquals(1, title1.length);
assertEquals("Empire Burlesque", title1[0]);

Object[] price1 = ((Instance) item1).getProperty(new QName(ns, "price"));
assertNotNull(price1);
BigDecimal bigDecimal = new BigDecimal("0.00000000000");
assertEquals(bigDecimal, price1[0]);

// item 2
Object item2 = items[1];
assertTrue(item2 instanceof Instance);
Expand All @@ -222,6 +230,35 @@ public void testLoadShiporder() throws Exception {
assertEquals(1, title2.length);
assertEquals("Hide your heart", title2[0]);

Object[] price2 = ((Instance) item2).getProperty(new QName(ns, "price"));
assertNotNull(price2);
bigDecimal = new BigDecimal("9.90");
assertEquals(bigDecimal, price2[0]);

Object[] priceInteger = ((Instance) item2).getProperty(new QName(ns, "priceInteger"));
int intNumber = 123;
assertEquals(intNumber, priceInteger[0]);

// Double
Object[] priceDouble = ((Instance) item2).getProperty(new QName(ns, "priceDouble"));
double doubleNumber = 12.3;
assertEquals(doubleNumber, priceDouble[0]);

// Float
Object[] priceFloat = ((Instance) item2).getProperty(new QName(ns, "priceFloat"));
float floatNumber = 123.456F;
assertEquals(floatNumber, priceFloat[0]);

// Long
Object[] priceLong = ((Instance) item2).getProperty(new QName(ns, "priceLong"));
long longNumber = 1234567890123456789L;
assertEquals(longNumber, priceLong[0]);

// short
Object[] priceShort = ((Instance) item2).getProperty(new QName(ns, "priceShort"));
short shortNumber = 12345;
assertEquals(shortNumber, priceShort[0]);

// only one object
assertFalse(it.hasNext());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,151 @@ class StreamGmlWriter2Test {

result
}

@Test
void testNumbers() throws Exception {
/**test DOUBLE numbers*/
testNumber("doubleNumber", 12345.123456789, null)
testNumber("doubleNumber", 12345.123456789, "12345.123456789")
double doubleNumber = 123456789.123456789
double expected = java.lang.Double.parseDouble(doubleNumber.toString())
testNumber("doubleSimpleNumber", doubleNumber, expected)
testNumber("doubleSimpleNumber", doubleNumber, expected.toString())
testFloatingPointNumber(doubleNumber, "doubleNumber")

/**test INT, SHORT numbers*/
testNumber("integerNumber", 12345, null)
testNumber("integerNumber", 12345, "12345")
testNumber("shortNumber", 12345 as short, null)
testNumber("shortNumber", 12345 as short, "12345")

float smallDecimal = 0.12345F
float largeDecimal = 12345.6789F
float verySmall = 0.0000001F
float veryLarge = 12345678.9F
float negative = -98765.4321F
float scientific = 1.23E4F
float maxFloat = Float.MAX_VALUE
float minFloat = Float.MIN_VALUE

// for Float and Double we are expecting to use using scientific notation
testFloatingPointNumber(smallDecimal, "floatNumber")
testNumber("floatNumber", smallDecimal, "0.12345")
testFloatingPointNumber(largeDecimal, "floatNumber")
testNumber("floatNumber", largeDecimal, "12345.679")
testFloatingPointNumber(verySmall, "floatNumber")
testNumber("floatNumber", verySmall, "1.0E-7")
testFloatingPointNumber(veryLarge, "floatNumber")
testNumber("floatNumber", veryLarge, "1.2345679E7")
testFloatingPointNumber(negative, "floatNumber")
testFloatingPointNumber(scientific, "floatNumber")
testFloatingPointNumber(maxFloat, "floatNumber")
testFloatingPointNumber(minFloat, "floatNumber")

/**test LONG numbers*/
long smallPositive = 12345L
long largePositive = 1234567890123456789L
long smallNegative = -12345L
long largeNegative = -1234567890123456789L
long maxLong = Long.MAX_VALUE
long minLong = Long.MIN_VALUE
long zero = 0L
long powerOfTwo = 1024L
long negativePowerOfTwo = -1024L
long nearMaxLong = 9223372036854775806L
long nearMinLong = -9223372036854775807L

testNumber("longNumber", smallPositive, "12345")
testNumber("longNumber", largePositive, "1234567890123456789")
testNumber("longNumber", smallNegative, null)
testNumber("longNumber", largeNegative, null)
testNumber("longNumber", maxLong, null)
testNumber("longNumber", minLong, null)
testNumber("longNumber", zero, null)
testNumber("longNumber", powerOfTwo, null)
testNumber("longNumber", negativePowerOfTwo, null)
testNumber("longNumber", nearMaxLong, null)
testNumber("longNumber", nearMinLong, null)

/**test DECIMAL numbers*/
testNumber("decimalNumber", new BigDecimal("1234567890.123456789"), null)
testNumber("decimalNumber", new BigDecimal(doubleNumber), null)

doubleNumber = 1.23456789123456789E8
testNumber("decimalNumber", new BigDecimal(doubleNumber), null)

String numberAsString = "1.23456789123456789E8"
testNumber("decimalNumber", new BigDecimal(numberAsString), null)
testNumber("decimalNumber", new BigDecimal(numberAsString), "123456789.123456789")
}

void testNumber(String elementName, number, expected) throws Exception {
def schema = loadSchema(getClass().getResource("/data/numbers/numbers.xsd").toURI())

def instance = new InstanceBuilder(types: schema).PrimitiveTestType {
"$elementName"(number)
}

def writer = new GmlInstanceWriter()
File outFile = writeFile(instance, schema, writer)
def xmlFile = outFile.getAbsolutePath()
def xml = new XmlSlurper().parse(xmlFile)

String actualText = xml.featureMember.PrimitiveTest."$elementName".text()
if (expected != null) {
if (expected instanceof String) {
assertEquals(expected.toString(), xml.featureMember.PrimitiveTest."$elementName".text())
} else {
compareValues(expected, actualText)
}
} else {
compareValues(number, actualText)
}


if (DEL_TEMP_FILES) {
outFile.delete()
}
}

@CompileStatic
private File writeFile(Instance instance, Schema schema, InstanceWriter writer) {
writer.setParameter(GmlInstanceWriter.PARAM_PRETTY_PRINT, Value.of((Boolean)true))
writer.setParameter(GeoInstanceWriter.PARAM_UNIFY_WINDING_ORDER, Value.simple('noChanges'))
writer.setInstances(new DefaultInstanceCollection([instance]))
DefaultSchemaSpace schemaSpace = new DefaultSchemaSpace()
schemaSpace.addSchema(schema)
writer.setTargetSchema(schemaSpace)
File outFile = File.createTempFile('gml-writer', '.gml')
writer.setTarget(new FileIOSupplier(outFile))

IOReport report = writer.execute(null);
List<? extends Locatable> validationSchemas = writer.getValidationSchemas()

return outFile;
}

void testFloatingPointNumber(floatNumber, type) {
def expected = java.lang.Double.parseDouble(floatNumber.toString())
println expected
testNumber(type, floatNumber, expected)
}

void compareValues(expected, actualText) {
if (expected instanceof BigDecimal) {
assertEquals(((BigDecimal) expected), new BigDecimal(actualText))
} else if (expected instanceof Double || expected instanceof Float) {
assertEquals(expected.doubleValue(), new Double(actualText).doubleValue(), 0.0000001)
} else if (expected instanceof Long) {
assertEquals(expected.longValue(), new Long(actualText).longValue())
} else if (expected instanceof Integer) {
assertEquals(expected.intValue(), (new Integer(actualText)).intValue())
} else if (expected instanceof BigInteger) {
assertEquals(expected, new BigInteger(actualText))
} else if (expected instanceof Short) {
assertEquals(expected.shortValue(), (new Short(actualText)).shortValue())
} else {
assertEquals(expected, actual)
}
}
}
36 changes: 36 additions & 0 deletions io/plugins/eu.esdihumboldt.hale.io.gml/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,42 @@
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlTimeToTimestamp">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlIntegerToInteger">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.IntegerToXmlInteger">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.FloatToXmlFloat">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlFloatToFloat">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.LongToXmlLong">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlLongToLong">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.DoubleToXmlDouble">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlDoubleToDouble">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.BigDecimalToXmlDecimal">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlDecimalToBigDecimal">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.BigIntegerToXmlInteger">
</converter>
<converter
class="eu.esdihumboldt.hale.io.gml.internal.simpletype.converters.XmlIntegerToBigInteger">
</converter>
</extension>
<extension
point="eu.esdihumboldt.util.groovy.sandbox">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
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.XmlInt;
import org.apache.xmlbeans.XmlInteger;
import org.apache.xmlbeans.XmlLong;
import org.apache.xmlbeans.XmlShort;
import org.apache.xmlbeans.XmlTime;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService;
Expand Down Expand Up @@ -62,6 +69,14 @@ 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("decimal", XmlDecimal.class); //$NON-NLS-1$
TYPE_MAP.put("double", XmlDouble.class); //$NON-NLS-1$
TYPE_MAP.put("float", XmlFloat.class); //$NON-NLS-1$
TYPE_MAP.put("int", XmlInt.class); //$NON-NLS-1$
TYPE_MAP.put("integer", XmlInteger.class); //$NON-NLS-1$
TYPE_MAP.put("long", XmlLong.class); //$NON-NLS-1$
TYPE_MAP.put("short", XmlShort.class); //$NON-NLS-1$
}

/**
Expand Down Expand Up @@ -110,8 +125,8 @@ else if (simpleTypeValue instanceof XmlDateTime) {

xmlDateTime.setGDateValue(gdate);
}

if (simpleTypeValue != null) {
else if (simpleTypeValue != null && simpleTypeValue instanceof XmlAnySimpleType) {
// Numbers should be handled here
return simpleTypeValue.getStringValue();
}
} catch (ConversionException e) {
Expand Down
Loading

0 comments on commit 0c119ef

Please sign in to comment.