001    /**
002     * Copyright (C) 2007-2011, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     *
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    
020    package org.dllearner.reasoning;
021    
022    import java.io.ByteArrayOutputStream;
023    import java.net.URI;
024    import java.net.URISyntaxException;
025    import java.net.URL;
026    
027    import javax.xml.transform.OutputKeys;
028    import javax.xml.transform.Transformer;
029    import javax.xml.transform.TransformerConfigurationException;
030    import javax.xml.transform.TransformerException;
031    import javax.xml.transform.TransformerFactory;
032    import javax.xml.transform.TransformerFactoryConfigurationError;
033    import javax.xml.transform.dom.DOMSource;
034    import javax.xml.transform.stream.StreamResult;
035    
036    import org.dllearner.core.OntologyFormat;
037    import org.semanticweb.owlapi.apibinding.OWLManager;
038    import org.semanticweb.owlapi.model.IRI;
039    import org.semanticweb.owlapi.model.OWLOntologyCreationException;
040    import org.semanticweb.owlapi.model.OWLOntologyManager;
041    import org.w3c.dom.Document;
042    
043    
044    public class OWLAPIDIGConverter {
045    
046            public static String getTellsString(URL file, OntologyFormat format, URI kbURI) {
047                    
048                    String ret = "";
049                    try {
050    
051                            // Load an ontology from a physical URI
052                            OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
053                            // the next function could return an ontology
054                            manager.loadOntologyFromOntologyDocument(IRI.create(file));
055    //TODO: OWLAPI3 conversion
056    //                      DIGTranslatorImpl dig = new DIGTranslatorImpl(manager);
057    //                      Document doc = dig.createTellsDocument(kbURI.toString());
058    //                      dig.translateToDIG(manager.getOntologies(), doc, doc.getDocumentElement());
059    //                      ret = xml2string(doc);
060    
061                    }catch (OWLOntologyCreationException e) {
062                            e.printStackTrace();
063                    } catch (URISyntaxException e) {
064                            e.printStackTrace();
065                    }
066    
067                    return ret;
068            }
069    
070            public static String xml2string(Document d) {
071                    // XML als String erzeugen (ziemlich umst�ndlich)
072                    String tellString = "";
073                    try {
074                            // transformer erzeugen mit identity transformation
075                            Transformer transformer = TransformerFactory.newInstance().newTransformer();
076                            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
077                            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
078                            // Quelle ist das DOM-Objekt
079                            DOMSource source = new DOMSource(d);
080                            // Resultat ist ein OutputStream, leider kann man das Resultat nicht
081                            // direkt als String bekommen
082                            ByteArrayOutputStream os = new ByteArrayOutputStream();
083                            StreamResult result = new StreamResult(os);
084                            // Transformation ausf�hren
085                            transformer.transform(source, result);
086                            // String aus OutputStream generieren
087                            tellString = os.toString();
088    
089                    } catch (TransformerConfigurationException e) {
090                            e.printStackTrace();
091                    } catch (TransformerFactoryConfigurationError e) {
092                            e.printStackTrace();
093                    } catch (TransformerException e) {
094                            e.printStackTrace();
095                    }
096                    return tellString;
097            }
098    }