Java Code Examples for org.apache.jena.riot.Lang#CSV

The following examples show how to use org.apache.jena.riot.Lang#CSV . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Run a SELECT query over the union of named graphs in a dataset and write the result to a file.
 * Prints violations to STDERR.
 *
 * @param dataset The dataset to query over.
 * @param ruleName The name of the rule to verify.
 * @param query The SPARQL query string.
 * @param outputPath The file path to write to, if there are results
 * @param outputFormat The file format.
 * @throws IOException if output file is not found or query cannot be parsed
 * @return true if the are results (so file is written), false otherwise
 */
public static boolean runVerify(
    Dataset dataset, String ruleName, String query, Path outputPath, Lang outputFormat)
    throws IOException {
  if (outputFormat == null) {
    outputFormat = Lang.CSV;
  }
  ResultSetRewindable results = ResultSetFactory.copyResults(execQuery(dataset, query));
  if (results.size() == 0) {
    System.out.println("PASS Rule " + ruleName + ": 0 violation(s)");
    return false;
  } else {
    System.out.println("FAIL Rule " + ruleName + ": " + results.size() + " violation(s)");
    ResultSetMgr.write(System.err, results, Lang.CSV);
    results.reset();
    FileOutputStream csvFile = new FileOutputStream(outputPath.toFile());
    writeResult(results, outputFormat, csvFile);
    return true;
  }
}
 
Example 2
Source File: QueryOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Convert a format name string to a language code.
 *
 * @param formatName the format name as a string
 * @return the format language code or null
 */
public static Lang getFormatLang(String formatName) {
  Lang format;
  formatName = formatName.toLowerCase();
  switch (formatName) {
    case "tsv":
      format = ResultSetLang.SPARQLResultSetTSV;
      break;
    case "ttl":
      format = Lang.TTL;
      break;
    case "jsonld":
      format = Lang.JSONLD;
      break;
    case "nt":
      format = Lang.NT;
      break;
    case "nq":
      format = Lang.NQ;
      break;
    case "csv":
      format = Lang.CSV;
      break;
    case "xml":
      format = Lang.RDFXML;
      break;
    case "sxml":
      format = ResultSetLang.SPARQLResultSetXML;
      break;
    default:
      format = null;
  }
  return format;
}
 
Example 3
Source File: QueryOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Run a SELECT query on a dataset and write the result to a file.
 *
 * @param dataset The dataset to query over.
 * @param query The SPARQL query string.
 * @param output The file to write to.
 * @param outputFormat The file format.
 * @throws IOException if output file is not found or query cannot be parsed
 */
public static void runQuery(Dataset dataset, String query, File output, Lang outputFormat)
    throws IOException {
  if (outputFormat == null) {
    outputFormat = Lang.CSV;
  }
  writeResult(execQuery(dataset, query), outputFormat, new FileOutputStream(output));
}
 
Example 4
Source File: QueryOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Write a result set to an output stream.
 *
 * @param resultSet the results to write
 * @param format the language to write in (if null, CSV)
 * @param output the output stream to write to
 */
public static void writeResult(ResultSet resultSet, Lang format, OutputStream output) {
  if (format == null) {
    format = Lang.CSV;
  }
  ResultSetMgr.write(output, resultSet, format);
}