use of org.eclipse.rdf4j.model.Resource in project graal by graphik-team.
the class RDF4jUtils method valueToTerm.
public Term valueToTerm(Value value) {
if (value instanceof Resource) {
return DefaultTermFactory.instance().createConstant(urizer.output(value.toString()));
} else {
// Literal
org.eclipse.rdf4j.model.Literal l = (org.eclipse.rdf4j.model.Literal) value;
URI uri = URIUtils.createURI(l.getDatatype().toString());
String label = l.getLabel();
if (uri.equals(URIUtils.RDF_LANG_STRING)) {
String opt = l.getLanguage();
label += "@";
label += opt;
}
return DefaultTermFactory.instance().createLiteral(uri, label);
}
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class LocalRepositoryManagerTest method testModifySystemRepository.
@Test
public void testModifySystemRepository() {
RepositoryConfig config = manager.getRepositoryConfig(TEST_REPO);
manager.addRepositoryConfig(new RepositoryConfig(SystemRepository.ID, new SystemRepositoryConfig()));
manager.shutDown();
manager = new LocalRepositoryManager(datadir);
manager.initialize();
try (RepositoryConnection con = manager.getSystemRepository().getConnection()) {
Model model = new TreeModel();
config.setTitle("Changed");
config.export(model, con.getValueFactory().createBNode());
Resource ctx = RepositoryConfigUtil.getContext(con, config.getID());
con.begin();
con.clear(ctx);
con.add(model, ctx == null ? con.getValueFactory().createBNode() : ctx);
con.commit();
}
assertEquals("Changed", manager.getRepositoryConfig(TEST_REPO).getTitle());
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TurtleParser method parseImplicitBlank.
/**
* Parses an implicit blank node. This method parses the token <tt>[]</tt>
* and predicateObjectLists that are surrounded by square brackets.
*/
protected Resource parseImplicitBlank() throws IOException, RDFParseException, RDFHandlerException {
verifyCharacterOrFail(readCodePoint(), "[");
Resource bNode = createNode();
if (subject != null) {
reportStatement(subject, predicate, bNode);
}
skipWSC();
int c = readCodePoint();
if (c != ']') {
unread(c);
// Remember current subject and predicate
Resource oldSubject = subject;
IRI oldPredicate = predicate;
// generated bNode becomes subject
subject = bNode;
// Enter recursion with nested predicate-object list
skipWSC();
parsePredicateObjectList();
skipWSC();
// Read closing bracket
verifyCharacterOrFail(readCodePoint(), "]");
// Restore previous subject and predicate
subject = oldSubject;
predicate = oldPredicate;
}
return bNode;
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class TriXWriter method handleStatement.
public void handleStatement(Statement st) throws RDFHandlerException {
if (!writingStarted) {
throw new RDFHandlerException("Document writing has not yet been started");
}
try {
Resource context = st.getContext();
if (inActiveContext && !contextsEquals(context, currentContext)) {
// Close currently active context
xmlWriter.endTag(CONTEXT_TAG);
inActiveContext = false;
}
if (!inActiveContext) {
// Open new context
xmlWriter.startTag(CONTEXT_TAG);
if (context != null) {
writeValue(context);
}
currentContext = context;
inActiveContext = true;
}
xmlWriter.startTag(TRIPLE_TAG);
writeValue(st.getSubject());
writeValue(st.getPredicate());
writeValue(st.getObject());
xmlWriter.endTag(TRIPLE_TAG);
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.model.Resource 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());
}
Aggregations