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: *
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.jopa.utils;
16:
17: import cz.cvut.kbss.jopa.CommonVocabulary;
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", CommonVocabulary.RDF_NAMESPACE);
38: registerNamespace("rdfs", CommonVocabulary.RDFS_NAMESPACE);
39: registerNamespace("xsd", CommonVocabulary.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 in this resolver.
56: *
57: * @param iri The IRI to resolve
58: * @return Full IRI, if this resolver was able to resolve it, or the original IRI
59: */
60: public String resolveFullIri(String iri) {
61: Objects.requireNonNull(iri);
62: final int colonIndex = iri.indexOf(':');
63:• if (colonIndex == -1 || colonIndex > iri.length()) {
64: return iri;
65: }
66: final String prefix = iri.substring(0, colonIndex);
67:• if (!namespaces.containsKey(prefix)) {
68: return iri;
69: }
70: final String localName = iri.substring(colonIndex + 1);
71: return namespaces.get(prefix) + localName;
72: }
73: }