use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.
the class AbstractHTTPQuery method getBindingsArray.
public Binding[] getBindingsArray() {
BindingSet bindings = this.getBindings();
Binding[] bindingsArray = new Binding[bindings.size()];
Iterator<Binding> iter = bindings.iterator();
for (int i = 0; i < bindings.size(); i++) {
bindingsArray[i] = iter.next();
}
return bindingsArray;
}
use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.
the class SPARQLConnection method toStatementIteration.
/**
* Converts a {@link TupleQueryResult} resulting from the {@link #EVERYTHING_WITH_GRAPH} to a statement by
* using the respective values from the {@link BindingSet} or (if provided) the ones from the arguments.
*
* @param iter
* the {@link TupleQueryResult}
* @param subj
* the subject {@link Resource} used as input or <code>null</code> if wildcard was used
* @param pred
* the predicate {@link IRI} used as input or <code>null</code> if wildcard was used
* @param obj
* the object {@link Value} used as input or <code>null</code> if wildcard was used
* @return the converted iteration
*/
protected Iteration<Statement, QueryEvaluationException> toStatementIteration(TupleQueryResult iter, final Resource subj, final IRI pred, final Value obj) {
return new ConvertingIteration<BindingSet, Statement, QueryEvaluationException>(iter) {
@Override
protected Statement convert(BindingSet b) throws QueryEvaluationException {
Resource s = subj == null ? (Resource) b.getValue("s") : subj;
IRI p = pred == null ? (IRI) b.getValue("p") : pred;
Value o = obj == null ? b.getValue("o") : obj;
Resource ctx = (Resource) b.getValue("ctx");
return SimpleValueFactory.getInstance().createStatement(s, p, o, ctx);
}
};
}
use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.
the class SPARQLConnection method getContextIDs.
public RepositoryResult<Resource> getContextIDs() throws RepositoryException {
TupleQueryResult iter = null;
RepositoryResult<Resource> result = null;
boolean allGood = false;
try {
TupleQuery query = prepareTupleQuery(SPARQL, NAMEDGRAPHS, "");
iter = query.evaluate();
result = new RepositoryResult<Resource>(new ExceptionConvertingIteration<Resource, RepositoryException>(new ConvertingIteration<BindingSet, Resource, QueryEvaluationException>(iter) {
@Override
protected Resource convert(BindingSet bindings) throws QueryEvaluationException {
return (Resource) bindings.getValue("_");
}
}) {
@Override
protected RepositoryException convert(Exception e) {
return new RepositoryException(e);
}
});
allGood = true;
return result;
} catch (MalformedQueryException e) {
throw new RepositoryException(e);
} catch (QueryEvaluationException e) {
throw new RepositoryException(e);
} finally {
if (!allGood) {
try {
if (result != null) {
result.close();
}
} finally {
if (iter != null) {
iter.close();
}
}
}
}
}
use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.
the class SPARQLResultsTSVParser method parse.
@Override
public void parse(InputStream in) throws IOException, QueryResultParseException, TupleQueryResultHandlerException {
InputStreamReader r = new InputStreamReader(in, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(r);
List<String> bindingNames = null;
String nextLine;
while ((nextLine = reader.readLine()) != null) {
if (bindingNames == null) {
// header is mandatory in SPARQL TSV
String[] names = nextLine.split("\t", -1);
bindingNames = new ArrayList<String>(names.length);
for (String name : names) {
// strip the '?' prefix
if ('?' == name.charAt(0)) {
bindingNames.add(name.substring(1));
} else {
bindingNames.add(name);
}
}
if (handler != null) {
handler.startQueryResult(bindingNames);
}
} else {
// process solution
String[] lineTokens = nextLine.split("\t", -1);
List<Value> values = new ArrayList<Value>();
for (String valueString : lineTokens) {
Value v = null;
if (valueString.startsWith("_:")) {
v = valueFactory.createBNode(valueString.substring(2));
} else if (valueString.startsWith("<") && valueString.endsWith(">")) {
try {
v = valueFactory.createIRI(valueString.substring(1, valueString.length() - 1));
} catch (IllegalArgumentException e) {
v = valueFactory.createLiteral(valueString);
}
} else if (valueString.startsWith("\"")) {
v = parseLiteral(valueString);
} else if (!"".equals(valueString)) {
if (valueString.matches("^[\\+\\-]?[\\d\\.].*")) {
IRI datatype = null;
if (XMLDatatypeUtil.isValidInteger(valueString)) {
if (XMLDatatypeUtil.isValidNegativeInteger(valueString)) {
datatype = XMLSchema.NEGATIVE_INTEGER;
} else {
datatype = XMLSchema.INTEGER;
}
} else if (XMLDatatypeUtil.isValidDecimal(valueString)) {
datatype = XMLSchema.DECIMAL;
} else if (XMLDatatypeUtil.isValidDouble(valueString)) {
datatype = XMLSchema.DOUBLE;
}
if (datatype != null) {
v = valueFactory.createLiteral(valueString, datatype);
} else {
v = valueFactory.createLiteral(valueString);
}
} else {
v = valueFactory.createLiteral(valueString);
}
}
values.add(v);
}
BindingSet bindingSet = new ListBindingSet(bindingNames, values.toArray(new Value[values.size()]));
if (handler != null) {
handler.handleSolution(bindingSet);
}
}
}
if (bindingNames != null && handler != null) {
handler.endQueryResult();
}
}
use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.
the class SPARQLResultsCSVParser method parse.
@Override
public void parse(InputStream in) throws IOException, QueryResultParseException, TupleQueryResultHandlerException {
CSVReader reader = new CSVReader(new InputStreamReader(in, Charset.forName("UTF-8")));
List<String> bindingNames = null;
String[] nextLine;
try {
while ((nextLine = reader.readNext()) != null) {
if (bindingNames == null) {
// header is mandatory in SPARQL CSV
bindingNames = Arrays.asList(nextLine);
if (handler != null) {
handler.startQueryResult(bindingNames);
}
} else {
// process solution
List<Value> values = new ArrayList<Value>();
for (String valueString : nextLine) {
Value v = null;
if (valueString.startsWith("_:")) {
v = valueFactory.createBNode(valueString.substring(2));
} else if (!"".equals(valueString)) {
if (valueString.matches("^[\\+\\-]?[\\d\\.].*")) {
IRI datatype = null;
if (XMLDatatypeUtil.isValidInteger(valueString)) {
if (XMLDatatypeUtil.isValidNegativeInteger(valueString)) {
datatype = XMLSchema.NEGATIVE_INTEGER;
} else {
datatype = XMLSchema.INTEGER;
}
} else if (XMLDatatypeUtil.isValidDecimal(valueString)) {
datatype = XMLSchema.DECIMAL;
} else if (XMLDatatypeUtil.isValidDouble(valueString)) {
datatype = XMLSchema.DOUBLE;
}
if (datatype != null) {
v = valueFactory.createLiteral(valueString, datatype);
} else {
v = valueFactory.createLiteral(valueString);
}
} else {
try {
v = valueFactory.createIRI(valueString);
} catch (IllegalArgumentException e) {
v = valueFactory.createLiteral(valueString);
}
}
}
values.add(v);
}
BindingSet bindingSet = new ListBindingSet(bindingNames, values.toArray(new Value[values.size()]));
if (handler != null) {
handler.handleSolution(bindingSet);
}
}
}
if (bindingNames != null && handler != null) {
handler.endQueryResult();
}
} finally {
reader.close();
}
}
Aggregations