Package: ValueConverter
ValueConverter
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ValueConverter(ValueFactory) |
|
|
|
|
|
||||||||||||||||||||
getValueAsIri(Value) |
|
|
|
|
|
||||||||||||||||||||
language(Assertion) |
|
|
|
|
|
||||||||||||||||||||
resolvePropertyValue(Assertion, Value) |
|
|
|
|
|
||||||||||||||||||||
toRdf4jValue(Assertion, Value) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 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.ontodriver.rdf4j;
16:
17: import cz.cvut.kbss.ontodriver.model.Assertion;
18: import cz.cvut.kbss.ontodriver.rdf4j.config.Constants;
19: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
20: import cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils;
21: import org.eclipse.rdf4j.model.IRI;
22: import org.eclipse.rdf4j.model.Value;
23: import org.eclipse.rdf4j.model.ValueFactory;
24:
25: class ValueConverter {
26:
27: private final ValueFactory vf;
28:
29: ValueConverter(ValueFactory vf) {
30: this.vf = vf;
31: }
32:
33: Value toRdf4jValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) throws Rdf4jDriverException {
34:• switch (assertion.getType()) {
35: case DATA_PROPERTY:
36: return Rdf4jUtils.createLiteral(val.getValue(), language(assertion), vf);
37: case CLASS:
38: case OBJECT_PROPERTY:
39: return getValueAsIri(val);
40: case ANNOTATION_PROPERTY: // Intentional fall-through
41: case PROPERTY:
42: return resolvePropertyValue(assertion, val);
43: default:
44: // Failsafe
45: throw new IllegalArgumentException("Unsupported assertion type " + assertion.getType());
46: }
47: }
48:
49: private static String language(Assertion assertion) {
50:• return assertion.hasLanguage() ? assertion.getLanguage() : Constants.DEFAULT_LANG;
51: }
52:
53: private IRI getValueAsIri(cz.cvut.kbss.ontodriver.model.Value<?> val) throws Rdf4jDriverException {
54: try {
55: return vf.createIRI(val.getValue().toString());
56: } catch (IllegalArgumentException e) {
57: throw new Rdf4jDriverException(e);
58: }
59: }
60:
61: private Value resolvePropertyValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) {
62:• if (Rdf4jUtils.isResourceIdentifier(val.getValue())) {
63: return vf.createIRI(val.getValue().toString());
64: } else {
65: return Rdf4jUtils.createLiteral(val.getValue(), language(assertion), vf);
66: }
67: }
68: }