2009-10-15 48 views
1

有沒有一種方法可以從OWL本體文件中獲取基本名稱空間,而不使用DOM或類似的內容,但僅使用Jena的API?例如,從OWL文件:從OWL本體獲取基本名稱空間

<rdf:RDF 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#" 
    xmlns="http://www.owl-ontologies.com/Ontology1254827934.owl#" 
    xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#" 
    xmlns:owl="http://www.w3.org/2002/07/owl#" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#" 
    xmlns:swrl="http://www.w3.org/2003/11/swrl#" 
    xmlns:swrlb="http://www.w3.org/2003/11/swrlb#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
    xml:base="http://www.owl-ontologies.com/Ontology1254827934.owl"> 

我怎麼能拿http://www.owl-ontologies.com/Ontology1254827934.owl在運行時?

+0

有沒有這樣的事情在OWL本體基地命名空間。本體可以用RDF/XML進行序列化,XML序列化可能有一個'xml:base',或一個空的前綴'xmlns =「...」'。本體也可以由IRI識別。 – 2013-09-24 20:57:27

回答

2

方式一:

//Create the Ontology Model 
OntModel model = ModelFactory.createOntologyModel(); 

//Read the ontology file 
model.begin(); 
InputStream in = FileManager.get().open(FILENAME_HERE); 
if (in == null) { 
    throw new IllegalArgumentException("File: " + filename + " not found"); 
}   
model.read(in,""); 
model.commit(); 

//Get the base namespace 
model.getNsPrefixURI(""); 
3

或者,如果你真的想在xml:基礎,而不是空的xmlns:

final ArrayList<String> baseUriDropHere = new ArrayList<>(); 

DefaultHandler handler = new DefaultHandler() { 

    @Override 
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 

    if("rdf:RDF".equals(qName)) { 
     for (int i=0; i<attributes.getLength(); i++) { 
     if("xml:base".equals(attributes.getQName(i))) { 
      baseUriDropHere.add(attributes.getValue(i)); 
      return; 
     } 
     } 
    } 
    } 
}; 


try { 

    SAXParserFactory factory = SAXParserFactory.newInstance(); 
    factory.setValidating(false); 
    SAXParser parser = factory.newSAXParser(); 
    parser.parse(FILENAME_HERE, handler); 

} catch (ParserConfigurationException e) { 
} catch (SAXException e) { 
} catch (IOException e) { 
} 

if(baseUriDropHere.isEmpty()) { 
    System.out.println("no base uri set"); 
} else { 
    System.out.println(baseUriDropHere.get(0)); 
}