Search in sources :

Example 1 with SPARQLResultsXMLWriter

use of org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter in project gocd by gocd.

the class SesameGraph method renderSPARQLResultsAsXML.

public void renderSPARQLResultsAsXML(String sparql, OutputStream stream) {
    try {
        Query query = conn.prepareQuery(QueryLanguage.SPARQL, sparql);
        contextualize(query);
        if (query instanceof TupleQuery) {
            renderTupleQuery(query, new SPARQLResultsXMLWriter(stream));
        } else {
            renderBooleanQuery(query, new SPARQLBooleanXMLWriter(stream));
        }
        stream.flush();
    } catch (UnsupportedSPARQLStatementException e) {
        throw e;
    } catch (Exception e) {
        throw new ShineRuntimeException("Could not render sparql results as XML: <<" + sparql + ">>", e);
    }
}
Also used : SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) SailQuery(org.openrdf.repository.sail.SailQuery) Query(org.openrdf.query.Query) TupleQuery(org.openrdf.query.TupleQuery) BooleanQuery(org.openrdf.query.BooleanQuery) UnsupportedSPARQLStatementException(com.thoughtworks.studios.shine.semweb.UnsupportedSPARQLStatementException) SPARQLBooleanXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLBooleanXMLWriter) TupleQuery(org.openrdf.query.TupleQuery) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) RepositoryException(org.openrdf.repository.RepositoryException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) MoreThanOneResultFoundException(com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException) MalformedSPARQLException(com.thoughtworks.studios.shine.semweb.MalformedSPARQLException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) OpenRDFException(org.openrdf.OpenRDFException) NoSuchElementException(java.util.NoSuchElementException) MalformedQueryException(org.openrdf.query.MalformedQueryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) UnsupportedSPARQLStatementException(com.thoughtworks.studios.shine.semweb.UnsupportedSPARQLStatementException)

Example 2 with SPARQLResultsXMLWriter

use of org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter in project qi4j-sdk by Qi4j.

the class SPARQLResource method get.

@Override
public Representation get(final Variant variant) throws ResourceException {
    try {
        // TODO There's probably a helper somewhere that can do this more nicely
        if (getRequest().getOriginalRef().getLastSegment().equals("sparqlhtml.xsl")) {
            InputStream resourceAsStream = getClass().getResourceAsStream("sparqlhtml.xsl");
            return new InputRepresentation(resourceAsStream, MediaType.TEXT_XML);
        }
        Form form;
        if (getRequest().getMethod().equals(Method.POST)) {
            form = new Form(getRequest().getEntity());
        } else {
            form = getRequest().getResourceRef().getQueryAsForm();
        }
        final RepositoryConnection conn = repository.getConnection();
        String queryStr = form.getFirstValue("query");
        if (queryStr == null) {
            InputStream resourceAsStream = getClass().getResourceAsStream("sparqlform.html");
            return new InputRepresentation(resourceAsStream, MediaType.TEXT_HTML);
        }
        Query query = getQuery(repository, conn, queryStr);
        if (query instanceof TupleQuery) {
            TupleQuery tQuery = (TupleQuery) query;
            final TupleQueryResult queryResult = tQuery.evaluate();
            if (variant.getMediaType().equals(MediaType.TEXT_HTML)) {
                return new OutputRepresentation(MediaType.TEXT_XML) {

                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        try {
                            PrintWriter out = new PrintWriter(outputStream);
                            out.println("<?xml version='1.0' encoding='UTF-8'?>");
                            out.println("<?xml-stylesheet type=\"text/xsl\" href=\"query/sparqlhtml.xsl\"?>");
                            out.flush();
                            TupleQueryResultWriter qrWriter = new SPARQLResultsXMLWriter(new XMLWriter(outputStream) {

                                @Override
                                public void startDocument() throws IOException {
                                // Ignore
                                }
                            });
                            QueryResultUtil.report(queryResult, qrWriter);
                        } catch (Exception e) {
                            throw new IOException(e);
                        } finally {
                            try {
                                conn.close();
                            } catch (RepositoryException e) {
                            // Ignore
                            }
                        }
                    }
                };
            } else if (variant.getMediaType().equals(MediaType.APPLICATION_RDF_XML)) {
                return new OutputRepresentation(MediaType.APPLICATION_XML) {

                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        try {
                            TupleQueryResultWriter qrWriter = new SPARQLResultsXMLWriter(new XMLWriter(outputStream));
                            QueryResultUtil.report(queryResult, qrWriter);
                        } catch (Exception e) {
                            throw new IOException(e);
                        } finally {
                            try {
                                conn.close();
                            } catch (RepositoryException e) {
                            // Ignore
                            }
                        }
                    }
                };
            } else if (variant.getMediaType().equals(RestApplication.APPLICATION_SPARQL_JSON)) {
                return new OutputRepresentation(RestApplication.APPLICATION_SPARQL_JSON) {

                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        try {
                            TupleQueryResultWriter qrWriter = new SPARQLResultsJSONWriterFactory().getWriter(outputStream);
                            QueryResultUtil.report(queryResult, qrWriter);
                        } catch (Exception e) {
                            throw new IOException(e);
                        } finally {
                            try {
                                conn.close();
                            } catch (RepositoryException e) {
                            // Ignore
                            }
                        }
                    }
                };
            }
        } else if (query instanceof GraphQuery) {
            GraphQuery gQuery = (GraphQuery) query;
            /*
                                queryResult = gQuery.evaluate();
                                registry = RDFWriterRegistry.getInstance();
                                view = GraphQueryResultView.getInstance();
                */
            conn.close();
        } else if (query instanceof BooleanQuery) {
            BooleanQuery bQuery = (BooleanQuery) query;
            /*
                                queryResult = bQuery.evaluate();
                                registry = BooleanQueryResultWriterRegistry.getInstance();
                                view = BooleanQueryResultView.getInstance();
                */
            conn.close();
        } else {
            conn.close();
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Unsupported query type: " + query.getClass().getName());
        }
    } catch (RepositoryException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    } catch (QueryEvaluationException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    return null;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) TupleQueryResultWriter(org.openrdf.query.resultio.TupleQueryResultWriter) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) XMLWriter(info.aduna.xml.XMLWriter) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) RepositoryException(org.openrdf.repository.RepositoryException) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) SPARQLResultsJSONWriterFactory(org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriterFactory) OutputRepresentation(org.restlet.representation.OutputRepresentation) ResourceException(org.restlet.resource.ResourceException) PrintWriter(java.io.PrintWriter)

Aggregations

IOException (java.io.IOException)2 SPARQLResultsXMLWriter (org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter)2 RepositoryException (org.openrdf.repository.RepositoryException)2 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)1 MalformedSPARQLException (com.thoughtworks.studios.shine.semweb.MalformedSPARQLException)1 MoreThanOneResultFoundException (com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException)1 UnsupportedSPARQLStatementException (com.thoughtworks.studios.shine.semweb.UnsupportedSPARQLStatementException)1 XMLWriter (info.aduna.xml.XMLWriter)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 NoSuchElementException (java.util.NoSuchElementException)1 OpenRDFException (org.openrdf.OpenRDFException)1 BooleanQuery (org.openrdf.query.BooleanQuery)1 MalformedQueryException (org.openrdf.query.MalformedQueryException)1 Query (org.openrdf.query.Query)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 TupleQuery (org.openrdf.query.TupleQuery)1 TupleQueryResultHandlerException (org.openrdf.query.TupleQueryResultHandlerException)1 TupleQueryResultWriter (org.openrdf.query.resultio.TupleQueryResultWriter)1