2121import fair .Constants ;
2222import fair .Utils ;
2323import org .jsoup .nodes .Document ;
24+ import org .semanticweb .owlapi .apibinding .OWLManager ;
25+ import org .semanticweb .owlapi .io .StreamDocumentSource ;
2426import org .semanticweb .owlapi .model .*;
2527import org .semanticweb .owlapi .search .EntitySearcher ;
2628import org .slf4j .Logger ;
2729import org .slf4j .LoggerFactory ;
2830import server .FileTooLargeException ;
2931
32+ import java .io .File ;
33+ import java .io .FileInputStream ;
3034import java .nio .file .Path ;
3135import java .util .ArrayList ;
3236import java .util .List ;
@@ -65,28 +69,29 @@ public class Ontology {
6569 private final ArrayList <String > supportedMetadata ;
6670 private final ArrayList <String > reusedMetadataVocabularies ;
6771 private final ArrayList <String > reusedVocabularies ;
68- List <OWLImportsDeclaration > importedVocabularies ;
72+ List <IRI > importedVocabularies ;
6973 private final ArrayList <String > termsWithLabel ;
7074 private final ArrayList <String > terms ; // all terms
7175 private final ArrayList <String > termsWithDescription ;
7276 private boolean isSKOS = false ;
7377
7478 /**
75- *
79+ * Ontology class constructor
7680 * @param o ontology path or URI
7781 * @param isFromFile flag to indicate whether the ontology should be loaded from file or URL
7882 * @param tmpFolder temporary folder where to store the downloaded ontology
7983 */
8084 public Ontology (String o , boolean isFromFile , Path tmpFolder ) throws FileTooLargeException {
8185 supportedMetadata = new ArrayList <>();
86+ importedVocabularies = new ArrayList <>();
8287 reusedMetadataVocabularies = new ArrayList <>();
8388 reusedVocabularies = new ArrayList <>();
8489 termsWithLabel = new ArrayList <>();
8590 termsWithDescription = new ArrayList <>();
8691 terms = new ArrayList <>();
8792 //Download ontology (any serialization)
8893 try {
89- this .ontologyModel = Utils . loadModelToDocument (o , isFromFile , tmpFolder .toString ());
94+ this .loadModelToDocument (o , isFromFile , tmpFolder .toString ());
9095 if (this .ontologyModel != null && this .ontologyModel .getOntologyID ().getOntologyIRI ().isPresent ()) {
9196 this .ontologyURI = this .ontologyModel .getOntologyID ().getOntologyIRI ().get ().toString ();
9297 }
@@ -456,8 +461,10 @@ private void completeMetadata(OWLAnnotation a) {
456461 private void getOntologyCoverage (){
457462 //metadata vocabularies
458463 this .ontologyModel .annotations ().forEach (this ::checkNamespaces );
459- //imports
460- this .importedVocabularies = this .ontologyModel .importsDeclarations ().collect (Collectors .toList ());
464+ // imports -> they are loaded when the ontology is loaded.
465+ // due to slow imports, we have disabled loading of imports
466+ //this.importedVocabularies = this.ontologyModel.importsDeclarations().collect(Collectors.toList());
467+
461468 //vocabulary reuse
462469 logger .info ("Extracting namespaces, labels, descriptions" );
463470 if (isSKOS ) {
@@ -643,7 +650,7 @@ public ArrayList<String> getReusedMetadataVocabularies() {
643650 return reusedMetadataVocabularies ;
644651 }
645652
646- public List <OWLImportsDeclaration > getImportedVocabularies () {
653+ public List <IRI > getImportedVocabularies () {
647654 return importedVocabularies ;
648655 }
649656
@@ -727,4 +734,71 @@ public String getNamespaceUri(){
727734 if (namespaceUri .isEmpty ()) return "unknown" ;
728735 else return namespaceUri ;
729736 }
737+
738+ /**
739+ * Method that will download the ontology to process with FOOPS!
740+ * Note: lazy loading of imports is implemented (making them fail).
741+ *
742+ * @param isFromFile boolean to indicate whether the ontology is from file or from URI.
743+ */
744+ private void loadModelToDocument (String pathOrURI ,boolean isFromFile , String downloadFolder ) throws Exception {
745+ String ontologyPath = pathOrURI ;
746+ if (!isFromFile ) {
747+ ontologyPath = downloadFolder + File .separator + "ontology" ;
748+ Utils .downloadOntology (pathOrURI , ontologyPath );
749+ }
750+ logger .info ("Loading ontology " );
751+ File ontologyFile = new File (ontologyPath );
752+ // if ontology is bigger than 50 MB, we do not process it
753+ if (ontologyFile .length () > Constants .MAX_ONTOLOGY_SIZE ) {
754+ throw new FileTooLargeException ("File is larger than maximum allowed (50 MB): " );
755+ }
756+ OWLOntologyManager manager = OWLManager .createOWLOntologyManager ();
757+
758+ // --- BLOCK TO PREVENT IMPORTS IN OWLAPI 5.5.0 ---
759+ manager .getIRIMappers ().add (iri -> {
760+ if (!iri .equals (IRI .create (pathOrURI ))) {
761+ logger .warn ("Blocking import: " + iri );
762+ //record the imported vocabulary for later purposes
763+ this .importedVocabularies .add (iri );
764+ return IRI .create ("file:///dev/null" );
765+ // we make the import fail along with MissingImportHandlingStrategy.SILENT
766+ // and follow redirects set to false
767+ }
768+ ////return IRI.create("urn:dummy:" + iri);
769+ return null ;
770+ });
771+
772+ // -----------------------------------------------------
773+
774+ //this is for debugging purposes, to check that the imports are being blocked.
775+ // but add helped information about the loading process
776+ manager .addOntologyLoaderListener (new OWLOntologyLoaderListener () {
777+ @ Override
778+ public void startedLoadingOntology (OWLOntologyLoaderListener .LoadingStartedEvent event ) {
779+ if (!event .getDocumentIRI ().equals (IRI .create (pathOrURI ))) {
780+ logger .debug ("Skipping import: " + event .getDocumentIRI ());
781+ } else {
782+ logger .debug ("Loading main ontology: " + event .getDocumentIRI ());
783+ }
784+ }
785+
786+ @ Override
787+ public void finishedLoadingOntology (OWLOntologyLoaderListener .LoadingFinishedEvent event ) {
788+ logger .info ("Finished loading: " + event .getDocumentIRI ());
789+ }
790+ });
791+
792+
793+ OWLOntologyLoaderConfiguration loadingConfig = new OWLOntologyLoaderConfiguration ();
794+ loadingConfig = loadingConfig .setMissingImportHandlingStrategy (MissingImportHandlingStrategy .SILENT );
795+ loadingConfig = loadingConfig .setFollowRedirects (false );
796+
797+ logger .info ("Parsing type: " +loadingConfig .isStrict ());
798+
799+ this .ontologyModel = manager .loadOntologyFromOntologyDocument (
800+ new StreamDocumentSource (new FileInputStream (ontologyFile ), IRI .create (pathOrURI )),
801+ loadingConfig
802+ );
803+ }
730804}
0 commit comments