use of com.bigdata.bop.IConstant in project wikidata-query-rdf by wikimedia.
the class MWApiServiceCall method parseResponse.
/**
* Parse XML response from WM API.
*
* @param responseStream Response body as stream
* @param binding Current binding set.
* @return Set of resulting bindings, or null if none found.
* @throws SAXException on error
* @throws IOException on error
* @throws XPathExpressionException on error
*/
public ResultWithContinue parseResponse(InputStream responseStream, IBindingSet binding) throws SAXException, IOException, XPathExpressionException {
if (outputVars.isEmpty()) {
return null;
}
Document doc = docBuilder.get().parse(responseStream);
XPath path = xpath.get();
ImmutableMap<String, String> searchContinue = parseContinue(doc, path);
// FIXME: we're re-compiling it each time. Should probably do it only
// once per template.
// Note though that XPathExpression is not thread-safe. Maybe use ThreadLocal?
XPathExpression itemsXPath = path.compile(template.getItemsPath());
NodeList nodes = (NodeList) itemsXPath.evaluate(doc, XPathConstants.NODESET);
if (nodes.getLength() == 0) {
return null;
}
IBindingSet[] results = new IBindingSet[nodes.getLength()];
final Map<OutputVariable, XPathExpression> compiledVars = new HashMap<>();
// Thanks, Oracle!
for (OutputVariable var : outputVars) {
compiledVars.put(var, xpath.get().compile(var.getPath()));
}
for (int i = 0; i < nodes.getLength(); i++) {
final Node node = nodes.item(i);
results[i] = binding.copy(null);
for (Map.Entry<OutputVariable, XPathExpression> var : compiledVars.entrySet()) {
final IConstant constant;
if (var.getKey().isOrdinal()) {
constant = makeConstant(lexiconRelation.getValueFactory(), i);
results[i].set(var.getKey().getVar(), constant);
continue;
}
final Node value = (Node) var.getValue().evaluate(node, XPathConstants.NODE);
if (value != null && value.getNodeValue() != null) {
if (var.getKey().isURI()) {
constant = makeConstant(lexiconRelation.getValueFactory(), var.getKey().getURI(value.getNodeValue()));
} else {
constant = makeConstant(lexiconRelation.getValueFactory(), value.getNodeValue());
}
results[i].set(var.getKey().getVar(), constant);
}
}
}
return new ResultWithContinue(results, searchContinue);
}
Aggregations