Search in sources :

Example 6 with Lang

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

the class QueryExecUtils method outputResultSet.

@SuppressWarnings("deprecation")
public static void outputResultSet(ResultSet results, Prologue prologue, ResultsFormat outputFormat) {
    // Proper ResultSet formats.
    Lang lang = ResultsFormat.convert(outputFormat);
    if (lang != null) {
        ResultSetMgr.write(System.out, results, lang);
        System.out.flush();
        return;
    }
    // Old way.
    boolean done = false;
    if (prologue == null)
        prologue = new Prologue(globalPrefixMap);
    if (outputFormat.equals(ResultsFormat.FMT_UNKNOWN))
        outputFormat = ResultsFormat.FMT_TEXT;
    if (outputFormat.equals(ResultsFormat.FMT_NONE) || outputFormat.equals(ResultsFormat.FMT_COUNT)) {
        int count = ResultSetFormatter.consume(results);
        if (outputFormat.equals(ResultsFormat.FMT_COUNT)) {
            System.out.println("Count = " + count);
        }
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RDF_XML) || outputFormat.equals(ResultsFormat.FMT_RDF_N3) || outputFormat.equals(ResultsFormat.FMT_RDF_TTL)) {
        Model m = RDFOutput.encodeAsModel(results);
        m.setNsPrefixes(prologue.getPrefixMapping());
        m.setNsPrefix("rs", ResultSetGraphVocab.getURI());
        RDFDataMgr.write(System.out, m, Lang.TURTLE);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_XML)) {
        ResultSetFormatter.outputAsXML(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_JSON)) {
        ResultSetFormatter.outputAsJSON(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_SSE)) {
        ResultSetFormatter.outputAsSSE(System.out, results, prologue);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_TEXT)) {
        ResultSetFormatter.out(System.out, results, prologue);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_TUPLES)) {
        PlainFormat pFmt = new PlainFormat(System.out, prologue);
        ResultSetApply a = new ResultSetApply(results, pFmt);
        a.apply();
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_CSV)) {
        ResultSetFormatter.outputAsCSV(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_TSV)) {
        ResultSetFormatter.outputAsTSV(System.out, results);
        done = true;
    }
    if (outputFormat.equals(ResultsFormat.FMT_RS_BIO)) {
        ResultSetFormatter.outputAsBIO(System.out, results);
        done = true;
    }
    if (!done)
        System.err.println("Unknown format request: " + outputFormat);
    results = null;
    System.out.flush();
}
Also used : Prologue(org.apache.jena.sparql.core.Prologue) ResultSetApply(org.apache.jena.sparql.resultset.ResultSetApply) Model(org.apache.jena.rdf.model.Model) Lang(org.apache.jena.riot.Lang) PlainFormat(org.apache.jena.sparql.resultset.PlainFormat)

Example 7 with Lang

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

the class ModLangOutput method processArgs.

@Override
public void processArgs(CmdArgModule cmdLine) {
    if (cmdLine.contains(argPretty)) {
        String langName = cmdLine.getValue(argPretty);
        Lang lang = RDFLanguages.nameToLang(langName);
        if (lang == null)
            throw new CmdException("Not recognized as an RDF language : '" + langName + "'");
        formattedOutput = RDFWriterRegistry.defaultSerialization(lang);
        if (formattedOutput == null) {
            System.err.println("Language '" + lang.getLabel() + "' not registered.");
            printRegistered(System.err);
            throw new CmdException("No output set: '" + langName + "'");
        }
    }
    if (cmdLine.contains(argStream)) {
        String langName = cmdLine.getValue(argStream);
        Lang lang = RDFLanguages.nameToLang(langName);
        if (lang == null)
            throw new CmdException("Not recognized as an RDF language : '" + langName + "'");
        streamOutput = StreamRDFWriter.defaultSerialization(lang);
        if (streamOutput == null) {
            System.err.println("Language '" + lang.getLabel() + "' not registered for streaming.");
            printRegistered(System.err);
            throw new CmdException("No output set: '" + langName + "'");
        }
    }
    if (cmdLine.contains(argOutput)) {
        String langName = cmdLine.getValue(argOutput);
        Lang lang = RDFLanguages.nameToLang(langName);
        if (lang == null)
            throw new CmdException("Not recognized as an RDF language : '" + langName + "'");
        if (StreamRDFWriter.registered(lang)) {
            streamOutput = StreamRDFWriter.defaultSerialization(lang);
        } else {
            formattedOutput = RDFWriterRegistry.defaultSerialization(lang);
            if (formattedOutput == null) {
                System.err.println("Language '" + lang.getLabel() + "' not recognized.");
                printRegistered(System.err);
                throw new CmdException("No output set: '" + langName + "'");
            }
        }
    }
    if (cmdLine.contains(argCompress))
        compressedOutput = true;
    if (streamOutput == null && formattedOutput == null)
        streamOutput = RDFFormat.NQUADS;
}
Also used : CmdException(jena.cmd.CmdException) Lang(org.apache.jena.riot.Lang)

Example 8 with Lang

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

the class BulkLoader method loadTriples$.

/** Load into a graph */
private static void loadTriples$(BulkStreamRDF dest, List<String> urls) {
    dest.startBulk();
    for (String url : urls) {
        loadLogger.info("Load: " + url + " -- " + DateTimeUtils.nowAsString());
        Lang lang = RDFLanguages.filenameToLang(url, Lang.NTRIPLES);
        RDFDataMgr.parse(dest, url, lang);
    }
    dest.finishBulk();
}
Also used : Lang(org.apache.jena.riot.Lang)

Example 9 with Lang

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

the class BulkLoader method loadQuads$.

/** Load quads into a dataset */
private static void loadQuads$(BulkStreamRDF dest, List<String> urls) {
    dest.startBulk();
    for (String url : urls) {
        loadLogger.info("Load: " + url + " -- " + DateTimeUtils.nowAsString());
        Lang lang = RDFLanguages.filenameToLang(url, Lang.NQUADS);
        RDFDataMgr.parse(dest, url, lang);
    }
    dest.finishBulk();
}
Also used : Lang(org.apache.jena.riot.Lang)

Example 10 with Lang

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

the class CmdNodeTableBuilder method processModulesAndArgs.

@Override
protected void processModulesAndArgs() {
    if (!super.contains(argLocation))
        throw new CmdException("Required: --loc DIR");
    //        if ( !super.contains(argTriplesOut) ) throw new CmdException("Required: --triples FILE") ;
    //        if ( !super.contains(argQuadsOut) ) throw new CmdException("Required: --quads FILE") ;
    locationString = super.getValue(argLocation);
    location = Location.create(locationString);
    dataFileTriples = super.getValue(argTriplesOut);
    if (dataFileTriples == null)
        dataFileTriples = location.getPath("triples", "tmp");
    dataFileQuads = super.getValue(argQuadsOut);
    if (dataFileQuads == null)
        dataFileQuads = location.getPath("quads", "tmp");
    if (Objects.equals(dataFileTriples, dataFileQuads))
        cmdError("Triples and Quads work files are the same");
    if (super.contains(argNoStats))
        collectStats = false;
    //datafiles  = getPositionalOrStdin() ;
    datafiles = getPositional();
    if (datafiles.isEmpty())
        datafiles = Arrays.asList("-");
    // ---- Checking.
    for (String filename : datafiles) {
        Lang lang = RDFLanguages.filenameToLang(filename, RDFLanguages.NQUADS);
        if (lang == null)
            // Does not happen due to default above.
            cmdError("File suffix not recognized: " + filename);
        if (!filename.equals("-") && !FileOps.exists(filename))
            cmdError("File does not exist: " + filename);
    }
}
Also used : CmdException(jena.cmd.CmdException) Lang(org.apache.jena.riot.Lang)

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