Search in sources :

Example 16 with RiotException

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

the class JenaRDF2JSONLD method parse.

@Override
public RDFDataset parse(Object object) throws JsonLdError {
    RDFDataset result = new RDFDataset();
    if (object instanceof DatasetGraph) {
        DatasetGraph dsg = (DatasetGraph) object;
        Iterator<Quad> iter = dsg.find();
        for (; iter.hasNext(); ) {
            Quad q = iter.next();
            Node s = q.getSubject();
            Node p = q.getPredicate();
            Node o = q.getObject();
            Node g = q.getGraph();
            String gq = null;
            if (g != null && !Quad.isDefaultGraph(g)) {
                gq = blankNodeOrIRIString(g);
                if (gq == null)
                    throw new RiotException("Graph node is not a URI or a blank node");
            }
            String sq = blankNodeOrIRIString(s);
            if (sq == null)
                throw new RiotException("Subject node is not a URI or a blank node");
            String pq = p.getURI();
            if (o.isLiteral()) {
                String lex = o.getLiteralLexicalForm();
                String lang = o.getLiteralLanguage();
                String dt = o.getLiteralDatatypeURI();
                if (lang != null && lang.length() == 0) {
                    lang = null;
                //dt = RDF.getURI()+"langString" ;
                }
                if (dt == null)
                    dt = XSDDatatype.XSDstring.getURI();
                result.addQuad(sq, pq, lex, dt, lang, gq);
            } else {
                String oq = blankNodeOrIRIString(o);
                result.addQuad(sq, pq, oq, gq);
            }
        }
    } else
        Log.warn(JenaRDF2JSONLD.class, "unknown");
    return result;
}
Also used : RDFDataset(com.github.jsonldjava.core.RDFDataset) Quad(org.apache.jena.sparql.core.Quad) RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 17 with RiotException

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

the class RDFJSONWriter method send.

private static void send(JSWriter out, Pair<Node, Map<Node, Set<Node>>> item) {
    Node s = item.getLeft();
    if (s.isBlank()) {
        out.key("_:" + s.getBlankNodeLabel());
    } else if (s.isURI()) {
        out.key(s.getURI());
    } else {
        throw new RiotException("Only URIs or blank nodes are legal subjects.");
    }
    out.startObject();
    // out.pair(key, value) ;
    Map<Node, Set<Node>> predicates = item.getRight();
    for (Node p : predicates.keySet()) {
        out.key(p.getURI());
        out.startArray();
        Set<Node> objects = predicates.get(p);
        int i = 0;
        for (Node o : objects) {
            out.startObject();
            if (o.isBlank()) {
                out.pair("type", "bnode");
                out.pair("value", "_:" + o.getBlankNodeLabel());
            } else if (o.isURI()) {
                out.pair("type", "uri");
                out.pair("value", o.getURI());
            } else if (o.isLiteral()) {
                String lex = o.getLiteralLexicalForm();
                out.pair("type", "literal");
                out.pair("value", lex);
                if (NodeUtils.isSimpleString(o)) {
                // No-op.
                } else if (NodeUtils.isLangString(o)) {
                    String lang = o.getLiteralLanguage();
                    out.pair("lang", lang);
                } else {
                    // Datatype, nothing special.
                    String dt = o.getLiteralDatatypeURI();
                    out.pair("datatype", dt);
                }
            }
            out.finishObject();
            if (i < objects.size() - 1) {
                out.arraySep();
            }
            i++;
        }
        out.finishArray();
    }
    out.finishObject();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RiotException(org.apache.jena.riot.RiotException) Node(org.apache.jena.graph.Node)

Example 18 with RiotException

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

the class Token method create.

static Token create(String s) {
    PeekReader pr = PeekReader.readString(s);
    TokenizerText tt = new TokenizerText(pr);
    if (!tt.hasNext())
        throw new RiotException("No token");
    Token t = tt.next();
    if (tt.hasNext())
        throw new RiotException("Extraneous charcaters");
    return t;
}
Also used : PeekReader(org.apache.jena.atlas.io.PeekReader) RiotException(org.apache.jena.riot.RiotException)

Example 19 with RiotException

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

the class FactoryTestRiot method makeTest.

@Override
public Test makeTest(Resource manifest, Resource item, String testName, Resource action, Resource result) {
    try {
        Resource r = TestUtils.getResource(item, RDF.type);
        if (r == null)
            r = dftTestType;
        if (r == null)
            throw new RiotException("Can't determine the test type");
        if (labelPrefix != null)
            testName = labelPrefix + testName;
        // In Turtle tests, the action directly names the file to process.
        Resource input = action;
        Resource output = result;
        if (r.equals(VocabLangRDF.TestPositiveSyntaxTTL))
            return new UnitTestSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.TURTLE, report);
        if (r.equals(VocabLangRDF.TestNegativeSyntaxTTL))
            return new UnitTestBadSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.TURTLE, report);
        if (r.equals(VocabLangRDF.TestPositiveSyntaxTriG))
            return new UnitTestSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.TRIG, report);
        if (r.equals(VocabLangRDF.TestNegativeSyntaxTriG))
            return new UnitTestBadSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.TRIG, report);
        if (r.equals(VocabLangRDF.TestPositiveSyntaxNT))
            return new UnitTestSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.NTRIPLES, report);
        if (r.equals(VocabLangRDF.TestNegativeSyntaxNT))
            return new UnitTestBadSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.NTRIPLES, report);
        if (r.equals(VocabLangRDF.TestPositiveSyntaxNQ))
            return new UnitTestSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.NQUADS, report);
        if (r.equals(VocabLangRDF.TestNegativeSyntaxNQ))
            return new UnitTestBadSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.NQUADS, report);
        if (r.equals(VocabLangRDF.TestPositiveSyntaxRJ))
            return new UnitTestSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.RDFJSON, report);
        if (r.equals(VocabLangRDF.TestNegativeSyntaxRJ))
            return new UnitTestBadSyntax(testName, item.getURI(), input.getURI(), RDFLanguages.RDFJSON, report);
        if (r.equals(VocabLangRDF.TestSurpressed))
            return new UnitTestSurpressed(testName, item.getURI(), report);
        if (r.equals(VocabLangRDF.TestEvalTTL)) {
            String base = rebase(input, assumedRootURITurtle);
            return new UnitTestEval(testName, item.getURI(), input.getURI(), result.getURI(), base, RDFLanguages.TURTLE, report);
        }
        if (r.equals(VocabLangRDF.TestEvalTriG)) {
            String base = rebase(input, assumedRootURITriG);
            return new UnitTestEval(testName, item.getURI(), input.getURI(), result.getURI(), base, RDFLanguages.TRIG, report);
        }
        if (r.equals(VocabLangRDF.TestNegativeEvalTTL))
            return new UnitTestBadEval(testName, item.getURI(), input.getURI(), RDFLanguages.TURTLE, report);
        if (r.equals(VocabLangRDF.TestNegativeEvalTriG))
            return new UnitTestBadEval(testName, item.getURI(), input.getURI(), RDFLanguages.TRIG, report);
        if (r.equals(VocabLangRDF.TestEvalNT))
            return new UnitTestEval(testName, item.getURI(), input.getURI(), result.getURI(), null, RDFLanguages.NTRIPLES, report);
        if (r.equals(VocabLangRDF.TestNegativeEvalNT))
            return new UnitTestBadEval(testName, item.getURI(), input.getURI(), RDFLanguages.NTRIPLES, report);
        if (r.equals(VocabLangRDF.TestEvalRJ)) {
            String base = rebase(input, assumedRootURIex);
            return new UnitTestEval(testName, item.getURI(), input.getURI(), result.getURI(), base, RDFLanguages.RDFJSON, report);
        }
        if (r.equals(VocabLangRDF.TestNegativeEvalRJ))
            return new UnitTestBadEval(testName, item.getURI(), input.getURI(), RDFLanguages.RDFJSON, report);
        System.err.println("Unrecognized turtle test : (" + r + ")" + testName);
        return null;
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        System.err.println("Failed to grok test : " + testName);
        return null;
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) Resource(org.apache.jena.rdf.model.Resource) RiotException(org.apache.jena.riot.RiotException)

Example 20 with RiotException

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

the class UnitTestBadSyntax method run3.

private void run3() {
    Model model = ModelFactory.createDefaultModel();
    try {
        RDFDataMgr.read(model, uri, uri, lang);
    } catch (RiotException ex) {
        return;
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.err);
        fail("Unexpected exception");
    }
    fail("Bad syntax test succeed in parsing the file");
}
Also used : RiotException(org.apache.jena.riot.RiotException) Model(org.apache.jena.rdf.model.Model)

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