Skip to content

Commit d20122a

Browse files
committed
Minor tweaks
1 parent 5816977 commit d20122a

File tree

5 files changed

+135
-140
lines changed

5 files changed

+135
-140
lines changed

src/main/java/com/apicatalog/jsonld/context/ActiveContextBuilder.java

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,10 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
120120
// its value MUST be boolean true or false, set propagate to that value.
121121
if (JsonUtils.isObject(localContext)) {
122122

123-
final JsonObject localContextObject = localContext.asJsonObject();
123+
final JsonValue propagateValue = localContext.asJsonObject()
124+
.get(Keywords.PROPAGATE);
124125

125-
if (localContextObject.containsKey(Keywords.PROPAGATE)) {
126-
127-
final JsonValue propagateValue = localContextObject.get(Keywords.PROPAGATE);
126+
if (propagateValue != null) {
128127

129128
if (JsonUtils.isNotBoolean(propagateValue)) {
130129
throw new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_PROPAGATE_VALUE);
@@ -171,7 +170,7 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
171170
// 5.2. if context is a string,
172171
if (JsonUtils.isString(itemContext)) {
173172

174-
fetch(((JsonString)itemContext).getString(), baseUrl);
173+
fetch(((JsonString) itemContext).getString(), baseUrl);
175174

176175
// 5.2.7
177176
continue;
@@ -186,10 +185,10 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
186185
// 5.4. Otherwise, context is a context definition
187186
JsonObject contextDefinition = itemContext.asJsonObject();
188187

189-
// 5.5. If context has an @version
190-
if (contextDefinition.containsKey(Keywords.VERSION)) {
188+
final JsonValue version = contextDefinition.get(Keywords.VERSION);
191189

192-
final JsonValue version = contextDefinition.get(Keywords.VERSION);
190+
// 5.5. If context has an @version
191+
if (version != null) {
193192

194193
String versionString = null;
195194

@@ -250,10 +249,10 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
250249
}
251250

252251
importedStructure = importedDocument
253-
.getJsonContent()
254-
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE));
252+
.getJsonContent()
253+
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE));
255254

256-
// 5.6.5
255+
// 5.6.5
257256
} catch (JsonLdError e) {
258257
throw new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE, e);
259258
}
@@ -263,27 +262,27 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
263262
throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT);
264263
}
265264

266-
JsonObject importedContext = importedStructure.asJsonObject();
265+
final JsonValue importedContext = importedStructure.asJsonObject().get(Keywords.CONTEXT);
267266

268-
if (!importedContext.containsKey(Keywords.CONTEXT)
269-
|| JsonUtils.isNotObject(importedContext.get(Keywords.CONTEXT))) {
267+
if (importedContext == null
268+
|| JsonUtils.isNotObject(importedContext)) {
270269
throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT);
271270
}
272271

273-
importedContext = importedContext.getJsonObject(Keywords.CONTEXT);
272+
final JsonObject importedContextObject = importedContext.asJsonObject();
274273

275274
// 5.6.7
276-
if (importedContext.containsKey(Keywords.IMPORT)) {
275+
if (importedContextObject.containsKey(Keywords.IMPORT)) {
277276
throw new JsonLdError(JsonLdErrorCode.INVALID_CONTEXT_ENTRY);
278277
}
279278

280279
// 5.6.8
281-
contextDefinition = JsonUtils.merge(importedContext, contextDefinition);
280+
contextDefinition = JsonUtils.merge(importedContextObject, contextDefinition);
282281
}
283282

284283
// 5.7. If context has an @base entry and remote contexts is empty,
285284
// i.e., the currently being processed context is not a remote context:
286-
if (contextDefinition.containsKey(Keywords.BASE) /*&& remoteContexts.isEmpty()*/) {
285+
if (contextDefinition.containsKey(Keywords.BASE) /* && remoteContexts.isEmpty() */) {
287286
// 5.7.1
288287
JsonValue value = contextDefinition.get(Keywords.BASE);
289288

@@ -303,18 +302,18 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
303302
if (valueUri.isAbsolute()) {
304303
result.setBaseUri(valueUri);
305304

306-
// 5.7.4
305+
// 5.7.4
307306
} else if (result.getBaseUri() != null) {
308307
result.setBaseUri(UriResolver.resolveAsUri(result.getBaseUri(), valueUri));
309308

310309
} else {
311310
LOGGER.log(Level.FINE,
312-
"5.7.4: valueString={0}, localContext={1}, baseUrl={2}",
313-
new Object[] {valueString, localContext, baseUrl});
311+
"5.7.4: valueString={0}, localContext={1}, baseUrl={2}",
312+
new Object[] { valueString, localContext, baseUrl });
314313

315314
throw new JsonLdError(JsonLdErrorCode.INVALID_BASE_IRI,
316315
"A relative base IRI cannot be resolved [@base = " + valueString +
317-
"]. Please use JsonLdOptions.setBase() method to set an absolute IRI.");
316+
"]. Please use JsonLdOptions.setBase() method to set an absolute IRI.");
318317
}
319318

320319
} else if (StringUtils.isNotBlank(valueString)) {
@@ -338,19 +337,18 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
338337
if (JsonUtils.isNull(value)) {
339338
result.setVocabularyMapping(null);
340339

341-
// 5.8.3
340+
// 5.8.3
342341
} else if (JsonUtils.isString(value)) {
343342

344343
final String valueString = ((JsonString) value).getString();
345344

346345
if (StringUtils.isBlank(valueString) || BlankNode.hasPrefix(valueString) || UriUtils.isURI(valueString)) {
347346

348-
final String vocabularyMapping =
349-
result
350-
.uriExpansion()
351-
.vocab(true)
352-
.documentRelative(true)
353-
.expand(valueString);
347+
final String vocabularyMapping = result
348+
.uriExpansion()
349+
.vocab(true)
350+
.documentRelative(true)
351+
.expand(valueString);
354352

355353
if (BlankNode.hasPrefix(valueString) || UriUtils.isURI(vocabularyMapping)) {
356354
result.setVocabularyMapping(vocabularyMapping);
@@ -378,10 +376,10 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
378376
if (JsonUtils.isNull(value)) {
379377
result.setDefaultLanguage(null);
380378

381-
// 5.9.3
379+
// 5.9.3
382380
} else if (JsonUtils.isString(value)) {
383381

384-
result.setDefaultLanguage(((JsonString)value).getString());
382+
result.setDefaultLanguage(((JsonString) value).getString());
385383

386384
if (!LanguageTag.isWellFormed(result.getDefaultLanguage())) {
387385
LOGGER.log(Level.WARNING, "Language tag [{0}] is not well formed.", result.getDefaultLanguage());
@@ -407,7 +405,7 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
407405
if (JsonUtils.isNull(value)) {
408406
result.setDefaultBaseDirection(DirectionType.NULL);
409407

410-
// 5.10.4.
408+
// 5.10.4.
411409
} else if (JsonUtils.isString(value)) {
412410

413411
final String direction = ((JsonString) value).getString();
@@ -439,11 +437,10 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
439437
}
440438
}
441439

442-
final TermDefinitionBuilder termBuilder =
443-
result
444-
.newTerm(contextDefinition, new HashMap<>())
445-
.baseUrl(baseUrl)
446-
.overrideProtectedFlag(overrideProtected);
440+
final TermDefinitionBuilder termBuilder = result
441+
.newTerm(contextDefinition, new HashMap<>())
442+
.baseUrl(baseUrl)
443+
.overrideProtectedFlag(overrideProtected);
447444

448445
// 5.13
449446
for (final String key : contextDefinition.keySet()) {
@@ -452,9 +449,9 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
452449
Keywords.PROPAGATE, Keywords.PROTECTED, Keywords.VERSION, Keywords.VOCAB)) {
453450

454451
termBuilder
455-
.protectedFlag(JsonUtils.isTrue(contextDefinition.get(Keywords.PROTECTED)))
456-
.remoteContexts(new ArrayList<>(remoteContexts))
457-
.create(key);
452+
.protectedFlag(JsonUtils.isTrue(contextDefinition.get(Keywords.PROTECTED)))
453+
.remoteContexts(new ArrayList<>(remoteContexts))
454+
.create(key);
458455
}
459456
}
460457
}
@@ -534,7 +531,7 @@ private void fetch(final String context, final URI baseUrl) throws JsonLdError {
534531

535532
remoteImport = activeContext.getOptions().getDocumentLoader().loadDocument(contextUri, loaderOptions);
536533

537-
// 5.2.5.1.
534+
// 5.2.5.1.
538535
} catch (JsonLdError e) {
539536
throw new JsonLdError(JsonLdErrorCode.LOADING_REMOTE_CONTEXT_FAILED, e);
540537
}
@@ -545,7 +542,7 @@ private void fetch(final String context, final URI baseUrl) throws JsonLdError {
545542
}
546543

547544
final JsonStructure importedStructure = remoteImport.getJsonContent()
548-
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, "Imported context is null."));
545+
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, "Imported context is null."));
549546

550547
// 5.2.5.2.
551548
if (JsonUtils.isNotObject(importedStructure)) {
@@ -575,10 +572,10 @@ private void fetch(final String context, final URI baseUrl) throws JsonLdError {
575572
// 5.2.6
576573
try {
577574
result = result
578-
.newContext()
579-
.remoteContexts(new ArrayList<>(remoteContexts))
580-
.validateScopedContext(validateScopedContext)
581-
.create(importedContext, remoteImport.getDocumentUrl());
575+
.newContext()
576+
.remoteContexts(new ArrayList<>(remoteContexts))
577+
.validateScopedContext(validateScopedContext)
578+
.create(importedContext, remoteImport.getDocumentUrl());
582579

583580
if (result.getOptions() != null && result.getOptions().getContextCache() != null && !validateScopedContext) {
584581
result.getOptions().getContextCache().put(contextKey, importedContext);

src/main/java/com/apicatalog/jsonld/context/InverseContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public boolean contains(final String variable, final String container, final Str
5353

5454
public boolean contains(final String variable, final String container, final String type, final String key) {
5555
return contains(variable)
56-
&& context.get(variable).containsKey(container)
57-
&& context.get(variable).get(container).containsKey(type)
58-
&& context.get(variable).get(container).get(type).containsKey(key);
56+
&& context.get(variable).containsKey(container)
57+
&& context.get(variable).get(container).containsKey(type)
58+
&& context.get(variable).get(container).get(type).containsKey(key);
5959
}
6060

6161
public InverseContext setIfAbsent(final String variable, final String container, final String type, final String key, final String value) {

0 commit comments

Comments
 (0)