diff --git a/scripts/java/src/main/java/main/GuardingXmlCatalogIriMapper.java b/scripts/java/src/main/java/main/GuardingXmlCatalogIriMapper.java new file mode 100644 index 00000000..999abb42 --- /dev/null +++ b/scripts/java/src/main/java/main/GuardingXmlCatalogIriMapper.java @@ -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); + } +} diff --git a/scripts/java/src/main/java/main/OntologyManager.java b/scripts/java/src/main/java/main/OntologyManager.java index dd4577d4..efefa6f7 100644 --- a/scripts/java/src/main/java/main/OntologyManager.java +++ b/scripts/java/src/main/java/main/OntologyManager.java @@ -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); }