Skip to content

Package: NamespaceResolver

NamespaceResolver

nameinstructionbranchcomplexitylinemethod
NamespaceResolver()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
registerDefaultPrefixes()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
registerNamespace(String, String)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
resolveFullIri(String)
M: 0 C: 58
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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.utils;
19:
20: import cz.cvut.kbss.jopa.vocabulary.RDF;
21: import cz.cvut.kbss.jopa.vocabulary.RDFS;
22: import cz.cvut.kbss.jopa.vocabulary.XSD;
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import java.util.HashMap;
27: import java.util.Map;
28: import java.util.Objects;
29:
30: /**
31: * Holds mapping of prefixes to namespaces and allows resolution of prefixed IRIs.
32: * <p>
33: * Prefixes for RDF (rdf:), RDFS (rdfs:) and XSD (xsd:) are pre-registered.
34: */
35: public class NamespaceResolver {
36:
37: private static final Logger LOG = LoggerFactory.getLogger(NamespaceResolver.class);
38:
39: public static final String URN_PREFIX = "urn";
40:
41: private final Map<String, String> namespaces = new HashMap<>();
42:
43: public NamespaceResolver() {
44: registerDefaultPrefixes();
45: }
46:
47: private void registerDefaultPrefixes() {
48: registerNamespace(RDF.PREFIX, RDF.NAMESPACE);
49: registerNamespace(RDFS.PREFIX, RDFS.NAMESPACE);
50: registerNamespace(XSD.PREFIX, XSD.NAMESPACE);
51: }
52:
53: /**
54: * Registers the specified namespace with the specified prefix.
55: *
56: * @param prefix Prefix representing the namespace
57: * @param namespace Namespace to represent
58: */
59: public void registerNamespace(String prefix, String namespace) {
60: Objects.requireNonNull(prefix);
61: Objects.requireNonNull(namespace);
62: namespaces.put(prefix, namespace);
63: }
64:
65: /**
66: * Replaces prefix in the specified IRI with a full namespace IRI, if the IRI contains a prefix and it is registered
67: * in this resolver.
68: *
69: * @param iri The IRI to resolve
70: * @return Full IRI, if this resolver was able to resolve it, or the original IRI
71: */
72: public String resolveFullIri(String iri) {
73: Objects.requireNonNull(iri);
74: final int colonIndex = iri.indexOf(':');
75:• if (colonIndex == -1) {
76: return iri;
77: }
78:• if (iri.charAt(colonIndex + 1) == '/') {
79: // iri is hierarchical
80: return iri;
81: }
82: final String prefix = iri.substring(0, colonIndex);
83:• if (URN_PREFIX.equalsIgnoreCase(prefix)) {
84: return iri;
85: }
86:• if (!namespaces.containsKey(prefix)) {
87: LOG.warn("Namespace for prefix '{}' not registered. Prefixed IRI '{}' will not be resolved.", prefix, iri);
88: return iri;
89: }
90: final String localName = iri.substring(colonIndex + 1);
91: return namespaces.get(prefix) + localName;
92: }
93: }