Skip to content

Commit

Permalink
Throwing now an explicit exception when trying to load an online onto…
Browse files Browse the repository at this point in the history
…logy
  • Loading branch information
mrnolte committed May 14, 2024
1 parent 02b850b commit 04a448b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
38 changes: 38 additions & 0 deletions scripts/java/src/main/java/main/GuardingXmlCatalogIriMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main;

import org.protege.xmlcatalog.CatalogUtilities;
import org.protege.xmlcatalog.XMLCatalog;
import org.protege.xmlcatalog.entry.Entry;
import org.protege.xmlcatalog.redirect.UriRedirectVisitor;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyIRIMapper;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Iterator;

public class GuardingXmlCatalogIriMapper implements OWLOntologyIRIMapper {
private XMLCatalog catalog;

public GuardingXmlCatalogIriMapper(File f) throws MalformedURLException, IOException {
this(CatalogUtilities.parseDocument(f.toURI().toURL()));
}

public GuardingXmlCatalogIriMapper(XMLCatalog catalog) {
this.catalog = catalog;
}

public IRI getDocumentIRI(IRI original) {
UriRedirectVisitor visitor = new UriRedirectVisitor(original.toURI());

for (Entry subEntry : catalog.getEntries()) {
subEntry.accept(visitor);
if (visitor.getRedirect() != null) {
return IRI.create(visitor.getRedirect());
}
}
throw new RuntimeException("You are trying to load an ontology from an online source, but we decided to self-host them, e.g, DUL. The ontology you are trying to load is: " + original);
}
}
6 changes: 5 additions & 1 deletion scripts/java/src/main/java/main/OntologyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ public OntologyManager(final OntologyConfig config) throws OWLOntologyCreationEx

@SuppressWarnings("OverlyBroadThrowsClause")
private void loadCatalog(final Path ontologyDirectory) throws IOException {
final OWLOntologyIRIMapper xmlCatalogIriMapper = new XMLCatalogIRIMapper(
// use this if you want to prohibit ontologies being loaded from online
final OWLOntologyIRIMapper xmlCatalogIriMapper = new GuardingXmlCatalogIriMapper(
ontologyDirectory.resolve(XML_CATALOG_PATH).toFile());
// use this if you want to allow to load ontologies from online
// final OWLOntologyIRIMapper xmlCatalogIriMapper = new XMLCatalogIRIMapper(
// ontologyDirectory.resolve(XML_CATALOG_PATH).toFile());
ontologyManager.getIRIMappers().add(xmlCatalogIriMapper);
}

Expand Down

0 comments on commit 04a448b

Please sign in to comment.