Search in sources :

Example 1 with MapBindingSet

use of org.eclipse.rdf4j.query.impl.MapBindingSet in project rdf4j by eclipse.

the class AbstractSPARQLJSONParser method parseQueryResultInternal.

protected boolean parseQueryResultInternal(InputStream in, boolean attemptParseBoolean, boolean attemptParseTuple) throws IOException, QueryResultParseException, QueryResultHandlerException {
    if (!attemptParseBoolean && !attemptParseTuple) {
        throw new IllegalArgumentException("Internal error: Did not specify whether to parse as either boolean and/or tuple");
    }
    JsonParser jp = JSON_FACTORY.createParser(in);
    boolean result = false;
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        throw new QueryResultParseException("Expected SPARQL Results JSON document to start with an Object", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
    }
    List<String> varsList = new ArrayList<String>();
    boolean varsFound = false;
    Set<BindingSet> bindings = new HashSet<BindingSet>();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        final String baseStr = jp.getCurrentName();
        if (baseStr.equals(HEAD)) {
            if (jp.nextToken() != JsonToken.START_OBJECT) {
                throw new QueryResultParseException("Did not find object under " + baseStr + " field", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
            }
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                final String headStr = jp.getCurrentName();
                if (headStr.equals(VARS)) {
                    if (!attemptParseTuple) {
                        throw new QueryResultParseException("Found tuple results variables when attempting to parse SPARQL Results JSON to boolean result");
                    }
                    if (jp.nextToken() != JsonToken.START_ARRAY) {
                        throw new QueryResultParseException("Expected variable labels to be an array", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                    }
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        varsList.add(jp.getText());
                    }
                    if (this.handler != null) {
                        handler.startQueryResult(varsList);
                    }
                    varsFound = true;
                    // out now.
                    if (!bindings.isEmpty() && this.handler != null) {
                        for (BindingSet nextBinding : bindings) {
                            handler.handleSolution(nextBinding);
                            handler.endQueryResult();
                        }
                        bindings.clear();
                    }
                } else if (headStr.equals(LINK)) {
                    List<String> linksList = new ArrayList<String>();
                    if (jp.nextToken() != JsonToken.START_ARRAY) {
                        throw new QueryResultParseException("Expected links to be an array", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                    }
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        linksList.add(jp.getText());
                    }
                    if (this.handler != null) {
                        handler.handleLinks(linksList);
                    }
                } else {
                    throw new QueryResultParseException("Found unexpected object in head field: " + headStr, jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                }
            }
        } else if (baseStr.equals(RESULTS)) {
            if (!attemptParseTuple) {
                throw new QueryResultParseException("Found tuple results bindings when attempting to parse SPARQL Results JSON to boolean result");
            }
            if (jp.nextToken() != JsonToken.START_OBJECT) {
                throw new QueryResultParseException("Found unexpected token in results object: " + jp.getCurrentName(), jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
            }
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                if (jp.getCurrentName().equals(BINDINGS)) {
                    if (jp.nextToken() != JsonToken.START_ARRAY) {
                        throw new QueryResultParseException("Found unexpected token in bindings object", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                    }
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        MapBindingSet nextBindingSet = new MapBindingSet();
                        if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
                            throw new QueryResultParseException("Did not find object in bindings array: " + jp.getCurrentName(), jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                        }
                        while (jp.nextToken() != JsonToken.END_OBJECT) {
                            if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
                                throw new QueryResultParseException("Did not find binding name", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                            }
                            final String bindingStr = jp.getCurrentName();
                            if (jp.nextToken() != JsonToken.START_OBJECT) {
                                throw new QueryResultParseException("Did not find object for binding value", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                            }
                            String lang = null;
                            String type = null;
                            String datatype = null;
                            String value = null;
                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
                                    throw new QueryResultParseException("Did not find value attribute under " + bindingStr + " field", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                                }
                                String fieldName = jp.getCurrentName();
                                // move to the value token
                                jp.nextToken();
                                // set the appropriate state variable
                                if (TYPE.equals(fieldName)) {
                                    type = jp.getText();
                                } else if (XMLLANG.equals(fieldName)) {
                                    lang = jp.getText();
                                } else if (DATATYPE.equals(fieldName)) {
                                    datatype = jp.getText();
                                } else if (VALUE.equals(fieldName)) {
                                    value = jp.getText();
                                } else {
                                    throw new QueryResultParseException("Unexpected field name: " + fieldName, jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                                }
                            }
                            nextBindingSet.addBinding(bindingStr, parseValue(type, value, lang, datatype));
                        }
                        // bindings state
                        if (!varsFound) {
                            // Buffer the bindings to fit with the
                            // QueryResultHandler contract so that startQueryResults
                            // is
                            // always called before handleSolution
                            bindings.add(nextBindingSet);
                        } else if (handler != null) {
                            handler.handleSolution(nextBindingSet);
                        }
                    }
                    if (handler != null) {
                        handler.endQueryResult();
                    }
                } else // SPARQL spec
                if (jp.getCurrentName().equals(DISTINCT) || jp.getCurrentName().equals(ORDERED)) {
                    jp.nextToken();
                } else {
                    throw new QueryResultParseException("Found unexpected field in results: " + jp.getCurrentName(), jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
                }
            }
        } else if (baseStr.equals(BOOLEAN)) {
            if (!attemptParseBoolean) {
                throw new QueryResultParseException("Found boolean results when attempting to parse SPARQL Results JSON to tuple results");
            }
            jp.nextToken();
            result = Boolean.parseBoolean(jp.getText());
            if (handler != null) {
                handler.handleBoolean(result);
            }
        } else {
            throw new QueryResultParseException("Found unexpected object in top level " + baseStr + " field", jp.getCurrentLocation().getLineNr(), jp.getCurrentLocation().getColumnNr());
        }
    }
    return result;
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) JsonParser(com.fasterxml.jackson.core.JsonParser) HashSet(java.util.HashSet)

Example 2 with MapBindingSet

use of org.eclipse.rdf4j.query.impl.MapBindingSet in project rdf4j by eclipse.

the class DAWGTestResultSetParser method reportSolution.

private void reportSolution(Resource solutionNode, List<String> bindingNames) throws RDFHandlerException, GraphUtilException {
    MapBindingSet bindingSet = new MapBindingSet(bindingNames.size());
    Iterator<Value> bindingIter = GraphUtil.getObjectIterator(graph, solutionNode, BINDING);
    while (bindingIter.hasNext()) {
        Value bindingNode = bindingIter.next();
        if (bindingNode instanceof Resource) {
            Binding binding = getBinding((Resource) bindingNode);
            bindingSet.addBinding(binding);
        } else {
            throw new RDFHandlerException("Value for " + BINDING + " is not a resource: " + bindingNode);
        }
    }
    try {
        tqrHandler.handleSolution(bindingSet);
    } catch (TupleQueryResultHandlerException e) {
        throw new RDFHandlerException(e.getMessage(), e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Value(org.eclipse.rdf4j.model.Value) Resource(org.eclipse.rdf4j.model.Resource) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet)

Aggregations

MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Resource (org.eclipse.rdf4j.model.Resource)1 Value (org.eclipse.rdf4j.model.Value)1 Binding (org.eclipse.rdf4j.query.Binding)1 BindingSet (org.eclipse.rdf4j.query.BindingSet)1 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)1 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)1 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)1 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)1