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

The following examples show how to use org.eclipse.rdf4j.rio.RDFParser#setDatatypeHandling() . 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: SPARQLServiceEvaluationTest.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
 */
private Set<Statement> readExpectedGraphQueryResult(String resultFile) throws Exception {
	RDFFormat rdfFormat = Rio.getParserFormatForFileName(resultFile).orElseThrow(Rio.unsupportedFormat(resultFile));

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

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

	InputStream in = SPARQLServiceEvaluationTest.class.getResourceAsStream(resultFile);
	try {
		parser.parse(in, null); // TODO check
	} finally {
		in.close();
	}

	return result;
}
 
Example 2
Source File: SPARQLQueryTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected 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(dataRep.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 3
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 4
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 5
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 6
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 7
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 8
Source File: JSONParserTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodsThatDoNothing() {
    RDFParser p = new JSONParser();
    p.setVerifyData(true);
    p.setPreserveBNodeIDs(true);
    p.setStopAtFirstError(true);
    p.setDatatypeHandling(RDFParser.DatatypeHandling.NORMALIZE);
    p.setParseErrorListener(null);
    p.setParseLocationListener(null);
}
 
Example 9
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 10
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");
		}
	}
}