Search in sources :

Example 36 with Lang

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

the class ModLangParse method processArgs.

@Override
public void processArgs(CmdArgModule cmdLine) {
    if (cmdLine.contains(argValidate)) {
        validate = true;
        strict = true;
        explicitCheck = true;
        bitbucket = true;
    }
    if (cmdLine.contains(argSyntax)) {
        String syntax = cmdLine.getValue(argSyntax);
        Lang lang$ = RDFLanguages.nameToLang(syntax);
        if (lang$ == null)
            throw new CmdException("Can not detemine the syntax from '" + syntax + "'");
        this.lang = lang$;
    }
    if (cmdLine.contains(argCheck))
        explicitCheck = true;
    if (cmdLine.contains(argNoCheck))
        explicitNoCheck = true;
    if (cmdLine.contains(argStrict))
        strict = true;
    if (cmdLine.contains(argSkip))
        skipOnBadTerm = true;
    if (cmdLine.contains(argNoSkip))
        skipOnBadTerm = false;
    if (cmdLine.contains(argBase)) {
        baseIRI = cmdLine.getValue(argBase);
        IRI iri = IRIResolver.resolveIRI(baseIRI);
        if (iri.hasViolation(false))
            throw new CmdException("Bad base IRI: " + baseIRI);
        if (!iri.isAbsolute())
            throw new CmdException("Base IRI must be an absolute IRI: " + baseIRI);
    }
    if (cmdLine.contains(argStop))
        stopOnBadTerm = true;
    if (cmdLine.contains(argSink))
        bitbucket = true;
    if (cmdLine.contains(argRDFS)) {
        try {
            rdfsVocabFilename = cmdLine.getArg(argRDFS).getValue();
            rdfsVocab = FileManager.get().loadModel(rdfsVocabFilename);
        } catch (RiotException ex) {
            throw new CmdException("Error in RDFS vocabulary: " + rdfsVocabFilename);
        } catch (Exception ex) {
            throw new CmdException("Error: " + ex.getMessage());
        }
    }
}
Also used : IRI(org.apache.jena.iri.IRI) RiotException(org.apache.jena.riot.RiotException) CmdException(jena.cmd.CmdException) Lang(org.apache.jena.riot.Lang) RiotException(org.apache.jena.riot.RiotException) CmdException(jena.cmd.CmdException)

Example 37 with Lang

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

the class ResultSetFactory method load.

/**
     * Load a result set from input stream into a result set (memory backed).
     * 
     * @param input
     * @param format
     * @return ResultSet
     */
public static ResultSet load(InputStream input, ResultsFormat format) {
    if (format == null) {
        Log.warn(ResultSet.class, "Null format - defaulting to XML");
        format = ResultsFormat.FMT_RS_XML;
    }
    // Old World - new world
    Lang lang = ResultsFormat.convert(format);
    if (lang != null)
        return ResultSetMgr.read(input, lang);
    if (format.equals(ResultsFormat.FMT_RS_JSON))
        return JSONInput.fromJSON(input);
    if (format.equals(ResultsFormat.FMT_RS_TSV))
        return TSVInput.fromTSV(input);
    if (format.equals(ResultsFormat.FMT_RS_CSV))
        return CSVInput.fromCSV(input);
    if (format.equals(ResultsFormat.FMT_RS_BIO))
        return BIOInput.fromBIO(input);
    if (format.equals(ResultsFormat.FMT_RS_XML))
        return ResultSetFactory.fromXML(input);
    if (format.equals(ResultsFormat.FMT_TEXT)) {
        Log.warn(ResultSet.class, "Can't read a text result set");
        throw new ResultSetException("Can't read a text result set");
    }
    if (format.equals(ResultsFormat.FMT_RDF_XML)) {
        Model m = ModelFactory.createDefaultModel();
        m.read(input, null);
        return RDFInput.fromRDF(m);
    }
    if (format.equals(ResultsFormat.FMT_RDF_TTL)) {
        Model m = ModelFactory.createDefaultModel();
        m.read(input, null, "TURTLE");
        return RDFInput.fromRDF(m);
    }
    if (format.equals(ResultsFormat.FMT_RDF_N3)) {
        Model m = ModelFactory.createDefaultModel();
        m.read(input, null, "N3");
        return RDFInput.fromRDF(m);
    }
    if (format.equals(ResultsFormat.FMT_RDF_NT)) {
        Model m = ModelFactory.createDefaultModel();
        m.read(input, null, "N-TRIPLES");
        return RDFInput.fromRDF(m);
    }
    Log.warn(ResultSet.class, "Unknown result set syntax: " + format);
    return null;
}
Also used : Model(org.apache.jena.rdf.model.Model) Lang(org.apache.jena.riot.Lang)

Example 38 with Lang

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

the class UpdateEngineWorker method visit.

@Override
public void visit(UpdateLoad update) {
    // LOAD SILENT? iri ( INTO GraphRef )? 
    String source = update.getSource();
    Node dest = update.getDest();
    try {
        // Read into temporary storage to protect against parse errors.
        if (dest != null) {
            Lang lang = RDFLanguages.filenameToLang(source);
            // load-to-graph - must be triples. 
            if (!RDFLanguages.isTriples(lang))
                throw new UpdateException("Attempt to load quads into a graph");
            // LOAD-INTO graph ... must be triples.
            Graph g = GraphFactory.createGraphMem();
            RDFParser.source(source).parse(g);
            Graph g2 = graph(datasetGraph, dest);
            GraphUtil.addInto(g2, g);
        } else {
            // LOAD <iri> -- allow quads.
            DatasetGraph dsg = DatasetGraphFactory.create();
            RDFParser.source(source).parse(dsg);
            Iterator<Quad> iter = dsg.find();
            for (; iter.hasNext(); ) {
                Quad q = iter.next();
                datasetGraph.add(q);
            }
        }
    } catch (RuntimeException ex) {
        if (!update.getSilent()) {
            if (ex instanceof UpdateException)
                throw ex;
            throw new UpdateException("Failed to LOAD '" + source + "'", ex);
        }
    }
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) TemplateLib.remapDefaultGraph(org.apache.jena.sparql.modify.TemplateLib.remapDefaultGraph) Graph(org.apache.jena.graph.Graph) Node(org.apache.jena.graph.Node) Lang(org.apache.jena.riot.Lang) UpdateException(org.apache.jena.update.UpdateException)

Example 39 with Lang

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

the class TestResultSetIO method data.

@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
    Lang[] langs = { SPARQLResultSetXML, SPARQLResultSetJSON, SPARQLResultSetCSV, SPARQLResultSetTSV };
    List<Object[]> x = new ArrayList<>();
    for (Lang lang : langs) {
        x.add(new Object[] { "test:" + lang.getName(), lang });
    }
    return x;
}
Also used : ArrayList(java.util.ArrayList) Lang(org.apache.jena.riot.Lang) Parameters(org.junit.runners.Parameterized.Parameters)

Example 40 with Lang

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

the class UnitTestEval method run4.

private void run4() {
    DatasetGraph dsg = DatasetGraphFactory.create();
    try {
        if (baseIRI != null)
            RDFDataMgr.read(dsg, input, baseIRI, lang);
        else
            RDFDataMgr.read(dsg, input, lang);
        Lang outLang = RDFLanguages.filenameToLang(output, Lang.NQUADS);
        DatasetGraph results = DatasetGraphFactory.create();
        try {
            RDFDataMgr.read(results, output, outLang);
        } catch (RiotException ex) {
            fail("Failed to read results: " + ex.getMessage());
        }
        boolean b = isomorphic(dsg, results);
        if (!b) {
            System.out.println("**** Test: " + getName());
            System.out.println("---- Parsed");
            RDFDataMgr.write(System.out, dsg, Lang.TRIG);
            System.out.println("---- Expected");
            RDFDataMgr.write(System.out, results, Lang.TRIG);
            System.out.println("--------");
        }
        assertTrue("Datasets not isomorphic", b);
    } catch (RiotException ex) {
        // Catch and rethrow - debugging.
        throw ex;
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.err);
        throw ex;
    }
}
Also used : RiotException(org.apache.jena.riot.RiotException) Lang(org.apache.jena.riot.Lang) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

Lang (org.apache.jena.riot.Lang)41 InputStream (java.io.InputStream)10 CmdException (jena.cmd.CmdException)8 IOException (java.io.IOException)6 Model (org.apache.jena.rdf.model.Model)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 GZIPInputStream (java.util.zip.GZIPInputStream)4 ServletOutputStream (javax.servlet.ServletOutputStream)4 ContentType (org.apache.jena.atlas.web.ContentType)4 MediaType (org.apache.jena.atlas.web.MediaType)4 RiotException (org.apache.jena.riot.RiotException)4 Dataset (org.apache.jena.query.Dataset)3 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)3 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 BaseTest (org.apache.jena.atlas.junit.BaseTest)2