001 /***************************************************************************/
002 /* Copyright (C) 2010-2011, Sebastian Hellmann */
003 /* Note: If you need parts of NLP2RDF in another licence due to licence */
004 /* incompatibility, please mail hellmann@informatik.uni-leipzig.de */
005 /* */
006 /* This file is part of NLP2RDF. */
007 /* */
008 /* NLP2RDF is free software; you can redistribute it and/or modify */
009 /* it under the terms of the GNU General Public License as published by */
010 /* the Free Software Foundation; either version 3 of the License, or */
011 /* (at your option) any later version. */
012 /* */
013 /* NLP2RDF is distributed in the hope that it will be useful, */
014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
016 /* GNU General Public License for more details. */
017 /* */
018 /* You should have received a copy of the GNU General Public License */
019 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
020 /***************************************************************************/
021
022 package org.nlp2rdf.ontology;
023
024 import com.hp.hpl.jena.ontology.OntModel;
025 import com.hp.hpl.jena.ontology.OntModelSpec;
026 import com.hp.hpl.jena.ontology.impl.OntModelImpl;
027 import com.hp.hpl.jena.rdf.model.Model;
028 import com.hp.hpl.jena.rdf.model.ModelFactory;
029 import org.slf4j.Logger;
030 import org.slf4j.LoggerFactory;
031
032 import java.io.IOException;
033 import java.io.InputStream;
034 import java.net.MalformedURLException;
035 import java.net.URL;
036 import java.util.HashSet;
037 import java.util.Set;
038
039 /**
040 * @author Sebastian Hellmann - http://bis.informatik.uni-leipzig.de/SebastianHellmann
041 * Created: 29.06.11
042 * <p/>
043 * //TODO there is some domain specific replacements in this class
044 * private String toClasspath(String ontologyUrl) throws MalformedURLException{
045 * ontologyUrl = ontologyUrl.replace("http://nlp2rdf.lod2.eu/schema/sso/", "http://nlp2rdf.lod2.eu/schema/sso/sso.owl");
046 * ontologyUrl = ontologyUrl.replace("http://nlp2rdf.lod2.eu/schema/string/", "http://nlp2rdf.lod2.eu/schema/string/string.owl");
047 */
048 public class ClasspathLoader implements OntologyLoader {
049 private static Logger log = LoggerFactory.getLogger(ClasspathLoader.class);
050
051 public static void main(String[] args) throws Exception {
052 String test = "http://nlp2rdf.lod2.eu/schema/sso/";
053 System.out.println(new ClasspathLoader().loadOntology(test));
054 }
055
056 public class CC extends OntModelImpl {
057 public CC(OntModelSpec spec, Model model) {
058 super(spec, model);
059 }
060 }
061
062 @Override
063 public OntModel loadOntology(String ontologyUri) {
064 return loadOntology(ontologyUri, OntModelSpec.OWL_DL_MEM);
065 }
066
067 @Override
068 public OntModel loadOntology(String ontologyUri, OntModelSpec spec) {
069 OntModel model = ModelFactory.createOntologyModel(spec);
070 model.getDocumentManager().setProcessImports(false);
071 try {
072 InputStream is = this.getClass().getClassLoader().getResourceAsStream(toClasspath(ontologyUri));
073 model.read(is, "");
074 log.info("ontology " + ontologyUri + " was loaded from classpath. ");
075 } catch (MalformedURLException e) {
076 log.error("" + ontologyUri, e);
077 } catch (IOException e) {
078 log.warn("ontology was not in classpath, loading from url" + ontologyUri, e);
079 log.error("exit for development");
080 System.exit(0);
081 model.read(ontologyUri);
082 }
083 return model;
084 }
085
086
087 @Override
088 public void loadImports(OntModel model) {
089 loadImports(model, new HashSet<String>());
090 }
091
092
093 private void loadImports(OntModel m, Set<String> loaded) {
094 for (String one : m.listImportedOntologyURIs()) {
095 log.debug("adding one more: " + one);
096 if (loaded.add(one) == false) {
097 log.debug("skipping " + one + " (already loaded)");
098 continue;
099 }
100 OntModel sm = loadOntology(one);
101 loadImports(sm);
102 m.addSubModel(sm);
103 }
104 }
105
106
107 public String toClasspath(String ontologyUrl) throws MalformedURLException {
108
109 ontologyUrl = ontologyUrl.replace("http://nlp2rdf.lod2.eu/schema/sso/", "http://nlp2rdf.lod2.eu/schema/sso/sso.owl");
110 ontologyUrl = ontologyUrl.replace("http://nlp2rdf.lod2.eu/schema/string/", "http://nlp2rdf.lod2.eu/schema/string/string.owl");
111
112 StringBuilder sb = new StringBuilder();
113
114 URL u = new URL(ontologyUrl);
115
116 //reverse host
117 String host = u.getHost();
118 for (String s : host.split("\\.")) {
119 sb.insert(0, s + "/");
120 }
121
122 //sb.insert(0, "classpath:");
123
124 //get path
125 sb.append(u.getPath());
126
127 return sb.toString().replace("//", "/");
128
129 }
130
131 }