Journal Quantum Jim's Journal: Preview of Nemo - RDF in Saxon
Here's something I hacked together in the last two days (after a few weeks of learning). The project is called Nemo, since I just watched the excellent movie, Finding Nemo.
It isn't that impressive right now, but I have a lot to do (1st priority - get a domain so I can create a good package name). The program copyrighted by me, James Francis Cerra, and this preview should be considered under the GPL, Version 2 (get the license text from the link). I currently intend release Nemo under a less restrictive license later (after I put more thought into it). Here's the code for the two classes:
File: Nemo.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdql.Query;
import com.hp.hpl.jena.rdql.ResultBinding;
public class Nemo {
static public RDFSequence find(String sourceURI, String queryString) throws FileNotFoundException {
Model model = ModelFactory.createDefaultModel();
model.read(new FileInputStream(sourceURI), "", "RDF/XML");
Query query = new Query(queryString);
query.setSource(model);
return new RDFSequence(query);
}
static public Object get(ResultBinding result, String val) {
return result.get(val);
}
}
File: RDFSequence.java
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.om.Item;
import net.sf.saxon.value.Value;
import net.sf.saxon.xpath.XPathException;
import com.hp.hpl.jena.rdql.Query;
import com.hp.hpl.jena.rdql.QueryEngine;
import com.hp.hpl.jena.rdql.QueryResults;
public class RDFSequence implements SequenceIterator {
RDFSequence(Query q) {
initialQuery = q;
QueryEngine qe = new QueryEngine(q);
results = qe.exec();
currentItem = null;
}
public Item current() {// Get the current value in the sequence (the one returned by the most recent call on next()).
return currentItem;
}
public RDFSequence getAnother() throws XPathException {// Get another SequenceIterator that iterates over the same items as the original, but which is repositioned at the start of the sequence.
return new RDFSequence(initialQuery);
}
public Item next() throws XPathException {// Get the next item in the sequence.
if (results.hasNext()) {
currentItem = Value.asItem(
Value.convertJavaObjectToXPath(
results.next(),
null
),
null
);
return currentItem;
} else {
return null;
}
}
public int position() {// Get the current position.
return results.getRowNumber();
}
private Query initialQuery;
private QueryResults results;
private Item currentItem;
}
Here's an example on how to use it:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jfc="java:Nemo"
extension-element-prefixes="jfc"
exclude-result-prefixes="jfc xsl"
>
<xsl:variable name="query"><![CDATA[SELECT ?x, ?y
WHERE (?x <http://www.w3.org/2001/vcard-rdf/3.0#FN> ?y)]]></xsl:variable>
<xsl:variable name="datasource" select="'datasource.rdf'"/>
<!-- Try to make the output look half decent -->
<xsl:output method="text"/>
<xsl:template match="/">
From:
<xsl:value-of select="$datasource"/>
Query:
<xsl:value-of select="$query"/>
Result:
<xsl:for-each select="jfc:find($datasource, $query)">
Name: <xsl:value-of select="jfc:get(.,'y')"/>
URI: <xsl:value-of select="jfc:get(.,'x')"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Hope you find that useful, and please excuse slash's lame support for inline code blocks.
Preview of Nemo - RDF in Saxon More Login
Preview of Nemo - RDF in Saxon
Slashdot Top Deals