summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabiana G. Rocha2013-08-05 14:13:03 (EDT)
committer Fabiana G. Rocha2013-08-05 14:36:19 (EDT)
commit7c0e140c2485b3f3061c75e3a639c709bb55e9a0 (patch)
treeb82f6714a0b979b73df5d00608d8520163bd741e
parent3f2829320d313d7b03a05a7988b7f7c8d43b7f24 (diff)
downloadorg.eclipse.lyo.core-7c0e140c2485b3f3061c75e3a639c709bb55e9a0.zip
org.eclipse.lyo.core-7c0e140c2485b3f3061c75e3a639c709bb55e9a0.tar.gz
org.eclipse.lyo.core-7c0e140c2485b3f3061c75e3a639c709bb55e9a0.tar.bz2
Bug 412789: fix for parse double, float and boolean values. refs/changes/42/15142/1
Change-Id: I7d5333fe4d7344f1955c04d8100bc7516d917302 Signed-off-by: Fabiana G. Rocha <fgrocha@br.ibm.com>
-rw-r--r--org.eclipse.lyo.oslc4j.core/src/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java70
1 files changed, 54 insertions, 16 deletions
diff --git a/org.eclipse.lyo.oslc4j.core/src/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java b/org.eclipse.lyo.oslc4j.core/src/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java
index 63b84f0..f781271 100644
--- a/org.eclipse.lyo.oslc4j.core/src/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java
+++ b/org.eclipse.lyo.oslc4j.core/src/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java
@@ -38,8 +38,10 @@ import javax.xml.namespace.QName;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;
import org.eclipse.lyo.oslc4j.core.model.XMLLiteral;
+import com.hp.hpl.jena.datatypes.DatatypeFormatException;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;
+import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
@@ -374,24 +376,60 @@ public class OSLC4JUtils {
// this is a literal type
if (null != dataTypeFromShape) {
+ try {
- // special treatment for XMLLiteral
- if (XMLLiteralType.theXMLLiteralType.getURI().equals(propValueType.toString())) {
- return new XMLLiteral(originalValue.toString());
- }
-
- // special treatment for Date
- Class<?> objClass = dataTypeFromShape.getJavaClass();
- if (objClass.getCanonicalName().equals(XSDDateTime.class.getCanonicalName())) {
- String dateStr = originalValue.toString();
- Calendar calendar;
- calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateStr).toGregorianCalendar();
- final XSDDateTime xsdDateTime = new XSDDateTime(calendar);
- return xsdDateTime.asCalendar().getTime();
+ // special treatment for XMLLiteral
+ if (XMLLiteralType.theXMLLiteralType.getURI().equals(propValueType.toString())) {
+ return new XMLLiteral(originalValue.toString());
+ }
+
+ // special treatment for Date
+ Class<?> objClass = dataTypeFromShape.getJavaClass();
+ if (objClass.getCanonicalName().equals(XSDDateTime.class.getCanonicalName())) {
+ String dateStr = originalValue.toString();
+ Calendar calendar;
+ calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateStr).toGregorianCalendar();
+ final XSDDateTime xsdDateTime = new XSDDateTime(calendar);
+ return xsdDateTime.asCalendar().getTime();
+ }
+
+ // special treatment for Boolean
+ if (objClass.getCanonicalName().equals(Boolean.class.getCanonicalName())) {
+ // XML supports both 'true' and '1' for a true Boolean.
+ // Cannot use Boolean.parseBoolean since it supports case-insensitive TRUE.
+ if ((Boolean.TRUE.toString().equals(originalValue.toString())) || ("1".equals(originalValue.toString()))) {
+ return Boolean.TRUE;
+ }
+ // XML supports both 'false' and '0' for a false Boolean.
+ else if ((Boolean.FALSE.toString().equals(originalValue.toString())) || ("0".equals(originalValue.toString()))) {
+ return Boolean.FALSE;
+ }
+ else {
+ throw new IllegalArgumentException("'" + originalValue.toString() + "' has wrong format for Boolean.");
+ }
+ }
+
+ // special treatment for double
+ if (objClass.getCanonicalName().equals(Double.class.getCanonicalName())) {
+ return XSDDatatype.XSDdouble.parseValidated(originalValue.toString());
+ }
+
+ // special treatment for float
+ if (objClass.getCanonicalName().equals(Float.class.getCanonicalName())) {
+ return XSDDatatype.XSDfloat.parseValidated(originalValue.toString());
+ }
+ Constructor<?> cons = objClass.getConstructor(String.class);
+ return cons.newInstance(originalValue.toString());
+ } catch (IllegalArgumentException e) {
+ String errorMessage = (null == e.getMessage()) ? e.getCause().toString() : e.getMessage();
+ throw new IllegalArgumentException(errorMessage, e);
+ } catch (InvocationTargetException e) {
+ String errorMessage = (null == e.getMessage()) ? e.getCause().toString() : e.getMessage();
+ throw new IllegalArgumentException(errorMessage, e);
+ } catch (DatatypeFormatException e) {
+ String errorMessage = (null == e.getMessage()) ? e.getCause().toString() : e.getMessage();
+ throw new IllegalArgumentException(errorMessage, e);
}
-
- Constructor<?> cons = objClass.getConstructor(String.class);
- return cons.newInstance(originalValue.toString());
}
}
}