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

The following examples show how to use org.eclipse.rdf4j.rio.RDFParser#parse() . 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: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = RDFParseException.class)
public void testBlankNodeIdentifiersWithDotAsLastCahracter() throws Exception {
	// The character . may appear anywhere except the first or last character.
	RDFParser ntriplesParser = new NTriplesParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	try {
		ntriplesParser.parse(new StringReader("_:123 <urn:test:predicate> _:456. ."), NTRIPLES_TEST_URL);
	} catch (RDFParseException e) {
		assertEquals(0, model.size());
		assertEquals(0, model.subjects().size());
		assertEquals(0, model.predicates().size());
		assertEquals(0, model.objects().size());
		throw e;
	}
	fail("Should have failed to parse invalid N-Triples bnode with '.' at the end of the bnode label");
}
 
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 testExceptionHandlingWithoutStopAtFirstError2() throws Exception {
	String data = "invalid nt";

	RDFParser ntriplesParser = createRDFParser();
	ntriplesParser.getParserConfig().set(NTriplesParserSettings.FAIL_ON_NTRIPLES_INVALID_LINES, false);

	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));

	ntriplesParser.parse(new StringReader(data), NTRIPLES_TEST_URL);

	assertEquals(0, model.size());
	assertEquals(0, model.subjects().size());
	assertEquals(0, model.predicates().size());
	assertEquals(0, model.objects().size());
}
 
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 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 5
Source File: RDFXMLParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test with Secure processing setting off.
 * <p>
 * IMPORTANT: Only turn this on to verify it is still working, as there is no way to safely perform this test.
 * <p>
 * WARNING: This test will cause an OutOfMemoryException when it eventually fails, as it will eventually fail.
 *
 * @throws Exception
 */
@Ignore
@Test(timeout = 10000)
public void testEntityExpansionNoSecureProcessing() throws Exception {
	final Model aGraph = new LinkedHashModel();
	ParseErrorCollector errorCollector = new ParseErrorCollector();
	RDFParser aParser = Rio.createParser(RDFFormat.RDFXML)
			.setRDFHandler(new StatementCollector(aGraph))
			.set(XMLParserSettings.SECURE_PROCESSING, false)
			.setParseErrorListener(errorCollector);

	try {
		// IMPORTANT: This will not use the entity limit
		aParser.parse(
				this.getClass().getResourceAsStream("/testcases/rdfxml/openrdf/bad-entity-expansion-limit.rdf"),
				"http://example.org");
		fail("Parser did not throw an exception");
	} catch (RDFParseException e) {
		// assertTrue(e.getMessage().contains(
		// "The parser has encountered more than \"64,000\" entity expansions in this document; this is the limit
		// imposed by the"));
	}
	assertEquals(0, errorCollector.getWarnings().size());
	assertEquals(0, errorCollector.getErrors().size());
	assertEquals(1, errorCollector.getFatalErrors().size());
}
 
Example 6
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 7
Source File: RDFImportServiceImpl.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
private void importInputStream(RepositoryConnection conn, ImportServiceConfig config, @Nonnull InputStream stream,
                               @Nonnull RDFFormat format) throws IOException {
    RDFParser parser = Rio.createParser(format);
    ParserConfig parserConfig = new ParserConfig();
    if (config.getContinueOnError()) {
        parserConfig.addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES);
        parserConfig.addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_LANGUAGES);
        parserConfig.addNonFatalError(BasicParserSettings.NORMALIZE_DATATYPE_VALUES);
    }
    parserConfig.addNonFatalError(BasicParserSettings.VERIFY_URI_SYNTAX);
    parser.setParserConfig(parserConfig);
    BatchInserter inserter = new BatchInserter(conn, transformer, config.getBatchSize());
    if (config.getLogOutput()) {
        inserter.setLogger(LOGGER);
    }
    if (config.getPrintOutput()) {
        inserter.setPrintToSystem(true);
    }
    parser.setRDFHandler(inserter);
    parser.parse(stream, "");
}
 
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 testEscapes() throws Exception {
	RDFParser ntriplesParser = createRDFParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(
			new StringReader("<urn:test:subject> <urn:test:predicate> \" \\t \\b \\n \\r \\f \\\" \\' \\\\ \" . "),
			"http://example/");
	assertEquals(1, model.size());
	assertEquals(" \t \b \n \r \f \" \' \\ ", Models.objectLiteral(model).get().getLabel());
}
 
Example 9
Source File: AbstractNQuadsParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * The N-Quads parser must be able to parse the N-Triples test file without error.
 */
public void testNTriplesFile() throws Exception {
	RDFParser nquadsParser = createRDFParser();
	nquadsParser.setRDFHandler(new AbstractRDFHandler() {
	});

	try (InputStream in = AbstractNQuadsParserUnitTest.class.getResourceAsStream(NTRIPLES_TEST_FILE)) {
		nquadsParser.parse(in, NTRIPLES_TEST_URL);
	} catch (RDFParseException e) {
		fail("NQuadsParser failed to parse N-Triples test document: " + e.getMessage());
	}
}
 
Example 10
Source File: CreateServlet.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
RepositoryConfig updateRepositoryConfig(final String configString) throws IOException, RDF4JException {
	final Model graph = new LinkedHashModel();
	final RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE, SimpleValueFactory.getInstance());
	rdfParser.setRDFHandler(new StatementCollector(graph));
	rdfParser.parse(new StringReader(configString), RepositoryConfigSchema.NAMESPACE);

	Resource res = Models.subject(graph.getStatements(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY))
			.orElseThrow(() -> new RepositoryException("could not find instance of Repository class in config"));
	final RepositoryConfig repConfig = RepositoryConfig.create(graph, res);
	repConfig.validate();
	manager.addRepositoryConfig(repConfig);
	return repConfig;
}
 
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 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 12
Source File: StatementsDeserializer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Statement> deserialize(final String topic, final byte[] data) {
    if(data == null || data.length == 0) {
        // Return null because that is the contract of this method.
        return null;
    }

    try {
        final RDFParser parser = PARSER_FACTORY.getParser();
        final Set<Statement> statements = new HashSet<>();

        parser.setRDFHandler(new AbstractRDFHandler() {
            @Override
            public void handleStatement(final Statement statement) throws RDFHandlerException {
                log.debug("Statement: " + statement);
                statements.add( statement );
            }
        });

        parser.parse(new ByteArrayInputStream(data), null);
        return statements;

    } catch(final RDFParseException | RDFHandlerException | IOException e) {
        log.error("Could not deserialize a Set of VisibilityStatement objects using the RDF4J Rio Binary format.", e);
        return null;
    }
}
 
Example 13
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 14
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEndOfLineCommentWithSpaceBefore() 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 testBlankNodeIdentifiersWithUnderScore() throws Exception {
	// The characters _ and [0-9] may appear anywhere in a blank node label.
	RDFParser ntriplesParser = new NTriplesParser();
	Model model = new LinkedHashModel();
	ntriplesParser.setRDFHandler(new StatementCollector(model));
	ntriplesParser.parse(new StringReader("_:123_ <urn:test:predicate> _:_456 ."), NTRIPLES_TEST_URL);

	assertEquals(1, model.size());
	assertEquals(1, model.subjects().size());
	assertEquals(1, model.predicates().size());
	assertEquals(1, model.objects().size());
}
 
Example 16
Source File: ExtensibleDynamicEvaluationStatisticsLowMemBenchmark.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Benchmark
public ExtensibleDynamicEvaluationStatistics addStatements() throws IOException, InterruptedException {
	ExtensibleDynamicEvaluationStatistics extensibleDynamicEvaluationStatistics = new ExtensibleDynamicEvaluationStatistics(
			null);

	RDFParser parser = Rio.createParser(RDFFormat.TURTLE);
	parser.setRDFHandler(new RDFHandler() {
		@Override
		public void startRDF() throws RDFHandlerException {

		}

		@Override
		public void endRDF() throws RDFHandlerException {

		}

		@Override
		public void handleNamespace(String prefix, String uri) throws RDFHandlerException {

		}

		@Override
		public void handleStatement(Statement st) throws RDFHandlerException {
			extensibleDynamicEvaluationStatistics
					.add(ExtensibleStatementHelper.getDefaultImpl().fromStatement(st, false));
		}

		@Override
		public void handleComment(String comment) throws RDFHandlerException {

		}
	});

	parser.parse(getResourceAsStream("bsbm-100.ttl"), "");

	extensibleDynamicEvaluationStatistics.waitForQueue();
	return extensibleDynamicEvaluationStatistics;
}
 
Example 17
Source File: RyaAccumuloSailFactoryTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
    public void testCreateFromTemplateName() throws Exception {
        LocalRepositoryManager repoman = new LocalRepositoryManager(Files.createTempDir());
        repoman.initialize();

        try(InputStream templateStream = RepositoryConfig.class.getResourceAsStream("RyaAccumuloSail.ttl")) {
            String template = IOUtils.toString(templateStream);

            final ConfigTemplate configTemplate = new ConfigTemplate(template);
            final Map<String, String> valueMap = ImmutableMap.<String, String> builder()
                    .put("Repository ID", "RyaAccumuloSail")
                    .put("Repository title", "RyaAccumuloSail Store")
                    .put("Rya Accumulo user", "root")
                    .put("Rya Accumulo password", "")
                    .put("Rya Accumulo instance", "dev")
                    .put("Rya Accumulo zookeepers", "zoo1,zoo2,zoo3")
                    .put("Rya Accumulo is mock", "true")
                    .build();

            final String configString = configTemplate.render(valueMap);

//            final Repository systemRepo = this.state.getManager().getSystemRepository();
            final Model model = new LinkedHashModel();
            final RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
            rdfParser.setRDFHandler(new StatementCollector(model));
            rdfParser.parse(new StringReader(configString), RepositoryConfigSchema.NAMESPACE);
            final Resource repositoryNode = Models.subject(model.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY)).get();
            final RepositoryConfig repConfig = RepositoryConfig.create(model, repositoryNode);
            repConfig.validate();


            repoman.addRepositoryConfig(repConfig);

            Repository r = repoman.getRepository("RyaAccumuloSail");
            r.initialize();

        }

    }
 
Example 18
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 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());
}