Skip to contentMethod: getTermMapping(String)
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:
9: import java.util.*;
10:
11: /**
12: * Represents the {@literal @context} JSON-LD attribute.
13: * <p>
14: * Note that this represents the JSON-LD 1.0 context, so no <a href="https://www.w3.org/TR/json-ld/#scoped-contexts">context
15: * scoping</a> is supported and attempting to register a different mapping for an already existing term will result in
16: * an {@link AmbiguousTermMappingException}.
17: */
18: public class MappingJsonLdContext implements JsonLdContext {
19:
20: private final Map<String, JsonNode> mapping = new HashMap<>();
21:
22: /**
23: * Registers the specified term mapping in this context.
24: * <p>
25: * Typically, the {@code term} would be Java attribute (field) name and {@code iri} would be the IRI to which this
26: * field is mapped.
27: *
28: * @param term Mapped term
29: * @param iri IRI to which the term is mapped
30: * @throws AmbiguousTermMappingException When term is already mapped to a different IRI
31: */
32: @Override
33: public void registerTermMapping(String term, String iri) {
34: Objects.requireNonNull(term);
35: Objects.requireNonNull(iri);
36: final JsonNode value = JsonNodeFactory.createLiteralNode(term, iri);
37: verifyMappingUnique(term, value);
38: mapping.put(term, value);
39: }
40:
41: private void verifyMappingUnique(String term, JsonNode value) {
42: if (mapping.containsKey(term) && !Objects.equals(mapping.get(term), value)) {
43: throw new AmbiguousTermMappingException("Context already contains mapping for term '" + term + "'.");
44: }
45: }
46:
47: /**
48: * Registers the specified term mapping in this context.
49: * <p>
50: * Compared to {@link #registerTermMapping(String, String)}, this method allows registering more complex mapping
51: * like language container.
52: *
53: * @param term Mapped term
54: * @param mappedNode Object node to which the term is mapped
55: * @throws AmbiguousTermMappingException When term is already mapped to a different IRI
56: */
57: public void registerTermMapping(String term, JsonNode mappedNode) {
58: Objects.requireNonNull(term);
59: Objects.requireNonNull(mappedNode);
60: verifyMappingUnique(term, mappedNode);
61: mapping.put(term, mappedNode);
62: }
63:
64: Map<String, JsonNode> getMapping() {
65: return Collections.unmodifiableMap(mapping);
66: }
67:
68: public Optional<JsonNode> getTermMapping(String term) {
69: return Optional.ofNullable(mapping.get(term));
70: }
71:
72: @Override
73: public boolean hasTermMapping(String term) {
74: return mapping.containsKey(term);
75: }
76:
77: /**
78: * Returns a {@link JsonNode} representing this context.
79: * <p>
80: * The result can thus be added to serialization output.
81: *
82: * @return {@code JsonNode} with registered mappings
83: */
84: public JsonNode getContextNode() {
85: final ObjectNode node = new ObjectNode(JsonLd.CONTEXT);
86: mapping.values().forEach(node::addItem);
87: return node;
88: }
89: }