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.impl;
023    
024    import com.hp.hpl.jena.ontology.OntModel;
025    import com.hp.hpl.jena.ontology.OntModelSpec;
026    import com.hp.hpl.jena.rdf.model.ModelFactory;
027    import org.apache.log4j.Logger;
028    import org.nlp2rdf.ontology.OntologyLoader;
029    
030    import java.io.File;
031    import java.io.FileOutputStream;
032    import java.io.IOException;
033    import java.io.UnsupportedEncodingException;
034    import java.net.URL;
035    import java.net.URLEncoder;
036    import java.nio.channels.Channels;
037    import java.nio.channels.ReadableByteChannel;
038    
039    /**
040     * Loads and caches Ontologies in cacheDir
041     * Use loadImports to recursively load any models. They will be included as Jena submodels
042     *
043     * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
044     */
045    public class OntologyCache extends SimpleLoader implements OntologyLoader {
046        private static final Logger log = Logger.getLogger(OntologyCache.class);
047        final private String cacheDir;
048    
049    
050        public OntologyCache(String cacheDir) {
051            this.cacheDir = cacheDir;
052            File f = new File(cacheDir);
053            if (!f.exists()) {
054                f.mkdir();
055            }
056        }
057    
058    
059        @Override
060        public OntModel loadOntology(String ontologyUri) {
061            OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, ModelFactory.createDefaultModel());
062            try {
063                if (!isOntologyCached(ontologyUri)) {
064    
065                    cache(ontologyUri);
066                    log.info("ontology " + ontologyUri + " was cached. To refresh, delete folder: " + new File(cacheDir).getAbsolutePath());
067                }
068    
069                model.read(new File(makeFilenameFromOntologyUrl(ontologyUri)).toURI().toURL().toString());
070                log.info("ontology " + ontologyUri + " was loaded from cache. To refresh, delete folder: " + new File(cacheDir).getAbsolutePath());
071    
072            } catch (IOException e) {
073                log.warn("caching ontology failed, trying to load from url");
074                model.read(ontologyUri);
075            } catch (Exception e) {
076                log.error("Could not load cached ontology trying to download", e);
077            }
078    
079            return model;
080        }
081    
082        public void clearCache() {
083    
084            File[] files = listAllFiles();
085            for (File file : files) {
086                try {
087                    if (file.delete()) {
088                        log.info("Deleted file " + file.toString());
089                    }
090                } catch (Exception e) {
091                    log.warn("could not delete file");
092                }
093    
094            }
095        }
096    
097        private File[] listAllFiles() {
098            File f = new File(cacheDir);
099            return f.listFiles();
100        }
101    
102        /*
103        private void initMapping() {
104            File[] files = listAllFiles();
105            LocationMapper l = FileManager.get().getLocationMapper();
106            for (File f : files) {
107                try {
108                    l.altMapping(URLDecoder.decode(f.toString(), "UTF-8"), f.toURI().toURL().toString());
109                } catch (MalformedURLException e) {
110                    log.error("", e);
111                } catch (UnsupportedEncodingException e) {
112                    log.error("", e);
113                }
114            }
115        }
116         */
117    
118        public void download(String from, String to) throws IOException {
119    
120            try {
121                URL google = new URL(from);
122                ReadableByteChannel rbc = Channels.newChannel(google.openStream());
123                FileOutputStream fos = new FileOutputStream(to);
124                fos.getChannel().transferFrom(rbc, 0, 1 << 24);
125                log.debug("successfully downloaded " + from + " to " + to + " ");
126            } catch (IOException e) {
127                log.warn("caching the ontology failed " + from, e);
128            }
129    
130        }
131    
132        //encodes the ontologyUrls
133        private String makeFilenameFromOntologyUrl(String ontologyUrl) throws UnsupportedEncodingException {
134            return cacheDir + URLEncoder.encode(ontologyUrl, "UTF-8");
135        }
136    
137        private void cache(String ontologyUrl) throws IOException {
138            download(ontologyUrl, makeFilenameFromOntologyUrl(ontologyUrl));
139        }
140    
141    
142        public static void main(String[] args) throws IOException {
143            OntologyCache oc = new OntologyCache("/tmp/");
144            OntModel m = oc.loadOntology("http://nachhalt.sfb632.uni-potsdam.de/owl/stanford.owl");
145            oc.loadImports(m);
146        }
147    
148        public boolean isOntologyCached(String ontologyUri) {
149            try {
150                return new File(makeFilenameFromOntologyUrl(ontologyUri)).exists();
151            } catch (Exception e) {
152                log.error("", e);
153            }
154            return false;
155    
156        }
157    }