Add Exporter interface and example Json exporter
Change-Id: If20a06d58455b8ebac39596cb14bd732291a5fa9
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java
new file mode 100644
index 0000000..7c5244b
--- /dev/null
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java
@@ -0,0 +1,24 @@
+package de.ids_mannheim.korap.plkexport;
+import com.fasterxml.jackson.databind.JsonNode;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import java.io.IOException;
+import java.io.Writer;
+
+interface Exporter {
+
+ // Implemented by MatchAggregator
+ public void init (String s) throws IOException;
+ public void setMeta(JsonNode n);
+ public void setQuery(JsonNode n);
+ public void setCollection(JsonNode n);
+ public void appendMatches (String s) throws IOException;
+
+ // Implemented by Exporter
+ public ResponseBuilder serve();
+
+ // Needs to be overwritten
+ public void writeHeader (Writer w) throws IOException;
+ public void addMatch (JsonNode n, Writer w) throws IOException;
+ public void writeFooter (Writer w) throws IOException;
+};
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/JsonExporter.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/JsonExporter.java
new file mode 100644
index 0000000..dfd9694
--- /dev/null
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/JsonExporter.java
@@ -0,0 +1,74 @@
+package de.ids_mannheim.korap.plkexport;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * This is a streaming exporter class for Json, so it's based on
+ * a string buffer.
+ */
+
+public class JsonExporter extends MatchAggregator implements Exporter {
+
+ private boolean firstMatch;
+
+ {
+ firstMatch = true;
+ }
+
+ @Override
+ public void writeHeader (Writer w) throws IOException {
+ w.append("{");
+
+ boolean header = false;
+
+ if (this.query != null) {
+ w.append("\"query\":")
+ .append(this.query.toString());
+ header = true;
+ };
+
+ if (this.meta != null) {
+ if (header) {
+ w.append(',');
+ } else {
+ header = true;
+ };
+ w.append("\"meta\":")
+ .append(this.meta.toString());
+ };
+
+ if (this.collection != null) {
+ if (header) {
+ w.append(',');
+ } else {
+ header = true;
+ };
+ w.append("\"collection\":")
+ .append(this.collection.toString());
+ };
+
+ if (header)
+ w.append(',');
+
+ w.append("\"matches\":[");
+ }
+
+ @Override
+ public void writeFooter (Writer w) throws IOException {
+ w.append("]}");
+ };
+
+ @Override
+ public void addMatch (JsonNode n, Writer w) throws IOException {
+ if (firstMatch) {
+ firstMatch = false;
+ }
+ else {
+ w.append(',');
+ };
+ w.append(n.toString());
+ return;
+ };
+};
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java
new file mode 100644
index 0000000..7b31087
--- /dev/null
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java
@@ -0,0 +1,39 @@
+package de.ids_mannheim.korap.plkexport;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+import org.junit.Test;
+
+import javax.ws.rs.core.Response;
+
+import de.ids_mannheim.korap.plkexport.JsonExporter;
+
+public class JsonExportTest {
+
+ @Test
+ public void testInit () throws IOException {
+ JsonExporter json = new JsonExporter();
+ json.init("{\"query\":\"cool\"}");
+
+ Response resp = json.serve().build();
+ String x = (String) resp.getEntity();
+ resp.close();
+ assertEquals(x,"{\"query\":\"cool\",\"matches\":[]}");
+ };
+
+ @Test
+ public void testInitFull () throws IOException {
+ JsonExporter json = new JsonExporter();
+ json.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\",\"matches\":[\"first\",\"second\"]}");
+
+ Response resp = json.serve().build();
+ String x = (String) resp.getEntity();
+ resp.close();
+ assertEquals(x,"{\"query\":\"cool\",\"meta\":\"ja\",\"collection\":\"hm\",\"matches\":[\"first\",\"second\"]}");
+ };
+};