<commit_before>/*******************************************************************************
 * Copyright (c) 2021 Eclipse RDF4J contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *******************************************************************************/

package org.eclipse.rdf4j.spring.support;

import java.lang.invoke.MethodHandles;
import java.util.Objects;
import java.util.Set;

import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.spring.dao.exception.RDF4JSpringException;
import org.eclipse.rdf4j.spring.support.connectionfactory.RepositoryConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @since 4.0.0
 * @author <NAME>
 */
@Component
public class DataInserter {

	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	@Autowired
	RepositoryConnectionFactory connectionFactory;

	@Transactional(propagation = Propagation.REQUIRED)
	public void insertData(Resource dataFile) {
		Objects.requireNonNull(dataFile);
		logger.debug("Loading data from {}...", dataFile);
		try {
			RepositoryConnection con = connectionFactory.getConnection();
			RDFFormat fmt = RDFFormat.matchFileName(
					dataFile.getFilename(),
					Set.of(
							RDFFormat.TURTLE,
							RDFFormat.TRIG,
							RDFFormat.NTRIPLES,
							RDFFormat.NQUADS))
					.orElseThrow(
							() -> new IllegalArgumentException(
									"Failed to determine file format of input file "
											+ dataFile));
			con.add(dataFile.getInputStream(), "", fmt);
		} catch (Exception e) {
			throw new RDF4JSpringException("Unable to load test data", e);
		}
		logger.debug("\tdone loading data");
	}
}<commit_msg>GH-3665: Improve data format recognition<commit_after>/*******************************************************************************
 * Copyright (c) 2021 Eclipse RDF4J contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *******************************************************************************/

package org.eclipse.rdf4j.spring.support;

import java.lang.invoke.MethodHandles;
import java.util.Objects;
import java.util.Set;

import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.spring.dao.exception.RDF4JSpringException;
import org.eclipse.rdf4j.spring.support.connectionfactory.RepositoryConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @since 4.0.0
 * @author <NAME>
 */
@Component
public class DataInserter {

	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	@Autowired
	RepositoryConnectionFactory connectionFactory;

	@Transactional(propagation = Propagation.REQUIRED)
	public void insertData(Resource dataFile) {
		Objects.requireNonNull(dataFile);
		logger.debug("Loading data from {}...", dataFile);
		try {
			RepositoryConnection con = connectionFactory.getConnection();
			RDFFormat fmt = Rio.getParserFormatForFileName(dataFile.getFilename())
					.orElseThrow(
							() -> new IllegalArgumentException(
									"Failed to determine file format of input file "
											+ dataFile));
			con.add(dataFile.getInputStream(), "", fmt);
		} catch (Exception e) {
			throw new RDF4JSpringException("Unable to load test data", e);
		}
		logger.debug("\tdone loading data");
	}
}
{'fraction_non_alphanumeric': 0.16764044943820225, 'fraction_numerical': 0.010561797752808988, 'mean_word_length': 5.225174825174825, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5401071', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1208, 'n_words': 272}