Search in sources :

Example 16 with QueryResultHandlerException

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

the class AbstractSPARQLJSONWriter method endHeader.

@Override
public void endHeader() throws QueryResultHandlerException {
    if (!headerComplete) {
        try {
            jg.writeEndObject();
            if (tupleVariablesFound) {
                // Write results
                jg.writeObjectFieldStart("results");
                jg.writeArrayFieldStart("bindings");
            }
            headerComplete = true;
        } catch (IOException e) {
            throw new QueryResultHandlerException(e);
        }
    }
}
Also used : IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 17 with QueryResultHandlerException

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

the class AbstractSPARQLJSONWriter method startDocument.

@Override
public void startDocument() throws QueryResultHandlerException {
    if (!documentOpen) {
        documentOpen = true;
        headerOpen = false;
        headerComplete = false;
        tupleVariablesFound = false;
        firstTupleWritten = false;
        linksFound = false;
        if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
            // SES-2011: Always use \n for consistency
            Indenter indenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
            // By default Jackson does not pretty print, so enable this unless
            // PRETTY_PRINT setting is disabled
            DefaultPrettyPrinter pp = new DefaultPrettyPrinter().withArrayIndenter(indenter).withObjectIndenter(indenter);
            jg.setPrettyPrinter(pp);
        }
        try {
            if (getWriterConfig().isSet(BasicQueryWriterSettings.JSONP_CALLBACK)) {
                // SES-1019 : Write the callbackfunction name as a wrapper for
                // the results here
                String callbackName = getWriterConfig().get(BasicQueryWriterSettings.JSONP_CALLBACK);
                jg.writeRaw(callbackName);
                jg.writeRaw("(");
            }
            jg.writeStartObject();
        } catch (IOException e) {
            throw new QueryResultHandlerException(e);
        }
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) Indenter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter.Indenter) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 18 with QueryResultHandlerException

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

the class AbstractSPARQLJSONWriter method handleSolution.

@Override
public void handleSolution(BindingSet bindingSet) throws TupleQueryResultHandlerException {
    try {
        if (!documentOpen) {
            startDocument();
        }
        if (!headerOpen) {
            startHeader();
        }
        if (!headerComplete) {
            endHeader();
        }
        if (!tupleVariablesFound) {
            throw new IllegalStateException("Must call startQueryResult before handleSolution");
        }
        firstTupleWritten = true;
        jg.writeStartObject();
        Iterator<Binding> bindingIter = bindingSet.iterator();
        while (bindingIter.hasNext()) {
            Binding binding = bindingIter.next();
            jg.writeFieldName(binding.getName());
            writeValue(binding.getValue());
        }
        jg.writeEndObject();
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    } catch (TupleQueryResultHandlerException e) {
        throw e;
    } catch (QueryResultHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 19 with QueryResultHandlerException

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

the class QueryResultIO method parseTupleInternal.

private static TupleQueryResult parseTupleInternal(InputStream in, QueryResultFormat format, boolean parseOnBackgroundThread) throws IOException, QueryResultParseException, TupleQueryResultHandlerException, UnsupportedQueryResultFormatException {
    TupleQueryResultParser parser = createTupleParser(format);
    if (parseOnBackgroundThread) {
        BackgroundTupleResult result = new BackgroundTupleResult(new QueueCursor<>(new LinkedBlockingQueue<>(1)), parser, in);
        // Start a new thread in the background, which will be completed
        // when the BackgroundTupleResult is either closed or interrupted
        boolean allGood = false;
        try {
            ForkJoinPool.commonPool().submit(result);
            allGood = true;
        } finally {
            if (!allGood) {
                result.close();
            }
        }
        return result;
    } else {
        TupleQueryResultBuilder qrBuilder = new TupleQueryResultBuilder();
        try {
            parser.setQueryResultHandler(qrBuilder).parseQueryResult(in);
        } catch (QueryResultHandlerException e) {
            if (e instanceof TupleQueryResultHandlerException) {
                throw (TupleQueryResultHandlerException) e;
            } else {
                throw new TupleQueryResultHandlerException(e);
            }
        }
        return qrBuilder.getQueryResult();
    }
}
Also used : TupleQueryResultBuilder(org.eclipse.rdf4j.query.impl.TupleQueryResultBuilder) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) BackgroundTupleResult(org.eclipse.rdf4j.query.resultio.helpers.BackgroundTupleResult) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException)

Example 20 with QueryResultHandlerException

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

the class QueryResultIO method parseTuple.

/**
 * Parses a query result document, reporting the parsed solutions to the supplied TupleQueryResultHandler.
 *
 * @param in
 *        An InputStream to read the query result document from.
 * @param format
 *        The query result format of the document to parse. Supported formats are
 *        {@link TupleQueryResultFormat#SPARQL} and {@link TupleQueryResultFormat#BINARY}.
 * @param handler
 *        The TupleQueryResultHandler to report the parse results to.
 * @throws IOException
 *         If an I/O error occured while reading the query result document from the stream.
 * @throws TupleQueryResultHandlerException
 *         If such an exception is thrown by the supplied TupleQueryResultHandler.
 * @throws UnsupportedQueryResultFormatException
 * @throws IllegalArgumentException
 *         If an unsupported query result file format was specified.
 */
public static void parseTuple(InputStream in, QueryResultFormat format, TupleQueryResultHandler handler, ValueFactory valueFactory) throws IOException, QueryResultParseException, TupleQueryResultHandlerException, UnsupportedQueryResultFormatException {
    TupleQueryResultParser parser = createTupleParser(format);
    parser.setValueFactory(valueFactory);
    parser.setQueryResultHandler(handler);
    try {
        parser.parseQueryResult(in);
    } catch (QueryResultHandlerException e) {
        if (e instanceof TupleQueryResultHandlerException) {
            throw (TupleQueryResultHandlerException) e;
        } else {
            throw new TupleQueryResultHandlerException(e);
        }
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException)

Aggregations

QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)20 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)18 IOException (java.io.IOException)16 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)3 Binding (org.eclipse.rdf4j.query.Binding)2 DefaultIndenter (com.fasterxml.jackson.core.util.DefaultIndenter)1 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)1 Indenter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter.Indenter)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 HttpResponse (org.apache.http.HttpResponse)1 UncloseableInputStream (org.eclipse.rdf4j.common.io.UncloseableInputStream)1 SimpleSAXParser (org.eclipse.rdf4j.common.xml.SimpleSAXParser)1 TupleQueryResultBuilder (org.eclipse.rdf4j.query.impl.TupleQueryResultBuilder)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)1 TupleQueryResultFormat (org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat)1 TupleQueryResultParser (org.eclipse.rdf4j.query.resultio.TupleQueryResultParser)1