Search in sources :

Example 11 with QueryResultParseException

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();
    }
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Value(org.eclipse.rdf4j.model.Value)

Example 12 with QueryResultParseException

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);
    }
}
Also used : QueryResultParseException(org.eclipse.rdf4j.query.resultio.QueryResultParseException) IRI(org.eclipse.rdf4j.model.IRI)

Aggregations

QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)12 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)5 QueryResultParser (org.eclipse.rdf4j.query.resultio.QueryResultParser)4 ParseErrorCollector (org.eclipse.rdf4j.rio.helpers.ParseErrorCollector)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)3 ArrayList (java.util.ArrayList)2 HttpResponse (org.apache.http.HttpResponse)2 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)2 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)2 TupleQueryResultFormat (org.eclipse.rdf4j.query.resultio.TupleQueryResultFormat)2 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HashSet (java.util.HashSet)1 List (java.util.List)1