use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class DAWGTestResultSetUtil method toBooleanQueryResult.
public static boolean toBooleanQueryResult(Iterable<? extends Statement> dawgGraph) throws DAWGTestResultSetParseException {
DAWGTestBooleanParser parser = new DAWGTestBooleanParser();
try {
parser.startRDF();
for (Statement st : dawgGraph) {
parser.handleStatement(st);
}
parser.endRDF();
return parser.getValue();
} catch (RDFHandlerException e) {
throw new DAWGTestResultSetParseException(e.getMessage(), e);
}
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class BinaryHandlingTest method writeBinary.
/**
* Helper method to write the given model to N-Triples and return an InputStream containing the results.
*
* @param statements
* @return An {@link InputStream} containing the results.
* @throws RDFHandlerException
*/
private InputStream writeBinary(Model statements) throws RDFHandlerException {
ByteArrayOutputStream output = new ByteArrayOutputStream(8096);
RDFWriter binaryWriter = new BinaryRDFWriter(output);
binaryWriter.startRDF();
for (Statement nextStatement : statements) {
binaryWriter.handleStatement(nextStatement);
}
binaryWriter.endRDF();
return new ByteArrayInputStream(output.toByteArray());
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class RDFJSONHandlingTest method writeRDFJSON.
/**
* Helper method to write the given model to RDFJSON and return an InputStream containing the results.
*
* @param statements
* @return An {@link InputStream} containing the results.
* @throws RDFHandlerException
*/
private InputStream writeRDFJSON(Model statements) throws RDFHandlerException {
StringWriter writer = new StringWriter();
RDFWriter rdfjsonWriter = new RDFJSONWriter(writer, RDFFormat.RDFJSON);
rdfjsonWriter.startRDF();
for (Statement nextStatement : statements) {
rdfjsonWriter.handleStatement(nextStatement);
}
rdfjsonWriter.endRDF();
return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class JSONLDParserHandlerTest method writeJSONLD.
/**
* Helper method to write the given model to JSON-LD and return an InputStream containing the results.
*
* @param statements
* @return An {@link InputStream} containing the results.
* @throws RDFHandlerException
*/
private InputStream writeJSONLD(Model statements) throws RDFHandlerException {
final StringWriter writer = new StringWriter();
final RDFWriter jsonldWriter = new JSONLDWriter(writer);
jsonldWriter.startRDF();
for (final Namespace prefix : statements.getNamespaces()) {
jsonldWriter.handleNamespace(prefix.getPrefix(), prefix.getName());
}
for (final Statement nextStatement : statements) {
jsonldWriter.handleStatement(nextStatement);
}
jsonldWriter.endRDF();
return new ByteArrayInputStream(writer.toString().getBytes(Charset.forName("UTF-8")));
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class JSONLDWriterTest method testRoundTripNamespaces.
@Test
public void testRoundTripNamespaces() throws Exception {
String exNs = "http://example.org/";
IRI uri1 = vf.createIRI(exNs, "uri1");
IRI uri2 = vf.createIRI(exNs, "uri2");
Literal plainLit = vf.createLiteral("plain", XMLSchema.STRING);
Statement st1 = vf.createStatement(uri1, uri2, plainLit);
ByteArrayOutputStream out = new ByteArrayOutputStream();
RDFWriter rdfWriter = rdfWriterFactory.getWriter(out);
rdfWriter.getWriterConfig().set(JSONLDSettings.JSONLD_MODE, JSONLDMode.COMPACT);
rdfWriter.handleNamespace("ex", exNs);
rdfWriter.startRDF();
rdfWriter.handleStatement(st1);
rdfWriter.endRDF();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
RDFParser rdfParser = rdfParserFactory.getParser();
ParserConfig config = new ParserConfig();
config.set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true);
config.set(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES, true);
rdfParser.setParserConfig(config);
rdfParser.setValueFactory(vf);
Model model = new LinkedHashModel();
rdfParser.setRDFHandler(new StatementCollector(model));
rdfParser.parse(in, "foo:bar");
assertEquals("Unexpected number of statements, found " + model.size(), 1, model.size());
assertTrue("missing namespaced statement", model.contains(st1));
if (rdfParser.getRDFFormat().supportsNamespaces()) {
assertTrue("Expected at least one namespace, found " + model.getNamespaces().size(), model.getNamespaces().size() >= 1);
assertEquals(exNs, model.getNamespace("ex").get().getName());
}
}
Aggregations