Search in sources :

Example 36 with Token

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

the class JSONInputIterator method expect.

protected final void expect(String msg, TokenType ttype) {
    if (!lookingAt(ttype)) {
        Token location = peekToken();
        exception(location, msg);
    }
    nextToken();
}
Also used : Token(org.apache.jena.riot.tokens.Token)

Example 37 with Token

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

the class JSONInputIterator method parseBoolean.

private void parseBoolean() {
    isBooleanResults = true;
    if (lookingAt(TokenType.KEYWORD)) {
        Token t = nextToken();
        String keyword = t.getImage();
        if (keyword.equals("true")) {
            boolResult = true;
        } else if (keyword.equals("false")) {
            boolResult = false;
        } else {
            exception(t, "Unexpected keyword %s encountered, expected true or false", keyword);
        }
    } else {
        exception(peekToken(), "Unexpected token when a true/false keyword was expected for the value of the boolean property");
    }
}
Also used : Token(org.apache.jena.riot.tokens.Token)

Example 38 with Token

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

the class NodeFactoryExtra method parseNode.

/**
     * Parse a string into a node.
     * <p>
     * Allows surrounding white space.
     * </p>
     * 
     * @param nodeString Node string to parse
     * @param pmap Prefix Map, null to use no prefix mappings
     * @return Parsed Node
     * @throws RiotException Thrown if a valid node cannot be parsed
     */
public static Node parseNode(String nodeString, PrefixMap pmap) {
    Tokenizer tokenizer = TokenizerFactory.makeTokenizerString(nodeString);
    if (!tokenizer.hasNext())
        throw new RiotException("Empty RDF term");
    Token token = tokenizer.next();
    Node node = token.asNode(pmap);
    if (node == null)
        throw new RiotException("Bad RDF Term: " + nodeString);
    if (tokenizer.hasNext())
        throw new RiotException("Trailing characters in string: " + nodeString);
    if (node.isURI()) {
        // Lightly test for bad URIs.
        String x = node.getURI();
        if (x.indexOf(' ') >= 0)
            throw new RiotException("Space(s) in  IRI: " + nodeString);
    }
    return node;
}
Also used : RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) Tokenizer(org.apache.jena.riot.tokens.Tokenizer)

Example 39 with Token

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

the class JSONInputIterator method ignoreValue.

private void ignoreValue() {
    if (isPropertyName()) {
        // Just a string value so can discard and then check for the
        // subsequent comma
        nextToken();
        checkComma(TokenType.RBRACE);
    } else if (lookingAt(TokenType.DECIMAL) || lookingAt(TokenType.INTEGER) || lookingAt(TokenType.DOUBLE) || lookingAt(TokenType.KEYWORD)) {
        // Just a numeric/keyword (boolean) value do discard and check for
        // subsequent comma
        nextToken();
        checkComma(TokenType.RBRACE);
    } else if (lookingAt(TokenType.LBRACE)) {
        // Start of an Object
        nextToken();
        // TODO We should really care about the syntactic validity of
        // objects we are ignoring but that seems like a bit too much effort
        int openBraces = 1;
        while (openBraces >= 1) {
            Token next = nextToken();
            if (next.getType().equals(TokenType.LBRACE)) {
                openBraces++;
            } else if (next.getType().equals(TokenType.RBRACE)) {
                openBraces--;
            }
        }
        checkComma(TokenType.RBRACE);
    } else if (lookingAt(TokenType.LBRACKET)) {
        // Start of an Array
        nextToken();
        // TODO We should really care about the syntactic validity of
        // objects we are ignoring but that seems like a bit too much effort
        int openBraces = 1;
        while (openBraces >= 1) {
            Token next = nextToken();
            if (next.getType().equals(TokenType.LBRACKET)) {
                openBraces++;
            } else if (next.getType().equals(TokenType.RBRACKET)) {
                openBraces--;
            }
        }
        checkComma(TokenType.RBRACE);
    } else {
        exception(peekToken(), "Unexpected Token");
    }
}
Also used : Token(org.apache.jena.riot.tokens.Token)

Example 40 with Token

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

the class JSONInputIterator method nextToken.

protected final Token nextToken() {
    if (eof())
        return tokenEOF;
    // Tokenizer errors appear here!
    try {
        Token t = peekIter.next();
        currLine = t.getLine();
        currCol = t.getColumn();
        return t;
    } catch (RiotParseException ex) {
        // Intercept to log it.
        raiseException(ex);
        throw ex;
    } catch (AtlasException ex) {
        // Bad I/O
        RiotParseException ex2 = new RiotParseException(ex.getMessage(), -1, -1);
        raiseException(ex2);
        throw ex2;
    }
}
Also used : RiotParseException(org.apache.jena.riot.RiotParseException) Token(org.apache.jena.riot.tokens.Token) AtlasException(org.apache.jena.atlas.AtlasException)

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