Skip to content

Commit 8de54a3

Browse files
authored
Merge pull request #353 from filip26/issue/347
Add undefined terms processing policy (Fail, Warn, Ignore)
2 parents 35424ce + 681700d commit 8de54a3

File tree

17 files changed

+244
-74
lines changed

17 files changed

+244
-74
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ JsonLd.toRdf("https://example/document.jsonld").loader(cachedLoader).get();
196196
JsonLd.toRdf("https://example/another-document.jsonld").loader(cachedLoader).get();
197197
```
198198

199+
#### Undefined Terms Processing Policy
200+
201+
Set processing policy on undefined terms. `Ignore` by default.
202+
203+
```javascript
204+
// since 1.4.1
205+
JsonLd.expand(...).undefinedTermsPolicy(Fail|Warn|Ignore).get();
206+
```
207+
199208
## Contributing
200209

201210
All PR's welcome!

pom_parent.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<version>3.7.0</version>
130130
<configuration>
131131
<doclint>all,-missing</doclint>
132+
<failOnWarnings>false</failOnWarnings>
132133
</configuration>
133134
<executions>
134135
<execution>

src/main/java/com/apicatalog/jsonld/JsonLdErrorCode.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
public enum JsonLdErrorCode {
3232

3333
/**
34-
* Two
35-
* <a href="https://www.w3.org/TR/rdf11-concepts/#dfn-property">properties</a>
34+
* Two <a href="https://www.w3.org/TR/rdf11-concepts/#dfn-property">properties</a>
3635
* which expand to the same keyword have been detected. This might occur if a
3736
* keyword and an alias thereof are used at the same time.
3837
*/
@@ -255,20 +254,22 @@ public enum JsonLdErrorCode {
255254
*/
256255
PROTECTED_TERM_REDEFINITION,
257256

258-
259257
// Framing Error Codes https://www.w3.org/TR/json-ld11-framing/#error-handling
260258

261259
/**
262260
* The frame is invalid.
263261
*
264-
* @see <a href="https://www.w3.org/TR/json-ld11-framing/#dom-jsonldframingerrorcode-invalid-frame">invalid frame</a>
262+
* @see <a href=
263+
* "https://www.w3.org/TR/json-ld11-framing/#dom-jsonldframingerrorcode-invalid-frame">invalid
264+
* frame</a>
265265
*/
266266
INVALID_FRAME,
267267

268268
/**
269-
* The value for <code>@embed</code> is not one recognized for the object embed flag.
269+
* The value for <code>@embed</code> is not one recognized for the object embed
270+
* flag.
270271
*
271-
* @see <a href="https://www.w3.org/TR/json-ld11-framing/#dom-jsonldframingerrorcode-invalid-@embed-value">invalid @embed value</a>
272+
* @see <a href="https://www.w3.org/TR/json-ld11-framing/#dom-jsonldframingerrorcode-invalid-@embed-value">invalid @embedvalue</a>
272273
*/
273274
INVALID_KEYWORD_EMBED_VALUE,
274275

@@ -289,9 +290,14 @@ public enum JsonLdErrorCode {
289290
INVALID_ANNOTATION,
290291

291292
// Custom
292-
293+
293294
PROCESSING_TIMEOUT_EXCEEDED,
294-
295+
296+
/**
297+
* An undefined term has been detected during expansion
298+
*/
299+
UNDEFINED_TERM,
300+
295301
UNSPECIFIED;
296302

297303
private static final Map<JsonLdErrorCode, String> CODE_TO_MESSAGE;
@@ -351,9 +357,10 @@ public enum JsonLdErrorCode {
351357
messages.put(PROTECTED_TERM_REDEFINITION, "An attempt was made to redefine a protected term");
352358
messages.put(INVALID_FRAME, "The frame is invalid");
353359
messages.put(INVALID_KEYWORD_EMBED_VALUE, "The value for @embed is not one recognized for the object embed flag");
354-
messages.put(INVALID_EMBEDDED_NODE, "An invalid embedded node has been detected.");
360+
messages.put(INVALID_EMBEDDED_NODE, "An invalid embedded node has been detected");
355361
messages.put(INVALID_ANNOTATION, "An invalid annotation has been detected");
356-
messages.put(PROCESSING_TIMEOUT_EXCEEDED, "A processing has exceeded a defined timeount");
362+
messages.put(PROCESSING_TIMEOUT_EXCEEDED, "A processing has exceeded a defined timeout");
363+
messages.put(UNDEFINED_TERM, "An undefined term has been found. Set policy to ignore to pass");
357364

358365
CODE_TO_MESSAGE = Collections.unmodifiableMap(messages);
359366
}
@@ -362,4 +369,3 @@ public String toMessage() {
362369
return CODE_TO_MESSAGE.getOrDefault(this, "Processing error") + " [code=" + this + "].";
363370
}
364371
}
365-

src/main/java/com/apicatalog/jsonld/JsonLdOptions.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public enum RdfDirection {
4545
COMPOUND_LITERAL
4646
}
4747

48+
public enum ProcessingPolicy {
49+
/** ignore, the current and default behavior */
50+
Ignore,
51+
/** stop processing with an error */
52+
Fail,
53+
/** print warning to log */
54+
Warn
55+
}
56+
4857
/* default values */
4958
public static final boolean DEFAULT_RDF_STAR = false;
5059
public static final boolean DEFAULT_NUMERIC_ID = false;
@@ -124,9 +133,12 @@ public enum RdfDirection {
124133
private Cache<String, Document> documentCache;
125134

126135
private boolean uriValidation;
127-
136+
128137
private Duration timeout;
129138

139+
// a policy on how proceed with undefined terms during expansion
140+
private ProcessingPolicy undefinedTerms;
141+
130142
public JsonLdOptions() {
131143
this(SchemeRouter.defaultInstance());
132144
}
@@ -163,6 +175,7 @@ public JsonLdOptions(DocumentLoader loader) {
163175
this.documentCache = null;
164176
this.uriValidation = DEFAULT_URI_VALIDATION;
165177
this.timeout = null;
178+
this.undefinedTerms = ProcessingPolicy.Ignore;
166179
}
167180

168181
public JsonLdOptions(JsonLdOptions options) {
@@ -195,6 +208,7 @@ public JsonLdOptions(JsonLdOptions options) {
195208
this.documentCache = options.documentCache;
196209
this.uriValidation = options.uriValidation;
197210
this.timeout = options.timeout;
211+
this.undefinedTerms = options.undefinedTerms;
198212
}
199213

200214
/**
@@ -500,7 +514,7 @@ public void setUriValidation(boolean enabled) {
500514
/**
501515
* A processing timeout. An exception is thrown when a processing time exceeds
502516
* the duration, if set. There is no currency that processing gets terminated
503-
* immediately, but eventually.
517+
* immediately, but eventually.
504518
*
505519
* Please note, the timeout does not include time consumed by
506520
* {@link DocumentLoader}.
@@ -510,17 +524,37 @@ public void setUriValidation(boolean enabled) {
510524
public Duration getTimeout() {
511525
return timeout;
512526
}
513-
527+
514528
/**
515-
* Set a pressing timeout. A processing is eventually terminated after the
516-
* specified duration. Set <code>null</code> for no timeout.
529+
* Set a pressing timeout. A processing is eventually terminated after the
530+
* specified duration. Set <code>null</code> for no timeout.
517531
*
518532
* Please note, the timeout does not include time consumed by
519533
* {@link DocumentLoader}.
520-
*
534+
*
521535
* @param timeout to limit processing time
522536
*/
523537
public void setTimeout(Duration timeout) {
524538
this.timeout = timeout;
525539
}
540+
541+
/**
542+
* A processing policy on how proceed with an undefined term during expansion.
543+
*
544+
* @return the processing policy, never <code>null</code>
545+
*/
546+
public ProcessingPolicy getUndefinedTermsPolicy() {
547+
return undefinedTerms;
548+
}
549+
550+
/**
551+
* Set processing policy on how proceed with an undefined term during expansion.
552+
* Ignore by default.
553+
*
554+
* @param undefinedTerms the processing policy, never <code>null</code>
555+
*
556+
*/
557+
public void setUndefinedTermsPolicy(ProcessingPolicy undefinedTerms) {
558+
this.undefinedTerms = undefinedTerms;
559+
}
526560
}

src/main/java/com/apicatalog/jsonld/api/ExpansionApi.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.apicatalog.jsonld.JsonLdError;
2121
import com.apicatalog.jsonld.JsonLdOptions;
2222
import com.apicatalog.jsonld.JsonLdVersion;
23+
import com.apicatalog.jsonld.JsonLdOptions.ProcessingPolicy;
2324
import com.apicatalog.jsonld.document.Document;
2425
import com.apicatalog.jsonld.document.JsonDocument;
2526
import com.apicatalog.jsonld.loader.DocumentLoader;
@@ -157,4 +158,16 @@ public ExpansionApi rdfStar() {
157158
options.setRdfStar(true);
158159
return this;
159160
}
161+
162+
/**
163+
* Set a processing policy determining how to proceed when an undefined term is
164+
* found during an expansion. An unknown term is ignored by default.
165+
*
166+
* @param policy a processing policy
167+
* @return builder instance
168+
*/
169+
public ExpansionApi undefinedTermsPolicy(ProcessingPolicy policy) {
170+
options.setUndefinedTermsPolicy(policy);
171+
return this;
172+
}
160173
}

src/main/java/com/apicatalog/jsonld/expansion/ObjectExpansion1314.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,23 @@ public void expand() throws JsonLdError {
137137
activeContext.runtime().tick();
138138

139139
// 13.2.
140-
String expandedProperty = activeContext
140+
final String expandedProperty = activeContext
141141
.uriExpansion()
142142
.documentRelative(false)
143143
.vocab(true)
144144
.expand(key);
145145

146-
// 13.3.
146+
// if the term is undefined and
147147
if (expandedProperty == null || (!expandedProperty.contains(":") && !Keywords.contains(expandedProperty))) {
148-
continue;
148+
switch (activeContext.runtime().getUndefinedTermPolicy()) {
149+
case Fail:
150+
throw new JsonLdError(JsonLdErrorCode.UNDEFINED_TERM,
151+
"An undefined term has been found [" + key + "]. Change policy to Ignore or Warn or define the term in a context");
152+
case Warn:
153+
LOGGER.log(Level.WARNING, "An undefined term has been found [{0}]", key);
154+
case Ignore:
155+
continue;
156+
}
149157
}
150158

151159
JsonValue value = element.get(key);
@@ -163,7 +171,6 @@ public void expand() throws JsonLdError {
163171
// 13.4.2
164172
if (result.containsKey(expandedProperty)
165173
&& Keywords.noneMatch(expandedProperty, Keywords.INCLUDED, Keywords.TYPE)) {
166-
167174
throw new JsonLdError(JsonLdErrorCode.COLLIDING_KEYWORDS,
168175
"Two properties which expand to the same keyword have been detected. A property '" + key + "'"
169176
+ " expands to '" + expandedProperty + "'"

src/main/java/com/apicatalog/jsonld/processor/ProcessingRuntime.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.apicatalog.jsonld.JsonLdError;
44
import com.apicatalog.jsonld.JsonLdOptions;
5+
import com.apicatalog.jsonld.JsonLdOptions.ProcessingPolicy;
56
import com.apicatalog.jsonld.JsonLdVersion;
67
import com.apicatalog.jsonld.context.cache.Cache;
78
import com.apicatalog.jsonld.document.Document;
@@ -79,4 +80,8 @@ public boolean isRdfStar() {
7980
public boolean isNumericId() {
8081
return options.isNumericId();
8182
}
83+
84+
public ProcessingPolicy getUndefinedTermPolicy() {
85+
return options.getUndefinedTermsPolicy();
86+
}
8287
}

src/test/java/com/apicatalog/jsonld/loader/LRUDocumentCacheTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package com.apicatalog.jsonld.loader;
22

3-
import com.apicatalog.jsonld.JsonLdError;
4-
import com.apicatalog.jsonld.document.Document;
5-
import com.apicatalog.jsonld.document.JsonDocument;
6-
import jakarta.json.JsonValue;
7-
import org.junit.jupiter.api.Assertions;
8-
import org.junit.jupiter.api.Test;
9-
103
import java.net.URI;
114
import java.util.ArrayList;
12-
import java.util.Arrays;
13-
import java.util.Collection;
145
import java.util.LinkedList;
156
import java.util.List;
16-
import java.util.Set;
7+
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import com.apicatalog.jsonld.JsonLdError;
12+
import com.apicatalog.jsonld.document.Document;
13+
import com.apicatalog.jsonld.document.JsonDocument;
14+
15+
import jakarta.json.JsonValue;
1716

1817
public class LRUDocumentCacheTest {
1918

0 commit comments

Comments
 (0)