Skip to contentMethod: JenaUtils()
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.ontodriver.jena.util;
19:
20: import cz.cvut.kbss.jopa.datatype.xsd.XsdDatatypeMapper;
21: import cz.cvut.kbss.jopa.datatype.xsd.XsdTemporalMapper;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.LangString;
24: import cz.cvut.kbss.ontodriver.model.Value;
25: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
26: import org.apache.jena.datatypes.BaseDatatype;
27: import org.apache.jena.datatypes.RDFDatatype;
28: import org.apache.jena.datatypes.TypeMapper;
29: import org.apache.jena.datatypes.xsd.impl.RDFLangString;
30: import org.apache.jena.rdf.model.Literal;
31: import org.apache.jena.rdf.model.RDFNode;
32: import org.apache.jena.rdf.model.ResourceFactory;
33:
34: import java.time.temporal.TemporalAccessor;
35: import java.time.temporal.TemporalAmount;
36: import java.util.Date;
37: import java.util.Objects;
38:
39: /**
40: * Utility methods for working with Jena API.
41: */
42: public class JenaUtils {
43:
44: private JenaUtils() {
45: throw new AssertionError();
46: }
47:
48: /**
49: * Transforms the specified {@link Value} to an {@link RDFNode}, be it a resource or a literal.
50: *
51: * @param assertion Assertion representing the asserted property
52: * @param value Value to transform
53: * @return Jena RDFNode
54: */
55: public static <T> RDFNode valueToRdfNode(Assertion assertion, Value<T> value) {
56: final T val = value.getValue();
57: return toRdfNode(assertion, val);
58: }
59:
60: /**
61: * Transforms the specified value to an {@link RDFNode}, be it a resource or a literal.
62: *
63: * @param assertion Assertion representing the asserted property
64: * @param value Value to transform
65: * @return Jena RDFNode
66: */
67: public static RDFNode toRdfNode(Assertion assertion, Object value) {
68: Objects.requireNonNull(value);
69: if (IdentifierUtils.isResourceIdentifierType(value.getClass())) {
70: return ResourceFactory.createResource(value.toString());
71: } else if (value instanceof LangString langString) {
72: return langString.getLanguage().map(lang -> ResourceFactory.createLangLiteral(langString.getValue(), lang))
73: .orElseGet(() -> ResourceFactory.createTypedLiteral(langString.getValue()));
74: } else if (value instanceof String) {
75: return assertion.hasLanguage() ? ResourceFactory.createLangLiteral((String) value,
76: assertion.getLanguage()) :
77: ResourceFactory.createTypedLiteral(value);
78: } else if (value instanceof cz.cvut.kbss.ontodriver.model.Literal ontoLiteral) {
79: return createLiteral(ontoLiteral);
80: } else if (value instanceof Date) {
81: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((Date) value).toInstant());
82: return createLiteral(ontoLiteral);
83: } else if (value instanceof TemporalAccessor) {
84: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAccessor) value));
85: return createLiteral(ontoLiteral);
86: } else if (value instanceof TemporalAmount) {
87: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAmount) value));
88: return createLiteral(ontoLiteral);
89: } else {
90: return ResourceFactory.createTypedLiteral(value);
91: }
92: }
93:
94: private static Literal createLiteral(cz.cvut.kbss.ontodriver.model.Literal ontoLiteral) {
95: final TypeMapper typeMapper = TypeMapper.getInstance();
96: RDFDatatype datatype = typeMapper.getTypeByName(ontoLiteral.getDatatype());
97: if (datatype == null) {
98: // If the datatype does not exist, register it as a base datatype without any special behavior
99: datatype = new BaseDatatype(ontoLiteral.getDatatype());
100: typeMapper.registerDatatype(datatype);
101: }
102: return ResourceFactory.createTypedLiteral(ontoLiteral.getLexicalForm(), datatype);
103: }
104:
105: public static Object literalToValue(Literal literal) {
106: if (RDFLangString.rdfLangString.equals(literal.getDatatype())) {
107: return new LangString(literal.getString(), literal.getLanguage());
108: }
109: final cz.cvut.kbss.ontodriver.model.Literal lit = cz.cvut.kbss.ontodriver.model.Literal.from(
110: literal.getLexicalForm(), literal.getDatatypeURI());
111: return XsdDatatypeMapper.getInstance().map(lit).orElse(lit);
112: }
113: }