@@ -39,14 +39,16 @@ public class RoCrateMetadataContext implements CrateMetadataContext {
39
39
protected final HashMap <String , String > other = new HashMap <>();
40
40
41
41
/**
42
- * Default constructor for the creation of the default context.
42
+ * Default constructor for the creation of the v1.1 default context.
43
43
*/
44
44
public RoCrateMetadataContext () {
45
45
this .addToContextFromUrl (DEFAULT_CONTEXT );
46
46
}
47
47
48
48
/**
49
49
* Constructor for creating the context from a list of url.
50
+ * <p>
51
+ * Note: Does NOT contain the default context if not explicitly given!
50
52
*
51
53
* @param urls the url list with different context.
52
54
*/
@@ -56,6 +58,8 @@ public RoCrateMetadataContext(Collection<String> urls) {
56
58
57
59
/**
58
60
* Constructor for creating the context from a json object.
61
+ * <p>
62
+ * Note: Does NOT contain the default context if not explicitly given!
59
63
*
60
64
* @param context the Json object of the context.
61
65
*/
@@ -84,6 +88,28 @@ public RoCrateMetadataContext(JsonNode context) {
84
88
}
85
89
}
86
90
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
+ */
87
113
@ Override
88
114
public ObjectNode getContextJsonEntity () {
89
115
ObjectMapper objectMapper = MyObjectMapper .getMapper ();
@@ -107,6 +133,21 @@ public ObjectNode getContextJsonEntity() {
107
133
return finalNode ;
108
134
}
109
135
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
+ */
110
151
@ Override
111
152
public boolean checkEntity (AbstractEntity entity ) {
112
153
ObjectMapper objectMapper = MyObjectMapper .getMapper ();
@@ -161,6 +202,13 @@ public boolean checkEntity(AbstractEntity entity) {
161
202
return true ;
162
203
}
163
204
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
+ */
164
212
@ Override
165
213
public void addToContextFromUrl (String url ) {
166
214
this .urls .add (url );
@@ -200,18 +248,31 @@ public void addToContextFromUrl(String url) {
200
248
}));
201
249
}
202
250
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
+ */
203
257
@ Override
204
258
public void addToContext (String key , String value ) {
205
259
this .contextMap .put (key , value );
206
260
this .other .put (key , value );
207
261
}
208
262
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
+ */
209
267
@ Override
210
268
public String getValueOf (String key ) {
211
269
return Optional .ofNullable (this .contextMap .get (key ))
212
270
.orElseGet (() -> this .other .get (key ));
213
271
}
214
272
273
+ /**
274
+ * @return the set of all keys in the context.
275
+ */
215
276
@ Override
216
277
public Set <String > getKeys () {
217
278
List <String > merged = new ArrayList <>();
@@ -220,6 +281,11 @@ public Set<String> getKeys() {
220
281
return Set .copyOf (merged );
221
282
}
222
283
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
+ */
223
289
@ Override
224
290
public Map <String , String > getPairs () {
225
291
Map <String , String > merged = new HashMap <>();
@@ -228,13 +294,18 @@ public Map<String, String> getPairs() {
228
294
return Map .copyOf (merged );
229
295
}
230
296
231
-
297
+ /**
298
+ * @param key the key to delete from the context.
299
+ */
232
300
@ Override
233
301
public void deleteValuePairFromContext (String key ) {
234
302
this .contextMap .remove (key );
235
303
this .other .remove (key );
236
304
}
237
305
306
+ /**
307
+ * @param url the URL to delete from the context.
308
+ */
238
309
@ Override
239
310
public void deleteUrlFromContext (String url ) {
240
311
this .urls .remove (url );
0 commit comments