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

The following examples show how to use org.eclipse.rdf4j.rio.RDFParser#setParserConfig() . 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: RDFLoader.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Adds the data that can be read from the supplied InputStream or Reader to this repository.
 *
 * @param inputStreamOrReader An {@link InputStream} or {@link Reader} containing RDF data that must be added to the
 *                            repository.
 * @param baseURI             The base URI for the data.
 * @param dataFormat          The file format of the data.
 * @param rdfHandler          handles all data from all documents
 * @throws IOException
 * @throws UnsupportedRDFormatException
 * @throws RDFParseException
 * @throws RDFHandlerException
 */
private void loadInputStreamOrReader(Object inputStreamOrReader, String baseURI, RDFFormat dataFormat,
		RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException {
	RDFParser rdfParser = Rio.createParser(dataFormat, vf);
	rdfParser.setParserConfig(config);
	rdfParser.setParseErrorListener(new ParseErrorLogger());

	rdfParser.setRDFHandler(rdfHandler);

	if (inputStreamOrReader instanceof InputStream) {
		rdfParser.parse((InputStream) inputStreamOrReader, baseURI);
	} else if (inputStreamOrReader instanceof Reader) {
		rdfParser.parse((Reader) inputStreamOrReader, baseURI);
	} else {
		throw new IllegalArgumentException(
				"Must be an InputStream or a Reader, is a: " + inputStreamOrReader.getClass());
	}
}
 
Example 2
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 3
Source File: JSONLDWriterBackgroundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	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());
	}
}
 
Example 4
Source File: JSONLDWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testRoundTripNamespaces() throws Exception {
	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.startRDF();
	rdfWriter.handleNamespace("ex", exNs);
	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());
	}
}
 
Example 5
Source File: SPARQLProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the response in a background thread. HTTP connections are dealt with in the {@link BackgroundGraphResult}
 * or (in the error-case) in this method.
 */
protected GraphQueryResult getRDFBackground(HttpUriRequest method, boolean requireContext)
		throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException,
		UnauthorizedException, QueryInterruptedException {

	boolean submitted = false;

	// Specify which formats we support using Accept headers
	Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
	if (rdfFormats.isEmpty()) {
		throw new RepositoryException("No tuple RDF parsers have been registered");
	}

	GraphQueryResult gRes = null;
	// send the tuple query
	HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
	try {

		// if we get here, HTTP code is 200
		String mimeType = getResponseMIMEType(response);
		RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats)
				.orElseThrow(() -> new RepositoryException(
						"Server responded with an unsupported file format: " + mimeType));
		RDFParser parser = Rio.createParser(format, getValueFactory());
		parser.setParserConfig(getParserConfig());
		parser.setParseErrorListener(new ParseErrorLogger());

		Charset charset = null;

		// SES-1793 : Do not attempt to check for a charset if the format is
		// defined not to have a charset
		// This prevents errors caused by people erroneously attaching a
		// charset to a binary formatted document
		HttpEntity entity = response.getEntity();
		if (format.hasCharset() && entity != null && entity.getContentType() != null) {
			// TODO copied from SPARQLGraphQuery repository, is this
			// required?
			try {
				charset = ContentType.parse(entity.getContentType().getValue()).getCharset();
			} catch (IllegalCharsetNameException e) {
				// work around for Joseki-3.2
				// Content-Type: application/rdf+xml;
				// charset=application/rdf+xml
			}
			if (charset == null) {
				charset = UTF8;
			}
		}

		if (entity == null) {
			throw new RepositoryException("Server response was empty.");
		}

		String baseURI = method.getURI().toASCIIString();
		gRes = background.parse(parser, entity.getContent(), charset, baseURI);
		submitted = true;
		return gRes;
	} finally {
		if (!submitted) {
			EntityUtils.consumeQuietly(response.getEntity());
		}
	}

}
 
Example 6
Source File: SPARQLProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the response in this thread using the provided {@link RDFHandler}. All HTTP connections are closed and
 * released in this method
 */
protected void getRDF(HttpUriRequest method, RDFHandler handler, boolean requireContext)
		throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException,
		UnauthorizedException, QueryInterruptedException {
	// Specify which formats we support using Accept headers
	Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
	if (rdfFormats.isEmpty()) {
		throw new RepositoryException("No tuple RDF parsers have been registered");
	}

	// send the tuple query
	HttpResponse response = sendGraphQueryViaHttp(method, requireContext, rdfFormats);
	try {

		String mimeType = getResponseMIMEType(response);
		try {
			RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats)
					.orElseThrow(() -> new RepositoryException(
							"Server responded with an unsupported file format: " + mimeType));
			// Check if we can pass through to the output stream directly
			if (handler instanceof RDFWriter) {
				RDFWriter rdfWriter = (RDFWriter) handler;
				if (rdfWriter.getRDFFormat().equals(format)) {
					OutputStream out = rdfWriter.getOutputStream().orElse(null);
					if (out != null) {
						InputStream in = response.getEntity().getContent();
						IOUtils.copy(in, out);
						return;
					}
				}
			}

			// we need to parse the result and re-serialize.
			RDFParser parser = Rio.createParser(format, getValueFactory());
			parser.setParserConfig(getParserConfig());
			parser.setParseErrorListener(new ParseErrorLogger());
			parser.setRDFHandler(handler);
			parser.parse(response.getEntity().getContent(), method.getURI().toASCIIString());
		} catch (RDFParseException e) {
			throw new RepositoryException("Malformed query result from server", e);
		}
	} finally {
		EntityUtils.consumeQuietly(response.getEntity());
	}
}