XMLStreamReader rdr;
try {
rdr = xmlStreamFactory.createXMLStreamReader(stream, "UTF-8");
} catch (XMLStreamException e) {
throw new SparqlException("Unable to open XML data", e);
}
List<String> cols = new ArrayList<String>();
List<String> md = new ArrayList<String>();
try {
if (rdr.nextTag() != START_ELEMENT || !nameIs(rdr, SPARQL)) {
throw new SparqlException("Result is not a SPARQL XML result document");
}
// Initialize the base URI to the
String base = null;
if (cmd != null) {
base = ((ProtocolDataSource)cmd.getConnection().getDataSource()).getUrl().toString();
}
// read the header information
parseHeader(base, rdr, cols, md);
// move the cursor into the results, and read in the first row
if (rdr.nextTag() != START_ELEMENT) throw new SparqlException("No body to result document");
String typeName = rdr.getLocalName();
if (typeName.equalsIgnoreCase(RESULTS.toString())) {
if (type != null && type != ResultType.SELECT) {
throw new SparqlException("Unexpected result type; expected " + type + " but found SELECT.");
}
return new XMLSelectResults(cmd, rdr, cols, md);
}
if (typeName.equalsIgnoreCase(BOOLEAN.toString())) {
if (type != null && type != ResultType.ASK) {
throw new SparqlException("Unexpected result type; expected " + type + " but found ASK.");
}
if (!cols.isEmpty()) {
logger.warn("Boolean result contained column definitions in head: {}", cols);
}
return parseBooleanResult(cmd, rdr, md);
}
throw new SparqlException("Unknown element type in result document. Expected <results> or <boolean> but got <" + typeName + ">");
} catch (XMLStreamException e) {
throw new SparqlException("Error reading the XML stream", e);
}
}