-
Notifications
You must be signed in to change notification settings - Fork 2
/
MappingJsonLdContext.java
91 lines (78 loc) · 3.23 KB
/
MappingJsonLdContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* JB4JSON-LD
* Copyright (C) 2023 Czech Technical University in Prague
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package cz.cvut.kbss.jsonld.serialization.context;
import cz.cvut.kbss.jsonld.JsonLd;
import cz.cvut.kbss.jsonld.exception.AmbiguousTermMappingException;
import cz.cvut.kbss.jsonld.serialization.JsonNodeFactory;
import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
import java.util.Optional;
/**
* Represents the {@literal @context} JSON-LD attribute.
* <p>
* Note that this represents the JSON-LD 1.0 context, so no <a href="https://www.w3.org/TR/json-ld/#scoped-contexts">context
* scoping</a> is supported and attempting to register a different mapping for an already existing term will result in
* an {@link AmbiguousTermMappingException}.
*/
public class MappingJsonLdContext implements JsonLdContext {
private TermMappingHolder mappingHolder;
public MappingJsonLdContext() {
this.mappingHolder = new EmbeddedTermMappingHolder();
}
public MappingJsonLdContext(JsonLdContext parent) {
assert parent instanceof MappingJsonLdContext;
this.mappingHolder = new WriteThroughTermMappingHolder(((MappingJsonLdContext) parent).mappingHolder);
}
@Override
public void registerTermMapping(String term, String iri) {
final JsonNode value = JsonNodeFactory.createStringLiteralNode(term, iri);
if (!mappingHolder.canRegisterTermMapping(term, value) && !mappingHolder.isRoot()) {
this.mappingHolder = new EmbeddedTermMappingHolder(mappingHolder);
}
mappingHolder.registerTermMapping(term, value);
}
@Override
public void registerTermMapping(String term, ObjectNode mappedNode) {
if (!mappingHolder.canRegisterTermMapping(term, mappedNode) && !mappingHolder.isRoot()) {
this.mappingHolder = new EmbeddedTermMappingHolder(mappingHolder);
}
mappingHolder.registerTermMapping(term, mappedNode);
}
@Override
public Optional<JsonNode> getTermMapping(String term) {
return mappingHolder.getTermMapping(term);
}
@Override
public boolean hasTermMapping(String term) {
return mappingHolder.hasTermMapping(term);
}
@Override
public Optional<String> getMappedTerm(String iri) {
return mappingHolder.getMappedTerm(iri);
}
@Override
public boolean isCurrentEmpty() {
return mappingHolder.isEmpty();
}
@Override
public ObjectNode getContextNode() {
final ObjectNode node = new ObjectNode(JsonLd.CONTEXT);
mappingHolder.getMapping().values().forEach(node::addItem);
return node;
}
}