public final void storeOntology(OWLOntologyManager manager, OWLOntology ontology, IRI documentIRI, OWLOntologyFormat ontologyFormat) throws
OWLOntologyStorageException {
try {
if (!documentIRI.isAbsolute()) {
throw new OWLOntologyStorageException("Document IRI must be absolute: " + documentIRI);
}
File tempFile = File.createTempFile("owlapi", ".owl");
FileOutputStream tempOutputStream=null;
FileInputStream tempInputStream=null;
InputStreamReader inputStreamReader=null;
OutputStreamWriter outputStreamWriter=null;
BufferedReader br=null;
BufferedWriter w=null;
try {
tempOutputStream = new FileOutputStream(tempFile);
BufferedWriter tempWriter = new BufferedWriter(new OutputStreamWriter(tempOutputStream, "UTF-8"));
storeOntology(manager, ontology, tempWriter, ontologyFormat);
tempWriter.flush();
tempWriter.close();
// Now copy across
OutputStream os;
if (documentIRI.getScheme().equals("file")) {
File file = new File(documentIRI.toURI());
// Ensure that the necessary directories exist.
file.getParentFile().mkdirs();
os = new FileOutputStream(file);
} else {
URL url = documentIRI.toURI().toURL();
URLConnection conn = url.openConnection();
os = conn.getOutputStream();
}
tempInputStream = new FileInputStream(tempFile);
inputStreamReader = new InputStreamReader(tempInputStream, "UTF-8");
br = new BufferedReader(inputStreamReader);
outputStreamWriter = new OutputStreamWriter(os, "UTF-8");
w = new BufferedWriter(outputStreamWriter);
String line;
while((line = br.readLine()) != null) {
w.write(line);
w.write("\n");
}
}
finally {
tempFile.delete();
if (br != null){
br.close();
}
if (w != null){
w.close();
}
if (tempOutputStream != null){
tempOutputStream.close();
}
if (tempInputStream != null){
tempInputStream.close();
}
if (inputStreamReader != null){
inputStreamReader.close();
}
if (outputStreamWriter != null){
outputStreamWriter.close();
}
}
}
catch (IOException e) {
throw new OWLOntologyStorageException(e);
}
}