Java Code Examples for org.eclipse.rdf4j.rio.RDFParser#setRDFHandler()

The following examples show how to use org.eclipse.rdf4j.rio.RDFParser#setRDFHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUriWithEscapeCharactersShouldFailToParse() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	String nt = "<http://example/\\n> <http://example/p> <http://example/o> .";

	try {
		ntriplesParser.parse(new StringReader(nt), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples uri with space");
	} catch (RDFParseException ignored) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example 2
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testExceptionHandlingWithDefaultSettings() throws Exception {
	String data = "invalid nt";

	RDFParser ntriplesParser = createRDFParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	try {
		ntriplesParser.parse(new StringReader(data), NTRIPLES_TEST_URL);
		fail("expected RDFParseException due to invalid data");
	} catch (RDFParseException expected) {
		assertEquals(expected.getLineNumber(), 1);
	}
}
 
Example 3
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testNTriplesFile() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	try (InputStream in = this.getClass().getResourceAsStream(NTRIPLES_TEST_FILE)) {
		ntriplesParser.parse(in, NTRIPLES_TEST_URL);
	} catch (RDFParseException e) {
		fail("Failed to parse N-Triples test document: " + e.getMessage());
	}

	assertEquals(30, model.size());
	assertEquals(28, model.subjects().size());
	assertEquals(1, model.predicates().size());
	assertEquals(23, model.objects().size());
}
 
Example 4
Source File: FedXBaseTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Read the expected graph query result from the specified resource
 *
 * @param resultFile
 * @return
 * @throws Exception
 */
protected Set<Statement> readExpectedGraphQueryResult(String resultFile) throws Exception {
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFile).get();

	if (rdfFormat != null) {
		RDFParser parser = Rio.createParser(rdfFormat);
		parser.setPreserveBNodeIDs(true);
		parser.setValueFactory(SimpleValueFactory.getInstance());

		Set<Statement> result = new LinkedHashSet<>();
		parser.setRDFHandler(new StatementCollector(result));

		InputStream in = SPARQLBaseTest.class.getResourceAsStream(resultFile);
		try {
			parser.parse(in, resultFile);
		} finally {
			in.close();
		}

		return result;
	} else {
		throw new RuntimeException("Unable to determine file type of results file");
	}
}
 
Example 5
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUriWithSpaceShouldFailToParse() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	String nt = "<http://example/ space> <http://example/p> <http://example/o> .";

	try {
		ntriplesParser.parse(new StringReader(nt), NTRIPLES_TEST_URL);
		fail("Should have failed to parse invalid N-Triples uri with space");
	} catch (RDFParseException ignored) {
	}

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
Example 6
Source File: SPARQLQueryComplianceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private final Set<Statement> readExpectedGraphQueryResult() throws Exception {
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFileURL)
			.orElseThrow(Rio.unsupportedFormat(resultFileURL));

	RDFParser parser = Rio.createParser(rdfFormat);
	parser.setDatatypeHandling(DatatypeHandling.IGNORE);
	parser.setPreserveBNodeIDs(true);
	parser.setValueFactory(getDataRepository().getValueFactory());

	Set<Statement> result = new LinkedHashSet<>();
	parser.setRDFHandler(new StatementCollector(result));

	InputStream in = new URL(resultFileURL).openStream();
	try {
		parser.parse(in, resultFileURL);
	} finally {
		in.close();
	}

	return result;
}
 
Example 7
Source File: HalyardExportTest.java    From Halyard with Apache License 2.0 6 votes vote down vote up
private static int getTriplesCount(String uri, String compression, RDFFormat format) throws Exception {
    InputStream in = FileSystem.get(URI.create(uri), HBaseServerTestInstance.getInstanceConfig()).open(new Path(uri));
    try {
        if (compression != null) {
            in = new CompressorStreamFactory().createCompressorInputStream(compression, in);
        }
        RDFParser parser = Rio.createParser(format);
        final AtomicInteger i = new AtomicInteger();
        parser.setRDFHandler(new AbstractRDFHandler(){
            @Override
            public void handleStatement(Statement st) throws RDFHandlerException {
                i.incrementAndGet();
            }
        });
        parser.parse(in, uri);
        return i.get();
    } finally {
        in.close();
    }
}
 
Example 8
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEndOfLineEmptyCommentWithSpaceAfter() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(new StringReader("<urn:test:subject> <urn:test:predicate> <urn:test:object> .# \n"),
			"http://example/");
	assertEquals(1, model.size());
	assertEquals(Collections.singleton("urn:test:object"), Models.objectStrings(model));
}
 
Example 9
Source File: RdfTestHelpers.java    From Wikidata-Toolkit with Apache License 2.0 5 votes vote down vote up
public static Model parseRdf(String rdfResource) throws RDFParseException,
		RDFHandlerException, IOException {
	InputStream inStream = new ByteArrayInputStream(rdfResource.getBytes());
	RDFParser parser = Rio.createParser(RDFFormat.TURTLE);
	Model graph = new LinkedHashModel();
	parser.setRDFHandler(new StatementCollector(graph));
	parser.parse(inStream, "http://test/");
	return graph;
}
 
Example 10
Source File: SPARQLQueryTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void upload(IRI graphURI, Resource context) throws Exception {
	RepositoryConnection con = dataRep.getConnection();

	try {
		con.begin();
		RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString()).orElse(RDFFormat.TURTLE);
		RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory());
		rdfParser.setVerifyData(false);
		rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
		// rdfParser.setPreserveBNodeIDs(true);

		RDFInserter rdfInserter = new RDFInserter(con);
		rdfInserter.enforceContext(context);
		rdfParser.setRDFHandler(rdfInserter);

		URL graphURL = new URL(graphURI.toString());
		InputStream in = graphURL.openStream();
		try {
			rdfParser.parse(in, graphURI.toString());
		} finally {
			in.close();
		}

		con.commit();
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
	}
}
 
Example 11
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEndOfLineCommentsWithSpaceBefore() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(new StringReader(
			"<urn:test:subject> <urn:test:predicate> <urn:test:object> . #endoflinecomment\n<urn:test:subject> <urn:test:predicate> <urn:test:secondobject> . # endoflinecomment\n"),
			"http://example/");
	assertEquals(2, model.size());
}
 
Example 12
Source File: SPARQLComplianceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void upload(IRI graphURI, Resource context) throws Exception {
	RepositoryConnection con = getDataRepository().getConnection();

	try {
		con.begin();
		RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString()).orElse(RDFFormat.TURTLE);
		RDFParser rdfParser = Rio.createParser(rdfFormat, getDataRepository().getValueFactory());
		rdfParser.setVerifyData(false);
		rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
		// rdfParser.setPreserveBNodeIDs(true);

		RDFInserter rdfInserter = new RDFInserter(con);
		rdfInserter.enforceContext(context);
		rdfParser.setRDFHandler(rdfInserter);

		URL graphURL = new URL(graphURI.toString());
		InputStream in = graphURL.openStream();
		try {
			rdfParser.parse(in, graphURI.toString());
		} finally {
			in.close();
		}

		con.commit();
	} catch (Exception e) {
		if (con.isActive()) {
			con.rollback();
		}
		throw e;
	} finally {
		con.close();
	}
}
 
Example 13
Source File: NDJSONLDParserTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testNDJSONLDParser() throws Exception {
    Model transformedModel = new LinkedHashModel();
    RDFParser parser = new NDJSONLDParser();
    parser.setRDFHandler(new ContextStatementCollector(transformedModel, SimpleValueFactory.getInstance()));
    parser.parse(NDJSONLDParserTest.class.getResourceAsStream("efo_test.ndjsonld"), "http://test/");

    Model expectedModel = Rio.parse(NDJSONLDParserTest.class.getResourceAsStream("efo_test.ttl"), "http://test/", RDFFormat.TURTLE);

    JSONParserParseTest.assertEquals(expectedModel, transformedModel);
}
 
Example 14
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEndOfLineCommentWithSpaceAfter() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(
			new StringReader("<urn:test:subject> <urn:test:predicate> <urn:test:object> .# endoflinecomment\n"),
			"http://example/");
	assertEquals(1, model.size());
	assertEquals(Collections.singleton("urn:test:object"), Models.objectStrings(model));
}
 
Example 15
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testBlankNodeIdentifiersWithOtherCharacters() throws Exception {
	// The characters -, U+00B7, U+0300 to U+036F and U+203F to U+2040 are permitted anywhere except the first
	// character.
	List<Character> charactersList = new ArrayList<>();
	charactersList.add('-');
	charactersList.add('\u00B7');
	charactersList.add('\u0300');
	charactersList.add('\u036F');
	charactersList.add('\u0301');
	charactersList.add('\u203F');

	for (int i = 0; i < charactersList.size(); i++) {
		Character character = charactersList.get(i);
		RDFParser ntriplesParser = new NTriplesParser();
		Model model = new LinkedHashModel();
		ntriplesParser.setRDFHandler(new StatementCollector(model));

		String triple = "<urn:test:subject> <urn:test:predicate> _:1" + character + " . ";
		try {
			ntriplesParser.parse(new StringReader(triple), NTRIPLES_TEST_URL);
		} catch (Exception e) {
			fail(" Failed to parse triple : " + triple + " containing character '" + character + "' at index " + i
					+ " in charactersList");
		}

		assertEquals("Should parse '" + character + "'", 1, model.size());
		assertEquals("Should have subject when triple has character : '" + character + "'", 1,
				model.subjects().size());
		assertEquals("Should have predicate when triple has character : '" + character + "'", 1,
				model.predicates().size());
		assertEquals("Should have object when triple has character : '" + character + "'", 1,
				model.objects().size());
	}

}
 
Example 16
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEndOfLineCommentsNoSpace() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(new StringReader(
			"<urn:test:subject> <urn:test:predicate> <urn:test:object> .#endoflinecomment\n<urn:test:subject> <urn:test:predicate> <urn:test:secondobject> . # endoflinecomment\n"),
			"http://example/");
	assertEquals(2, model.size());
}
 
Example 17
Source File: N3ParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void runTest() throws Exception {
	// Parse input data
	RDFParser turtleParser = createRDFParser();
	turtleParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> inputCollection = new LinkedHashSet<>();
	StatementCollector inputCollector = new StatementCollector(inputCollection);
	turtleParser.setRDFHandler(inputCollector);

	InputStream in = inputURL.openStream();
	turtleParser.parse(in, base(inputURL.toExternalForm()));
	in.close();

	// Parse expected output data
	NTriplesParser ntriplesParser = new NTriplesParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> outputCollection = new LinkedHashSet<>();
	StatementCollector outputCollector = new StatementCollector(outputCollection);
	ntriplesParser.setRDFHandler(outputCollector);

	in = outputURL.openStream();
	ntriplesParser.parse(in, base(outputURL.toExternalForm()));
	in.close();

	// Check equality of the two models
	if (!Models.isomorphic(inputCollection, outputCollection)) {
		System.err.println("===models not equal===");
		// System.err.println("Expected: " + outputCollection);
		// System.err.println("Actual : " + inputCollection);
		// System.err.println("======================");

		List<Statement> missing = new LinkedList<>(outputCollection);
		missing.removeAll(inputCollection);

		List<Statement> unexpected = new LinkedList<>(inputCollection);
		unexpected.removeAll(outputCollection);

		if (!missing.isEmpty()) {
			System.err.println("Missing   : " + missing);
		}
		if (!unexpected.isEmpty()) {
			System.err.println("Unexpected: " + unexpected);
		}

		fail("models not equal");
	}
}
 
Example 18
Source File: RDFJSONParserTestCase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void runTest() throws Exception {
	// Parse input data
	RDFParser rdfjsonParser = createRDFParser();
	rdfjsonParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> inputCollection = new LinkedHashSet<>();
	StatementCollector inputCollector = new StatementCollector(inputCollection);
	rdfjsonParser.setRDFHandler(inputCollector);

	InputStream in = this.getClass().getResourceAsStream(inputURL);
	rdfjsonParser.parse(in, baseURL);
	in.close();

	// Parse expected output data
	NTriplesParser ntriplesParser = new NTriplesParser();
	ntriplesParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);

	Set<Statement> outputCollection = new LinkedHashSet<>();
	StatementCollector outputCollector = new StatementCollector(outputCollection);
	ntriplesParser.setRDFHandler(outputCollector);

	if (outputURL != null) {
		// System.out.println(this.outputURL);
		//
		// NTriplesWriter nTriplesWriter = new NTriplesWriter(System.out);
		// nTriplesWriter.startRDF();
		// for(Statement nextStatment : inputCollection) {
		// nTriplesWriter.handleStatement(nextStatment);
		// }
		// nTriplesWriter.endRDF();

		in = this.getClass().getResourceAsStream(outputURL);
		ntriplesParser.parse(in, baseURL);
		in.close();

		// Check equality of the two models
		if (!Models.isomorphic(inputCollection, outputCollection)) {
			System.err.println("===models not equal===");
			System.err.println("Expected: " + outputCollection);
			System.err.println("Actual  : " + inputCollection);
			System.err.println("======================");

			fail("models not equal");
		}
	}
}
 
Example 19
Source File: KafkaLoadStatements.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public void fromFile(final Path statementsPath, final String visibilities) throws RyaStreamsException {
    requireNonNull(statementsPath);
    requireNonNull(visibilities);

    if(!statementsPath.toFile().exists()) {
        throw new RyaStreamsException("Could not load statements at path '" + statementsPath + "' because that " +
                "does not exist. Make sure you've entered the correct path.");
    }

    // Create an RDF Parser whose format is derived from the statementPath's file extension.
    final String filename = statementsPath.getFileName().toString();
    final RDFFormat format = RdfFormatUtils.forFileName(filename);
    if (format == null) {
        throw new UnsupportedRDFormatException("Unknown RDF format for the file: " + filename);
    }
    final RDFParser parser = Rio.createParser(format);

    // Set a handler that writes the statements to the specified kafka topic.
    parser.setRDFHandler(new AbstractRDFHandler() {
        @Override
        public void startRDF() throws RDFHandlerException {
            log.trace("Starting loading statements.");
        }

        @Override
        public void handleStatement(final Statement stmnt) throws RDFHandlerException {
            final VisibilityStatement visiStatement = new VisibilityStatement(stmnt, visibilities);
            producer.send(new ProducerRecord<>(topic, visiStatement));
        }

        @Override
        public void endRDF() throws RDFHandlerException {
            producer.flush();
            log.trace("Done.");
        }
    });

    // Do the parse and load.
    try {
        parser.parse(Files.newInputStream(statementsPath), "");
    } catch (RDFParseException | RDFHandlerException | IOException e) {
        throw new RyaStreamsException("Could not load the RDF file's Statements into Rya Streams.", e);
    }
}
 
Example 20
Source File: TurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws IOException {
	URL zipfileUrl = TurtleParserTest.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);

	try (InputStream in = new URL(url).openStream()) {
		parser.parse(in, url);
	}

	Collection<Statement> stmts = sc.getStatements();

	assertThat(stmts).hasSize(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).startsWith("jar:" + zipfileUrl + "!");

	URL javaUrl = new URL(resourceUrl);
	assertEquals("jar", javaUrl.getProtocol());

	try (InputStream uc = javaUrl.openStream()) {
		assertEquals("The resource stream should be empty", -1, uc.read());
	}

	assertEquals(res, stmt2.getSubject());
	assertEquals(DC.TITLE, stmt2.getPredicate());
	assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}