Skip to content

Package: XsdDateMapper

XsdDateMapper

nameinstructionbranchcomplexitylinemethod
XsdDateMapper()
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: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
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.exception.DatatypeMappingException;
21:
22: import javax.xml.datatype.XMLGregorianCalendar;
23: import java.time.LocalDate;
24:
25: /**
26: * Maps values of {@link cz.cvut.kbss.jopa.vocabulary.XSD#DATE} to Java {@link LocalDate}.
27: * <p>
28: * Note that this mapping loses timezone information, if it were present in the literal.
29: */
30: public class XsdDateMapper {
31:
32: private XsdDateMapper() {
33: throw new AssertionError();
34: }
35:
36: /**
37: * Maps the specified value to {@link LocalDate}.
38: *
39: * @param value Value to map
40: * @return Parsed local date
41: */
42: public static LocalDate map(String value) {
43: try {
44: final XMLGregorianCalendar cal = DatatypeFactoryProvider.getFactory().newXMLGregorianCalendar(value);
45: return LocalDate.of(cal.getYear(), cal.getMonth(), cal.getDay());
46: } catch (IllegalArgumentException e) {
47: throw new DatatypeMappingException("Invalid value provided as xsd:date.", e);
48: }
49: }
50: }