use of org.eclipse.rdf4j.common.io.UncloseableInputStream 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;
}
use of org.eclipse.rdf4j.common.io.UncloseableInputStream in project rdf4j by eclipse.
the class RDFLoader method loadZip.
private void loadZip(InputStream in, String baseURI, RDFFormat dataFormat, RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException {
try (ZipInputStream zipIn = new ZipInputStream(in)) {
for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) {
if (entry.isDirectory()) {
continue;
}
try {
RDFFormat format = Rio.getParserFormatForFileName(entry.getName()).orElse(dataFormat);
// Prevent parser (Xerces) from closing the input stream
UncloseableInputStream wrapper = new UncloseableInputStream(zipIn);
load(wrapper, baseURI, format, rdfHandler);
} catch (RDFParseException e) {
String msg = e.getMessage() + " in " + entry.getName();
RDFParseException pe = new RDFParseException(msg, e.getLineNumber(), e.getColumnNumber());
pe.initCause(e);
throw pe;
} finally {
zipIn.closeEntry();
}
}
// end for
}
}
Aggregations