Package: JenaUtils
JenaUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
JenaUtils() |
|
|
|
|
|
||||||||||||||||||||
isResourceIdentifier(Object) |
|
|
|
|
|
||||||||||||||||||||
literalToValue(Literal) |
|
|
|
|
|
||||||||||||||||||||
valueToRdfNode(Assertion, Value) |
|
|
|
|
|
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.ontodriver.jena.util;
16:
17: import cz.cvut.kbss.ontodriver.model.Assertion;
18: import cz.cvut.kbss.ontodriver.model.NamedResource;
19: import cz.cvut.kbss.ontodriver.model.Value;
20: import org.apache.jena.datatypes.xsd.XSDDatatype;
21: import org.apache.jena.rdf.model.Literal;
22: import org.apache.jena.rdf.model.RDFNode;
23: import org.apache.jena.rdf.model.ResourceFactory;
24:
25: import java.net.URL;
26:
27: /**
28: * Utility methods for working with Jena API.
29: */
30: public class JenaUtils {
31:
32: private JenaUtils() {
33: throw new AssertionError();
34: }
35:
36: /**
37: * Resolves whether the specified value is a resource identifier.
38: * <p>
39: * Only absolute IRIs are supported (i.e. no blank node identifiers).
40: *
41: * @param value The value to check
42: * @return {@code true} if the value is either an URI or an URL
43: */
44: public static boolean isResourceIdentifier(Object value) {
45:• if (value instanceof NamedResource || value instanceof java.net.URI || value instanceof URL) {
46: return true;
47: }
48:• if (!(value instanceof String)) {
49: return false;
50: }
51: try {
52: final java.net.URI uri = java.net.URI.create(value.toString());
53: return uri.isAbsolute();
54: } catch (IllegalArgumentException e) {
55: return false;
56: }
57: }
58:
59: /**
60: * Transforms the specified value to an {@link RDFNode}, be it a resource or a literal.
61: *
62: * @param assertion Assertion representing the asserted property
63: * @param value Value to transform
64: * @return Jena RDFNode
65: */
66: public static RDFNode valueToRdfNode(Assertion assertion, Value<?> value) {
67:• if (JenaUtils.isResourceIdentifier(value.getValue())) {
68: return ResourceFactory.createResource(value.stringValue());
69: } else {
70:• return assertion.hasLanguage() ?
71: ResourceFactory.createLangLiteral(value.stringValue(), assertion.getLanguage()) :
72: ResourceFactory.createTypedLiteral(value.getValue());
73: }
74: }
75:
76: public static Object literalToValue(Literal literal) {
77: // This is because Jena returns XSD:long values as Integers, when they fit. But we don't want this.
78:• return literal.getDatatype().equals(XSDDatatype.XSDlong) ? literal.getLong() : literal.getValue();
79: }
80: }