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: package cz.cvut.kbss.jopa.utils;
2:
3: import cz.cvut.kbss.jopa.CommonVocabulary;
4:
5: import java.util.HashMap;
6: import java.util.Map;
7: import java.util.Objects;
8:
9: /**
10: * Holds mapping of prefixes to namespaces and allows resolution of prefixed IRIs.
11: * <p>
12: * Prefixes for RDF (rdf:), RDFS (rdfs:) and XSD (xsd:) are pre-registered.
13: */
14: public class NamespaceResolver {
15:
16: private final Map<String, String> namespaces = new HashMap<>();
17:
18: public NamespaceResolver() {
19: registerDefaultPrefixes();
20: }
21:
22: private void registerDefaultPrefixes() {
23: registerNamespace("rdf", CommonVocabulary.RDF_NAMESPACE);
24: registerNamespace("rdfs", CommonVocabulary.RDFS_NAMESPACE);
25: registerNamespace("xsd", CommonVocabulary.XSD_NAMESPACE);
26: }
27:
28: /**
29: * Registers the specified namespace with the specified prefix.
30: *
31: * @param prefix Prefix representing the namespace
32: * @param namespace Namespace to represent
33: */
34: public void registerNamespace(String prefix, String namespace) {
35: Objects.requireNonNull(prefix);
36: Objects.requireNonNull(namespace);
37: namespaces.put(prefix, namespace);
38: }
39:
40: /**
41: * Replaces prefix in the specified IRI with a full namespace IRI, if the IRI contains a prefix and it is registered in this resolver.
42: *
43: * @param iri The IRI to resolve
44: * @return Full IRI, if this resolver was able to resolve it, or the original IRI
45: */
46: public String resolveFullIri(String iri) {
47: Objects.requireNonNull(iri);
48: final int colonIndex = iri.indexOf(':');
49:• if (colonIndex == -1 || colonIndex > iri.length()) {
50: return iri;
51: }
52: final String prefix = iri.substring(0, colonIndex);
53:• if (!namespaces.containsKey(prefix)) {
54: return iri;
55: }
56: final String localName = iri.substring(colonIndex + 1);
57: return namespaces.get(prefix) + localName;
58: }
59: }