Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI params as PATH, add basepath to functionsPath #14

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions src/main/java/com/cefriel/template/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@
import com.beust.jcommander.Parameter;
import com.cefriel.template.io.Formatter;
import com.cefriel.template.io.Reader;
import com.cefriel.template.io.json.JSONReader;
import com.cefriel.template.io.sql.SQLReader;
import com.cefriel.template.utils.*;
import com.cefriel.template.utils.TemplateFunctions;
import com.cefriel.template.utils.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.*;

import static com.cefriel.template.utils.Util.validInputFormat;
import java.util.List;

public class Main {
@Parameter(names={"--template","-t"})
Expand All @@ -50,13 +49,13 @@ public class Main {
@Parameter(names={"--baseiri","-iri"})
private String baseIri = "http://www.cefriel.com/data/";
@Parameter(names={"--basepath","-b"})
private String basePath;
private Path basePath;
Copy link
Member

@marioscrock marioscrock Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AuPath Directly parsed as Path by JCommander?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@Parameter(names={"--output","-o"})
private String destinationPath = "output.txt";
private Path destinationPath = Path.of("output.txt");
@Parameter(names={"--key-value","-kv"})
private String keyValuePairsPath;
private Path keyValuePairsPath;
@Parameter(names={"--key-value-csv","-kvc"})
private String keyValueCsvPath;
private Path keyValueCsvPath;
@Parameter(names={"--format","-f"})
private String format;
@Parameter(names={"--trim","-tr"})
Expand All @@ -74,18 +73,17 @@ public class Main {
@Parameter(names={"--contextIRI","-c"})
private String context;
@Parameter(names={"--query","-q"})
private String queryPath;
private Path queryPath;
@Parameter(names={"--debug-query","-dq"})
private boolean debugQuery;
@Parameter(names={"--verbose","-v"})
private boolean verbose;
@Parameter(names={"--time","-tm"})
private String timePath;
private Path timePath;
@Parameter(names={"--functions","-fun"})
private String functionsPath;

private final Logger log = LoggerFactory.getLogger(Main.class);
private Path functionsPath;

private final Logger log = LoggerFactory.getLogger(Main.class);

public static void main(String ... argv) throws Exception {

Expand All @@ -102,21 +100,23 @@ public static void main(String ... argv) throws Exception {

public void updateBasePath(){
if (basePath != null) {
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
templatePath = Path.of(basePath + templatePath.toString());
basePath = basePath.endsWith("/") ? basePath : basePath.resolve("/");
templatePath = basePath.resolve(templatePath);
if (inputFilesPaths != null)
for (int i = 0; i < inputFilesPaths.size(); i++)
if(inputFilesPaths.get(i) != null)
inputFilesPaths.set(i, Path.of(basePath.toString() + inputFilesPaths.get(i)));
destinationPath = basePath + destinationPath;
inputFilesPaths.set(i, basePath.resolve(inputFilesPaths.get(i)));
destinationPath = basePath.resolve(destinationPath);
if (queryPath != null)
queryPath = basePath + queryPath;
queryPath = basePath.resolve(queryPath);
if (keyValueCsvPath != null)
keyValueCsvPath = basePath + keyValueCsvPath;
keyValueCsvPath = basePath.resolve(keyValueCsvPath);
if (keyValuePairsPath != null)
keyValuePairsPath = basePath + keyValuePairsPath;
keyValuePairsPath = basePath.resolve(keyValuePairsPath);
if (timePath != null)
timePath = basePath + timePath;
timePath = basePath.resolve(timePath);
if (functionsPath != null)
functionsPath = basePath.resolve(functionsPath);
}
}

Expand All @@ -130,7 +130,7 @@ public void exec() throws Exception {
if (queryPath == null)
log.error("Provide a query using the --query option");
else {
String debugQueryFromFile = Files.readString(Paths.get(queryPath));
String debugQueryFromFile = Files.readString(queryPath);
reader.debugQuery(debugQueryFromFile, destinationPath);
}
}
Expand All @@ -149,17 +149,18 @@ public void exec() throws Exception {
Formatter formatter = null;
if(format != null) {
formatter = Util.createFormatter(format);
if(reader != null)
if(reader != null)
reader.setOutputFormat(format);
}


TemplateFunctions templateFunctions = new TemplateFunctions();
if (functionsPath != null) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
File utilsFile = new File(functionsPath);
File utilsFile = functionsPath.toFile();
compiler.run(null, null, null, utilsFile.getPath());

File classDir = new File(utilsFile.getParent());
File classDir = functionsPath.getParent() != null ?
functionsPath.getParent().toFile() : Path.of("./").resolve(functionsPath).toFile();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{(classDir).toURI().toURL()});

// List all the files in the directory and identify the class file
Expand All @@ -178,20 +179,20 @@ public void exec() throws Exception {
}

if(timePath != null)
try (FileWriter pw = new FileWriter(timePath, true)) {
try (FileWriter pw = new FileWriter(timePath.toFile(), true)) {
long start = Instant.now().toEpochMilli();
if(queryPath != null)
tl.executeMappingParametric(reader, templatePath, templateInResources, trimTemplate, Paths.get(queryPath), Paths.get(destinationPath), templateMap, formatter, templateFunctions);
tl.executeMappingParametric(reader, templatePath, templateInResources, trimTemplate, queryPath, destinationPath, templateMap, formatter, templateFunctions);
else
tl.executeMapping(reader, templatePath, templateInResources, trimTemplate, Paths.get(destinationPath), templateMap, formatter, templateFunctions);
tl.executeMapping(reader, templatePath, templateInResources, trimTemplate, destinationPath, templateMap, formatter, templateFunctions);
long duration = Instant.now().toEpochMilli() - start;
pw.write(templatePath + "," + destinationPath + "," + duration + "\n");
}
else{
if(queryPath != null)
tl.executeMappingParametric(reader, templatePath, templateInResources, trimTemplate, Paths.get(queryPath), Paths.get(destinationPath), templateMap, formatter, templateFunctions);
tl.executeMappingParametric(reader, templatePath, templateInResources, trimTemplate, queryPath, destinationPath, templateMap, formatter, templateFunctions);
else
tl.executeMapping(reader, templatePath, templateInResources, trimTemplate, Paths.get(destinationPath), templateMap, formatter, templateFunctions);
tl.executeMapping(reader, templatePath, templateInResources, trimTemplate, destinationPath, templateMap, formatter, templateFunctions);
}

if(reader != null)
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/cefriel/template/TemplateMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@

public class TemplateMap extends HashMap<String, String> {
private static final Logger log = LoggerFactory.getLogger(TemplateMap.class);
public TemplateMap(String filePath, boolean isCsv) throws IOException {
Path path = FileSystems.getDefault().getPath(filePath);
Stream<String> lines = Files.lines(path);
public TemplateMap(Path filePath, boolean isCsv) throws IOException {
Stream<String> lines = Files.lines(filePath);
if (isCsv)
putAll(parseCsvMap(lines));
else
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cefriel/template/io/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.cefriel.template.io;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -51,7 +52,7 @@ public interface Reader {
* @param destinationPath The path of the destination file
* @throws Exception
*/
void debugQuery(String query, String destinationPath) throws Exception;
void debugQuery(String query, Path destinationPath) throws Exception;

/**
* If {@code verbose} is set, debug information about executed queries are logged
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/cefriel/template/io/csv/CSVReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.cefriel.template.io.csv;

import com.cefriel.template.io.Reader;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import de.siegmar.fastcsv.reader.NamedCsvReader;
import de.siegmar.fastcsv.reader.NamedCsvRow;

Expand Down Expand Up @@ -70,7 +68,7 @@ public List<Map<String, String>> getDataframe(String query) {
}

@Override
public void debugQuery(String query, String destinationPath) throws Exception {
public void debugQuery(String query, Path destinationPath) throws Exception {

}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cefriel/template/io/json/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

Expand Down Expand Up @@ -136,7 +136,7 @@ public List<Map<String, String>> getDataframe() throws Exception {
}

@Override
public void debugQuery(String query, String destinationPath) throws Exception {
public void debugQuery(String query, Path destinationPath) throws Exception {
log.warn("Debug operation not implemented");
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/cefriel/template/io/rdf/RDFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
package com.cefriel.template.io.rdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -223,8 +221,8 @@ private List<Map<String, String>> getDataframeXMLEscaped(List<Map<String, String
* @param destinationPath File to save the results of the SPARQL query
* @throws IOException If an error occurs in handling the files
*/
public void debugQuery(String query, String destinationPath) throws IOException {
SPARQLResultsTSVWriter writer = new SPARQLResultsTSVWriter(new FileOutputStream(destinationPath));
public void debugQuery(String query, Path destinationPath) throws IOException {
SPARQLResultsTSVWriter writer = new SPARQLResultsTSVWriter(new BufferedOutputStream(Files.newOutputStream(destinationPath)));
try (RepositoryConnection con = this.repository.getConnection()) {
con.prepareTupleQuery(query).evaluate(writer);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/cefriel/template/io/sql/SQLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.*;
import java.sql.Statement;
import java.util.ArrayList;
Expand Down Expand Up @@ -129,9 +131,9 @@ public List<Map<String, String>> getDataframe() throws Exception {
* @param destinationPath File to save the results of the SQL query
* @throws IOException If an error occurs in handling the files
*/
public void debugQuery(String query, String destinationPath) throws IOException {
public void debugQuery(String query, Path destinationPath) throws IOException {
try (ResultSet resultSet = executeQuery(query)) {
try (FileWriter writer = new FileWriter(destinationPath)) {
try (BufferedWriter writer = new BufferedWriter(Files.newBufferedWriter(destinationPath))) {
int columnCount = resultSet.getMetaData().getColumnCount();

for (int i = 1; i <= columnCount; i++) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/cefriel/template/io/xml/XMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.PrintWriter;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
Expand Down Expand Up @@ -163,7 +164,7 @@ public List<Map<String, String>> getDataframe() throws Exception {
}

@Override
public void debugQuery(String query, String destinationPath) throws Exception {
public void debugQuery(String query, Path destinationPath) throws Exception {
List<Map<String, String>> result = getDataframe(query);
StringBuilder sb = new StringBuilder();

Expand All @@ -186,7 +187,7 @@ public void debugQuery(String query, String destinationPath) throws Exception {
}
sb.setLength(sb.length() - 1);

try (PrintWriter out = new PrintWriter(destinationPath)) {
try (PrintWriter out = new PrintWriter(destinationPath.toString())) {
out.println(sb);
}
}
Expand Down