Search in sources :

Example 1 with Token

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

the class IteratorStreamRDFText method moveToNext.

@Override
protected StreamRowRDF moveToNext() {
    if (!in.hasNext())
        return null;
    List<Token> line = in.next();
    StreamRowRDF row = line2row(line);
    return row;
}
Also used : Token(org.apache.jena.riot.tokens.Token) StreamRowRDF(org.apache.jena.riot.system.StreamRowRDF)

Example 2 with Token

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

the class ParserProfileStd method create.

@Override
public Node create(Node currentGraph, Token token) {
    // Dispatches to the underlying ParserFactory operation
    long line = token.getLine();
    long col = token.getColumn();
    String str = token.getImage();
    switch(token.getType()) {
        case BNODE:
            return createBlankNode(currentGraph, str, line, col);
        case IRI:
            return createURI(str, line, col);
        case PREFIXED_NAME:
            {
                String prefix = str;
                String suffix = token.getImage2();
                String expansion = expandPrefixedName(prefix, suffix, token);
                return createURI(expansion, line, col);
            }
        case DECIMAL:
            return createTypedLiteral(str, XSDDatatype.XSDdecimal, line, col);
        case DOUBLE:
            return createTypedLiteral(str, XSDDatatype.XSDdouble, line, col);
        case INTEGER:
            return createTypedLiteral(str, XSDDatatype.XSDinteger, line, col);
        case LITERAL_DT:
            {
                Token tokenDT = token.getSubToken2();
                String uriStr;
                switch(tokenDT.getType()) {
                    case IRI:
                        uriStr = tokenDT.getImage();
                        break;
                    case PREFIXED_NAME:
                        {
                            String prefix = tokenDT.getImage();
                            String suffix = tokenDT.getImage2();
                            uriStr = expandPrefixedName(prefix, suffix, tokenDT);
                            break;
                        }
                    default:
                        throw new RiotException("Expected IRI for datatype: " + token);
                }
                uriStr = resolveIRI(uriStr, tokenDT.getLine(), tokenDT.getColumn());
                RDFDatatype dt = NodeFactory.getType(uriStr);
                return createTypedLiteral(str, dt, line, col);
            }
        case LITERAL_LANG:
            return createLangLiteral(str, token.getImage2(), line, col);
        case STRING:
            return createStringLiteral(str, line, col);
        default:
            {
                Node x = createNodeFromToken(currentGraph, token, line, col);
                if (x != null)
                    return x;
                errorHandler.fatal("Not a valid token for an RDF term: " + token, line, col);
                return null;
            }
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Example 3 with Token

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

the class RiotLib method parse.

/** Parse a string to get one Node (the first token in the string) */
public static Node parse(String string) {
    Tokenizer tokenizer = TokenizerFactory.makeTokenizerString(string);
    if (!tokenizer.hasNext())
        return null;
    Token t = tokenizer.next();
    Node n = profile.create(null, t);
    if (tokenizer.hasNext())
        Log.warn(RiotLib.class, "String has more than one token in it: " + string);
    return n;
}
Also used : LabelToNode(org.apache.jena.riot.lang.LabelToNode) Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) Tokenizer(org.apache.jena.riot.tokens.Tokenizer)

Example 4 with Token

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

the class JSONInputIterator method parseVars.

private void parseVars() {
    if (lookingAt(TokenType.LBRACKET)) {
        nextToken();
        vars.clear();
        do {
            if (lookingAt(TokenType.STRING)) {
                Token t = nextToken();
                String var = t.getImage();
                vars.add(var);
                checkComma(TokenType.RBRACKET);
            } else if (lookingAt(TokenType.RBRACKET)) {
                nextToken();
                return;
            } else {
                exception(peekToken(), "Unexpected Token encountered while parsing the variables list in the head object");
            }
        } while (true);
    } else {
        exception(peekToken(), "Unexpected Token ecountered, expected a [ to start the array of variables in the head object");
    }
}
Also used : Token(org.apache.jena.riot.tokens.Token)

Example 5 with Token

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

the class NodecSSE method decode.

@Override
public Node decode(ByteBuffer bb, PrefixMapping pmap) {
    // Ideally, this would be straight from the byte buffer.
    // But currently we go bytes -> string -> node 
    // Byte -> String
    String str = BlockUTF8.toString(bb);
    // Easy cases.
    if (str.startsWith("_:")) {
        // Must be done this way.
        // In particular, bnode labels can contain ":" from Jena
        // TokenizerText does not recognize these.
        str = str.substring(2);
        return NodeFactory.createBlankNode(str);
    }
    if (str.startsWith("<")) {
        // Do directly.
        // (is it quicker?)
        str = str.substring(1, str.length() - 1);
        str = StrUtils.unescapeString(str);
        str = StrUtils.decodeHex(str, MarkerChar);
        return NodeFactory.createURI(str);
    }
    Tokenizer tokenizer = TokenizerFactory.makeTokenizerString(str);
    if (!tokenizer.hasNext())
        throw new TDBException("Failed to tokenise: " + str);
    Token t = tokenizer.next();
    try {
        Node n = t.asNode();
        if (n == null)
            throw new TDBException("Not a node: " + str);
        return n;
    } catch (RiotException ex) {
        throw new TDBException("Bad string for node: " + str);
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) TDBException(org.apache.jena.tdb.TDBException) Node(org.apache.jena.graph.Node) Token(org.apache.jena.riot.tokens.Token) Tokenizer(org.apache.jena.riot.tokens.Tokenizer)

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