Skip to content

Commit

Permalink
#511 allow file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-asprino committed Oct 28, 2024
1 parent 74dd6de commit 5a12d08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -87,11 +88,17 @@ public void parse(String[] args) throws ParseException {

public String getQuery() throws IOException {
String queryArgument = commandLine.getOptionValue(CLI.QUERY);
logger.trace("Parsing query argument {}", queryArgument);
try{
logger.trace("Trying interpreting as a URL...");
return IOUtils.toString(new URL(queryArgument).toURI(), Charset.defaultCharset());
} catch (MalformedURLException | URISyntaxException e) {
return queryArgument;
logger.trace("Trying interpreting as a file path...");
if(new File(queryArgument).exists())
return IOUtils.toString(new File(queryArgument).toURI(), Charset.defaultCharset());
}
logger.trace("Trying interpreting as a inline query...");
return queryArgument;
}
void init(){
this.options = new Options();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.StringReader;
Expand Down Expand Up @@ -69,6 +68,14 @@ public void infileQueryWithValues() throws Exception {
query(new String[]{"-q", queryFile, "-v", "loc=" + f, "-f", "CSV"});
}

@Test
public void infileQueryWithValuesAndPath() throws Exception {
String f = Objects.requireNonNull(getClass().getClassLoader().getResource("books.xml")).toURI().toString();
String queryFile = Objects.requireNonNull(getClass().getClassLoader().getResource("CLITestOnFileQuery1.sparql")).getFile();
System.out.println(queryFile);
query(new String[]{"-q", queryFile, "-v", "loc=" + f, "-f", "CSV"});
}

@Test
public void infileQuery() throws Exception {
String f = Objects.requireNonNull(getClass().getClassLoader().getResource("books.xml")).toURI().toString();
Expand Down

0 comments on commit 5a12d08

Please sign in to comment.