Search in sources :

Example 31 with Token

use of org.apache.jena.riot.tokens.Token in project jena by apache.

the class LangNQuads method parseOne.

@Override
protected final Quad parseOne() {
    Token sToken = nextToken();
    if (sToken.getType() == TokenType.EOF)
        exception(sToken, "Premature end of file: %s", sToken);
    Token pToken = nextToken();
    if (pToken.getType() == TokenType.EOF)
        exception(pToken, "Premature end of file: %s", pToken);
    Token oToken = nextToken();
    if (oToken.getType() == TokenType.EOF)
        exception(oToken, "Premature end of file: %s", oToken);
    // Maybe DOT
    Token xToken = nextToken();
    if (xToken.getType() == TokenType.EOF)
        exception(xToken, "Premature end of file: Quad not terminated by DOT: %s", xToken);
    // Process graph node first, before S,P,O
    // to set bnode label scope (if not global)
    Node c = null;
    if (xToken.getType() != TokenType.DOT) {
        // Allow bNodes for graph names.
        checkIRIOrBNode(xToken);
        // Allow only IRIs
        //checkIRI(xToken) ;
        c = tokenAsNode(xToken);
        xToken = nextToken();
        currentGraph = c;
    } else {
        c = Quad.defaultGraphNodeGenerated;
        currentGraph = null;
    }
    // createQuad may also check but these checks are cheap and do form syntax errors.
    checkIRIOrBNode(sToken);
    checkIRI(pToken);
    checkRDFTerm(oToken);
    // xToken already checked.
    Node s = tokenAsNode(sToken);
    Node p = tokenAsNode(pToken);
    Node o = tokenAsNode(oToken);
    if (xToken.getType() != TokenType.DOT)
        exception(xToken, "Quad not terminated by DOT: %s", xToken);
    return profile.createQuad(c, s, p, o, sToken.getLine(), sToken.getColumn());
}
Also used : Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token)

Example 32 with Token

use of org.apache.jena.riot.tokens.Token in project jena by apache.

the class LangNTriples method parseOne.

@Override
protected final Triple parseOne() {
    Token sToken = nextToken();
    if (sToken.isEOF())
        exception(sToken, "Premature end of file: %s", sToken);
    Token pToken = nextToken();
    if (pToken.isEOF())
        exception(pToken, "Premature end of file: %s", pToken);
    Token oToken = nextToken();
    if (oToken.isEOF())
        exception(oToken, "Premature end of file: %s", oToken);
    // Check in createTriple - but this is cheap so do it anyway.
    checkIRIOrBNode(sToken);
    checkIRI(pToken);
    checkRDFTerm(oToken);
    Token x = nextToken();
    if (x.getType() != TokenType.DOT)
        exception(x, "Triple not terminated by DOT: %s", x);
    Node s = tokenAsNode(sToken);
    Node p = tokenAsNode(pToken);
    Node o = tokenAsNode(oToken);
    return profile.createTriple(s, p, o, sToken.getLine(), sToken.getColumn());
}
Also used : Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token)

Example 33 with Token

use of org.apache.jena.riot.tokens.Token in project jena by apache.

the class LangTurtleBase method directive.

protected final void directive() {
    // It's a directive ...
    Token t = peekToken();
    String x = t.getImage();
    nextToken();
    if (x.equals("base")) {
        directiveBase();
        if (isStrictMode)
            // The line number is probably one ahead due to the newline
            expect("Base directive not terminated by a dot", DOT);
        else
            skipIf(DOT);
        return;
    }
    if (x.equals("prefix")) {
        directivePrefix();
        if (isStrictMode)
            // The line number is probably one ahead due to the newline
            expect("Prefix directive not terminated by a dot", DOT);
        else
            skipIf(DOT);
        return;
    }
    exception(t, "Unrecognized directive: %s", x);
}
Also used : Token(org.apache.jena.riot.tokens.Token)

Example 34 with Token

use of org.apache.jena.riot.tokens.Token in project jena by apache.

the class IteratorStreamRDFText method line2row.

private StreamRowRDF line2row(List<Token> line) {
    if (line.size() != 3 && line.size() != 4)
        throw new RiotException("Input line is not 3 or 4 items long");
    Node[] tuple = new Node[line.size()];
    int idx = 0;
    for (Token token : line) {
        Node n = null;
        if ((token.isWord() && token.getImage().equals("R"))) //|| ( token.isCtlCode() && token.getCntrlCode() == -1 )     // *
        {
            if (previousTuple == null)
                throw new RiotException("Repeat without previous data row");
            if (idx >= previousTuple.length)
                throw new RiotException("Repeat position beyond previous data row");
            n = previousTuple[idx];
        } else if (token.isNode()) {
            n = asNode(token);
        }
        if (n == null)
            throw new RiotException("Unrecognized token : " + token);
        tuple[idx] = n;
        idx++;
    }
    previousTuple = tuple;
    // Needs rethink.
    throw new NotImplemented();
//        if ( line.size() == 3 )
//            return new StreamRowRDFBase(Triple.create(tuple[0], tuple[1], tuple[2])) ;  
//        else 
//            return new StreamRowRDFBase(Quad.create(tuple[0], tuple[1], tuple[2], tuple[3])) ;
//        return new StreamRowRDFBase(Tuple.create(tuple)) ;
}
Also used : RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node) NotImplemented(org.apache.jena.atlas.lib.NotImplemented) Token(org.apache.jena.riot.tokens.Token)

Example 35 with Token

use of org.apache.jena.riot.tokens.Token in project jena by apache.

the class JSONInputIterator method preParse.

/**
     * Does the pre-parsing which attempts to read the header of the results
     * file and determine variables present
     * <p>
     * If the header is encountered first then we read this, if the results are
     * encountered first we parse the first 100 results and determine the
     * variables present from those instead
     * </p>
     */
private void preParse() {
    // First off the { to start the object
    expect("Expected the start of the JSON Results Object", TokenType.LBRACE);
    // Loop here because we might see some things we can discard first
    do {
        if (!isPropertyName()) {
            Token t = nextToken();
            String name = t.getImage();
            checkColon();
            if (name.equals("head")) {
                if (headerSeen)
                    exception(t, "Invalid duplicate header property");
                parseHeader();
            // Continue afterwards because we want to be in place to
            // start streaming results
            } else if (name.equals("boolean")) {
                parseBoolean();
            // Afterwards we continue because we want to see an empty
            // head
            } else if (name.equals("results")) {
                if (isBooleanResults)
                    exception(t, "Encountered results property when boolean property has already been countered");
                // Scroll to first result
                parseToFirstResult();
                // If we already saw the header then exit at this point
                if (headerSeen)
                    return;
                // If not we're going to pre-cache some chunk of results so
                // we can infer the variable names
                boolean complete = cacheResults(100);
                // header later...
                if (!complete) {
                    // TODO Now determine variables present from this
                    return;
                }
            } else {
                ignoreValue();
            }
            checkComma(TokenType.RBRACE);
        } else if (lookingAt(TokenType.RBRACE)) {
            // We hit the end of the result object already
            if (!headerSeen)
                exception(peekToken(), "End of JSON Results Object encountered before a valid header was seen");
            nextToken();
            // Shouldn't be any further content
            if (!lookingAt(TokenType.EOF))
                exception(peekToken(), "Unexpected content after end of JSON Results Object");
            // Can stop our initial buffering at this stage
            return;
        } else {
            exception(peekToken(), "Expected a JSON property name but got %s", peekToken());
        }
    } while (true);
}
Also used : Token(org.apache.jena.riot.tokens.Token)

Aggregations

Token (org.apache.jena.riot.tokens.Token)40 Node (org.apache.jena.graph.Node)16 RiotException (org.apache.jena.riot.RiotException)4 Tokenizer (org.apache.jena.riot.tokens.Tokenizer)4 AtlasException (org.apache.jena.atlas.AtlasException)2 RiotParseException (org.apache.jena.riot.RiotParseException)2 InputStream (java.io.InputStream)1 NoSuchElementException (java.util.NoSuchElementException)1 NotImplemented (org.apache.jena.atlas.lib.NotImplemented)1 Timer (org.apache.jena.atlas.lib.Timer)1 RDFDatatype (org.apache.jena.datatypes.RDFDatatype)1 IRI (org.apache.jena.iri.IRI)1 LabelToNode (org.apache.jena.riot.lang.LabelToNode)1 StreamRowRDF (org.apache.jena.riot.system.StreamRowRDF)1 StringType (org.apache.jena.riot.tokens.StringType)1 IRI (org.apache.jena.riot.tokens.TokenType.IRI)1 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)1 TDBException (org.apache.jena.tdb.TDBException)1