Skip to content

Method: getMapping()

1: package cz.cvut.kbss.jsonld.serialization.context;
2:
3: import cz.cvut.kbss.jsonld.JsonLd;
4: import cz.cvut.kbss.jsonld.exception.AmbiguousTermMappingException;
5: import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
6: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
7: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
8: import cz.cvut.kbss.jsonld.serialization.model.StringLiteralNode;
9:
10: import java.util.*;
11:
12: /**
13: * Represents the {@literal @context} JSON-LD attribute.
14: * <p>
15: * Note that this represents the JSON-LD 1.0 context, so no <a href="https://www.w3.org/TR/json-ld/#scoped-contexts">context
16: * scoping</a> is supported and attempting to register a different mapping for an already existing term will result in
17: * an {@link AmbiguousTermMappingException}.
18: */
19: public class MappingJsonLdContext implements JsonLdContext {
20:
21: private final Map<String, JsonNode> mapping = new HashMap<>();
22:
23: /**
24: * Registers the specified term mapping in this context.
25: * <p>
26: * Typically, the {@code term} would be Java attribute (field) name and {@code iri} would be the IRI to which this
27: * field is mapped.
28: *
29: * @param term Mapped term
30: * @param iri IRI to which the term is mapped
31: * @throws AmbiguousTermMappingException When term is already mapped to a different IRI
32: */
33: @Override
34: public void registerTermMapping(String term, String iri) {
35: Objects.requireNonNull(term);
36: Objects.requireNonNull(iri);
37: final JsonNode value = JsonNodeFactory.createStringLiteralNode(term, iri);
38: verifyMappingUnique(term, value);
39: mapping.put(term, value);
40: }
41:
42: private void verifyMappingUnique(String term, JsonNode value) {
43: if (mapping.containsKey(term) && !Objects.equals(mapping.get(term), value)) {
44: throw new AmbiguousTermMappingException("Context already contains mapping for term '" + term + "'.");
45: }
46: }
47:
48: /**
49: * Registers the specified term mapping in this context.
50: * <p>
51: * Compared to {@link #registerTermMapping(String, String)}, this method allows registering more complex mapping
52: * like language container.
53: *
54: * @param term Mapped term
55: * @param mappedNode Object node to which the term is mapped
56: * @throws AmbiguousTermMappingException When term is already mapped to a different IRI
57: */
58: public void registerTermMapping(String term, ObjectNode mappedNode) {
59: Objects.requireNonNull(term);
60: Objects.requireNonNull(mappedNode);
61: verifyMappingUnique(term, mappedNode);
62: mapping.put(term, mappedNode);
63: }
64:
65: Map<String, JsonNode> getMapping() {
66: return Collections.unmodifiableMap(mapping);
67: }
68:
69: public Optional<JsonNode> getTermMapping(String term) {
70: return Optional.ofNullable(mapping.get(term));
71: }
72:
73: @Override
74: public boolean hasTermMapping(String term) {
75: return mapping.containsKey(term);
76: }
77:
78: @Override
79: public Optional<String> getMappedTerm(String iri) {
80: Objects.requireNonNull(iri);
81: for (Map.Entry<String, JsonNode> e : mapping.entrySet()) {
82: final Optional<String> id = extractId(e.getValue());
83: if (id.isPresent() && iri.equals(id.get())) {
84: return Optional.of(e.getKey());
85: }
86: }
87: return Optional.empty();
88: }
89:
90: private Optional<String> extractId(JsonNode node) {
91: assert node instanceof StringLiteralNode || node instanceof ObjectNode;
92: if (node instanceof StringLiteralNode) {
93: return Optional.of(((StringLiteralNode) node).getValue());
94: } else {
95: final ObjectNode on = (ObjectNode) node;
96: return on.getItems().stream().filter(item -> JsonLd.ID.equals(item.getName()))
97: .map(idNode -> ((StringLiteralNode) idNode).getValue()).findAny();
98: }
99: }
100:
101: /**
102: * Returns a {@link JsonNode} representing this context.
103: * <p>
104: * The result can thus be added to serialization output.
105: *
106: * @return {@code JsonNode} with registered mappings
107: */
108: public JsonNode getContextNode() {
109: final ObjectNode node = new ObjectNode(JsonLd.CONTEXT);
110: mapping.values().forEach(node::addItem);
111: return node;
112: }
113: }