Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 package roboearth.wp5.owl;
00050
00051 import java.io.ByteArrayInputStream;
00052 import java.io.ByteArrayOutputStream;
00053 import java.io.File;
00054 import java.io.FileNotFoundException;
00055 import java.io.IOException;
00056 import java.io.InputStream;
00057 import java.io.OutputStream;
00058 import java.io.UnsupportedEncodingException;
00059 import java.net.UnknownHostException;
00060 import java.util.Map;
00061
00062 import org.semanticweb.owlapi.apibinding.OWLManager;
00063 import org.semanticweb.owlapi.io.OWLOntologyCreationIOException;
00064 import org.semanticweb.owlapi.io.OWLParser;
00065 import org.semanticweb.owlapi.io.OWLParserException;
00066 import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
00067 import org.semanticweb.owlapi.io.UnparsableOntologyException;
00068 import org.semanticweb.owlapi.model.IRI;
00069 import org.semanticweb.owlapi.model.OWLOntology;
00070 import org.semanticweb.owlapi.model.OWLOntologyCreationException;
00071 import org.semanticweb.owlapi.model.OWLOntologyFormat;
00072 import org.semanticweb.owlapi.model.OWLOntologyManager;
00073 import org.semanticweb.owlapi.model.UnloadableImportException;
00074 import org.semanticweb.owlapi.vocab.PrefixOWLOntologyFormat;
00075
00084 public class OWLIO {
00085
00086
00090 public static final OWLOntologyFormat ONTOLOGY_FORMAT_RDFXML = new RDFXMLOntologyFormat();
00091
00092
00098 public static OWLOntology loadOntologyFromWeb(String url) {
00099
00100 OWLOntology ontology = null;
00101
00102 try {
00103
00104 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00105
00106
00107 IRI iri = IRI.create(url);
00108 ontology = manager.loadOntologyFromOntologyDocument(iri);
00109 } catch (OWLOntologyCreationIOException e) {
00110
00111 IOException ioException = e.getCause();
00112 if(ioException instanceof UnknownHostException) {
00113 System.out.println("Could not load ontology. Unknown host: " + ioException.getMessage());
00114 }
00115 else {
00116 System.out.println("Could not load ontology: " + ioException.getClass().getSimpleName() + " " + ioException.getMessage());
00117 }
00118 } catch (UnparsableOntologyException e) {
00119
00120
00121 System.out.println("Could not parse the ontology: " + e.getMessage());
00122
00123 Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
00124
00125 for(OWLParser parser : exceptions.keySet()) {
00126 System.out.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
00127 System.out.println("Failed because: " + exceptions.get(parser).getMessage());
00128 }
00129 } catch (UnloadableImportException e) {
00130
00131
00132 System.out.println("Could not load import: " + e.getImportsDeclaration());
00133
00134 OWLOntologyCreationException cause = e.getOntologyCreationException();
00135 System.out.println("Reason: " + cause.getMessage());
00136 } catch (OWLOntologyCreationException e) {
00137 System.out.println("Could not load ontology: " + e.getMessage());
00138 } catch (Exception e) {
00139 System.out.println("Could not load ontology due to unknown error: " + e.getMessage());
00140 }
00141
00142 return ontology;
00143
00144 }
00145
00146
00152 public static OWLOntology loadOntologyFromFile(String file) {
00153
00154 OWLOntology ontology = null;
00155
00156 try {
00157
00158 File f = new File(file);
00159
00160
00161 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00162
00163
00164 ontology = manager.loadOntologyFromOntologyDocument(f);
00165
00166 } catch (OWLOntologyCreationIOException e) {
00167
00168 IOException ioException = e.getCause();
00169 if(ioException instanceof FileNotFoundException) {
00170 System.err.println("Could not load ontology. File not found: " + ioException.getMessage());
00171 }
00172 else {
00173 System.err.println("Could not load ontology: " + ioException.getClass().getSimpleName() + " " + ioException.getMessage());
00174 }
00175 } catch (UnparsableOntologyException e) {
00176
00177
00178 System.err.println("Could not parse the ontology: " + e.getMessage());
00179
00180 Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
00181
00182 for(OWLParser parser : exceptions.keySet()) {
00183 System.err.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
00184 System.err.println("Failed because: " + exceptions.get(parser).getMessage());
00185 }
00186 } catch (UnloadableImportException e) {
00187
00188
00189 System.err.println("Could not load import: " + e.getImportsDeclaration());
00190
00191 OWLOntologyCreationException cause = e.getOntologyCreationException();
00192 System.err.println("Reason: " + cause.getMessage());
00193 } catch (OWLOntologyCreationException e) {
00194 System.err.println("Could not load ontology: " + e.getMessage());
00195 } catch (Exception e) {
00196 System.err.println("Could not load ontology due to unknown error: " + e.getMessage());
00197 }
00198
00199 return ontology;
00200
00201 }
00202
00203
00209 public static OWLOntology loadOntologyFromStream(InputStream stream) {
00210
00211 OWLOntology ontology = null;
00212
00213 try {
00214
00215
00216 OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
00217
00218
00219 ontology = manager.loadOntologyFromOntologyDocument(stream);
00220
00221 } catch (OWLOntologyCreationIOException e) {
00222
00223 IOException ioException = e.getCause();
00224 if(ioException instanceof FileNotFoundException) {
00225 System.out.println("Could not load ontology. File not found: " + ioException.getMessage());
00226 }
00227 else {
00228 System.out.println("Could not load ontology: " + ioException.getClass().getSimpleName() + " " + ioException.getMessage());
00229 }
00230 } catch (UnparsableOntologyException e) {
00231
00232
00233 System.out.println("Could not parse the ontology: " + e.getMessage());
00234
00235 Map<OWLParser, OWLParserException> exceptions = e.getExceptions();
00236
00237 for(OWLParser parser : exceptions.keySet()) {
00238 System.out.println("Tried to parse the ontology with the " + parser.getClass().getSimpleName() + " parser");
00239 System.out.println("Failed because: " + exceptions.get(parser).getMessage());
00240 }
00241 } catch (UnloadableImportException e) {
00242
00243
00244 System.out.println("Could not load import: " + e.getImportsDeclaration());
00245
00246 OWLOntologyCreationException cause = e.getOntologyCreationException();
00247 System.out.println("Reason: " + cause.getMessage());
00248 } catch (OWLOntologyCreationException e) {
00249 System.out.println("Could not load ontology: " + e.getMessage());
00250 } catch (Exception e) {
00251 System.out.println("Could not load ontology due to unknown error: " + e.getMessage());
00252 }
00253
00254 return ontology;
00255
00256 }
00257
00258
00264 public static OWLOntology loadOntologyFromString(String s) {
00265
00266 InputStream inStream = new ByteArrayInputStream(s.getBytes());
00267 OWLOntology ontology = OWLIO.loadOntologyFromStream(inStream);
00268
00269 return ontology;
00270
00271 }
00272
00273
00281 public static boolean saveOntologyToFile(OWLOntology ontology, String file) {
00282
00283 boolean ok = false;
00284
00285 try {
00286 OWLOntologyFormat format = ontology.getOWLOntologyManager().getOntologyFormat(ontology);
00287 ok = saveOntologyToFile(ontology, file, format);
00288 } catch (NullPointerException e) {
00289 System.out.println("Could not save ontology: null pointer argument found\n" + e.getMessage());
00290 }
00291
00292 return ok;
00293
00294 }
00295
00296
00305 public static boolean saveOntologyToFile(OWLOntology ontology, String file, OWLOntologyFormat format) {
00306
00307 boolean ok = false;
00308
00309 try {
00310
00311
00312 File f = new File(file);
00313
00314
00315 OWLOntologyManager manager = ontology.getOWLOntologyManager();
00316
00317
00318
00319 OWLOntologyFormat currFormat = manager.getOntologyFormat(ontology);
00320
00321
00322 IRI documentIRI = IRI.create(f.toURI());
00323
00324 if (currFormat.equals(format)) {
00325
00326
00327 manager.saveOntology(ontology, documentIRI);
00328
00329 } else {
00330
00331
00332
00333
00334 if (format.isPrefixOWLOntologyFormat() && currFormat.isPrefixOWLOntologyFormat()) {
00335 ((PrefixOWLOntologyFormat)format).copyPrefixesFrom(currFormat.asPrefixOWLOntologyFormat());
00336 }
00337 manager.saveOntology(ontology, format, documentIRI);
00338
00339 }
00340
00341 ok = true;
00342
00343 } catch (Exception e) {
00344 System.out.println("Could not save ontology: " + e.getMessage());
00345 }
00346
00347 return ok;
00348 }
00349
00350
00358 public static boolean saveOntologyToStream(OWLOntology ontology, OutputStream stream) {
00359
00360 boolean ok = false;
00361
00362 try {
00363 OWLOntologyFormat format = ontology.getOWLOntologyManager().getOntologyFormat(ontology);
00364 ok = saveOntologyToStream(ontology, stream, format);
00365 } catch (NullPointerException e) {
00366 System.out.println("Could not save ontology: null pointer argument found\n" + e.getMessage());
00367 }
00368
00369 return ok;
00370
00371 }
00372
00373
00382 public static boolean saveOntologyToStream(OWLOntology ontology, OutputStream stream, OWLOntologyFormat format) {
00383
00384 boolean ok = false;
00385
00386 if (stream != null) {
00387
00388 try {
00389
00390
00391 OWLOntologyManager manager = ontology.getOWLOntologyManager();
00392
00393
00394
00395 OWLOntologyFormat currFormat = manager.getOntologyFormat(ontology);
00396
00397 if (currFormat.equals(format)) {
00398
00399
00400 manager.saveOntology(ontology, stream);
00401
00402 } else {
00403
00404
00405
00406
00407 if (format.isPrefixOWLOntologyFormat() && currFormat.isPrefixOWLOntologyFormat()) {
00408 ((PrefixOWLOntologyFormat)format).copyPrefixesFrom(currFormat.asPrefixOWLOntologyFormat());
00409 }
00410 manager.saveOntology(ontology, format, stream);
00411
00412 }
00413
00414 ok = true;
00415
00416 } catch (Exception e) {
00417 System.out.println("Could not save ontology: " + e.getMessage());
00418 }
00419
00420 }
00421
00422 return ok;
00423 }
00424
00425
00431 public static String saveOntologyToString(OWLOntology ontology) {
00432
00433 return saveOntologyToString(ontology, ontology.getOWLOntologyManager().getOntologyFormat(ontology));
00434
00435 }
00436
00437
00444 public static String saveOntologyToString(OWLOntology ontology, OWLOntologyFormat format) {
00445
00446 String s = null;
00447 ByteArrayOutputStream os = new ByteArrayOutputStream(4096);
00448
00449 if (saveOntologyToStream(ontology, os, format)) {
00450 try {
00451 s = new String(os.toByteArray(), "UTF-8");
00452 } catch (UnsupportedEncodingException e) {
00453 System.out.println("UTF-8 encoding is unsupported: " + e.getMessage());
00454 }
00455 }
00456
00457 return s;
00458
00459 }
00460
00461
00462 }