org.eclipse.rdf4j.rio.helpers.ParseErrorLogger Java Examples

The following examples show how to use org.eclipse.rdf4j.rio.helpers.ParseErrorLogger. 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: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test780IRISpace() throws Exception {
	String ttl = "_:b25978837	a <http://purl.bioontology.org/ontology/UATC/\\u0020SERINE\\u0020\\u0020> .";
	try {
		Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE);
		fail();
	} catch (RDFParseException e) {
		// Invalid IRI
	}
	Model model = Rio.parse(new StringReader(ttl), "", RDFFormat.TURTLE,
			new ParserConfig().set(BasicParserSettings.VERIFY_URI_SYNTAX, false), SimpleValueFactory.getInstance(),
			new ParseErrorLogger());
	assertEquals(1, model.size());
	model.filter(null, RDF.TYPE, null)
			.objects()
			.forEach(obj -> assertEquals("http://purl.bioontology.org/ontology/UATC/ SERINE  ", obj.stringValue()));
}
 
Example #3
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParsingNamespacesWithOption() throws Exception {
	ParserConfig aConfig = new ParserConfig();

	aConfig.set(BasicParserSettings.NAMESPACES,
			Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));

	Model model = Rio.parse(new StringReader("<urn:a> foo:broader <urn:b>."), "", RDFFormat.TURTLE, aConfig, vf,
			new ParseErrorLogger());

	assertEquals(1, model.size());
	assertTrue(model.contains(vf.createURI("urn:a"), SKOS.BROADER, vf.createURI("urn:b")));
}
 
Example #4
Source File: CustomTurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParsingNamespacesWithOverride() throws Exception {
	ParserConfig aConfig = new ParserConfig();

	aConfig.set(BasicParserSettings.NAMESPACES,
			Collections.<Namespace>singleton(new NamespaceImpl("foo", SKOS.NAMESPACE)));

	Model model = Rio.parse(new StringReader("@prefix skos : <urn:not_skos:> ." + "<urn:a> skos:broader <urn:b>."),
			"", RDFFormat.TURTLE, aConfig, vf, new ParseErrorLogger());

	assertEquals(1, model.size());
	assertTrue(model.contains(vf.createIRI("urn:a"), vf.createIRI("urn:not_skos:broader"), vf.createIRI("urn:b")));
}
 
Example #5
Source File: HalyardStatsTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatsTarget() throws Exception {
    final HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), "statsTable", true, -1, true, 0, null, null);
    sail.initialize();
    try (InputStream ref = HalyardStatsTest.class.getResourceAsStream("testData.trig")) {
        RDFParser p = Rio.createParser(RDFFormat.TRIG);
        p.setPreserveBNodeIDs(true);
        p.setRDFHandler(new AbstractRDFHandler() {
            @Override
            public void handleStatement(Statement st) throws RDFHandlerException {
                sail.addStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
            }
        }).parse(ref, "");
    }
    sail.commit();
    sail.close();

    File root = File.createTempFile("test_stats", "");
    root.delete();
    root.mkdirs();

    assertEquals(0, ToolRunner.run(HBaseServerTestInstance.getInstanceConfig(), new HalyardStats(),
            new String[]{"-s", "statsTable", "-t", root.toURI().toURL().toString() + "stats{0}.trig", "-r", "100", "-g", "http://whatever/myStats"}));

    File stats = new File(root, "stats0.trig");
    assertTrue(stats.isFile());
    try (InputStream statsStream = new FileInputStream(stats)) {
        try (InputStream refStream = HalyardStatsTest.class.getResourceAsStream("testStatsTarget.trig")) {
            Model statsM = Rio.parse(statsStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger());
            Model refM = Rio.parse(refStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger(), SimpleValueFactory.getInstance().createIRI("http://whatever/myStats"));
            assertEqualModels(refM, statsM);
        }
    }
}
 
Example #6
Source File: HalyardStatsTest.java    From Halyard with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatsTargetPartial() throws Exception {
    final HBaseSail sail = new HBaseSail(HBaseServerTestInstance.getInstanceConfig(), "statsTable3", true, -1, true, 0, null, null);
    sail.initialize();
    try (InputStream ref = HalyardStatsTest.class.getResourceAsStream("testData.trig")) {
        RDFParser p = Rio.createParser(RDFFormat.TRIG);
        p.setPreserveBNodeIDs(true);
        p.setRDFHandler(new AbstractRDFHandler() {
            @Override
            public void handleStatement(Statement st) throws RDFHandlerException {
                sail.addStatement(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
            }
        }).parse(ref, "");
    }
    sail.commit();
    sail.close();

    File root = File.createTempFile("test_stats", "");
    root.delete();
    root.mkdirs();

    assertEquals(0, ToolRunner.run(HBaseServerTestInstance.getInstanceConfig(), new HalyardStats(),
            new String[]{"-s", "statsTable3", "-t", root.toURI().toURL().toString() + "stats{0}.trig", "-r", "100", "-g", "http://whatever/myStats", "-c", "http://whatever/graph0"}));

    File stats = new File(root, "stats0.trig");
    assertTrue(stats.isFile());
    try (InputStream statsStream = new FileInputStream(stats)) {
        try (InputStream refStream = HalyardStatsTest.class.getResourceAsStream("testStatsTargetPartial.trig")) {
            Model statsM = Rio.parse(statsStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger());
            Model refM = Rio.parse(refStream, "", RDFFormat.TRIG, new ParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true), SimpleValueFactory.getInstance(), new ParseErrorLogger(), SimpleValueFactory.getInstance().createIRI("http://whatever/myStats"));
            assertEqualModels(refM, statsM);
        }
    }
}
 
Example #7
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 #8
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());
	}
}
 
Example #9
Source File: JSONLDInternalTripleCallback.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public JSONLDInternalTripleCallback(RDFHandler nextHandler, ValueFactory vf) {
	this(nextHandler, vf, new ParserConfig(), new ParseErrorLogger(), nodeID -> vf.createBNode(nodeID),
			() -> vf.createBNode());
}
 
Example #10
Source File: Rio.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Adds RDF data from an {@link InputStream} to a {@link Model}, optionally to one or more named contexts.
 *
 * @param in         An InputStream from which RDF data can be read.
 * @param baseURI    The base URI to resolve any relative URIs that are in the data against.
 * @param dataFormat The serialization format of the data.
 * @param contexts   The contexts to add the data to. If one or more contexts are supplied the method ignores
 *                   contextual information in the actual data. If no contexts are supplied the contextual
 *                   information in the input stream is used, if no context information is available the data is
 *                   added without any context.
 * @return A {@link Model} containing the parsed statements.
 * @throws IOException                  If an I/O error occurred while reading from the input stream.
 * @throws UnsupportedRDFormatException If no {@link RDFParser} is available for the specified RDF format.
 * @throws RDFParseException            If an error was found while parsing the RDF data.
 */
public static Model parse(InputStream in, String baseURI, RDFFormat dataFormat, Resource... contexts)
		throws IOException, RDFParseException, UnsupportedRDFormatException {
	return parse(in, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(),
			new ParseErrorLogger(), contexts);
}
 
Example #11
Source File: Rio.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Adds RDF data from a {@link Reader} to a {@link Model}, optionally to one or more named contexts. <b>Note: using
 * a Reader to upload byte-based data means that you have to be careful not to destroy the data's character encoding
 * by enforcing a default character encoding upon the bytes. If possible, adding such data using an InputStream is
 * to be preferred.</b>
 *
 * @param reader     A Reader from which RDF data can be read.
 * @param baseURI    The base URI to resolve any relative URIs that are in the data against.
 * @param dataFormat The serialization format of the data.
 * @param contexts   The contexts to add the data to. If one or more contexts are specified the data is added to
 *                   these contexts, ignoring any context information in the data itself.
 * @return A {@link Model} containing the parsed statements.
 * @throws IOException                  If an I/O error occurred while reading from the reader.
 * @throws UnsupportedRDFormatException If no {@link RDFParser} is available for the specified RDF format.
 * @throws RDFParseException            If an error was found while parsing the RDF data.
 */
public static Model parse(Reader reader, String baseURI, RDFFormat dataFormat, Resource... contexts)
		throws IOException, RDFParseException, UnsupportedRDFormatException {
	return parse(reader, baseURI, dataFormat, new ParserConfig(), SimpleValueFactory.getInstance(),
			new ParseErrorLogger(), contexts);
}