You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by wi...@apache.org on 2014/06/13 11:02:37 UTC
[08/20] git commit: add JSON output format for LDQuery CLI tool
add JSON output format for LDQuery CLI tool
Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/4807b593
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/4807b593
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/4807b593
Branch: refs/heads/ldp
Commit: 4807b5931bdfd0b858d7b517db79769863c267fb
Parents: fa98d10
Author: Chris Beer <ca...@stanford.edu>
Authored: Wed Jun 4 15:00:46 2014 -0700
Committer: Chris Beer <ca...@stanford.edu>
Committed: Wed Jun 4 15:06:52 2014 -0700
----------------------------------------------------------------------
libraries/ldpath/ldpath-ldquery-cli/pom.xml | 5 +++
.../apache/marmotta/ldpath/ldquery/LDQuery.java | 46 +++++++++++++++-----
2 files changed, 39 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/marmotta/blob/4807b593/libraries/ldpath/ldpath-ldquery-cli/pom.xml
----------------------------------------------------------------------
diff --git a/libraries/ldpath/ldpath-ldquery-cli/pom.xml b/libraries/ldpath/ldpath-ldquery-cli/pom.xml
index a81d56a..ced4597 100644
--- a/libraries/ldpath/ldpath-ldquery-cli/pom.xml
+++ b/libraries/ldpath/ldpath-ldquery-cli/pom.xml
@@ -300,6 +300,11 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.jr</groupId>
+ <artifactId>jackson-jr-objects</artifactId>
+ <version>2.3.3</version>
+ </dependency>
<!-- Testing -->
http://git-wip-us.apache.org/repos/asf/marmotta/blob/4807b593/libraries/ldpath/ldpath-ldquery-cli/src/main/java/org/apache/marmotta/ldpath/ldquery/LDQuery.java
----------------------------------------------------------------------
diff --git a/libraries/ldpath/ldpath-ldquery-cli/src/main/java/org/apache/marmotta/ldpath/ldquery/LDQuery.java b/libraries/ldpath/ldpath-ldquery-cli/src/main/java/org/apache/marmotta/ldpath/ldquery/LDQuery.java
index 71a9226..4db6b39 100644
--- a/libraries/ldpath/ldpath-ldquery-cli/src/main/java/org/apache/marmotta/ldpath/ldquery/LDQuery.java
+++ b/libraries/ldpath/ldpath-ldquery-cli/src/main/java/org/apache/marmotta/ldpath/ldquery/LDQuery.java
@@ -18,8 +18,10 @@
package org.apache.marmotta.ldpath.ldquery;
import ch.qos.logback.classic.Level;
+import com.fasterxml.jackson.jr.ob.JSON;
import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.output.ProxyOutputStream;
import org.apache.marmotta.ldpath.LDPath;
import org.apache.marmotta.ldpath.backend.linkeddata.LDCacheBackend;
import org.apache.marmotta.ldpath.exception.LDPathParseException;
@@ -101,20 +103,38 @@ public class LDQuery {
Map<String,Collection<?>> result = ldpath.programQuery(context,new FileReader(file));
- for(String field : result.keySet()) {
- StringBuilder line = new StringBuilder();
- line.append(field);
- line.append(" = ");
- line.append("{");
- for (Iterator<?> it = result.get(field).iterator(); it.hasNext();) {
- line.append(it.next().toString());
- if(it.hasNext()) {
- line.append(", ");
- }
+ if (cmd.hasOption("format")) {
+ final String format = cmd.getOptionValue("format");
+ if (format.equals("json")) {
+ // Jackson.jr closes the output stream.
+ final ProxyOutputStream proxyOutputStream = new ProxyOutputStream(System.out) {
+ @Override
+ public void close() throws IOException {
+ flush();
+ }
+ };
+ JSON.std.write(result, proxyOutputStream);
+ System.out.println("");
+ } else {
+ System.err.println("Unknown format: " + format);
+ System.exit(1);
}
- line.append("}");
- System.out.println(line);
+ } else {
+ for (String field : result.keySet()) {
+ StringBuilder line = new StringBuilder();
+ line.append(field);
+ line.append(" = ");
+ line.append("{");
+ for (Iterator<?> it = result.get(field).iterator(); it.hasNext(); ) {
+ line.append(it.next().toString());
+ if (it.hasNext()) {
+ line.append(", ");
+ }
+ }
+ line.append("}");
+ System.out.println(line);
+ }
}
}
}
@@ -167,6 +187,8 @@ public class LDQuery {
Option store = OptionBuilder.withArgName("dir").hasArg().withDescription("cache the retrieved data in this directory").create("store");
result.addOption(store);
+ Option format = OptionBuilder.withArgName("format").hasArg().withDescription("output format").create("format");
+ result.addOption(format);
return result;
}