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 45bbdbd6..819ca992 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 @@ -19,7 +19,9 @@ import org.apache.commons.cli.*; import org.apache.jena.query.Query; +import org.apache.jena.riot.Lang; import org.apache.jena.util.FileUtils; +import org.joda.time.format.FormatUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +29,7 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.util.Locale; public class CLI { private static final Logger logger = LoggerFactory.getLogger(CLI.class); @@ -60,13 +63,9 @@ public CLI(){ init(); } - public void parse(String[] args) throws IOException { + public void parse(String[] args) throws MissingOptionException, ParseException { CommandLineParser cmdLineParser = new DefaultParser(); - try { - this.commandLine = cmdLineParser.parse(options, args); - } catch (ParseException e) { - throw new IOException(e); - } + this.commandLine = cmdLineParser.parse(options, args); } private static String getQuery(String queryArgument) throws IOException { @@ -158,26 +157,51 @@ public String[] getValues() { return commandLine.getOptionValues(CLI.VALUES); } + public static String guessLang(String name) { + String suffix = FileUtils.getFilenameExt(name).toLowerCase(Locale.ROOT); + if (suffix.equals("n3")) { + return Lang.N3.getName(); + } else if (suffix.equals("nq")) { + return Lang.NQ.getName(); + } else if (suffix.equals("json")) { + return "JSON"; + } else if (suffix.equals("csv")) { + return "CSV"; + } else if (suffix.equals("txt")) { + return "TEXT"; + } else if (suffix.equals("xml")) { + return "xml"; + } else if (suffix.equals("nt")) { + return Lang.NTRIPLES.getName(); + } else if (suffix.equals("ttl")) { + return Lang.TTL.getName(); + } else if (suffix.equals("rdf")) { + return Lang.RDFXML.getName(); + } else { + return suffix.equals("owl") ? Lang.RDFXML.getName() : null; + } + } public String getFormat(Query q) { if (commandLine.hasOption(CLI.FORMAT)) { return commandLine.getOptionValue(CLI.FORMAT).toUpperCase(); } + String format = null; // Set default format for query type and STDOUT or FILE if (commandLine.getOptionValue(CLI.OUTPUT) != null) { // Guess the format from the extension - return FileUtils.guessLang(commandLine.getOptionValue(CLI.OUTPUT)); - } else { + format = guessLang(commandLine.getOptionValue(CLI.OUTPUT)); + } + + if(format == null){ if (q.isAskType() || q.isSelectType()) { - return "JSON"; + return Lang.CSV.getName(); } else if (q.isConstructType() || q.isDescribeType()) { - return "TTL"; + return Lang.TTL.getName(); + } else if (q.isDescribeType() || q.isConstructType()) { + return Lang.TTL.getName(); } } - // - if (q.isDescribeType() || q.isConstructType()) { - return "TTL"; - } - return "TEXT"; + return format; } } 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 3e2d3327..5f94a5b3 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 @@ -63,6 +63,7 @@ import org.apache.jena.sparql.core.Var; import org.apache.jena.sparql.engine.binding.Binding; import org.apache.jena.sparql.engine.main.QC; +import org.apache.jena.update.UpdateExecutionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -125,28 +126,33 @@ private static void executeQuery(String outputFormat, Dataset kb, Query query, P m = QueryExecutionFactory.create(q, kb).execDescribe(); // d = new DatasetImpl(m); } - if (format.equals("JSON")) { - // JSON-LD + if (format.equals("JSON") || format.equals(Lang.JSONLD.getName()) ) { + // JSON-LD format.equals(Lang.JSONLD11.getName()) RDFDataMgr.write(pw, m, Lang.JSONLD); + } else if ( format.equals(Lang.JSONLD11.getName()) ) { + RDFDataMgr.write(pw, m, Lang.JSONLD11); } else if (format.equals("XML")) { // RDF/XML RDFDataMgr.write(pw, m, Lang.RDFXML); - } else if (format.equals("TTL")) { + } else if (format.equals("TTL") || format.equals(Lang.TURTLE.getName())) { // TURTLE RDFDataMgr.write(pw, m, Lang.TTL); - } else if (format.equals("NT")) { + } else if (format.equals("NT") || format.equals(Lang.NTRIPLES.getName())) { // N-Triples RDFDataMgr.write(pw, m, Lang.NT); - } else if (format.equals("NQ")) { + } else if (format.equals("NQ") || format.equals(Lang.NQUADS.getName())) { // NQ RDFDataMgr.write(pw, d, Lang.NQ); - } else if (format.equals("TRIG")) { + } else if (format.equals(Lang.TRIG.getName())) { // TRIG RDFDataMgr.write(pw, d, Lang.TRIG); + } else if (format.equals(Lang.TRIX.getName())) { + // TRIG + RDFDataMgr.write(pw, d, Lang.TRIX); } else { throw new RuntimeException("Unsupported format: " + format); } - } + } } private static PrintStream getPrintWriter(String fileName) throws FileNotFoundException { @@ -391,7 +397,11 @@ public static ResultSet prepareResultSetFromArgValues(String[] values) { public static void main(String[] args) throws Exception { logger.info("SPARQL anything"); + CLI cli = new CLI(); + if(args.length == 0){ + cli.printHelp(); + } try { cli.parse(args); String query = cli.getQuery(); @@ -511,7 +521,11 @@ public static void main(String[] args) throws Exception { } } catch (FileNotFoundException e) { logger.error("File not found: {}", e.getMessage()); + } catch(org.apache.commons.cli.MissingOptionException e1){ + logger.error("{}",e1.getMessage()); + cli.printHelp(); } catch (ParseException e) { + logger.error("{}",e.getMessage()); cli.printHelp(); } }