Skip to content
Browse files

GEOT-4455, properly parsing unsigned byte, short, int and long types

  • Loading branch information...
1 parent bd684ec commit 5c51ef169714eb8eaa5d5cd10934be872f29b207 jdeolive committed Apr 26, 2013
View
2 .../extension/xsd/xsd-core/src/main/java/org/geotools/xs/bindings/XSUnsignedByteBinding.java
@@ -85,7 +85,7 @@ public Class getType() {
*/
public Object parse(InstanceComponent instance, Object value)
throws Exception {
- return new Short((String) value);
+ return new Short(((Number)value).shortValue());
}
/**
View
2 ...s/extension/xsd/xsd-core/src/main/java/org/geotools/xs/bindings/XSUnsignedIntBinding.java
@@ -85,7 +85,7 @@ public Class getType() {
*/
public Object parse(InstanceComponent instance, Object value)
throws Exception {
- return new Long((String) value);
+ return ((Number)value).longValue();
}
/**
View
2 .../extension/xsd/xsd-core/src/main/java/org/geotools/xs/bindings/XSUnsignedLongBinding.java
@@ -90,7 +90,7 @@ public Class getType() {
*/
public Object parse(InstanceComponent instance, Object value)
throws Exception {
- return new BigDecimal((String) value);
+ return new BigDecimal(((Number)value).longValue());
}
/**
View
2 ...extension/xsd/xsd-core/src/main/java/org/geotools/xs/bindings/XSUnsignedShortBinding.java
@@ -85,7 +85,7 @@ public Class getType() {
*/
public Object parse(InstanceComponent instance, Object value)
throws Exception {
- return new Integer((String) value);
+ return new Integer(((Number)value).intValue());
}
/**
View
82 ...extension/xsd/xsd-core/src/test/java/org/geotools/xs/bindings/XSUnsignedBindingsTest.java
@@ -0,0 +1,82 @@
+/*
+ * GeoTools - The Open Source Java GIS Toolkit
+ * http://geotools.org
+ *
+ * (C) 2002-2013, Open Source Geospatial Foundation (OSGeo)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+package org.geotools.xs.bindings;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
+
+import org.geotools.xml.Parser;
+import org.geotools.xs.TestSchema;
+import org.geotools.xs.XSConfiguration;
+
+import junit.framework.TestCase;
+
+public class XSUnsignedBindingsTest extends TestCase {
+
+ Parser p;
+
+ @Override
+ protected void setUp() throws Exception {
+ XSConfiguration xs = new XSConfiguration();
+ p = new Parser(xs);
+ }
+
+ public void testParseUnsignedInt() throws Exception {
+ Long l = (Long) p.parse(new ByteArrayInputStream(doc("unsignedInt", "12").getBytes()));
+ assertEquals(12l, l.longValue());
+ }
+
+ public void testParseUnsignedByte() throws Exception {
+ Short s = (Short) p.parse(new ByteArrayInputStream(doc("unsignedByte", "12").getBytes()));
+ assertEquals(12, s.shortValue());
+ }
+
+ public void testParseUnsignedShort() throws Exception {
+ Integer i = (Integer) p.parse(new ByteArrayInputStream(doc("unsignedShort", "12").getBytes()));
+ assertEquals(12, i.longValue());
+ }
+
+ public void testParseUnsignedLong() throws Exception {
+ BigDecimal l = (BigDecimal) p.parse(new ByteArrayInputStream(doc("unsignedLong", "12").getBytes()));
+ assertEquals(12l, l.longValue());
+ }
+
+ String doc(String elName, String elValue) throws IOException {
+ File sampleXsd = File.createTempFile("sample", "xsd", new File("target"));
+ copy(TestSchema.class.getResourceAsStream("sample.xsd"), sampleXsd);
+
+ return String.format("<my:%s xmlns:my='http://localhost/xob/test' " +
+ "schemaLocation='http://localhost/xob/test %s'>%s</my:%s>",
+ elName, sampleXsd.getAbsolutePath(), elValue, elName);
+ }
+
+ void copy(InputStream in, File to) throws IOException {
+ FileOutputStream fout = new FileOutputStream(to);
+ byte[] buffer = new byte[1024];
+ int n = 0;
+
+ while (( n = in.read(buffer)) > 0) {
+ fout.write(buffer, 0, n);
+ }
+ fout.flush();
+ fout.close();
+ }
+}
View
6 modules/extension/xsd/xsd-core/src/test/resources/org/geotools/xs/sample.xsd
@@ -10,7 +10,11 @@
<xsd:element name="date" type="xsd:date" />
<xsd:element name="time" type="xsd:time" />
<xsd:element name="dateTime" type="xsd:dateTime" />
-
+ <xsd:element name="unsignedByte" type="xsd:unsignedByte"/>
+ <xsd:element name="unsignedShort" type="xsd:unsignedShort"/>
+ <xsd:element name="unsignedInt" type="xsd:unsignedInt"/>
+ <xsd:element name="unsignedLong" type="xsd:unsignedLong"/>
+
<xsd:complexType name="stuffType">
<xsd:sequence>
<xsd:element name="thing" type="xsd:string" />

0 comments on commit 5c51ef1

Please sign in to comment.
Something went wrong with that request. Please try again.