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: 47
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.utils;
14:
15: import cz.cvut.kbss.jopa.vocabulary.RDF;
16: import cz.cvut.kbss.jopa.vocabulary.RDFS;
17: import cz.cvut.kbss.jopa.vocabulary.XSD;
18:
19: import java.util.HashMap;
20: import java.util.Map;
21: import java.util.Objects;
22:
23: /**
24: * Holds mapping of prefixes to namespaces and allows resolution of prefixed IRIs.
25: * <p>
26: * Prefixes for RDF (rdf:), RDFS (rdfs:) and XSD (xsd:) are pre-registered.
27: */
28: public class NamespaceResolver {
29:
30: private final Map<String, String> namespaces = new HashMap<>();
31:
32: public NamespaceResolver() {
33: registerDefaultPrefixes();
34: }
35:
36: private void registerDefaultPrefixes() {
37: registerNamespace(RDF.PREFIX, RDF.NAMESPACE);
38: registerNamespace(RDFS.PREFIX, RDFS.NAMESPACE);
39: registerNamespace(XSD.PREFIX, XSD.NAMESPACE);
40: }
41:
42: /**
43: * Registers the specified namespace with the specified prefix.
44: *
45: * @param prefix Prefix representing the namespace
46: * @param namespace Namespace to represent
47: */
48: public void registerNamespace(String prefix, String namespace) {
49: Objects.requireNonNull(prefix);
50: Objects.requireNonNull(namespace);
51: namespaces.put(prefix, namespace);
52: }
53:
54: /**
55: * Replaces prefix in the specified IRI with a full namespace IRI, if the IRI contains a prefix and it is registered
56: * in this resolver.
57: *
58: * @param iri The IRI to resolve
59: * @return Full IRI, if this resolver was able to resolve it, or the original IRI
60: */
61: public String resolveFullIri(String iri) {
62: Objects.requireNonNull(iri);
63: final int colonIndex = iri.indexOf(':');
64:• if (colonIndex == -1 || colonIndex > iri.length()) {
65: return iri;
66: }
67: final String prefix = iri.substring(0, colonIndex);
68:• if (!namespaces.containsKey(prefix)) {
69: return iri;
70: }
71: final String localName = iri.substring(colonIndex + 1);
72: return namespaces.get(prefix) + localName;
73: }
74: }