Skip to content

Commit

Permalink
Added error when context cannot be looked up.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav committed Mar 19, 2024
1 parent 3d04c30 commit e0424a2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/phyloref/jphyloref/helpers/JSONLDHelper.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
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.
*
* @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 <code>parse()</code> method is called,
* its contents will be added to the OWLOntology passed to this method.
Expand Down Expand Up @@ -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;
}
}

0 comments on commit e0424a2

Please sign in to comment.