Package: TermMappingHolder
TermMappingHolder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TermMappingHolder(TermMappingHolder) |
|
|
|
|
|
||||||||||||||||||||
extractId(JsonNode) |
|
|
|
|
|
||||||||||||||||||||
getMappedTerm(String) |
|
|
|
|
|
||||||||||||||||||||
lambda$extractId$0(JsonNode) |
|
|
|
|
|
||||||||||||||||||||
lambda$extractId$1(JsonNode) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * JB4JSON-LD
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.jsonld.serialization.context;
19:
20: import cz.cvut.kbss.jsonld.JsonLd;
21: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
22: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
23: import cz.cvut.kbss.jsonld.serialization.model.StringLiteralNode;
24:
25: import java.util.Map;
26: import java.util.Objects;
27: import java.util.Optional;
28:
29: abstract class TermMappingHolder {
30:
31: final TermMappingHolder parentContext;
32:
33: TermMappingHolder(TermMappingHolder parentContext) {
34: this.parentContext = parentContext;
35: }
36:
37: abstract boolean canRegisterTermMapping(String term, JsonNode mappedNode);
38:
39: abstract void registerTermMapping(String Term, JsonNode node);
40:
41: abstract Optional<JsonNode> getTermMapping(String term);
42:
43: abstract Map<String, JsonNode> getMapping();
44:
45: abstract boolean hasTermMapping(String term);
46:
47: abstract boolean hasTermMapping(String term, JsonNode mappedNode);
48:
49: abstract boolean isEmpty();
50:
51: abstract boolean isRoot();
52:
53: public Optional<String> getMappedTerm(String iri) {
54: Objects.requireNonNull(iri);
55:• for (Map.Entry<String, JsonNode> e : getMapping().entrySet()) {
56: final Optional<String> id = extractId(e.getValue());
57:• if (id.isPresent() && iri.equals(id.get())) {
58: return Optional.of(e.getKey());
59: }
60: }
61: return Optional.empty();
62: }
63:
64: private static Optional<String> extractId(JsonNode node) {
65:• assert node instanceof StringLiteralNode || node instanceof ObjectNode;
66:• if (node instanceof StringLiteralNode) {
67: return Optional.of(((StringLiteralNode) node).getValue());
68: } else {
69: final ObjectNode on = (ObjectNode) node;
70: return on.getItems().stream().filter(item -> JsonLd.ID.equals(item.getName()))
71: .map(idNode -> ((StringLiteralNode) idNode).getValue()).findAny();
72: }
73: }
74: }