Search in sources :

Example 1 with SimpleSAXParser

use of org.eclipse.rdf4j.common.xml.SimpleSAXParser 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)

Example 2 with SimpleSAXParser

use of org.eclipse.rdf4j.common.xml.SimpleSAXParser in project rdf4j by eclipse.

the class TransactionReader method parse.

/**
 * parse the transaction from the serialization
 *
 * @throws SAXException
 *         If the SimpleSAXParser was unable to create an XMLReader or if the XML is faulty.
 * @throws IOException
 *         If IO problems during parsing.
 */
public Collection<TransactionOperation> parse(InputStream in) throws SAXException, IOException {
    SimpleSAXParser saxParser = new SimpleSAXParser();
    saxParser.setPreserveWhitespace(true);
    TransactionSAXParser handler = new TransactionSAXParser();
    saxParser.setListener(handler);
    saxParser.parse(in);
    return handler.getTxn();
}
Also used : SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser)

Example 3 with SimpleSAXParser

use of org.eclipse.rdf4j.common.xml.SimpleSAXParser in project rdf4j by eclipse.

the class TransactionReader method parse.

/**
 * parse the transaction from the serialization
 *
 * @throws SAXException
 *         If the SimpleSAXParser was unable to create an XMLReader or if the XML is faulty.
 * @throws IOException
 *         If IO problems during parsing.
 */
public Collection<TransactionOperation> parse(Reader in) throws SAXException, IOException {
    SimpleSAXParser saxParser = new SimpleSAXParser();
    TransactionSAXParser handler = new TransactionSAXParser();
    saxParser.setPreserveWhitespace(true);
    saxParser.setListener(handler);
    saxParser.parse(in);
    return handler.getTxn();
}
Also used : SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser)

Example 4 with SimpleSAXParser

use of org.eclipse.rdf4j.common.xml.SimpleSAXParser in project rdf4j by eclipse.

the class TriXParser method parse.

private void parse(InputSource inputStreamOrReader) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        if (rdfHandler != null) {
            rdfHandler.startRDF();
        }
        XMLReader xmlReader;
        if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
            xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
        } else {
            xmlReader = XMLReaderFactory.createXMLReader();
        }
        xmlReader.setErrorHandler(this);
        saxParser = new SimpleSAXParser(xmlReader);
        saxParser.setPreserveWhitespace(true);
        saxParser.setListener(new TriXSAXHandler());
        saxParser.parse(inputStreamOrReader);
    } catch (SAXParseException e) {
        Exception wrappedExc = e.getException();
        if (wrappedExc == null) {
            reportFatalError(e, e.getLineNumber(), e.getColumnNumber());
        } else {
            reportFatalError(wrappedExc, e.getLineNumber(), e.getColumnNumber());
        }
    } catch (SAXException e) {
        Exception wrappedExc = e.getException();
        if (wrappedExc == null) {
            reportFatalError(e);
        } else if (wrappedExc instanceof RDFParseException) {
            throw (RDFParseException) wrappedExc;
        } else if (wrappedExc instanceof RDFHandlerException) {
            throw (RDFHandlerException) wrappedExc;
        } else {
            reportFatalError(wrappedExc);
        }
    } finally {
        clear();
    }
    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}
Also used : SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) SAXParseException(org.xml.sax.SAXParseException) XMLReader(org.xml.sax.XMLReader) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Aggregations

SimpleSAXParser (org.eclipse.rdf4j.common.xml.SimpleSAXParser)4 IOException (java.io.IOException)2 SAXException (org.xml.sax.SAXException)2 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)2 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)2 SAXParseException (org.xml.sax.SAXParseException)2 XMLReader (org.xml.sax.XMLReader)2 BufferedInputStream (java.io.BufferedInputStream)1 UncloseableInputStream (org.eclipse.rdf4j.common.io.UncloseableInputStream)1 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)1 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)1 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)1 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)1