use of com.bigdata.bop.IBindingSet 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.
* @param recordsCount Count of records processed up to this batch
* @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, int recordsCount) throws SAXException, IOException, XPathExpressionException {
if (outputVars.isEmpty()) {
log.debug("MWAPI: outputVars is empty");
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);
IBindingSet[] results = new IBindingSet[nodes.getLength()];
if (results.length == 0) {
log.debug("MWAPI: item xpath {} returned 0 node (empty?)", template.getItemsPath());
return new ResultWithContinue(results, searchContinue);
}
final Map<OutputVariable, XPathExpression> compiledVars = new HashMap<>();
// Thanks, Oracle!
for (OutputVariable v : outputVars) {
compiledVars.put(v, xpath.get().compile(v.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> v : compiledVars.entrySet()) {
final IConstant constant;
if (v.getKey().isOrdinal()) {
constant = makeConstant(lexiconRelation.getValueFactory(), i + recordsCount);
results[i].set(v.getKey().getVar(), constant);
continue;
}
final Node value = (Node) v.getValue().evaluate(node, XPathConstants.NODE);
if (value != null && value.getNodeValue() != null) {
if (v.getKey().isURI()) {
constant = makeConstant(lexiconRelation.getValueFactory(), v.getKey().getURI(value.getNodeValue()));
} else {
constant = makeConstant(lexiconRelation.getValueFactory(), value.getNodeValue());
}
results[i].set(v.getKey().getVar(), constant);
}
}
}
return new ResultWithContinue(results, searchContinue);
}
Aggregations