Search in sources :

Example 26 with RiotException

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

the class SPARQL_GSP_RW method addDataIntoNonTxn.

/** Add data where the destination does not support full transactions.
     *  In particular, with no abort, and actions probably going to the real storage
     *  parse errors can lead to partial updates.  Instead, parse to a temporary
     *  graph, then insert that data.  
     * @param action
     * @param cleanDest Whether to remove data first (true = PUT, false = POST)
     * @return whether the target existed beforehand.
     */
protected static UploadDetails addDataIntoNonTxn(HttpAction action, boolean overwrite) {
    Graph graphTmp = GraphFactory.createGraphMem();
    StreamRDF dest = StreamRDFLib.graph(graphTmp);
    UploadDetails details;
    try {
        details = Upload.incomingData(action, dest);
    } catch (RiotException ex) {
        ServletOps.errorBadRequest(ex.getMessage());
        return null;
    }
    // Now insert into dataset
    action.beginWrite();
    Target target = determineTarget(action);
    boolean existedBefore = false;
    try {
        if (action.log.isDebugEnabled())
            action.log.debug("  ->" + target);
        existedBefore = target.exists();
        if (overwrite && existedBefore)
            clearGraph(target);
        FusekiLib.addDataInto(graphTmp, target.dsg, target.graphName);
        details.setExistedBefore(existedBefore);
        action.commit();
        return details;
    } catch (Exception ex) {
        // but it might and there is no harm safely trying. 
        try {
            action.abort();
        } catch (Exception ex2) {
        }
        ServletOps.errorOccurred(ex.getMessage());
        return null;
    } 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)

Example 27 with RiotException

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

the class StreamRDFLimited method triple.

@Override
public void triple(Triple triple) {
    count++;
    if (count > limit)
        throw new RiotException("Limit (" + limit + ") reached");
    super.triple(triple);
}
Also used : RiotException(org.apache.jena.riot.RiotException)

Example 28 with RiotException

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

the class BindingOutputStream method send.

@Override
public void send(Binding binding) {
    try {
        if (needOutputPMap) {
            if (pmap != null) {
                for (Map.Entry<String, IRI> e : pmap.getMapping().entrySet()) {
                    bw.write("PREFIX ");
                    bw.write(e.getKey());
                    bw.write(": <");
                    bw.write(e.getValue().toASCIIString());
                    bw.write("> .\n");
                }
            }
            needOutputPMap = false;
        }
        // Is the current VARS applicable?
        if (needVars(vars, binding)) {
            vars = Iter.toList(binding.vars());
            needOutputVars = true;
        }
        if (needOutputVars) {
            // No vars, empty binding.
            if (binding.size() == 0 && vars.size() == 0) {
                bw.write(".\n");
                needOutputVars = false;
                return;
            }
            bw.write("VARS");
            for (Var v2 : vars) {
                bw.write(" ?");
                bw.write(v2.getVarName());
            }
            bw.write(" .\n");
            needOutputVars = false;
        }
        for (Var v : vars) {
            Node n = binding.get(v);
            if (n == null) {
                bw.write("- ");
                continue;
            }
            // NodeFormatters should write safe bNode labels.
            nodeFormatter.format(bw, n);
            bw.write(" ");
        }
        bw.write(".\n");
    } catch (IOException ex) {
        throw new RiotException(ex);
    }
}
Also used : IRI(org.apache.jena.iri.IRI) RiotException(org.apache.jena.riot.RiotException) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) IOException(java.io.IOException) PrefixMap(org.apache.jena.riot.system.PrefixMap) Map(java.util.Map)

Example 29 with RiotException

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

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

the class DataValidatorHTML method executeHTML.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("data validation request") ;
        String syntax = FusekiLib.safeParameter(httpRequest, paramSyntax);
        if (syntax == null || syntax.equals(""))
            syntax = RDFLanguages.NQUADS.getName();
        Lang language = RDFLanguages.shortnameToLang(syntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + syntax);
            return;
        }
        Reader input = createInput(httpRequest, httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        ErrorHandlerMsg errorHandler = new ErrorHandlerMsg(outStream);
        // Capture logging errors.
        PrintStream stderr = System.err;
        System.setErr(new PrintStream(outStream));
        // Headers
        setHeaders(httpResponse);
        outStream.println("<html>");
        printHead(outStream, "Jena Data Validator Report");
        outStream.println("<body>");
        outStream.println("<h1>RIOT Parser Report</h1>");
        outStream.println("<p>Line and column numbers refer to original input</p>");
        outStream.println("<p>&nbsp;</p>");
        // Need to escape HTML. 
        OutputStream output1 = new OutputStreamNoHTML(new BufferedOutputStream(outStream));
        StreamRDF output = StreamRDFWriter.getWriterStream(output1, Lang.NQUADS);
        try {
            startFixed(outStream);
            RDFParser parser = RDFParser.create().lang(language).errorHandler(errorHandler).resolveURIs(false).build();
            RiotException exception = null;
            startFixed(outStream);
            try {
                output.start();
                parser.parse(output);
                output.finish();
                output1.flush();
                outStream.flush();
                System.err.flush();
            } catch (RiotException ex) {
                ex.printStackTrace(stderr);
                exception = ex;
            }
        } finally {
            finishFixed(outStream);
            System.err.flush();
            System.setErr(stderr);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in validationRequest", ex);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) Lang(org.apache.jena.riot.Lang) RDFParser(org.apache.jena.riot.RDFParser) RiotException(org.apache.jena.riot.RiotException) RiotException(org.apache.jena.riot.RiotException) StreamRDF(org.apache.jena.riot.system.StreamRDF)

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