use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class ArrangedWriter method getUsedNamespaces.
private void getUsedNamespaces(Set<Statement> stmts, Set<String> used) {
for (Statement st : stmts) {
used.add(st.getPredicate().getNamespace());
if (st.getObject() instanceof IRI) {
IRI uri = (IRI) st.getObject();
used.add(uri.getNamespace());
} else if (st.getObject() instanceof Literal) {
Literal lit = (Literal) st.getObject();
if (lit.getDatatype() != null) {
used.add(lit.getDatatype().getNamespace());
}
}
}
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class ArrangedWriter method flushStatements.
private synchronized void flushStatements() throws RDFHandlerException {
if (!stmtBySubject.isEmpty() || !blanks.isEmpty()) {
flushNamespaces();
Statement st;
while ((st = nextStatement()) != null) {
delegate.handleStatement(st);
}
assert queueSize == 0;
}
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class ArrangedWriter method queueBlankStatements.
private synchronized Set<Statement> queueBlankStatements(SubjectInContext key) {
Model firstMatch = blanks.filter(key.getSubject(), null, null, key.getContext());
Model matches = firstMatch.isEmpty() ? blankReferences.filter(key.getSubject(), null, null, key.getContext()) : firstMatch;
if (matches.isEmpty()) {
return null;
}
Set<Statement> set = stmtBySubject.get(key);
if (set == null) {
stmtBySubject.put(key, set = new TreeSet<Statement>(comparator));
}
set.addAll(matches);
if (firstMatch.isEmpty()) {
// repeat blank node values
queueSize += matches.size();
} else {
if (repeatBlankNodes && key.getSubject() instanceof BNode && isStillReferenced(key)) {
blankReferences.addAll(matches);
}
blanks.remove(key.getSubject(), null, null, key.getContext());
}
return set;
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class TestTurtleParser method rdfXmlLoadedFromInsideAJarResolvesRelativeUris.
@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws Exception {
URL zipfileUrl = TestTurtleParser.class.getResource("sample-with-turtle-data.zip");
assertNotNull("The sample-with-turtle-data.zip file must be present for this test", zipfileUrl);
String url = "jar:" + zipfileUrl + "!/index.ttl";
RDFParser parser = new TurtleParser();
StatementCollector sc = new StatementCollector();
parser.setRDFHandler(sc);
InputStream in = new URL(url).openStream();
parser.parse(in, url);
in.close();
Collection<Statement> stmts = sc.getStatements();
assertThat(stmts, Matchers.<Statement>iterableWithSize(2));
Iterator<Statement> iter = stmts.iterator();
Statement stmt1 = iter.next(), stmt2 = iter.next();
assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject());
assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate());
Resource res = (Resource) stmt1.getObject();
String resourceUrl = res.stringValue();
assertThat(resourceUrl, CoreMatchers.startsWith("jar:" + zipfileUrl + "!"));
URL javaUrl = new URL(resourceUrl);
assertEquals("jar", javaUrl.getProtocol());
InputStream uc = javaUrl.openStream();
assertEquals("The resource stream should be empty", -1, uc.read());
uc.close();
assertEquals(res, stmt2.getSubject());
assertEquals(DC.TITLE, stmt2.getPredicate());
assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class QueryResults method report.
/**
* Reports a graph query result to an {@link RDFHandler}. <br>
* The {@link GraphQueryResult#close()} method will always be called before this method returns.<br>
* If there is an exception generated by the GraphQueryResult, {@link RDFHandler#endRDF()} will not be
* called.
*
* @param gqr
* The query result to report.
* @param rdfHandler
* The handler to report the query result to.
* @throws RDFHandlerException
* If such an exception is thrown by the used RDF writer.
* @throws QueryEvaluationException
*/
public static void report(GraphQueryResult gqr, RDFHandler rdfHandler) throws RDFHandlerException, QueryEvaluationException {
try {
rdfHandler.startRDF();
for (Map.Entry<String, String> entry : gqr.getNamespaces().entrySet()) {
String prefix = entry.getKey();
String namespace = entry.getValue();
rdfHandler.handleNamespace(prefix, namespace);
}
while (gqr.hasNext()) {
Statement st = gqr.next();
rdfHandler.handleStatement(st);
}
} finally {
gqr.close();
}
rdfHandler.endRDF();
}
Aggregations