org.eclipse.rdf4j.rio.RDFParseException Java Examples

The following examples show how to use org.eclipse.rdf4j.rio.RDFParseException. 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: NTriplesParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Verifies that there is only whitespace or comments until the end of the line.
 */
protected int assertLineTerminates(int c) throws IOException, RDFParseException {
	c = readCodePoint();

	c = skipWhitespace(c);

	if (c == '#') {
		// c = skipToEndOfLine(c);
	} else {
		if (c != -1 && c != '\r' && c != '\n') {
			reportFatalError("Content after '.' is not allowed");
		}
	}

	return c;
}
 
Example #2
Source File: SPARQLServiceEvaluationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Load a dataset. Note: the repositories are cleared before loading data
 *
 * @param rep
 * @param datasetFile
 * @throws RDFParseException
 * @throws RepositoryException
 * @throws IOException
 */
protected void loadDataSet(Repository rep, String datasetFile)
		throws RDFParseException, RepositoryException, IOException {
	logger.debug("loading dataset...");
	InputStream dataset = SPARQLServiceEvaluationTest.class.getResourceAsStream(datasetFile);

	if (dataset == null) {
		throw new IllegalArgumentException("Datasetfile " + datasetFile + " not found.");
	}

	RepositoryConnection con = rep.getConnection();
	try {
		con.clear();
		con.add(dataset, "",
				Rio.getParserFormatForFileName(datasetFile).orElseThrow(Rio.unsupportedFormat(datasetFile)));
	} finally {
		dataset.close();
		con.close();
	}
	logger.debug("dataset loaded.");
}
 
Example #3
Source File: MemTripleSourceTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void loadTestData(String dataFile, Resource... contexts)
		throws RDFParseException, IOException, SailException {
	logger.debug("loading dataset {}", dataFile);
	InputStream dataset = this.getClass().getResourceAsStream(dataFile);
	SailConnection con = store.getConnection();
	try {
		con.begin();
		for (Statement nextStatement : Rio.parse(dataset, "", RDFFormat.TURTLE, contexts)) {
			con.addStatement(nextStatement.getSubject(), nextStatement.getPredicate(), nextStatement.getObject(),
					nextStatement.getContext());
		}
	} finally {
		con.commit();
		con.close();
		dataset.close();
	}
	logger.debug("dataset loaded.");
}
 
Example #4
Source File: RdfConverterTest.java    From Wikidata-Toolkit with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteSiteLinks() throws RDFHandlerException, IOException,
		RDFParseException {
	this.sites.setSiteInformation("enwiki", "wikipedia", "en", "mediawiki",
			"http://en.wikipedia.org/w/$1",
			"http://en.wikipedia.org/wiki/$1");
	this.sites.setSiteInformation("dewiki", "wikipedia", "de", "mediawiki",
			"http://de.wikipedia.org/w/$1",
			"http://de.wikipedia.org/wiki/$1");
	Map<String, SiteLink> siteLinks = objectFactory.createSiteLinks();
	this.rdfConverter.writeSiteLinks(this.resource, siteLinks);
	this.rdfWriter.finish();
	Model model = RdfTestHelpers.parseRdf(out.toString());
	assertEquals(model, RdfTestHelpers.parseRdf(RdfTestHelpers
			.getResourceFromFile("SiteLinks.rdf")));

}
 
Example #5
Source File: NTriplesParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected int parsePredicate(int c) throws IOException, RDFParseException {
	StringBuilder sb = new StringBuilder(100);

	// predicate must be an uriref (<foo://bar>)
	if (c == '<') {
		// predicate is an uriref
		c = parseUriRef(c, sb);
		predicate = createURI(sb.toString());
	} else if (c == -1) {
		throwEOFException();
	} else {
		throw new RDFParseException("Expected '<', found: " + new String(Character.toChars(c)), lineNo, c);
	}

	return c;
}
 
Example #6
Source File: AbstractNQuadsParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Tests N-Quads parsing with literal and datatype.
 */
@Test
public void testParseBasicLiteralDatatype() throws RDFHandlerException, IOException, RDFParseException {
	final ByteArrayInputStream bais = new ByteArrayInputStream(
			("<http://www.v/dat/4b2-21> " + "<http://www.w3.org/20/ica#dtend> "
					+ "\"2010\"^^<http://www.w3.org/2001/XMLSchema#integer> " + "<http://sin.siteserv.org/def/>.")
							.getBytes());
	final TestRDFHandler rdfHandler = new TestRDFHandler();
	parser.setRDFHandler(rdfHandler);
	parser.parse(bais, "http://test.base.uri");
	final Statement statement = rdfHandler.getStatements().iterator().next();
	Assert.assertEquals("http://www.v/dat/4b2-21", statement.getSubject().stringValue());
	Assert.assertEquals("http://www.w3.org/20/ica#dtend", statement.getPredicate().stringValue());
	Assert.assertTrue(statement.getObject() instanceof Literal);
	Literal object = (Literal) statement.getObject();
	Assert.assertEquals("2010", object.stringValue());
	Assert.assertFalse(object.getLanguage().isPresent());
	Assert.assertEquals("http://www.w3.org/2001/XMLSchema#integer", object.getDatatype().toString());
	Assert.assertEquals("http://sin.siteserv.org/def/", statement.getContext().stringValue());
}
 
Example #7
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 #8
Source File: AbstractNQuadsParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Tests N-Quads parsing with literal and datatype using a prefix, which is illegal in NQuads, but legal in
 * N3/Turtle that may otherwise look like NQuads
 */
@Test
public void testParseBasicLiteralDatatypePrefix() throws RDFHandlerException, IOException {

	final ByteArrayInputStream bais = new ByteArrayInputStream(
			("<http://www.v/dat/4b2-21> " + "<http://www.w3.org/20/ica#dtend> " + "\"2010\"^^xsd:integer "
					+ "<http://sin.siteserv.org/def/>.").getBytes());
	final TestRDFHandler rdfHandler = new TestRDFHandler();
	parser.setRDFHandler(rdfHandler);
	try {
		parser.parse(bais, "http://test.base.uri");
		Assert.fail("Expected exception when passing in a datatype using an N3 style prefix");
	} catch (RDFParseException rdfpe) {
		Assert.assertEquals(1, rdfpe.getLineNumber());
		// FIXME: Enable column numbers when parser supports them
		// Assert.assertEquals(69, rdfpe.getColumnNumber());
	}
}
 
Example #9
Source File: SailUpdateExecutor.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @param insertDataExpr
 * @param uc
 * @throws SailException
 */
protected void executeInsertData(InsertData insertDataExpr, UpdateContext uc, int maxExecutionTime)
		throws SailException {

	SPARQLUpdateDataBlockParser parser = new SPARQLUpdateDataBlockParser(vf);
	RDFHandler handler = new RDFSailInserter(con, vf, uc);
	if (maxExecutionTime > 0) {
		handler = new TimeLimitRDFHandler(handler, 1000L * maxExecutionTime);
	}
	parser.setRDFHandler(handler);
	parser.setLineNumberOffset(insertDataExpr.getLineNumberOffset());
	parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_DATATYPE_VALUES);
	parser.getParserConfig().addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES);
	parser.getParserConfig().set(BasicParserSettings.SKOLEMIZE_ORIGIN, null);
	try {
		// TODO process update context somehow? dataset, base URI, etc.
		parser.parse(new StringReader(insertDataExpr.getDataBlock()), "");
	} catch (RDFParseException | RDFHandlerException | IOException e) {
		throw new SailException(e);
	}
}
 
Example #10
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 #11
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 #12
Source File: HTTPRepositoryConnection.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void add(File file, String baseURI, RDFFormat dataFormat, Resource... contexts)
		throws IOException, RDFParseException, RepositoryException {
	if (baseURI == null) {
		// default baseURI to file
		baseURI = file.toURI().toString();
	}
	if (dataFormat == null) {
		dataFormat = Rio.getParserFormatForFileName(file.getName())
				.orElseThrow(Rio.unsupportedFormat(file.getName()));
	}

	try (InputStream in = new FileInputStream(file)) {
		add(in, baseURI, dataFormat, contexts);
	}
}
 
Example #13
Source File: TriGStarParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTripleInDatatype() throws IOException {
	String data = "@prefix ex: <http://example.com/>.\ngraph ex:g { ex:Example ex:p \"foo\"^^<<<urn:a><urn:b><urn:c>>> }";
	try (Reader r = new StringReader(data)) {
		parser.parse(r, baseURI);
		fail("Must fail with RDFParseException");
	} catch (RDFParseException e) {
		assertEquals("Illegal datatype value: <<urn:a urn:b urn:c>> [line 2]", e.getMessage());
	}
}
 
Example #14
Source File: NQuadsParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public synchronized void parse(final InputStream inputStream, final String baseURI)
		throws IOException, RDFParseException, RDFHandlerException {
	if (inputStream == null) {
		throw new IllegalArgumentException("Input stream can not be 'null'");
	}
	// Note: baseURI will be checked in parse(Reader, String)

	try {
		parse(new InputStreamReader(new BOMInputStream(inputStream, false), StandardCharsets.UTF_8), baseURI);
	} catch (UnsupportedEncodingException e) {
		// Every platform should support the UTF-8 encoding...
		throw new RuntimeException(e);
	}
}
 
Example #15
Source File: RDF4JProtocolSession.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void upload(InputStream contents, String baseURI, RDFFormat dataFormat, boolean overwrite,
		boolean preserveNodeIds, Action action, Resource... contexts)
		throws IOException, RDFParseException, RepositoryException, UnauthorizedException {
	// Set Content-Length to -1 as we don't know it and we also don't want to
	// cache
	HttpEntity entity = new InputStreamEntity(contents, -1, ContentType.parse(dataFormat.getDefaultMIMEType()));
	upload(entity, baseURI, overwrite, preserveNodeIds, action, contexts);
}
 
Example #16
Source File: RDFXMLParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Retrieves the resource of a node element (subject or object) using relevant attributes (rdf:ID, rdf:about and
 * rdf:nodeID) from its attributes list.
 *
 * @return a resource or a bNode.
 */
private Resource getNodeResource(Atts atts) throws RDFParseException {
	Att id = atts.removeAtt(RDF.NAMESPACE, "ID");
	Att about = atts.removeAtt(RDF.NAMESPACE, "about");
	Att nodeID = atts.removeAtt(RDF.NAMESPACE, "nodeID");

	if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
		int definedAttsCount = 0;

		if (id != null) {
			definedAttsCount++;
		}
		if (about != null) {
			definedAttsCount++;
		}
		if (nodeID != null) {
			definedAttsCount++;
		}

		if (definedAttsCount > 1) {
			reportError("Only one of the attributes rdf:ID, rdf:about or rdf:nodeID can be used here",
					XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
		}
	}

	Resource result = null;

	if (id != null) {
		result = buildURIFromID(id.getValue());
	} else if (about != null) {
		result = resolveURI(about.getValue());
	} else if (nodeID != null) {
		result = createNode(nodeID.getValue());
	} else {
		// No resource specified, generate a bNode
		result = createNode();
	}

	return result;
}
 
Example #17
Source File: AbstractNTriplesParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = RDFParseException.class)
public void testBlankNodeIdentifiersWithOtherCharactersAsFirstCharacter() throws Exception {
	// The characters -, U+00B7, U+0300 to U+036F and U+203F to U+2040 are permitted anywhere except the first
	// character.
	List<Character> charactersList = new ArrayList<>();
	charactersList.add('-');
	charactersList.add('\u00B7');
	charactersList.add('\u0300');
	charactersList.add('\u036F');
	charactersList.add('\u0301');
	charactersList.add('\u203F');

	for (Character character : charactersList) {
		RDFParser ntriplesParser = new NTriplesParser();
		Model model = new LinkedHashModel();
		ntriplesParser.setRDFHandler(new StatementCollector(model));

		try {
			ntriplesParser.parse(
					new StringReader("<urn:test:subject> <urn:test:predicate> _:" + character + "1 . "),
					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 '" + character
				+ "' at the begining of the bnode label");
	}
}
 
Example #18
Source File: RDFXMLParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Overrides {@link AbstractRDFParser#reportError(String, RioSetting)}, adding line- and column number information
 * to the error.
 */
@Override
protected void reportError(Exception e, RioSetting<Boolean> setting) throws RDFParseException {
	Locator locator = saxFilter.getLocator();
	if (locator != null) {
		reportError(e, locator.getLineNumber(), locator.getColumnNumber(), setting);
	} else {
		reportError(e, -1, -1, setting);
	}
}
 
Example #19
Source File: RDFXMLParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected Literal createLiteral(String label, String lang, IRI datatype) throws RDFParseException {
	Locator locator = saxFilter.getLocator();
	if (locator != null) {
		return createLiteral(label, lang, datatype, locator.getLineNumber(), locator.getColumnNumber());
	} else {
		return createLiteral(label, lang, datatype, -1, -1);
	}
}
 
Example #20
Source File: AbstractNQuadsParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Tests the behaviour with non-whitespace characters after a period character without a context.
 *
 * @throws RDFHandlerException
 * @throws IOException
 * @throws RDFParseException
 */
@Test
public void testNonWhitespaceAfterPeriodWithContext() throws RDFHandlerException, IOException, RDFParseException {
	final ByteArrayInputStream bais = new ByteArrayInputStream(
			"<http://www.wrong.com> <http://wrong.com/1.1/tt> \"x\"^^<http://xxx.net/int> <http://path.to.graph> . <thisisnotlegal> "
					.getBytes());
	try {
		parser.parse(bais, "http://base-uri");
		Assert.fail("Expected exception when there is non-whitespace characters after a period.");
	} catch (RDFParseException rdfpe) {
		Assert.assertEquals(1, rdfpe.getLineNumber());
		// FIXME: Enable column numbers when parser supports them
		// Assert.assertEquals(44, rdfpe.getColumnNumber());
	}
}
 
Example #21
Source File: TurtlePrettyWriterTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void testRoundTripWithoutXSDString() throws RDFHandlerException, IOException, RDFParseException {
	try {
		inlineBlankNodes = false;
		super.testRoundTripWithoutXSDString();
	} finally {
		inlineBlankNodes = true;
	}
}
 
Example #22
Source File: TurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testOverflowingUnicodeInTripleSubject() throws IOException {
	String data = "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n<r:\uD800\uDF32\uD800\uDF3F\uD800\uDF44\uD800\uDF39\uD800\uDF43\uD800\uDF3A> a xsd:string .";
	Reader r = new StringReader(data);

	try {
		parser.parse(r, baseURI);
		assertTrue(statementCollector.getStatements().size() == 1);
	} catch (RDFParseException e) {
		fail("Complex unicode characters should be parsed correctly (" + e.getMessage() + ")");
	}
}
 
Example #23
Source File: JSONLDParserCustomTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testAllowYamlCommentsDisabled() throws Exception {
	thrown.expect(RDFParseException.class);
	thrown.expectMessage("Could not parse JSONLD");
	parser.set(JSONSettings.ALLOW_YAML_COMMENTS, false);
	parser.parse(new StringReader(YAML_COMMENTS_TEST_STRING), "");
}
 
Example #24
Source File: TurtleParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testParseIllegalDatatypeValueFatalIRI() throws IOException {
	String data = " <urn:foo_bar> <urn:foo> \"a\"^^\"b\" ; <urn:foo2> <urn:bar2> . <urn:foobar> <urn:food> <urn:barf> . ";

	try {
		parser.parse(new StringReader(data), baseURI);
		fail("default config should result in fatal error / parse exception");
	} catch (RDFParseException e) {
		// expected
	}
}
 
Example #25
Source File: TriXParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Implementation of SAX ErrorHandler.error
 */
@Override
public void error(SAXParseException exception) throws SAXException {
	try {
		this.reportError(exception.getMessage(), XMLParserSettings.FAIL_ON_SAX_NON_FATAL_ERRORS);
	} catch (RDFParseException rdfpe) {
		throw new SAXException(rdfpe);
	}
}
 
Example #26
Source File: TurtleStarParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testTripleInPredicate() throws IOException {
	String data = "@prefix ex: <http://example.com/>.\nex:Example << <urn:a> <urn:b> <urn:c> >> \"foo\" .";
	try (Reader r = new StringReader(data)) {
		parser.parse(r, baseURI);
		fail("Must fail with RDFParseException");
	} catch (RDFParseException e) {
		assertEquals("Illegal predicate value: <<urn:a urn:b urn:c>> [line 2]", e.getMessage());
	}
}
 
Example #27
Source File: AbstractNQuadsParserUnitTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testStatementWithInvalidLiteralContentAndIgnoreValidation()
		throws RDFHandlerException, IOException, RDFParseException {
	// Note: Float declare as int.
	final ByteArrayInputStream bais = new ByteArrayInputStream(
			("<http://dbpedia.org/resource/Camillo_Benso,_conte_di_Cavour> "
					+ "<http://dbpedia.org/property/mandatofine> "
					+ "\"1380.0\"^^<http://www.w3.org/2001/XMLSchema#int> "
					+ "<http://it.wikipedia.org/wiki/Camillo_Benso,_conte_di_Cavour#absolute-line=20> .")
							.getBytes());
	parser.getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, false);
	parser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, false);
	parser.parse(bais, "http://base-uri");
}
 
Example #28
Source File: AccumuloRyaSailFactoryLoadFilesIT.java    From rya with Apache License 2.0 5 votes vote down vote up
private static void addTriples(final SailRepository repo, final InputStream triplesStream, final RDFFormat rdfFormat) throws RDFParseException, RepositoryException, IOException {
    SailRepositoryConnection conn = null;
    try {
        conn = repo.getConnection();
        conn.begin();
        conn.add(triplesStream, "", rdfFormat);
        conn.commit();
    } finally {
        closeQuietly(conn);
    }
}
 
Example #29
Source File: DatasetRepository.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private synchronized void load(URL url, URLConnection urlCon, IRI context, ParserConfig config)
		throws RepositoryException, RDFParseException, IOException {
	long modified = urlCon.getLastModified();
	if (lastModified.containsKey(url) && lastModified.get(url) >= modified) {
		return;
	}

	// Try to determine the data's MIME type
	String mimeType = urlCon.getContentType();
	int semiColonIdx = mimeType.indexOf(';');
	if (semiColonIdx >= 0) {
		mimeType = mimeType.substring(0, semiColonIdx);
	}
	RDFFormat format = Rio.getParserFormatForMIMEType(mimeType)
			.orElse(Rio.getParserFormatForFileName(url.getPath()).orElseThrow(Rio.unsupportedFormat(mimeType)));

	try (InputStream stream = urlCon.getInputStream()) {
		try (RepositoryConnection repCon = super.getConnection()) {

			repCon.setParserConfig(config);
			repCon.begin();
			repCon.clear(context);
			repCon.add(stream, url.toExternalForm(), format, context);
			repCon.commit();
			lastModified.put(url, modified);
		}
	}
}
 
Example #30
Source File: RdfSerializerTest.java    From Wikidata-Toolkit with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerialization() throws RDFParseException,
		RDFHandlerException, IOException {
	this.rdfSerializer.open();
	this.rdfSerializer.processItemDocument(this.objectFactory
			.createItemDocument());
	this.rdfSerializer.close();
	Model model = RdfTestHelpers.parseRdf(this.out.toString());
	assertEquals(RdfTestHelpers.parseRdf(RdfTestHelpers
			.getResourceFromFile("completeRDFDocument.rdf")), model);
}