OWLFileUtils.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.owl.utils;
00002 
00003 import java.io.*;
00004 import org.semanticweb.owlapi.apibinding.OWLManager;
00005 import org.semanticweb.owlapi.model.*;
00006 import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
00007 
00008 
00019 public class OWLFileUtils {
00020 
00028         public static OWLOntology loadOntologyFromFile(String filename) throws OWLOntologyCreationException {
00029                 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00030                 return manager.loadOntologyFromOntologyDocument(new File(filename));
00031         }
00032         
00033         
00042         public static boolean saveOntologyToStream(OWLOntology ontology, OutputStream stream, OWLOntologyFormat format) {
00043 
00044                 boolean ok = false;
00045 
00046                 if (stream != null) {
00047 
00048                         try {
00049 
00050                                 // Get hold of the ontology manager
00051                                 OWLOntologyManager manager = ontology.getOWLOntologyManager();
00052 
00053                                 // By default ontologies are saved in the format from which they were loaded.
00054                                 // We can get information about the format of an ontology from its manager
00055                                 OWLOntologyFormat currFormat = manager.getOntologyFormat(ontology);
00056 
00057                                 if (currFormat.equals(format)) {
00058 
00059                                         // Save a copy of the ontology to the given stream.
00060                                         manager.saveOntology(ontology, stream);
00061 
00062                                 } else {
00063 
00064                                         // Some ontology formats support prefix names and prefix IRIs. When we save the ontology in
00065                                         // the new format we will copy the prefixes over so that we have nicely abbreviated IRIs in
00066                                         // the new ontology document
00067                                         if (format.isPrefixOWLOntologyFormat() && currFormat.isPrefixOWLOntologyFormat()) {
00068                                                 ((PrefixOWLOntologyFormat)format).copyPrefixesFrom(currFormat.asPrefixOWLOntologyFormat());
00069                                         }
00070                                         manager.saveOntology(ontology, format, stream);
00071 
00072                                 }
00073 
00074                                 ok = true;
00075 
00076                         } catch (Exception e) {
00077                                 System.out.println("Could not save ontology: " + e.getMessage());
00078                         }
00079 
00080                 }
00081 
00082                 return ok;
00083         }
00084 
00085 
00092         public static String saveOntologytoString(OWLOntology ontology, OWLOntologyFormat format) {
00093 
00094                 String s = null;
00095                 ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
00096 
00097                 if (saveOntologyToStream(ontology, os, format)) {
00098                         try {
00099                                 s = new String(os.toByteArray(), "UTF-8");
00100                         } catch (UnsupportedEncodingException e) {
00101                                 System.out.println("UTF-8 encoding is unsupported: " + e.getMessage());
00102                         }                       
00103                 }
00104 
00105                 return s;
00106 
00107         }
00108 
00109 
00110 
00118         public static boolean saveOntologyToFile(OWLOntology ontology, OWLOntologyFormat format, String file) {
00119 
00120                 boolean ok = false;
00121 
00122                 try {
00123                         ok = saveOntologyToFile(ontology, file, format);                        
00124                 } catch (NullPointerException e) {
00125                         System.out.println("Could not save ontology: null pointer argument found\n" + e.getMessage());
00126                 }
00127 
00128                 return ok; 
00129 
00130         }
00131 
00132 
00141         public static boolean saveOntologyToFile(OWLOntology ontology, String file, OWLOntologyFormat format) {
00142 
00143                 boolean ok = false;
00144 
00145                 try {
00146 
00147                         File f = new File(file);
00148                         OWLOntologyManager manager = ontology.getOWLOntologyManager();
00149                         manager.saveOntology(ontology, format, IRI.create(f.toURI()));
00150 
00151                         ok = true;
00152 
00153                 } catch (Exception e) {
00154                         System.out.println("Could not save ontology: " + e.getMessage());
00155                 }
00156 
00157                 return ok;
00158         }
00159 
00160 
00161 }


knowrob_common
Author(s): Moritz Tenorth
autogenerated on Sat Dec 28 2013 17:09:28