Skip to content

Commit

Permalink
Fix #26
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Feb 7, 2022
1 parent 71eae28 commit c2ea0ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@

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;

import java.io.BufferedReader;
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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit c2ea0ac

Please sign in to comment.