From e0424a2d4c67cd4fbb379667a34ad022f669fcf2 Mon Sep 17 00:00:00 2001 From: Gaurav Vaidya Date: Tue, 19 Mar 2024 14:59:35 -0400 Subject: [PATCH] Added error when context cannot be looked up. --- .../jphyloref/helpers/JSONLDHelper.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/org/phyloref/jphyloref/helpers/JSONLDHelper.java b/src/main/java/org/phyloref/jphyloref/helpers/JSONLDHelper.java index 43d071c..a08ac9b 100644 --- a/src/main/java/org/phyloref/jphyloref/helpers/JSONLDHelper.java +++ b/src/main/java/org/phyloref/jphyloref/helpers/JSONLDHelper.java @@ -1,14 +1,20 @@ package org.phyloref.jphyloref.helpers; +import com.github.jsonldjava.core.DocumentLoader; +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.RemoteDocument; import org.eclipse.rdf4j.rio.RDFFormat; import org.eclipse.rdf4j.rio.RDFParser; import org.eclipse.rdf4j.rio.Rio; +import org.eclipse.rdf4j.rio.helpers.JSONLDSettings; import org.semanticweb.owlapi.formats.RDFJsonLDDocumentFormat; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration; import org.semanticweb.owlapi.rio.RioOWLRDFConsumerAdapter; import org.semanticweb.owlapi.util.AnonymousNodeChecker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * JSONLDHelper provides methods to help read and process JSON-LD files. @@ -16,6 +22,8 @@ * @author Gaurav Vaidya */ public class JSONLDHelper { + private static final Logger logger = LoggerFactory.getLogger(JSONLDHelper.class); + /** * Create an RDFParser for JSON-LD files. When the parser's parse() method is called, * its contents will be added to the OWLOntology passed to this method. @@ -60,6 +68,23 @@ public boolean isAnonymousNode(IRI iri) { RDFParser parser = Rio.createParser(RDFFormat.JSONLD); parser.setRDFHandler(rdfHandler); + // We get some indistinct errors if any of the context URLs in the JSON-LD file are + // incorrect or inaccessible. However, we can set up our own document loader so we + // can detect when this occurs. + parser.set( + JSONLDSettings.DOCUMENT_LOADER, + new DocumentLoader() { + @Override + public RemoteDocument loadDocument(String url) throws JsonLdError { + try { + return super.loadDocument(url); + } catch (JsonLdError err) { + logger.error("Error occurred while loading document " + url + ": " + err); + throw err; + } + } + }); + return parser; } }