Search in sources :

Example 6 with QueryResultHandlerException

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

the class AbstractSPARQLXMLWriter method startHeader.

@Override
public void startHeader() throws QueryResultHandlerException {
    if (!documentOpen) {
        startDocument();
    }
    if (!headerOpen) {
        try {
            xmlWriter.startTag(ROOT_TAG);
            xmlWriter.startTag(HEAD_TAG);
            headerOpen = 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 7 with QueryResultHandlerException

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

the class AbstractSPARQLXMLWriter method endHeader.

@Override
public void endHeader() throws QueryResultHandlerException {
    if (!documentOpen) {
        startDocument();
    }
    if (!headerOpen) {
        startHeader();
    }
    if (!headerComplete) {
        try {
            xmlWriter.endTag(HEAD_TAG);
            if (tupleVariablesFound) {
                // Write start of results, which must always exist, even if there
                // are no result bindings
                xmlWriter.startTag(RESULT_SET_TAG);
            }
            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 8 with QueryResultHandlerException

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

the class AbstractSPARQLXMLWriter 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");
        }
        xmlWriter.startTag(RESULT_TAG);
        for (Binding binding : bindingSet) {
            xmlWriter.setAttribute(BINDING_NAME_ATT, binding.getName());
            xmlWriter.startTag(BINDING_TAG);
            writeValue(binding.getValue());
            xmlWriter.endTag(BINDING_TAG);
        }
        xmlWriter.endTag(RESULT_TAG);
    } 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 9 with QueryResultHandlerException

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

the class AbstractSPARQLXMLWriter method startQueryResult.

@Override
public void startQueryResult(List<String> bindingNames) throws TupleQueryResultHandlerException {
    try {
        if (!documentOpen) {
            startDocument();
        }
        if (!headerOpen) {
            startHeader();
        }
        tupleVariablesFound = true;
        // Write binding names
        for (String name : bindingNames) {
            xmlWriter.setAttribute(VAR_NAME_ATT, name);
            xmlWriter.emptyElement(VAR_TAG);
        }
    } catch (IOException e) {
        throw new TupleQueryResultHandlerException(e);
    } catch (TupleQueryResultHandlerException e) {
        throw e;
    } catch (QueryResultHandlerException e) {
        throw new TupleQueryResultHandlerException(e);
    }
}
Also used : TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 10 with QueryResultHandlerException

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

the class AbstractSPARQLXMLParser 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");
    }
    BufferedInputStream buff = new BufferedInputStream(in);
    // Wrap in a custom InputStream that doesn't allow close to be called by dependencies before we are ready to call it
    UncloseableInputStream uncloseable = new UncloseableInputStream(buff);
    SAXException caughtException = null;
    boolean result = false;
    try {
        if (attemptParseBoolean) {
            buff.mark(Integer.MAX_VALUE);
            try {
                SPARQLBooleanSAXParser valueParser = new SPARQLBooleanSAXParser();
                XMLReader xmlReader;
                if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
                    xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
                } else {
                    xmlReader = XMLReaderFactory.createXMLReader();
                }
                xmlReader.setErrorHandler(this);
                // not explicitly set
                for (RioSetting<Boolean> aSetting : getCompulsoryXmlFeatureSettings()) {
                    try {
                        xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
                    }
                }
                // not explicitly set
                for (RioSetting<?> aSetting : getCompulsoryXmlPropertySettings()) {
                    try {
                        xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
                    }
                }
                // the parser config
                for (RioSetting<Boolean> aSetting : getOptionalXmlFeatureSettings()) {
                    try {
                        if (getParserConfig().isSet(aSetting)) {
                            xmlReader.setFeature(aSetting.getKey(), getParserConfig().get(aSetting));
                        }
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX feature.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX feature.", aSetting.getKey()));
                    }
                }
                // the parser config
                for (RioSetting<?> aSetting : getOptionalXmlPropertySettings()) {
                    try {
                        if (getParserConfig().isSet(aSetting)) {
                            xmlReader.setProperty(aSetting.getKey(), getParserConfig().get(aSetting));
                        }
                    } catch (SAXNotRecognizedException e) {
                        reportWarning(String.format("%s is not a recognized SAX property.", aSetting.getKey()));
                    } catch (SAXNotSupportedException e) {
                        reportWarning(String.format("%s is not a supported SAX property.", aSetting.getKey()));
                    }
                }
                internalSAXParser = new SimpleSAXParser(xmlReader);
                internalSAXParser.setPreserveWhitespace(true);
                internalSAXParser.setListener(valueParser);
                internalSAXParser.parse(uncloseable);
                result = valueParser.getValue();
                try {
                    if (this.handler != null) {
                        this.handler.handleBoolean(result);
                    }
                } catch (QueryResultHandlerException e) {
                    if (e.getCause() != null && e.getCause() instanceof IOException) {
                        throw (IOException) e.getCause();
                    } else {
                        throw new QueryResultParseException("Found an issue with the query result handler", e);
                    }
                }
                // result;
                return result;
            } catch (SAXException e) {
                caughtException = e;
            }
            // Reset the buffered input stream and try again looking for tuple
            // results
            buff.reset();
        }
        if (attemptParseTuple) {
            try {
                XMLReader xmlReader = XMLReaderFactory.createXMLReader();
                xmlReader.setErrorHandler(this);
                internalSAXParser = new SimpleSAXParser(xmlReader);
                internalSAXParser.setPreserveWhitespace(true);
                internalSAXParser.setListener(new SPARQLResultsSAXParser(this.valueFactory, this.handler));
                internalSAXParser.parse(uncloseable);
                // we had success, so remove the exception that we were tracking
                // from
                // the boolean failure
                caughtException = null;
            } catch (SAXException e) {
                caughtException = e;
            }
        }
        if (caughtException != null) {
            Exception wrappedExc = caughtException.getException();
            if (wrappedExc == null) {
                throw new QueryResultParseException(caughtException);
            } else if (wrappedExc instanceof QueryResultParseException) {
                throw (QueryResultParseException) wrappedExc;
            } else if (wrappedExc instanceof QueryResultHandlerException) {
                throw (QueryResultHandlerException) wrappedExc;
            } else {
                throw new QueryResultParseException(wrappedExc);
            }
        }
    } finally {
        // Explicitly call the delegator to the close method to actually close it
        uncloseable.doClose();
    }
    return result;
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) IOException(java.io.IOException) QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser) BufferedInputStream(java.io.BufferedInputStream) UncloseableInputStream(org.eclipse.rdf4j.common.io.UncloseableInputStream) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) XMLReader(org.xml.sax.XMLReader)

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