Skip to content

Commit

Permalink
SPARQL Shell tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
sandervd committed Jul 28, 2022
0 parents commit 9fed20a
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test: tools/apache-jena/lib/sparql-shell-1.0-SNAPSHOT.jar test/test-query
./tools/apache-jena/bin/sparql --query=test/test-query-1


tools/apache-jena/lib/sparql-shell-1.0-SNAPSHOT.jar: src/main/java/eu/europa/ec/sparql/shell/Exec.java
mvn package
cp target/sparql-shell-1.0-SNAPSHOT.jar tools/apache-jena/lib


tools/apache-jena:
mkdir -p tools
rm -rf tools/apache-jena
wget https://dlcdn.apache.org/jena/binaries/apache-jena-4.5.0.zip -O tools/apache-jena.zip
unzip tools/apache-jena.zip -d tools
rm tools/apache-jena.zip
cd tools; ln -s apache-jena-4.5.0 apache-jena

.PHONY: test
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPARQL Shell
SPARQL shell is an Apache Jena plugin, that allows executing shell commands as a SPARQL function.

## Setup
Build the jar:
```
mvn package
```
Copy the resulting package to the 'lib' folder of your Jena installation.

Alternatively grab the JAR from the release on GitHub.

## Example usage
To convert Markdown to HTML, we can use the following query:
```
PREFIX f: <java:eu.europa.ec.sparql.shell.>
SELECT ?s
{
BIND("# Title\n - Item 1\n - Item 2" as ?t)
BIND(f:Exec("pandoc -f markdown -t html", ?t) as ?s)
}
```
This will execute the pandoc tool to do the convertion.

The output of this query is:
```
--------------------------------------------------------------------------------
| s |
================================================================================
| "<h1 id=\"title\">Title</h1>\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>" |
--------------------------------------------------------------------------------
```

## Security consideration
The executed process runs as a subprocess of the Jena SPARQL/Fuseki process.
This is a serious security risk. It should not be used in a public endpoint or where the data is untrusted.

80 changes: 80 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>eu.europa.ec.sparql.shell</groupId>
<artifactId>sparql-shell</artifactId>
<version>1.0-SNAPSHOT</version>

<name>sparql-shell</name>
<url>github.com/sandervd/sparql-shell</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<jena.version>4.5.0</jena.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-tdb</artifactId>
<version>${jena.version}</version>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
48 changes: 48 additions & 0 deletions src/main/java/eu/europa/ec/sparql/shell/Exec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package eu.europa.ec.sparql.shell;

import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.function.FunctionBase2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

/**
* SPARQL function to execute a shell.
*/
public class Exec extends FunctionBase2
{

@Override
public NodeValue exec(NodeValue cmd, NodeValue pipe) {
try {
Process p = Runtime.getRuntime().exec(cmd.asString());
OutputStream outputStream = p.getOutputStream();
outputStream.write(pipe.asString().getBytes());
outputStream.close();
BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
p.waitFor();
StringBuilder result = new StringBuilder();
if (p.exitValue() != 0) {
result.append("Error while executing ").append(cmd.asString()).append(":\n");
dump(error, result);
throw new RuntimeException(error.toString());
}
dump(output,result);
return NodeValue.makeString(result.toString().trim());

} catch (IOException | InterruptedException e ) {
throw new RuntimeException(e);
}
}

private static StringBuilder dump(BufferedReader reader, StringBuilder builder) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
reader.close();
return builder;
}
}
12 changes: 12 additions & 0 deletions src/main/main.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.apache.jena:jena-arq:4.5.0" level="project" />
</component>
</module>
20 changes: 20 additions & 0 deletions src/test/java/eu/europa/ec/sparql/shell/ExecTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package eu.europa.ec.sparql.shell;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
* Unit test for simple App.
*/
public class ExecTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
11 changes: 11 additions & 0 deletions src/test/test.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
7 changes: 7 additions & 0 deletions test/test-query
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX f: <java:eu.europa.ec.sparql.shell.>
SELECT ?s
{
BIND("Some test" as ?t)
BIND(f:Exec("cat -", ?t) as ?s)

}
7 changes: 7 additions & 0 deletions test/test-query-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PREFIX f: <java:eu.europa.ec.sparql.shell.>
SELECT ?s
{
BIND("# Title\n - Item 1\n - Item 2" as ?t)
BIND(f:Exec("pandoc -f markdown -t html", ?t) as ?s)

}

0 comments on commit 9fed20a

Please sign in to comment.