Skip to content

Package: XsdDatatypeMapper

XsdDatatypeMapper

nameinstructionbranchcomplexitylinemethod
XsdDatatypeMapper()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getInstance()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
map(Literal)
M: 0 C: 93
100%
M: 0 C: 16
100%
M: 0 C: 16
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toDouble(String)
M: 0 C: 18
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
toFloat(String)
M: 0 C: 18
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.datatype.xsd;
19:
20: import cz.cvut.kbss.jopa.datatype.DatatypeMapper;
21: import cz.cvut.kbss.jopa.datatype.exception.DatatypeMappingException;
22: import cz.cvut.kbss.jopa.vocabulary.XSD;
23: import cz.cvut.kbss.ontodriver.model.Literal;
24:
25: import java.math.BigDecimal;
26: import java.math.BigInteger;
27: import java.net.URI;
28: import java.util.Objects;
29: import java.util.Optional;
30:
31: /**
32: * Maps XML Schema types to Java.
33: * <p>
34: * The mapping logic is based on the known <a href="https://docs.oracle.com/javase/tutorial/jaxb/intro/bind.html">JAXB</a>/
35: * <a href="https://xmlbeans.apache.org/docs/2.0.0/guide/conXMLBeansSupportBuiltInSchemaTypes.html">Apache XML
36: * Beans</a>
37: * mapping with utilization of the Java 8 Date/Time API.
38: */
39: public class XsdDatatypeMapper implements DatatypeMapper {
40:
41: public static final String NEGATIVE_INFINITY = "-INF";
42:
43: public static final String POSITIVE_INFINITY = "INF";
44:
45: private static final XsdDatatypeMapper INSTANCE = new XsdDatatypeMapper();
46:
47: /**
48: * Gets an instance of this mapper.
49: * <p>
50: * Convenience method returning one shared instance (the mapper has no state and is thread-safe).
51: *
52: * @return Shared mapper instance
53: */
54: public static XsdDatatypeMapper getInstance() {
55: return INSTANCE;
56: }
57:
58: @Override
59: public Optional<Object> map(Literal literal) {
60: Objects.requireNonNull(literal);
61: final String value = literal.getLexicalForm();
62: try {
63:• switch (literal.getDatatype()) {
64: case XSD.BOOLEAN:
65: return Optional.of(Boolean.parseBoolean(value));
66: case XSD.BYTE:
67: return Optional.of(Byte.parseByte(value));
68: case XSD.SHORT:
69: case XSD.UNSIGNED_BYTE:
70: return Optional.of(Short.parseShort(value));
71: case XSD.INT:
72: case XSD.UNSIGNED_SHORT:
73: return Optional.of(Integer.parseInt(value));
74: case XSD.LONG:
75: case XSD.UNSIGNED_INT:
76: return Optional.of(Long.parseLong(value));
77: case XSD.FLOAT:
78: return Optional.of(toFloat(value));
79: case XSD.DOUBLE:
80: return Optional.of(toDouble(value));
81: case XSD.STRING:
82: case XSD.NORMALIZED_STRING:
83: return Optional.of(value);
84: case XSD.DATETIME:
85: return Optional.of(XsdDateTimeMapper.map(value));
86: case XSD.DATE:
87: return Optional.of(XsdDateMapper.map(value));
88: case XSD.TIME:
89: return Optional.of(XsdTimeMapper.map(value));
90: case XSD.DURATION:
91: return Optional.of(XsdDurationMapper.map(value));
92: case XSD.INTEGER:
93: case XSD.NON_NEGATIVE_INTEGER:
94: case XSD.NON_POSITIVE_INTEGER:
95: case XSD.NEGATIVE_INTEGER:
96: case XSD.POSITIVE_INTEGER:
97: case XSD.UNSIGNED_LONG:
98: return Optional.of(new BigInteger(value));
99: case XSD.DECIMAL:
100: return Optional.of(new BigDecimal(value));
101: case XSD.ANY_URI:
102: return Optional.of(URI.create(value));
103: default:
104: return Optional.empty();
105: }
106: } catch (IllegalArgumentException e) {
107: throw new DatatypeMappingException("Unable to map literal " + literal, e);
108: }
109: }
110:
111: private static Float toFloat(String lexicalForm) {
112:• if (NEGATIVE_INFINITY.equals(lexicalForm)) {
113: return Float.NEGATIVE_INFINITY;
114:• } else if (POSITIVE_INFINITY.equals(lexicalForm)) {
115: return Float.POSITIVE_INFINITY;
116: }
117: return Float.parseFloat(lexicalForm);
118: }
119:
120: private static Double toDouble(String lexicalForm) {
121:• if (NEGATIVE_INFINITY.equals(lexicalForm)) {
122: return Double.NEGATIVE_INFINITY;
123:• } else if (POSITIVE_INFINITY.equals(lexicalForm)) {
124: return Double.POSITIVE_INFINITY;
125: }
126: return Double.parseDouble(lexicalForm);
127: }
128: }