📄 owlreaderimpl.java
字号:
// The MIT License
//
// Copyright (c) 2004 Evren Sirin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package org.mindswap.owl.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.mindswap.owl.OWLCache;
import org.mindswap.owl.OWLReader;
import org.mindswap.owl.Util;
import org.mindswap.owl.vocabulary.OWL;
import org.mindswap.owl.vocabulary.RDF;
import org.mindswap.owl.vocabulary.RDFS;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFErrorHandler;
import com.hp.hpl.jena.rdf.model.RDFReader;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
/**
* @author Evren Sirin
*
*/
public class OWLReaderImpl implements OWLReader {
private static String UNKNOWN_URI = "http://unknown/uri";
private static Hashtable modelCache = new Hashtable();
private static Hashtable modelNames = new Hashtable();
private static Set ignoreImports = new HashSet();
private RDFErrorHandler handler = null;
/**
*
*/
public OWLReaderImpl() {
addLoaded(ignoreImports, OWL.getURI());
addLoaded(ignoreImports, RDF.getURI());
addLoaded(ignoreImports, RDFS.getURI());
}
public void addLoaded(Set loadedFiles, URI file) {
file = Util.standardURI(file);
loadedFiles.add(file);
}
public boolean isLoaded(Set loadedFiles, URI file) {
file = Util.standardURI(file);
return loadedFiles.contains(file) || ignoreImports.contains(file);
}
private void readFile(List models, Set loadedFiles, URI uri, boolean withImports) throws Exception {
if (isLoaded(loadedFiles, uri))
return;
Model model = (Model) modelCache.get(uri);
if(model == null) {
model = ModelFactory.createDefaultModel();
try {
InputStream in = createInputStream(uri);
RDFReader reader = model.getReader();
if(handler != null)
reader.setErrorHandler(handler);
reader.read(model, in, uri.toString());
in.close();
modelNames.put(model, uri);
modelCache.put(uri, model);
} catch (Exception e) {
if(loadedFiles.isEmpty())
throw e;
else {
if(handler != null) handler.warning(e);
System.err.println("WARNING: The import file " + uri + " cannot be parsed");
}
}
}
addLoaded(loadedFiles, uri);
models.add(model);
if(withImports) {
Vector imports = new Vector();
StmtIterator i = model.listStatements(null, Util.toProperty(OWL.imports), (Resource) null);
while (i.hasNext()) {
Statement stmt = i.nextStatement();
String importFile = stmt.getResource().toString();
try {
readFile(models, loadedFiles, new URI(importFile), withImports);
//imports.add(new URI(s));
} catch(URISyntaxException e) {
System.err.println("WARNING: The import file is not a valid URI: " + importFile);
}
} // while
}
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLParser#parseSeperate(java.lang.String)
*/
public Model[] readSeparate(URI url) throws Exception {
List models = new ArrayList();
Set loadedFiles = new HashSet();
readFile(models, loadedFiles, url, true);
return (Model []) models.toArray(new Model[models.size()]);
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLParser#parseSeperate(java.lang.String)
*/
private Model readSingle(URI url) throws Exception {
List models = new ArrayList();
Set loadedFiles = new HashSet();
readFile(models, loadedFiles, url, false);
return (Model) models.get(0);
}
public URI getModelURI(Model model) {
return (URI) modelNames.get(model);
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLParser#mergeAll(com.hp.hpl.jena.rdf.model.Model[])
*/
private Model mergeAll(Model[] models) {
Model model = ModelFactory.createDefaultModel();
for(int i = 0; i < models.length; i++)
model.add(models[i], false);
return model;
}
private InputStream createInputStream(URI uri) throws Exception {
InputStream in = null;
if(OWLCache.isForced()) {
File cachedFile = OWLCache.getCachedFile(uri.toString());
if(cachedFile != null) {
System.err.println("WARNING: Force using cached file " + cachedFile);
in = new FileInputStream(cachedFile);
}
else {
try {
in = uri.toURL().openConnection().getInputStream();
} catch(Exception e) {
System.err.println("WARNING: Cannot read file " + uri);
throw new FileNotFoundException("The file " + uri + " cannot be parsed");
}
}
}
else {
try {
in = uri.toURL().openConnection().getInputStream();
} catch(Exception e) {
System.err.println("WARNING: Cannot read file " + uri);
File cachedFile = OWLCache.getCachedFile(uri.toString());
if(cachedFile != null) {
System.err.println("WARNING: Try cached file " + cachedFile);
in = new FileInputStream(cachedFile);
}
else
throw new FileNotFoundException("The file " + uri + " cannot be parsed");
}
}
return in;
}
public Model read(URI uri) throws Exception {
return read(uri, true);
}
public Model read(URI uri, boolean withImports) throws Exception {
Model model = null;
if(withImports)
model = mergeAll(readSeparate(uri));
else
model = readSingle(uri);
return model;
}
public List getImports(URI uri) {
List imports = new ArrayList();
Model[] models = null;
try {
models = readSeparate(uri);
} catch(Exception e) {
return null;
}
for(int i = 0; i < models.length; i++) {
imports.add(getModelURI(models[i]));
}
return imports;
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReader#getNameSpaces(java.lang.String)
*/
public Map getNameSpaces(URI uri) {
Model model = null;
try {
model = read(uri, false);
} catch(Exception e) {
return null;
}
return model.getNsPrefixMap();
}
/* (non-Javadoc)
* @see org.mindswap.owl.OWLReader#setErrorHandler(com.hp.hpl.jena.rdf.model.RDFErrorHandler)
*/
public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) {
RDFErrorHandler old = handler;
handler = errHandler;
return old;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -