use of org.eclipse.rdf4j.query.resultio.QueryResultParseException in project rdf4j by eclipse.
the class BinaryQueryResultParser method parse.
@Override
public synchronized void parse(InputStream in) throws IOException, QueryResultParseException, TupleQueryResultHandlerException {
if (in == null) {
throw new IllegalArgumentException("Input stream can not be 'null'");
}
this.in = new DataInputStream(in);
// Check magic number
byte[] magicNumber = IOUtil.readBytes(in, MAGIC_NUMBER.length);
if (!Arrays.equals(magicNumber, MAGIC_NUMBER)) {
throw new QueryResultParseException("File does not contain a binary RDF table result");
}
// Check format version (parser is backward-compatible with version 1 and
// version 2)
formatVersion = this.in.readInt();
if (formatVersion > FORMAT_VERSION && formatVersion < 1) {
throw new QueryResultParseException("Incompatible format version: " + formatVersion);
}
if (formatVersion == 2) {
// read format version 2 FLAG byte (ordered and distinct flags) and
// ignore them
this.in.readByte();
}
// Read column headers
int columnCount = this.in.readInt();
if (columnCount < 0) {
throw new QueryResultParseException("Illegal column count specified: " + columnCount);
}
List<String> columnHeaders = new ArrayList<String>(columnCount);
for (int i = 0; i < columnCount; i++) {
columnHeaders.add(readString());
}
columnHeaders = Collections.unmodifiableList(columnHeaders);
if (handler != null) {
handler.startQueryResult(columnHeaders);
}
// Read value tuples
List<Value> currentTuple = new ArrayList<Value>(columnCount);
List<Value> previousTuple = Collections.nCopies(columnCount, (Value) null);
int recordTypeMarker = this.in.readByte();
while (recordTypeMarker != TABLE_END_RECORD_MARKER) {
if (recordTypeMarker == ERROR_RECORD_MARKER) {
processError();
} else if (recordTypeMarker == NAMESPACE_RECORD_MARKER) {
processNamespace();
} else if (recordTypeMarker == EMPTY_ROW_RECORD_MARKER) {
if (handler != null) {
handler.handleSolution(EmptyBindingSet.getInstance());
}
} else {
Value value = null;
switch(recordTypeMarker) {
case NULL_RECORD_MARKER:
// do nothing
break;
case REPEAT_RECORD_MARKER:
value = previousTuple.get(currentTuple.size());
break;
case QNAME_RECORD_MARKER:
value = readQName();
break;
case URI_RECORD_MARKER:
value = readURI();
break;
case BNODE_RECORD_MARKER:
value = readBnode();
break;
case PLAIN_LITERAL_RECORD_MARKER:
case LANG_LITERAL_RECORD_MARKER:
case DATATYPE_LITERAL_RECORD_MARKER:
value = readLiteral(recordTypeMarker);
break;
default:
throw new IOException("Unkown record type: " + recordTypeMarker);
}
currentTuple.add(value);
if (currentTuple.size() == columnCount) {
previousTuple = Collections.unmodifiableList(currentTuple);
currentTuple = new ArrayList<Value>(columnCount);
if (handler != null) {
handler.handleSolution(new ListBindingSet(columnHeaders, previousTuple));
}
}
}
recordTypeMarker = this.in.readByte();
}
if (handler != null) {
handler.endQueryResult();
}
}
use of org.eclipse.rdf4j.query.resultio.QueryResultParseException in project rdf4j by eclipse.
the class BinaryQueryResultParser method readLiteral.
private Literal readLiteral(int recordTypeMarker) throws IOException, QueryResultParseException {
String label = readString();
if (recordTypeMarker == DATATYPE_LITERAL_RECORD_MARKER) {
IRI datatype = null;
int dtTypeMarker = in.readByte();
switch(dtTypeMarker) {
case QNAME_RECORD_MARKER:
datatype = readQName();
break;
case URI_RECORD_MARKER:
datatype = readURI();
break;
default:
throw new QueryResultParseException("Illegal record type marker for literal's datatype");
}
return valueFactory.createLiteral(label, datatype);
} else if (recordTypeMarker == LANG_LITERAL_RECORD_MARKER) {
String language = readString();
return valueFactory.createLiteral(label, language);
} else {
return valueFactory.createLiteral(label);
}
}
Aggregations