Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use context cache for imported contexts #304

Merged
merged 14 commits into from
Jan 22, 2024
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
Loading