<commit_before>package org.callimachusproject.engine;

import info.aduna.iteration.CloseableIteration;
import info.aduna.iteration.EmptyIteration;
import info.aduna.text.ASCIIUtil;

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.callimachusproject.engine.model.TermFactory;
import org.openrdf.model.BNode;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.model.vocabulary.XMLSchema;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.evaluation.QueryBindingSet;
import org.openrdf.query.algebra.evaluation.TripleSource;
import org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl;
import org.openrdf.query.parser.sparql.SPARQLParser;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.turtle.TurtleUtil;

public class ParameterizedQuery {
	private static final Pattern INFER = Pattern
			.compile(
					"(?:^|\n|\r).*#[ \t]*@infer[ \t]*(?::[ \t]*)?(true|false)[ \t]*(?:$|\n|\r)",
					Pattern.CASE_INSENSITIVE);
	private static final Pattern CACHE_CONTROL = Pattern
			.compile(
					"(?:^|\n|\r).*#[ \t]*@Cache-Control[ \t]*(?::[ \t]*)?([\\w ,=\\-\"]+)[ \t]*(?:$|\n|\r)",
					Pattern.CASE_INSENSITIVE);
	private static final Pattern VIEW_TEMPLATE = Pattern
			.compile(
					"(?:^|\n|\r).*#[ \t]*@view[ \t]*(?::[ \t]*)?([^\\s'\"<>]+)[ \t]*(?:$|\n|\r)",
					Pattern.CASE_INSENSITIVE);
	private static final Pattern PARAM_EXPRESSION = Pattern
			.compile("(?<!\\\\)\\$\\{([^}]*)\\}");
	private static ValueFactory vf = ValueFactoryImpl.getInstance();
	private final String sparql;
	private final String systemId;
	private final Map<String, String> prefixes;
	private final List<String> bindingNames;
	private final Map<String, Value> bindings;
	private final TermFactory tf;

	ParameterizedQuery(String sparql, String systemId,
			Map<String, String> prefixes, Map<String, Value> bindings) {
		assert sparql != null;
		assert systemId != null;
		assert bindings != null;
		this.sparql = sparql;
		this.systemId = systemId;
		this.prefixes = prefixes;
		this.bindingNames = new ArrayList<String>(bindings.keySet());
		this.bindings = bindings;
		this.tf = TermFactory.newInstance(systemId);
	}

	public String toString() {
		return sparql;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + sparql.hashCode();
		result = prime * result + systemId.hashCode();
		result = prime * result + bindings.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParameterizedQuery other = (ParameterizedQuery) obj;
		if (!sparql.equals(other.sparql))
			return false;
		if (!systemId.equals(other.systemId))
			return false;
		if (!bindings.equals(other.bindings))
			return false;
		return true;
	}

	public boolean isParameterPresent() {
		return !bindings.isEmpty();
	}

	public Boolean getIncludeInferred() {
		Matcher matcher = INFER.matcher(sparql);
		if (matcher.find())
			return Boolean.valueOf(matcher.group(1));
		return null;
	}

	public String getCacheControl() {
		StringBuilder sb = new StringBuilder();
		Matcher matcher = CACHE_CONTROL.matcher(sparql);
		while (matcher.find()) {
			if (sb.length() > 0) {
				sb.append(",");
			}
			sb.append(matcher.group(1));
		}
		if (sb.length() == 0)
			return null;
		return sb.toString();
	}

	public String getViewTemplate() {
		Matcher matcher = VIEW_TEMPLATE.matcher(sparql);
		if (matcher.find())
			return tf.resolve(matcher.group(1));
		return null;
	}

	public String prepare() throws IllegalArgumentException {
		Map<String, ?> parameters = Collections.emptyMap();
		return prepare(parameters);
	}
<commit_msg>Check for empty cache-control header<commit_after>package org.callimachusproject.engine;

import info.aduna.iteration.CloseableIteration;
import info.aduna.iteration.EmptyIteration;
import info.aduna.text.ASCIIUtil;

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.callimachusproject.engine.model.TermFactory;
import org.openrdf.model.BNode;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.model.vocabulary.XMLSchema;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.algebra.TupleExpr;
import org.openrdf.query.algebra.evaluation.QueryBindingSet;
import org.openrdf.query.algebra.evaluation.TripleSource;
import org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl;
import org.openrdf.query.parser.sparql.SPARQLParser;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.rio.turtle.TurtleUtil;

public class ParameterizedQuery {
	private static final Pattern INFER = Pattern
			.compile(
					"(?:^|\n|\r).*#[ \t]*@infer[ \t]*(?::[ \t]*)?(true|false)[ \t]*(?:$|\n|\r)",
					Pattern.CASE_INSENSITIVE);
	private static final Pattern CACHE_CONTROL = Pattern
			.compile(
					"(?:^|\n|\r).*#[ \t]*@Cache-Control[ \t]*(?::[ \t]*)?(\\w[\\w ,=\\-\"]+)[ \t]*(?:$|\n|\r)",
					Pattern.CASE_INSENSITIVE);
	private static final Pattern VIEW_TEMPLATE = Pattern
			.compile(
					"(?:^|\n|\r).*#[ \t]*@view[ \t]*(?::[ \t]*)?([^\\s'\"<>]+)[ \t]*(?:$|\n|\r)",
					Pattern.CASE_INSENSITIVE);
	private static final Pattern PARAM_EXPRESSION = Pattern
			.compile("(?<!\\\\)\\$\\{([^}]*)\\}");
	private static ValueFactory vf = ValueFactoryImpl.getInstance();
	private final String sparql;
	private final String systemId;
	private final Map<String, String> prefixes;
	private final List<String> bindingNames;
	private final Map<String, Value> bindings;
	private final TermFactory tf;

	ParameterizedQuery(String sparql, String systemId,
			Map<String, String> prefixes, Map<String, Value> bindings) {
		assert sparql != null;
		assert systemId != null;
		assert bindings != null;
		this.sparql = sparql;
		this.systemId = systemId;
		this.prefixes = prefixes;
		this.bindingNames = new ArrayList<String>(bindings.keySet());
		this.bindings = bindings;
		this.tf = TermFactory.newInstance(systemId);
	}

	public String toString() {
		return sparql;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + sparql.hashCode();
		result = prime * result + systemId.hashCode();
		result = prime * result + bindings.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParameterizedQuery other = (ParameterizedQuery) obj;
		if (!sparql.equals(other.sparql))
			return false;
		if (!systemId.equals(other.systemId))
			return false;
		if (!bindings.equals(other.bindings))
			return false;
		return true;
	}

	public boolean isParameterPresent() {
		return !bindings.isEmpty();
	}

	public Boolean getIncludeInferred() {
		Matcher matcher = INFER.matcher(sparql);
		if (matcher.find())
			return Boolean.valueOf(matcher.group(1));
		return null;
	}

	public String getCacheControl() {
		StringBuilder sb = new StringBuilder();
		Matcher matcher = CACHE_CONTROL.matcher(sparql);
		while (matcher.find()) {
			if (sb.length() > 0) {
				sb.append(",");
			}
			sb.append(matcher.group(1));
		}
		if (sb.length() == 0)
			return null;
		String control = sb.toString().trim();
		if (control.length() == 0)
			return null;
		return control;
	}

	public String getViewTemplate() {
		Matcher matcher = VIEW_TEMPLATE.matcher(sparql);
		if (matcher.find())
			return tf.resolve(matcher.group(1));
		return null;
	}

	public String prepare() throws IllegalArgumentException {
		Map<String, ?> parameters = Collections.emptyMap();
		return prepare(parameters);
	}

{'fraction_non_alphanumeric': 0.1478530323151837, 'fraction_numerical': 0.0018813634351482957, 'mean_word_length': 4.6235220908525205, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3953044', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3026, 'n_words': 642}