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

The following examples show how to use org.eclipse.rdf4j.rio.RDFParser#setParseErrorListener() . 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: 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 3
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 4
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());
	}
}