Skip to content

Package: XsdTimeMapper

XsdTimeMapper

nameinstructionbranchcomplexitylinemethod
XsdTimeMapper()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
map(String)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2023 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.exception.DatatypeMappingException;
21:
22: import java.time.LocalTime;
23: import java.time.OffsetTime;
24: import java.time.format.DateTimeFormatter;
25: import java.time.format.DateTimeParseException;
26:
27: import static cz.cvut.kbss.jopa.datatype.DateTimeUtil.SYSTEM_OFFSET;
28:
29: /**
30: * Maps values of {@link cz.cvut.kbss.jopa.vocabulary.XSD#TIME} to Java {@link OffsetTime}.
31: * <p>
32: * Note that if the time does not specify offset information, system local is used.
33: */
34: public class XsdTimeMapper {
35:
36: private XsdTimeMapper() {
37: throw new AssertionError();
38: }
39:
40: /**
41: * Maps the specified value to {@link OffsetTime}.
42: *
43: * @param value Value to map
44: * @return Parsed offset time
45: */
46: public static OffsetTime map(String value) {
47: try {
48: return OffsetTime.parse(value, DateTimeFormatter.ISO_OFFSET_TIME);
49: } catch (DateTimeParseException e) {
50: try {
51: return LocalTime.parse(value).atOffset(SYSTEM_OFFSET);
52: } catch (DateTimeParseException e2) {
53: throw new DatatypeMappingException("Invalid value provided as xsd:time.", e2);
54: }
55: }
56: }
57: }