Skip to content

Package: DatatypeTransformer

DatatypeTransformer

nameinstructionbranchcomplexitylinemethod
DatatypeTransformer()
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%
isSupportedJavaType(Class)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 75
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
transform(OWLLiteral)
M: 91 C: 0
0%
M: 14 C: 0
0%
M: 12 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
transformOWLType(OWLDatatype)
M: 12 C: 15
56%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 5
83%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 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.jopa.owlapi;
16:
17: import java.math.BigDecimal;
18: import java.net.URI;
19: import java.text.ParseException;
20: import java.text.SimpleDateFormat;
21: import java.util.Date;
22: import java.util.HashMap;
23: import java.util.Map;
24:
25: import org.semanticweb.owlapi.model.OWLDatatype;
26: import org.semanticweb.owlapi.model.OWLLiteral;
27: import org.semanticweb.owlapi.vocab.OWL2Datatype;
28:
29: public class DatatypeTransformer {
30:
31:         private static Map<OWL2Datatype, Class<?>> map = new HashMap<OWL2Datatype, Class<?>>();
32:
33:         static {
34:                 map.put(OWL2Datatype.RDF_PLAIN_LITERAL, String.class);
35:                 map.put(OWL2Datatype.XSD_STRING, String.class);
36:                 map.put(OWL2Datatype.RDF_XML_LITERAL, String.class);
37:                 map.put(OWL2Datatype.XSD_INT, Integer.class);
38:                 map.put(OWL2Datatype.XSD_INTEGER, Integer.class);
39:                 map.put(OWL2Datatype.XSD_DOUBLE, Double.class);
40:                 map.put(OWL2Datatype.XSD_FLOAT, Float.class);
41:                 map.put(OWL2Datatype.XSD_BOOLEAN, Boolean.class);
42:                 map.put(OWL2Datatype.XSD_DATE_TIME, Date.class);
43:                 map.put(OWL2Datatype.XSD_DATE_TIME_STAMP, Date.class);
44:                 map.put(OWL2Datatype.XSD_SHORT, Short.class);
45:                 map.put(OWL2Datatype.XSD_LONG, Long.class);
46: map.put(OWL2Datatype.XSD_ANY_URI, URI.class);
47: map.put(OWL2Datatype.XSD_DECIMAL, BigDecimal.class);
48:         }
49:
50:         public static Class<?> transformOWLType(final OWLDatatype dt) {
51:                 Class<?> type = null;
52:
53:•                if (dt.isBuiltIn()) {
54:                         type = map.get(dt.getBuiltInDatatype());
55:                 }
56:
57:•                if (type == null) {
58:                         throw new IllegalArgumentException("Unsupported datatype: " + dt);
59:                 }
60:
61:                 return type;
62:         }
63:
64:         public static Object transform(final OWLLiteral l) {
65:•                if (l.isRDFPlainLiteral()) {
66:                         return l.getLiteral();
67:•                } else if (l.getDatatype().isBuiltIn())
68:•                        switch (l.getDatatype().getBuiltInDatatype()) {
69:                         case XSD_SHORT:
70:                                 return Short.parseShort(l.getLiteral());
71:                         case XSD_LONG:
72:                                 return Long.parseLong(l.getLiteral());
73:                         case XSD_INT:
74:                         case XSD_INTEGER:
75:                                 return Integer.parseInt(l.getLiteral());
76:                         case XSD_DOUBLE:
77:                         case XSD_DECIMAL:
78:                                 return Double.parseDouble(l.getLiteral());
79:                         case XSD_FLOAT:
80:                                 return Float.parseFloat(l.getLiteral());
81:                         case XSD_STRING:
82:                         case RDF_XML_LITERAL:
83:                                 return l.getLiteral();
84:                         case XSD_BOOLEAN:
85:                                 return Boolean.parseBoolean(l.getLiteral());
86:                         case XSD_ANY_URI:
87:                                 return URI.create(l.getLiteral());
88:                         case XSD_DATE_TIME_STAMP:
89:                         case XSD_DATE_TIME:
90:                                 try {
91:                                         return new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")
92:                                                         .parse(l.getLiteral());
93:                                 } catch (ParseException e) {
94:                                         throw new IllegalArgumentException("The date time '"
95:                                                         + l.getLiteral() + "' cannot be parsed");
96:                                 }
97:                         }
98:
99:                 throw new IllegalArgumentException("Unsupported datatype: "
100:                                 + l.getDatatype());
101:         }
102:
103:         public static boolean isSupportedJavaType(Class<?> dt) {
104:                 return map.values().contains(dt);
105:         }
106: }