Search in sources :

Example 1 with IConstant

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);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IConstant(com.bigdata.bop.IConstant) Document(org.w3c.dom.Document) OutputVariable(org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable) IBindingSet(com.bigdata.bop.IBindingSet) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Aggregations

IBindingSet (com.bigdata.bop.IBindingSet)1 IConstant (com.bigdata.bop.IConstant)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 XPath (javax.xml.xpath.XPath)1 XPathExpression (javax.xml.xpath.XPathExpression)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 OutputVariable (org.wikidata.query.rdf.blazegraph.mwapi.ApiTemplate.OutputVariable)1