001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: ImportManager.java,v 1.7 2008/01/02 12:05:51 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.graph.Graph;
012: import com.hp.hpl.jena.graph.compose.MultiUnion;
013: import com.hp.hpl.jena.rdf.model.*;
014: import com.hp.hpl.jena.util.FileManager;
015: import com.hp.hpl.jena.vocabulary.*;
016:
017: public class ImportManager {
018: public ImportManager() {
019: }
020:
021: /**
022: A shared instance of ImportManage, used as a default by several other
023: assembler methods.
024: */
025: public final static ImportManager instance = new ImportManager();
026:
027: /**
028: The cache of models already read by this manager.
029: */
030: protected Map cache = new HashMap();
031:
032: /**
033: Clear this ImportManager's cache.
034: */
035: public void clear() {
036: cache.clear();
037: }
038:
039: /**
040: Answer <code>model</code> if it has no imports, or a union model with
041: <code>model</code> as its base and its imported models as the other
042: components. The default file manager is used to load the models.
043: */
044: public Model withImports(Model model) {
045: return withImports(FileManager.get(), model);
046: }
047:
048: /**
049: Answer <code>model</code> if it has no imports, or a union model with
050: <code>model</code> as its base and its imported models as the other
051: components. The file manager <code>fm</code> is used to load the
052: imported models.
053: */
054: public Model withImports(FileManager fm, Model model) {
055: return withImports(fm, model, new HashSet());
056: }
057:
058: private Model withImports(FileManager fm, Model model, Set loading) {
059: StmtIterator oit = model.listStatements(null, OWL.imports,
060: (RDFNode) null);
061: StmtIterator jit = model.listStatements(null, JA.imports,
062: (RDFNode) null);
063: if (oit.hasNext() || jit.hasNext()) {
064: MultiUnion g = new MultiUnion(new Graph[] { model
065: .getGraph() });
066: addImportedGraphs(fm, loading, oit, g);
067: addImportedGraphs(fm, loading, jit, g);
068: return ModelFactory.createModelForGraph(g);
069: } else
070: return model;
071: }
072:
073: private void addImportedGraphs(FileManager fm, Set loading,
074: StmtIterator oit, MultiUnion g) {
075: while (oit.hasNext()) {
076: String path = getObjectURI(oit.nextStatement());
077: if (loading.add(path))
078: g.addGraph(graphFor(fm, loading, path));
079: }
080: }
081:
082: private String getObjectURI(Statement s) {
083: RDFNode ob = s.getObject();
084: if (ob.isLiteral())
085: return AssemblerHelp.getString(s);
086: if (ob.isAnon())
087: throw new BadObjectException(s);
088: return ((Resource) ob).getURI();
089: }
090:
091: protected Graph graphFor(FileManager fm, Set loading, String path) {
092: Graph already = (Graph) cache.get(path);
093: if (already == null) {
094: Graph result = withImports(fm, fm.loadModel(path), loading)
095: .getGraph();
096: cache.put(path, result);
097: return result;
098: } else
099: return already;
100: }
101: }
102:
103: /*
104: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
105: * All rights reserved.
106: *
107: * Redistribution and use in source and binary forms, with or without
108: * modification, are permitted provided that the following conditions
109: * are met:
110: * 1. Redistributions of source code must retain the above copyright
111: * notice, this list of conditions and the following disclaimer.
112: * 2. Redistributions in binary form must reproduce the above copyright
113: * notice, this list of conditions and the following disclaimer in the
114: * documentation and/or other materials provided with the distribution.
115: * 3. The name of the author may not be used to endorse or promote products
116: * derived from this software without specific prior written permission.
117: *
118: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
119: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
120: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
121: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
122: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
123: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
124: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
125: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
126: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
127: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
128: */
|