001 // This file is part of AceWiki. 002 // Copyright 2008-2012, AceWiki developers. 003 // 004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU 005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of 006 // the License, or (at your option) any later version. 007 // 008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 010 // Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If 013 // not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.acewiki.core; 016 017 import java.io.BufferedWriter; 018 import java.io.IOException; 019 import java.io.OutputStream; 020 import java.io.OutputStreamWriter; 021 import java.io.Writer; 022 import java.util.List; 023 024 /** 025 * This abstract class is used to export AceWiki ontologies in different formats. 026 * 027 * @author Tobias Kuhn 028 */ 029 public abstract class OntologyExporter { 030 031 private Ontology ontology; 032 private OutputStream outputStream; 033 private Writer writer; 034 035 /** 036 * Creates a new exporter. 037 */ 038 protected OntologyExporter() { 039 } 040 041 void init(Ontology ontology) { 042 this.ontology = ontology; 043 } 044 045 /** 046 * Writes the export content into the given output stream. The stream is closed at the end. 047 * 048 * @param outputStream The output stream. 049 */ 050 public void export(OutputStream outputStream) { 051 this.outputStream = outputStream; 052 try { 053 writeContent(); 054 if (writer != null) writer.close(); 055 outputStream.close(); 056 this.writer = null; 057 this.outputStream = null; 058 } catch (IOException ex) { 059 ex.printStackTrace(); 060 } 061 } 062 063 /** 064 * Returns whether this ontology exporter is applicable in the current context. 065 * 066 * @return true if this ontology exporter is applicable. 067 */ 068 public abstract boolean isApplicable(); 069 070 /** 071 * Returns the name of this exporter as shown to the user. 072 * 073 * @return The name of this exporter. 074 */ 075 public abstract String getName(); 076 077 /** 078 * This internal method should write the export content. 079 * 080 * @throws IOException when an IO problem occurs. 081 */ 082 protected abstract void writeContent() throws IOException; 083 084 /** 085 * Returns the file suffix for the given export type. 086 * 087 * @return The file suffix. 088 */ 089 public abstract String getFileSuffix(); 090 091 /** 092 * Returns the content type for the given export type. 093 * 094 * @return The content type. 095 */ 096 public abstract String getContentType(); 097 098 /** 099 * Returns the AceWiki ontology for this exporter. 100 * 101 * @return The AceWiki ontology. 102 */ 103 protected Ontology getOntology() { 104 return ontology; 105 } 106 107 /** 108 * Returns the list of all ontology elements. 109 * 110 * @return The ontology elements. 111 */ 112 protected List<OntologyElement> getOntologyElements() { 113 return ontology.getOntologyElements(); 114 } 115 116 /** 117 * Returns the current output stream. 118 * 119 * @return The current output stream. 120 */ 121 protected OutputStream getOutputStream() { 122 return outputStream; 123 } 124 125 /** 126 * Writes the given string into the current output stream. 127 * 128 * @param str The string to be written. 129 * @throws IOException when an IO problem occurs. 130 */ 131 protected void write(String str) throws IOException { 132 if (writer == null) { 133 writer = new BufferedWriter(new OutputStreamWriter(outputStream)); 134 } 135 writer.write(str); 136 } 137 138 }