The Model interface extends the PrefixMapping interface, so you've got
the functionality you need. Here's some sample code and output.  Sorry
about indentation;  Google's new compose doesn't seem to handle it
very well.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class Prefixes {
public static void main( String[] args ) throws FileNotFoundException {
Model model = ModelFactory.createDefaultModel();
FileInputStream foaf = new FileInputStream( new File(
"/home/taylorj/Downloads/foaf.rdf" ));
model.read( foaf,  null );
// Null because "http://xmlns.com/foaf/0.1/Person"; isn't a prefix in the model
System.out.println( "shortform: " + model.shortForm(
"http://xmlns.com/foaf/0.1/Person"; ) );
// This isn't null, since it corresponds to the foaf: prefix
System.out.println( "shortform: " + model.shortForm(
"http://xmlns.com/foaf/0.1/"; ) );
// Since this starts with a something that is a prefix, it has a short form
System.out.println( "shortform: " + model.shortForm(
"http://xmlns.com/foaf/0.1/Person"; ) );
// It has a qname too!  (The shortform might not be a valid qname, say the docs)
System.out.println( "qnameFor: " + model.qnameFor(
"http://xmlns.com/foaf/0.1/Person"; ) );

// To find all the available prefixes, take a look at the map
System.out.println( "== Show ALL THE PREFIXES!!! ==");
for ( Map.Entry<String, String> entry : model.getNsPrefixMap().entrySet() ) {
System.out.println( entry.getKey() + ": " + entry.getValue() );
}
}
}

shortform: foaf:Person
shortform: foaf:
shortform: foaf:Person
qnameFor: foaf:Person
== Show ALL THE PREFIXES!!! ==
rdfs: http://www.w3.org/2000/01/rdf-schema#
geo: http://www.w3.org/2003/01/geo/wgs84_pos#
foaf: http://xmlns.com/foaf/0.1/
admin: http://webns.net/mvcb/
rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#

On Fri, Apr 12, 2013 at 1:17 PM, Evan Patton <[email protected]> wrote:
> Hello all,
>
> Is there any way to access the prefixes from loaded documents via the Model 
> interface? For example, I create a new model and read in a number of 
> ontologies (e.g. DBpedia, FOAF) via the Model.read(InputStream, String) 
> method. If I then call the getNsURIPrefix(String) with a URI (e.g. 
> http://xmlns.com/foaf/0.1/Person) I would expect that it should return "foaf" 
> as that prefix was bound when I loaded the FOAF ontology, but instead it just 
> returns null. Is it possible to capture the prefix mappings when the files 
> are loaded?
>
> Thanks in advance for any solutions,
> Evan Patton



-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Reply via email to