Skip to content

Commit

Permalink
Use context cache for imported contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
skodapetr committed Dec 29, 2023
1 parent bf179c0 commit e9b3ee1
Showing 1 changed file with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,27 +233,35 @@ public ActiveContext create(final JsonValue localContext, final URI baseUrl) thr
throw new JsonLdError(JsonLdErrorCode.LOADING_REMOTE_CONTEXT_FAILED);
}

final DocumentLoaderOptions loaderOptions = new DocumentLoaderOptions();
loaderOptions.setProfile(ProfileConstants.CONTEXT);
loaderOptions.setRequestProfile(Arrays.asList(loaderOptions.getProfile()));

JsonStructure importedStructure = null;
JsonValue importedStructure;
final String contextImportUriKey = contextImportUri.toString();
if (activeContext.runtime().getContextCache() != null
&& activeContext.runtime().getContextCache().containsKey(contextImportUriKey)) {
importedStructure = activeContext.runtime().getContextCache().get(contextImportUriKey);
} else {
try {
final DocumentLoaderOptions loaderOptions = new DocumentLoaderOptions();
loaderOptions.setProfile(ProfileConstants.CONTEXT);
loaderOptions.setRequestProfile(Arrays.asList(loaderOptions.getProfile()));

try {
final Document importedDocument = activeContext.runtime().getDocumentLoader().loadDocument(contextImportUri, loaderOptions);

final Document importedDocument = activeContext.runtime().getDocumentLoader().loadDocument(contextImportUri, loaderOptions);
if (importedDocument == null) {
throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, "Imported context[" + contextImportUri + "] is null.");
}

if (importedDocument == null) {
throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, "Imported context[" + contextImportUri + "] is null.");
}
importedStructure = importedDocument
.getJsonContent()
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE));

importedStructure = importedDocument
.getJsonContent()
.orElseThrow(() -> new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE));
if (activeContext.runtime().getContextCache() != null) {
activeContext.runtime().getContextCache().put(contextImportUriKey, importedStructure.asJsonObject());
}

// 5.6.5
} catch (JsonLdError e) {
throw new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE, e);
} catch (JsonLdError e) {
// 5.6.5
throw new JsonLdError(JsonLdErrorCode.INVALID_KEYWORD_IMPORT_VALUE, e);
}
}

// 5.6.6
Expand Down

0 comments on commit e9b3ee1

Please sign in to comment.