Is there a Notation 3 (N3) parser for RDF2go in order to create
statements from a N3 files ? For example, to parse files from DBpedia
(http://wiki.dbpedia.org/Downloads35) in order to import triples in a
datastore by using RDF2Go.
Kind Regards
Laurent
1/ Creates a new RDFHandler for RDF2Go which wraps types by using the
ConversionUtil class from RDF2Go :
import org.ontoware.rdf2go.model.Statement;
import org.ontoware.rdf2go.model.impl.StatementImpl;
import org.ontoware.rdf2go.model.node.Resource;
import org.ontoware.rdf2go.model.node.impl.URIImpl;
import org.openrdf.rdf2go.ConversionUtil;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;
/**
* An interface defining methods related to RDF2go data handling.
* <tt>RDFHandler</tt> is both used as a "consumer" and as a
"producer"
* interface. As such it can be used both as an interface for
receiving RDF
* data, for example by listening to the results of an RDF parser, and
as an
* interface for reporting RDF data, for example to an object that
serializes
* RDF data to an RDF/XML document.
*/
public abstract class RDF2GoHandler implements RDFHandler {
/**
* Signals the start of the RDF data. This method is called before
any data
* is reported.
*
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void startRDF() throws RDFHandlerException;
/**
* Signals the end of the RDF data. This method is called when all
data has
* been reported.
*
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void endRDF() throws RDFHandlerException;
/**
* Handles a namespace declaration/definition. A namespace
declaration
* associates a (short) prefix string with the namespace's URI.
The prefix
* for default namespaces, which do not have an associated prefix,
are
* represented as empty strings.
*
* @param prefix
* The prefix for the namespace, or an empty string in
case of a
* default namespace.
* @param uri
* The URI that the prefix maps to.
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void handleNamespace(String prefix, String uri)
throws RDFHandlerException;
/**
* Handles a statement.
*
* @param st
* The statement.
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public void handleStatement(org.openrdf.model.Statement stmt)
throws RDFHandlerException {
this.handleStatement(new StatementImpl(stmt.getContext() ==
null ? null : new URIImpl(stmt
.getContext().toString()), (Resource)
ConversionUtil.toRdf2go(stmt.getSubject()),
ConversionUtil.toRdf2go(stmt.getPredicate()),
ConversionUtil.toRdf2go(stmt.getObject())));
}
public abstract void handleStatement(Statement stmt) throws
RDFHandlerException;
/**
* Handles a comment.
*
* @param comment
* The comment.
* @throws RDFHandlerException
* If the RDF handler has encountered an unrecoverable
error.
*/
public abstract void handleComment(String comment) throws
RDFHandlerException;
}
2/ Now uses it with your parser :
NTriplesParser parser = new NTriplesParser();
parser.setRDFHandler(new RDF2GoHandler() {
@Override
public void startRDF() throws RDFHandlerException {
System.out.println("startRDF");
}
@Override
public void handleNamespace(String prefix, String uri)
throws RDFHandlerException {
System.out.println("handleNamespace ");
}
@Override
public void handleComment(String comment) throws
RDFHandlerException {
System.out.println("handleComment");
}
@Override
public void endRDF() throws RDFHandlerException {
System.out.println("endRDF");
}
@Override
public void
handleStatement(org.ontoware.rdf2go.model.Statement stmt) throws
RDFHandlerException {
System.out.println("handleStatement=" + stmt);
}
});
try {
parser.parse(new FileInputStream("path/to/file.nt"), "");
} catch (RDFParseException e) {
e.printStackTrace();
} catch (RDFHandlerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Kind Regards
Laurent
You are making this too complicated :)
You can simply call model.readFrom:
model.readFrom(myInputStream, Syntax.Ntriples);
- Gunnar
--
Gunnar Aastrand Grimnes
DFKI GmbH
http://www.dfki.uni-kl.de/~grimnes