Skip to content

Package: XSDTypeCoercer

XSDTypeCoercer

nameinstructionbranchcomplexitylinemethod
XSDTypeCoercer()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
coerceType(String, String)
M: 22 C: 43
66%
M: 4 C: 9
69%
M: 4 C: 8
67%
M: 3 C: 9
75%
M: 0 C: 1
100%
parseDateTime(String)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 5 C: 7
58%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 4
67%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.deserialization.util;
16:
17: import cz.cvut.kbss.jopa.vocabulary.XSD;
18: import org.slf4j.Logger;
19: import org.slf4j.LoggerFactory;
20:
21: import javax.xml.datatype.DatatypeConfigurationException;
22: import javax.xml.datatype.DatatypeFactory;
23: import java.time.*;
24: import java.time.format.DateTimeFormatter;
25: import java.time.format.DateTimeParseException;
26:
27: public class XSDTypeCoercer {
28:
29: private static final Logger LOG = LoggerFactory.getLogger(XSDTypeCoercer.class);
30:
31: private static DatatypeFactory DATATYPE_FACTORY;
32:
33: static {
34: try {
35: DATATYPE_FACTORY = DatatypeFactory.newInstance();
36: } catch (DatatypeConfigurationException e) {
37: LOG.error("Unable to create XML DatatypeFactory", e);
38: }
39: }
40:
41: public static Object coerceType(String value, String type) {
42:• switch (type) {
43: case XSD.BOOLEAN:
44: return Boolean.parseBoolean(value);
45: case XSD.BYTE:
46: return Byte.parseByte(value);
47: case XSD.SHORT:
48: case XSD.UNSIGNED_BYTE:
49: return Short.parseShort(value);
50: case XSD.INT:
51: case XSD.INTEGER:
52: case XSD.NON_NEGATIVE_INTEGER:
53: case XSD.NON_POSITIVE_INTEGER:
54: case XSD.POSITIVE_INTEGER:
55: case XSD.NEGATIVE_INTEGER:
56: case XSD.UNSIGNED_LONG:
57: return Integer.parseInt(value);
58: case XSD.LONG:
59: case XSD.UNSIGNED_INT:
60: return Long.parseLong(value);
61: case XSD.FLOAT:
62: return Float.parseFloat(value);
63: case XSD.DOUBLE:
64: return Double.parseDouble(value);
65: case XSD.DATE:
66: case XSD.DATETIME:
67: return parseDateTime(value);
68: case XSD.TIME:
69: return DateTimeFormatter.ISO_TIME.parse(value, LocalTime::from);
70: case XSD.DURATION:
71:• return DATATYPE_FACTORY != null ? DATATYPE_FACTORY.newDuration(value) : Duration.parse(value);
72: default:
73: throw new IllegalArgumentException("Unsupported type for XSD type coercion: " + type);
74: }
75: }
76:
77: private static Object parseDateTime(String value) {
78: try {
79: return DateTimeFormatter.ISO_DATE_TIME.parse(value, ZonedDateTime::from);
80: } catch (DateTimeParseException e) {
81: // If it's not zoned date time, let's try local date time
82: return DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(value, LocalDateTime::from);
83: }
84: }
85: }