Search in sources :

Example 1 with RiotException

use of org.apache.jena.riot.RiotException 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 2 with RiotException

use of org.apache.jena.riot.RiotException in project jena by apache.

the class SPARQL_REST_RW method addDataIntoTxn.

/** Directly add data in a transaction.
     * Assumes recovery from parse errors by transaction abort.
     * Return whether the target existed before.
     * @param action
     * @param cleanDest Whether to remove data first (true = PUT, false = POST)
     * @return whether the target existed beforehand
     */
protected static boolean addDataIntoTxn(HttpAction action, boolean overwrite) {
    action.beginWrite();
    Target target = determineTarget(action);
    boolean existedBefore = false;
    try {
        if (log.isDebugEnabled())
            log.debug("  ->" + target);
        existedBefore = target.exists();
        Graph g = target.graph();
        if (overwrite && existedBefore)
            clearGraph(target);
        StreamRDF sink = StreamRDFLib.graph(g);
        incomingData(action, sink);
        action.commit();
        return existedBefore;
    } catch (RiotException ex) {
        // Parse error
        action.abort();
        errorBadRequest(ex.getMessage());
        return existedBefore;
    } catch (Exception ex) {
        // Something else went wrong.  Backout.
        action.abort();
        errorOccurred(ex.getMessage());
        return existedBefore;
    } finally {
        action.endWrite();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF) RiotException(org.apache.jena.riot.RiotException) IOException(java.io.IOException)

Example 3 with RiotException

use of org.apache.jena.riot.RiotException 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)

Example 4 with RiotException

use of org.apache.jena.riot.RiotException in project jena by apache.

the class TSVInput method fromTSV.

/**
	 * Reads SPARQL Results from TSV format into a {@link ResultSet} instance
	 * @param in Input Stream
	 */
public static ResultSet fromTSV(InputStream in) {
    BufferedReader reader = IO.asBufferedUTF8(in);
    List<Var> vars = new ArrayList<>();
    List<String> varNames = new ArrayList<>();
    String str = null;
    try {
        // Here we try to parse only the Header Row
        str = reader.readLine();
        if (str == null)
            throw new ARQException("TSV Results malformed, input is empty (no header row)");
        if (!str.isEmpty()) {
            String[] tokens = pattern.split(str, -1);
            for (String token : tokens) {
                Node v;
                try {
                    v = NodeFactoryExtra.parseNode(token);
                    if (v == null || !v.isVariable())
                        throw new ResultSetException("TSV Results malformed, not a variable: " + token);
                } catch (RiotException ex) {
                    throw new ResultSetException("TSV Results malformed, variable names must begin with a ? in the header: " + token);
                }
                Var var = Var.alloc(v);
                vars.add(var);
                varNames.add(var.getName());
            }
        }
    } catch (IOException ex) {
        throw new ARQException(ex);
    }
    //This will parse actual result rows as needed thus minimising memory usage
    return new ResultSetStream(varNames, null, new TSVInputIterator(reader, vars));
}
Also used : Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) RiotException(org.apache.jena.riot.RiotException) ARQException(org.apache.jena.sparql.ARQException) BufferedReader(java.io.BufferedReader)

Example 5 with RiotException

use of org.apache.jena.riot.RiotException in project jena by apache.

the class TSVInputIterator method parseNextBinding.

private boolean parseNextBinding() {
    String line;
    try {
        line = this.reader.readLine();
        // return false because there are no further bindings
        if (line == null)
            return false;
        this.lineNum++;
    } catch (IOException e) {
        throw new ResultSetException("Error parsing TSV results - " + e.getMessage());
    }
    if (line.isEmpty()) {
        // which means a non-empty string which we handle normally
        if (expectedItems > 1)
            throw new ResultSetException(format("Error Parsing TSV results at Line %d - The result row had 0/1 values when %d were expected", this.lineNum, expectedItems));
        this.binding = BindingFactory.create();
        return true;
    }
    String[] tokens = TSVInput.pattern.split(line, -1);
    if (tokens.length != expectedItems)
        throw new ResultSetException(format("Error Parsing TSV results at Line %d - The result row '%s' has %d values instead of the expected %d.", this.lineNum, line, tokens.length, expectedItems));
    this.binding = BindingFactory.create();
    for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i];
        // If we see an empty string this denotes an unbound value
        if (token.equals(""))
            continue;
        // Bound value so parse it and add to the binding
        try {
            Node node = NodeFactoryExtra.parseNode(token);
            if (!node.isConcrete())
                throw new ResultSetException(format("Line %d: Not a concrete RDF term: %s", lineNum, token));
            this.binding.add(this.vars.get(i), node);
        } catch (RiotException ex) {
            throw new ResultSetException(format("Line %d: Data %s contains error: %s", lineNum, token, ex.getMessage()));
        }
    }
    return true;
}
Also used : RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node) IOException(java.io.IOException)

Aggregations

RiotException (org.apache.jena.riot.RiotException)33 Node (org.apache.jena.graph.Node)10 IOException (java.io.IOException)7 StreamRDF (org.apache.jena.riot.system.StreamRDF)7 Graph (org.apache.jena.graph.Graph)4 Model (org.apache.jena.rdf.model.Model)4 Lang (org.apache.jena.riot.Lang)4 Token (org.apache.jena.riot.tokens.Token)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 Dataset (org.apache.jena.query.Dataset)3 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)2 IRI (org.apache.jena.iri.IRI)2 Tokenizer (org.apache.jena.riot.tokens.Tokenizer)2 Var (org.apache.jena.sparql.core.Var)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1 RDFDataset (com.github.jsonldjava.core.RDFDataset)1 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1