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