From a083c63c75ec3bc9e9da11b1a9e4e98e0c263458 Mon Sep 17 00:00:00 2001 From: Enrico Daga Date: Mon, 6 Feb 2023 12:24:47 +0000 Subject: [PATCH] Implement #342 --- .../src/main/java/com/github/sparqlanything/cli/CLI.java | 9 +++++++++ .../com/github/sparqlanything/cli/SPARQLAnything.java | 9 +++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/CLI.java b/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/CLI.java index 9dea8f8e..ec5e8bd5 100644 --- a/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/CLI.java +++ b/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/CLI.java @@ -47,6 +47,9 @@ public class CLI { public static final String OUTPUT = "o"; public static final String OUTPUT_LONG = "output"; + public static final String OUTPUT_APPEND = "a"; + public static final String OUTPUT_APPEND_LONG = "append"; + public static final String FORMAT = "f"; public static final String FORMAT_LONG = "format"; @@ -118,6 +121,9 @@ void init(){ options.addOption(Option.builder(OUTPUT).argName("file").hasArg() .desc("OPTIONAL - The path to the output file. [Default: STDOUT]").longOpt(OUTPUT_LONG).build()); + options.addOption(Option.builder(OUTPUT_APPEND).hasArg(false) + .desc("OPTIONAL - Should output to file be appended? WARNING: this option does not ensure that the whole file is valid -- that is up to the user to set up the conditions (such as using NQ serialization and not using bnodes)").longOpt(OUTPUT_APPEND_LONG).build()); + options.addOption(Option.builder(EXPLAIN).argName("explain").hasArg(false) .desc("OPTIONAL - Explain query execution").longOpt(EXPLAIN_LONG).build()); @@ -172,6 +178,9 @@ public String getOutputFile() { return commandLine.getOptionValue(CLI.OUTPUT); } + public boolean getOutputAppend() { + return commandLine.hasOption(CLI.OUTPUT_APPEND); + } public String getOutputPattern() { return commandLine.getOptionValue(CLI.OUTPUTPATTERN); } diff --git a/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/SPARQLAnything.java b/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/SPARQLAnything.java index 6c379b37..49f4dd2c 100644 --- a/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/SPARQLAnything.java +++ b/sparql-anything-cli/src/main/java/com/github/sparqlanything/cli/SPARQLAnything.java @@ -58,6 +58,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.PrintStream; import java.net.MalformedURLException; import java.net.URL; @@ -166,10 +167,10 @@ private static void executeQuery(String outputFormat, Dataset kb, Query query, P } } - private static PrintStream getPrintWriter(String fileName) throws FileNotFoundException { + private static PrintStream getPrintWriter(String fileName, boolean append) throws FileNotFoundException { if (fileName != null) { - return new PrintStream(new File(fileName)); + return new PrintStream(new FileOutputStream(fileName, append)); } return System.out; @@ -531,7 +532,7 @@ public static void main(String[] args) throws Exception { if (inputFile == null && values == null) { logger.debug("No input file"); Query q = QueryFactory.create(query); - executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFileName)); + executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFileName, cli.getOutputAppend())); } else { if (inputFile != null && values != null) { @@ -576,7 +577,7 @@ public static void main(String[] args) throws Exception { } try { logger.trace("Executing Query: {}", q); - executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFile)); + executeQuery(cli.getFormat(q), kb, q, getPrintWriter(outputFile, cli.getOutputAppend())); } catch (Exception e1) { logger.error( "Iteration " + parameters.getRowNumber() + " failed with error: " + e1.getMessage());