Skip to contentMethod: getMappedTerm(String)
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.traversal;
19:
20: import cz.cvut.kbss.jsonld.serialization.context.JsonLdContext;
21: import cz.cvut.kbss.jsonld.serialization.model.JsonNode;
22: import cz.cvut.kbss.jsonld.serialization.model.ObjectNode;
23:
24: import java.lang.reflect.Field;
25: import java.util.Objects;
26: import java.util.Optional;
27:
28: /**
29: * Represents the current serialization context.
30: * <p>
31: * This means the value being serialized, and optionally the target JSON-LD term identifier and field.
32: * <p>
33: * Note that term and field may not always be available, e.g., when a collection element is being serialized, neither is
34: * set.
35: * <p>
36: * This class also provides access to the JSON-LD context (if it is used). However, note that for selected serialization
37: * methods the context may be just a stub with no bearing on the serialization output.
38: *
39: * @param <T> Type of the value
40: */
41: public class SerializationContext<T> implements JsonLdContext {
42:
43: private String term;
44:
45: private final Field field;
46:
47: private final T value;
48:
49: private JsonLdContext jsonLdContext;
50:
51: public SerializationContext(String term, T value, JsonLdContext jsonLdContext) {
52: this(term, null, value, jsonLdContext);
53: }
54:
55: public SerializationContext(Field field, T value, JsonLdContext jsonLdContext) {
56: this(null, field, value, jsonLdContext);
57: }
58:
59: public SerializationContext(T value, JsonLdContext jsonLdContext) {
60: this(null, null, value, jsonLdContext);
61: }
62:
63: public SerializationContext(String term, Field field, T value, JsonLdContext jsonLdContext) {
64: this.term = term;
65: this.field = field;
66: this.value = value;
67: this.jsonLdContext = jsonLdContext;
68: }
69:
70: public String getTerm() {
71: return term;
72: }
73:
74: public Field getField() {
75: return field;
76: }
77:
78: public String getFieldName() {
79: return field != null ? field.getName() : null;
80: }
81:
82: public T getValue() {
83: return value;
84: }
85:
86: public JsonLdContext getJsonLdContext() {
87: return jsonLdContext;
88: }
89:
90: public void setJsonLdContext(JsonLdContext jsonLdContext) {
91: this.jsonLdContext = jsonLdContext;
92: }
93:
94: @Override
95: public void registerTermMapping(String term, String iri) {
96: jsonLdContext.registerTermMapping(term, iri);
97: this.term = term;
98: }
99:
100: @Override
101: public void registerTermMapping(String term, ObjectNode mappedNode) {
102: jsonLdContext.registerTermMapping(term, mappedNode);
103: this.term = term;
104: }
105:
106: @Override
107: public Optional<JsonNode> getTermMapping(String term) {
108: return jsonLdContext.getTermMapping(term);
109: }
110:
111: @Override
112: public boolean hasTermMapping(String term) {
113: return jsonLdContext.hasTermMapping(term);
114: }
115:
116: @Override
117: public Optional<String> getMappedTerm(String iri) {
118: return jsonLdContext.getMappedTerm(iri);
119: }
120:
121: @Override
122: public boolean isCurrentEmpty() {
123: return jsonLdContext.isCurrentEmpty();
124: }
125:
126: @Override
127: public ObjectNode getContextNode() {
128: return jsonLdContext.getContextNode();
129: }
130:
131: @Override
132: public boolean equals(Object o) {
133: if (this == o) return true;
134: if (o == null || getClass() != o.getClass()) return false;
135: SerializationContext<?> that = (SerializationContext<?>) o;
136: return Objects.equals(getTerm(), that.getTerm()) && Objects
137: .equals(getField(), that.getField()) && Objects.equals(getValue(), that.getValue());
138: }
139:
140: @Override
141: public int hashCode() {
142: return Objects.hash(getTerm(), getField(), getValue());
143: }
144:
145: }