Skip to content

Commit 39518cc

Browse files
committed
doc: improve javadoc for context class
1 parent 78b1957 commit 39518cc

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

src/main/java/edu/kit/datamanager/ro_crate/context/RoCrateMetadataContext.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ public class RoCrateMetadataContext implements CrateMetadataContext {
3939
protected final HashMap<String, String> other = new HashMap<>();
4040

4141
/**
42-
* Default constructor for the creation of the default context.
42+
* Default constructor for the creation of the v1.1 default context.
4343
*/
4444
public RoCrateMetadataContext() {
4545
this.addToContextFromUrl(DEFAULT_CONTEXT);
4646
}
4747

4848
/**
4949
* Constructor for creating the context from a list of url.
50+
* <p>
51+
* Note: Does NOT contain the default context if not explicitly given!
5052
*
5153
* @param urls the url list with different context.
5254
*/
@@ -56,6 +58,8 @@ public RoCrateMetadataContext(Collection<String> urls) {
5658

5759
/**
5860
* Constructor for creating the context from a json object.
61+
* <p>
62+
* Note: Does NOT contain the default context if not explicitly given!
5963
*
6064
* @param context the Json object of the context.
6165
*/
@@ -84,6 +88,28 @@ public RoCrateMetadataContext(JsonNode context) {
8488
}
8589
}
8690

91+
/**
92+
* Converts the context into a JSON-LD representation.
93+
* <p>
94+
* The resulting JSON structure depends on the content:
95+
* - If there's only one URL and no key-value pairs: {"@context": "url"}
96+
* - If there are multiple URLs and/or key-value pairs: {"@context": ["url1", "url2", {"key1": "value1", "key2": "value2"}]}
97+
* <p>
98+
* Example output:
99+
* <pre>
100+
* {
101+
* "@context": [
102+
* "https://w3id.org/ro/crate/1.1/context",
103+
* {
104+
* "schema": "http://schema.org/",
105+
* "rdfs": "http://www.w3.org/2000/01/rdf-schema#"
106+
* }
107+
* ]
108+
* }
109+
* </pre>
110+
*
111+
* @return an ObjectNode containing the JSON-LD context representation
112+
*/
87113
@Override
88114
public ObjectNode getContextJsonEntity() {
89115
ObjectMapper objectMapper = MyObjectMapper.getMapper();
@@ -107,6 +133,21 @@ public ObjectNode getContextJsonEntity() {
107133
return finalNode;
108134
}
109135

136+
/**
137+
* Checks if the given entity is valid according to the context.
138+
* <p>
139+
* - full URLs in the @type and field names are considered valid without further checks.
140+
* - The "@id" value is treated as a special case, where it refers to the entity's ID.
141+
* - The "@json" type is a linked data built-in type and is always considered valid.
142+
* - If a type or field name is not found in the context, it will print an error message and return false.
143+
* - This method checks both the types in the @type array and the field names in the entity's properties.
144+
* - Prefixes in the context are considered valid if they match the context keys.
145+
* - Suffixes after a valid prefix are considered valid in any case. This is not perfect,
146+
* but it would be hard to handle correctly.
147+
*
148+
* @param entity the entity to check
149+
* @return true if the entity is valid, false otherwise
150+
*/
110151
@Override
111152
public boolean checkEntity(AbstractEntity entity) {
112153
ObjectMapper objectMapper = MyObjectMapper.getMapper();
@@ -161,6 +202,13 @@ public boolean checkEntity(AbstractEntity entity) {
161202
return true;
162203
}
163204

205+
/**
206+
* Adds a URL to the context.
207+
* <p>
208+
* It will try to fetch the context from the URL.
209+
*
210+
* @param url the URL to add
211+
*/
164212
@Override
165213
public void addToContextFromUrl(String url) {
166214
this.urls.add(url);
@@ -200,18 +248,31 @@ public void addToContextFromUrl(String url) {
200248
}));
201249
}
202250

251+
/**
252+
* Adds a key-value pair to the context.
253+
*
254+
* @param key the key to add. It may be a prefix or a term.
255+
* @param value the value to add
256+
*/
203257
@Override
204258
public void addToContext(String key, String value) {
205259
this.contextMap.put(key, value);
206260
this.other.put(key, value);
207261
}
208262

263+
/**
264+
* @param key the key for the value to retrieve.
265+
* @return the value of the key if it exists in the context, null otherwise.
266+
*/
209267
@Override
210268
public String getValueOf(String key) {
211269
return Optional.ofNullable(this.contextMap.get(key))
212270
.orElseGet(() -> this.other.get(key));
213271
}
214272

273+
/**
274+
* @return the set of all keys in the context.
275+
*/
215276
@Override
216277
public Set<String> getKeys() {
217278
List<String> merged = new ArrayList<>();
@@ -220,6 +281,11 @@ public Set<String> getKeys() {
220281
return Set.copyOf(merged);
221282
}
222283

284+
/**
285+
* @return a map of all key-value pairs in the context. Note that some pairs may come
286+
* from URLs or a pair may not be available as a context was not successfully resolved
287+
* from a URL.
288+
*/
223289
@Override
224290
public Map<String, String> getPairs() {
225291
Map<String, String> merged = new HashMap<>();
@@ -228,13 +294,18 @@ public Map<String, String> getPairs() {
228294
return Map.copyOf(merged);
229295
}
230296

231-
297+
/**
298+
* @param key the key to delete from the context.
299+
*/
232300
@Override
233301
public void deleteValuePairFromContext(String key) {
234302
this.contextMap.remove(key);
235303
this.other.remove(key);
236304
}
237305

306+
/**
307+
* @param url the URL to delete from the context.
308+
*/
238309
@Override
239310
public void deleteUrlFromContext(String url) {
240311
this.urls.remove(url);

0 commit comments

Comments
 (0)