Search in sources :

Example 11 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class TransactionSAXParser method startTag.

@Override
public void startTag(String tagName, Map<String, String> atts, String text) throws SAXException {
    if (TransactionXMLConstants.URI_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.BNODE_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createBNode(text));
    } else if (TransactionXMLConstants.LITERAL_TAG.equals(tagName)) {
        String lang = atts.get(TransactionXMLConstants.LANG_ATT);
        String datatype = atts.get(TransactionXMLConstants.DATATYPE_ATT);
        String encoding = atts.get(TransactionXMLConstants.ENCODING_ATT);
        if (encoding != null && "base64".equalsIgnoreCase(encoding)) {
            text = new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(text));
        }
        Literal lit;
        if (lang != null) {
            lit = valueFactory.createLiteral(text, lang);
        } else if (datatype != null) {
            IRI dtURI = valueFactory.createIRI(datatype);
            lit = valueFactory.createLiteral(text, dtURI);
        } else {
            lit = valueFactory.createLiteral(text);
        }
        parsedValues.add(lit);
    } else if (TransactionXMLConstants.NULL_TAG.equals(tagName)) {
        parsedValues.add(null);
    } else if (TransactionXMLConstants.SET_NAMESPACE_TAG.equals(tagName)) {
        String prefix = atts.get(TransactionXMLConstants.PREFIX_ATT);
        String name = atts.get(TransactionXMLConstants.NAME_ATT);
        txn.add(new SetNamespaceOperation(prefix, name));
    } else if (TransactionXMLConstants.REMOVE_NAMESPACE_TAG.equals(tagName)) {
        String prefix = atts.get(TransactionXMLConstants.PREFIX_ATT);
        txn.add(new RemoveNamespaceOperation(prefix));
    } else if (TransactionXMLConstants.CLEAR_NAMESPACES_TAG.equals(tagName)) {
        txn.add(new ClearNamespacesOperation());
    } else if (TransactionXMLConstants.SPARQL_UPDATE_TAG.equals(tagName)) {
        if (currentSPARQLUpdate != null) {
            throw new SAXException("unexpected start of SPARQL Update operation");
        }
        currentSPARQLUpdate = new SPARQLUpdateOperation();
        String baseURI = atts.get(TransactionXMLConstants.BASE_URI_ATT);
        boolean includeInferred = Boolean.parseBoolean(atts.get(TransactionXMLConstants.INCLUDE_INFERRED_ATT));
        currentSPARQLUpdate.setIncludeInferred(includeInferred);
        currentSPARQLUpdate.setBaseURI(baseURI);
    } else if (TransactionXMLConstants.UPDATE_STRING_TAG.equals(tagName)) {
        currentSPARQLUpdate.setUpdateString(text);
    } else if (TransactionXMLConstants.DATASET_TAG.equals(tagName)) {
        currentDataset = new SimpleDataset();
    } else if (TransactionXMLConstants.DEFAULT_INSERT_GRAPH.equals(tagName)) {
        currentDataset.setDefaultInsertGraph(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.GRAPH_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.BINDINGS.equals(tagName)) {
        if (bindings != null) {
            throw new SAXException("unexpected start of SPARQL Update operation bindings");
        }
        bindings = new ArrayList<Binding>();
    } else if (TransactionXMLConstants.BINDING_URI.equals(tagName) || TransactionXMLConstants.BINDING_BNODE.equals(tagName) || TransactionXMLConstants.BINDING_LITERAL.equals(tagName)) {
        if (bindings == null) {
            throw new SAXException("unexpected start of SPARQL Update operation binding (without <bindings>)");
        }
        String value = text;
        String name = atts.get(TransactionXMLConstants.NAME_ATT);
        if (name != null && value != null) {
            Value v;
            if (TransactionXMLConstants.BINDING_URI.equals(tagName)) {
                v = valueFactory.createIRI(value);
            } else if (TransactionXMLConstants.BINDING_BNODE.equals(tagName)) {
                v = valueFactory.createBNode(value);
            } else {
                String language = atts.get(TransactionXMLConstants.LANGUAGE_ATT);
                String dataType = atts.get(TransactionXMLConstants.DATA_TYPE_ATT);
                if (language != null) {
                    v = valueFactory.createLiteral(value, language);
                } else if (dataType != null) {
                    v = valueFactory.createLiteral(value, valueFactory.createIRI(dataType));
                } else {
                    v = valueFactory.createLiteral(value);
                }
            }
            bindings.add(new SimpleBinding(name, v));
        }
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) IRI(org.eclipse.rdf4j.model.IRI) ClearNamespacesOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) SPARQLUpdateOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) RemoveNamespaceOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation) SAXException(org.xml.sax.SAXException) SetNamespaceOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation) Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value)

Example 12 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class TransactionSAXParser method endTag.

@Override
public void endTag(String tagName) throws SAXException {
    if (TransactionXMLConstants.ADD_STATEMENT_TAG.equals(tagName)) {
        txn.add(createAddStatementOperation());
    } else if (TransactionXMLConstants.REMOVE_STATEMENTS_TAG.equals(tagName)) {
        txn.add(createRemoveStatementsOperation());
    } else if (TransactionXMLConstants.CLEAR_TAG.equals(tagName)) {
        txn.add(createClearOperation());
    } else if (TransactionXMLConstants.SPARQL_UPDATE_TAG.equals(tagName)) {
        txn.add(currentSPARQLUpdate);
        currentSPARQLUpdate = null;
    } else if (TransactionXMLConstants.DEFAULT_GRAPHS_TAG.equals(tagName)) {
        for (Value parsedValue : parsedValues) {
            try {
                currentDataset.addDefaultGraph((IRI) parsedValue);
            } catch (ClassCastException e) {
                throw new SAXException("unexpected value in default graph list: " + parsedValue);
            }
        }
        parsedValues.clear();
    } else if (TransactionXMLConstants.NAMED_GRAPHS_TAG.equals(tagName)) {
        for (Value parsedValue : parsedValues) {
            try {
                currentDataset.addNamedGraph((IRI) parsedValue);
            } catch (ClassCastException e) {
                throw new SAXException("unexpected value in named graph list: " + parsedValue);
            }
        }
        parsedValues.clear();
    } else if (TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG.equals(tagName)) {
        for (Value parsedValue : parsedValues) {
            try {
                currentDataset.addDefaultRemoveGraph((IRI) parsedValue);
            } catch (ClassCastException e) {
                throw new SAXException("unexpected value in default remove graph list: " + parsedValue);
            }
        }
        parsedValues.clear();
    } else if (TransactionXMLConstants.DATASET_TAG.equals(tagName)) {
        currentSPARQLUpdate.setDataset(currentDataset);
        currentDataset = null;
    } else if (TransactionXMLConstants.BINDINGS.equals(tagName)) {
        Binding[] b = bindings.toArray(new Binding[0]);
        currentSPARQLUpdate.setBindings(b);
        bindings.clear();
        bindings = null;
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) IRI(org.eclipse.rdf4j.model.IRI) Value(org.eclipse.rdf4j.model.Value) SAXException(org.xml.sax.SAXException)

Example 13 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class ServiceJoinConversionIteration method convert.

@Override
protected BindingSet convert(BindingSet bIn) throws QueryEvaluationException {
    // overestimate the capacity
    SPARQLQueryBindingSet res = new SPARQLQueryBindingSet(bIn.size() + bindings.size());
    int bIndex = -1;
    Iterator<Binding> bIter = bIn.iterator();
    while (bIter.hasNext()) {
        Binding b = bIter.next();
        String name = b.getName();
        if (name.equals("__rowIdx")) {
            bIndex = Integer.parseInt(b.getValue().stringValue());
            continue;
        }
        res.addBinding(b.getName(), b.getValue());
    }
    // is dealt with in another place)
    if (bIndex == -1)
        throw new QueryEvaluationException("Invalid join. Probably this is due to non-standard behavior of the SPARQL endpoint. " + "Please report to the developers.");
    res.addAll(bindings.get(bIndex));
    return res;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) SPARQLQueryBindingSet(org.eclipse.rdf4j.repository.sparql.query.SPARQLQueryBindingSet)

Example 14 with Binding

use of org.eclipse.rdf4j.query.Binding 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)

Example 15 with Binding

use of org.eclipse.rdf4j.query.Binding in project rdf4j by eclipse.

the class SPARQLCSVTupleBackgroundTest method bindingSetsMatch.

private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) {
    if (bs1.size() != bs2.size()) {
        return false;
    }
    for (Binding binding1 : bs1) {
        Value value1 = binding1.getValue();
        Value value2 = bs2.getValue(binding1.getName());
        if (value1 == null && value2 != null) {
            return false;
        } else if (value1 != null && value2 == null) {
            return false;
        } else if (value1 != null && value2 != null) {
            if (!equals(value1, value2) && !value1.stringValue().equals(value2.stringValue())) {
                return false;
            }
        }
    }
    return true;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) Value(org.eclipse.rdf4j.model.Value)

Aggregations

Binding (org.eclipse.rdf4j.query.Binding)18 Value (org.eclipse.rdf4j.model.Value)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 BNode (org.eclipse.rdf4j.model.BNode)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)4 IRI (org.eclipse.rdf4j.model.IRI)3 MalformedQueryException (org.eclipse.rdf4j.query.MalformedQueryException)3 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)3 SimpleBinding (org.eclipse.rdf4j.query.impl.SimpleBinding)3 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Literal (org.eclipse.rdf4j.model.Literal)2 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)2 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)2 SAXException (org.xml.sax.SAXException)2 ClearNamespacesOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation)1 RemoveNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation)1 SPARQLUpdateOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation)1 SetNamespaceOperation (org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation)1