You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@any23.apache.org by le...@apache.org on 2018/05/10 06:00:56 UTC

[1/6] any23 git commit: Use RDF4J writer to return JSON-LD outputs.

Repository: any23
Updated Branches:
  refs/heads/master c271cdb33 -> ed662baee


Use RDF4J writer to return JSON-LD outputs.


Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/f8e94d18
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/f8e94d18
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/f8e94d18

Branch: refs/heads/master
Commit: f8e94d188baa4a7dabf08d4dbc9b6bb47ec26ea2
Parents: c271cdb
Author: Julio Caguano <ju...@gmail.com>
Authored: Mon Apr 30 16:24:05 2018 -0500
Committer: Julio Caguano <ju...@gmail.com>
Committed: Mon Apr 30 16:24:05 2018 -0500

----------------------------------------------------------------------
 .../org/apache/any23/writer/JSONWriter.java     | 214 +------------------
 .../org/apache/any23/writer/JSONWriterTest.java |  90 ++++----
 .../org/apache/any23/servlet/ServletTest.java   |  23 +-
 3 files changed, 52 insertions(+), 275 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/f8e94d18/core/src/main/java/org/apache/any23/writer/JSONWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONWriter.java b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
index 331a65b..567c014 100644
--- a/core/src/main/java/org/apache/any23/writer/JSONWriter.java
+++ b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
@@ -14,226 +14,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.any23.writer;
 
-import org.apache.any23.extractor.ExtractionContext;
-import org.eclipse.rdf4j.model.BNode;
-import org.eclipse.rdf4j.model.Literal;
-import org.eclipse.rdf4j.model.Resource;
-import org.eclipse.rdf4j.model.IRI;
-import org.eclipse.rdf4j.model.Value;
-
-import java.io.BufferedOutputStream;
 import java.io.OutputStream;
-import java.io.PrintStream;
-import java.util.Optional;
+import org.eclipse.rdf4j.rio.RDFFormat;
+import org.eclipse.rdf4j.rio.Rio;
 
 /**
  * Implementation of <i>JSON</i> format writer.
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
-public class JSONWriter implements FormatWriter {
-
-    private final PrintStream ps;
-
-    private boolean documentStarted = false;
-
-    private boolean firstArrayElemWritten = false;
-    private boolean firstObjectWritten    = false;
+public class JSONWriter extends RDFWriterTripleHandler implements FormatWriter {
 
     public JSONWriter(OutputStream os) {
-        if(os == null) {
-            throw new NullPointerException("Output stream cannot be null.");
-        }
-        this.ps = new PrintStream(new BufferedOutputStream(os));
-    }
-
-    @Override
-    public void startDocument(IRI documentIRI) throws TripleHandlerException {
-        if(documentStarted) {
-            throw new IllegalStateException("Document already started.");
-        }
-        documentStarted = true;
-
-        firstArrayElemWritten = false;
-        ps.print("{ \"quads\" : [");
-    }
-
-    @Override
-    public void openContext(ExtractionContext context) throws TripleHandlerException {
-        // Empty.
-    }
-
-    @Override
-    public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
-    throws TripleHandlerException {
-        validateDocumentStarted();
-
-        if(firstArrayElemWritten) {
-            ps.print(", ");
-        } else {
-            firstArrayElemWritten = true;
-        }
-        firstObjectWritten    = false;
-        
-        ps.print('[');
-
-        if(s instanceof IRI) {
-            printExplicitIRI(s.stringValue(), ps);
-        } else {
-            printBNode(s.stringValue(), ps);
-        }
-
-        printIRI(p.stringValue(), ps);
-
-         if(o instanceof IRI) {
-            printExplicitIRI(o.stringValue(), ps);
-        } else if(o instanceof BNode) {
-            printBNode(o.stringValue(), ps);
-        } else {
-            printLiteral((Literal) o, ps);
-        }
-
-        printIRI(g == null ? null : g.stringValue(), ps);
-
-        ps.print(']');
-    }
-
-    @Override
-    public void receiveNamespace(String prefix, String uri, ExtractionContext context)
-    throws TripleHandlerException {
-        // Empty.
-    }
-
-    @Override
-    public void closeContext(ExtractionContext context) throws TripleHandlerException {
-        // Empty.
-    }
-
-    @Override
-    public void endDocument(IRI documentIRI) throws TripleHandlerException {
-        validateDocumentStarted();
-        ps.print("]}");
-        documentStarted = false;
-    }
-
-    @Override
-    public void setContentLength(long contentLength) {
-        // Empty.
-    }
-
-    @Override
-    public void close() throws TripleHandlerException {
-    	if(documentStarted) {
-    		endDocument(null);
-    	}
-        ps.close();
-    }
-
-    private void validateDocumentStarted() {
-       if(!documentStarted) {
-            throw new IllegalStateException("Document didn't start.");
-        }
-    }
-
-    private void printIRI(String uri, PrintStream ps) {
-        printValue(uri, ps);
-    }
-
-    private void printExplicitIRI(String uri, PrintStream ps) {
-        printValue("uri", uri, ps);
-    }
-
-    private void printBNode(String bnode, PrintStream ps) {
-        printValue("bnode", bnode, ps);
-    }
-
-    private void printCommaIfNeeded(PrintStream ps) {
-        if(firstObjectWritten) {
-            ps.print(", ");
-        } else {
-            firstObjectWritten = true;
-        }
-    }
-
-    private void printLiteral(Literal literal, PrintStream ps) {
-        printCommaIfNeeded(ps);
-
-        ps.print('{');
-
-        ps.print("\"type\" : \"literal\"");
-
-        ps.print(", ");
-
-        ps.print("\"value\" : ");
-        ps.print('"');
-        ps.print(literal.stringValue());
-        ps.print('"');
-
-        ps.print(", ");
-
-        ps.print("\"lang\" : ");
-        final Optional<String> language = literal.getLanguage();
-        if (language.isPresent()) {
-            ps.print('"');
-            ps.print(literal.getLanguage().get());
-            ps.print('"');
-        } else {
-            ps.print("null");
-        }
-
-        ps.print(", ");
-
-        ps.print("\"datatype\" : ");
-        final IRI datatype = literal.getDatatype();
-        if(datatype != null) {
-        ps.print('"');
-        ps.print(datatype.stringValue());
-        ps.print('"');
-        } else {
-            ps.print("null");
-        }
-
-        ps.print('}');
-    }
-
-    private void printValue(String type, String value, PrintStream ps) {
-        printCommaIfNeeded(ps);
-
-        ps.print("{ \"type\" : \"");
-        ps.print(type);
-        ps.print("\", \"value\" : ");
-        if (value != null) {
-            ps.print('"');
-            ps.print(value);
-            ps.print('"');
-        } else {
-            ps.print("null");
-        }
-        ps.print('}');
-    }
-
-    private void printValue(String value, PrintStream ps) {
-        printCommaIfNeeded(ps);
-
-        if (value != null) {
-            ps.print('"');
-            ps.print(value);
-            ps.print('"');
-        } else {
-            ps.print("null");
-        }
-    }
-
-    @Override
-    public boolean isAnnotated() {
-        return false; // TODO: add annotation support.
-    }
-
-    @Override
-    public void setAnnotated(boolean f) {
-        // Empty.
+        super(Rio.createWriter(RDFFormat.JSONLD, os));
     }
 }

http://git-wip-us.apache.org/repos/asf/any23/blob/f8e94d18/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
index 92ae30f..1b1d846 100644
--- a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
+++ b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
@@ -14,14 +14,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.any23.writer;
 
-import org.junit.Assert;
-import org.junit.Test;
+import java.io.ByteArrayOutputStream;
+import org.apache.any23.extractor.ExtractionContext;
 import org.eclipse.rdf4j.model.IRI;
 import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
-import java.io.ByteArrayOutputStream;
+import org.junit.Assert;
+import org.junit.Test;
 
 /**
  * Test case for {@link JSONWriter} class.
@@ -33,57 +33,41 @@ public class JSONWriterTest {
     @Test
     public void testWriting() throws TripleHandlerException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try(JSONWriter jsonWriter = new JSONWriter(baos);) {
-	        final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
-	        jsonWriter.startDocument(documentIRI);
-	        jsonWriter.receiveTriple(
-	                SimpleValueFactory.getInstance().createBNode("bn1"),
-	                SimpleValueFactory.getInstance().createIRI("http://pred/1"),
-	                SimpleValueFactory.getInstance().createIRI("http://value/1"),
-	                SimpleValueFactory.getInstance().createIRI("http://graph/1"),
-	                null
-	        );
-	        jsonWriter.receiveTriple(
-	                SimpleValueFactory.getInstance().createIRI("http://sub/2"),
-	                SimpleValueFactory.getInstance().createIRI("http://pred/2"),
-	                SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
-	                SimpleValueFactory.getInstance().createIRI("http://graph/2"),
-	                null
-	        );
-	        jsonWriter.receiveTriple(
-	                SimpleValueFactory.getInstance().createIRI("http://sub/3"),
-	                SimpleValueFactory.getInstance().createIRI("http://pred/3"),
-	                SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
-	                null,
-	                null
-	        );
-	        jsonWriter.endDocument(documentIRI);
+        try (JSONWriter jsonWriter = new JSONWriter(baos);) {
+            final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
+            ExtractionContext extractionContext = new ExtractionContext("rdf-nq", SimpleValueFactory.getInstance().createIRI("http://any23.org/tmp/"));
+           
+            jsonWriter.startDocument(documentIRI);
+            jsonWriter.receiveTriple(
+                    SimpleValueFactory.getInstance().createBNode("bn1"),
+                    SimpleValueFactory.getInstance().createIRI("http://pred/1"),
+                    SimpleValueFactory.getInstance().createIRI("http://value/1"),
+                    SimpleValueFactory.getInstance().createIRI("http://graph/1"),
+                    null
+            );
+            jsonWriter.receiveTriple(
+                    SimpleValueFactory.getInstance().createIRI("http://sub/2"),
+                    SimpleValueFactory.getInstance().createIRI("http://pred/2"),
+                    SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
+                    SimpleValueFactory.getInstance().createIRI("http://graph/2"),
+                    null
+            );
+            jsonWriter.receiveTriple(
+                    SimpleValueFactory.getInstance().createIRI("http://sub/3"),
+                    SimpleValueFactory.getInstance().createIRI("http://pred/3"),
+                    SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
+                    null,
+                    extractionContext
+            );
+            jsonWriter.endDocument(documentIRI);
         }
 
-        final String expected =
-            "{ " +
-            "\"quads\" : " +
-            "[" +
-            "[" +
-            "{ \"type\" : \"bnode\", \"value\" : \"bn1\"}, " +
-            "\"http://pred/1\", " +
-            "{ \"type\" : \"uri\", \"value\" : \"http://value/1\"}, " +
-            "\"http://graph/1\"" +
-            "], " +
-            "[" +
-            "{ \"type\" : \"uri\", \"value\" : \"http://sub/2\"}, " +
-            "\"http://pred/2\", " +
-            "{\"type\" : \"literal\", \"value\" : \"language literal\", \"lang\" : \"en\", \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"}, " +
-            "\"http://graph/2\"" +
-            "], " +
-            "[" +
-            "{ \"type\" : \"uri\", \"value\" : \"http://sub/3\"}, " +
-            "\"http://pred/3\", " +
-            "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, " +
-            "null" +
-            "]" +
-            "]" +
-            "}";
+        final String expected
+                = "["
+                + "{\"@graph\":[{\"@id\":\"http://sub/3\",\"http://pred/3\":[{\"@type\":\"http://datatype\",\"@value\":\"123\"}]}],\"@id\":\"http://any23.org/tmp/\"},"
+                + "{\"@graph\":[{\"@id\":\"_:bn1\",\"http://pred/1\":[{\"@id\":\"http://value/1\"}]}],\"@id\":\"http://graph/1\"},"
+                + "{\"@graph\":[{\"@id\":\"http://sub/2\",\"http://pred/2\":[{\"@language\":\"en\",\"@value\":\"language literal\"}]}],\"@id\":\"http://graph/2\"}"
+                + "]";
         Assert.assertEquals(expected, baos.toString());
     }
 }

http://git-wip-us.apache.org/repos/asf/any23/blob/f8e94d18/service/src/test/java/org/apache/any23/servlet/ServletTest.java
----------------------------------------------------------------------
diff --git a/service/src/test/java/org/apache/any23/servlet/ServletTest.java b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
index bc87737..02c0603 100644
--- a/service/src/test/java/org/apache/any23/servlet/ServletTest.java
+++ b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
@@ -17,23 +17,22 @@
 
 package org.apache.any23.servlet;
 
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URLEncoder;
 import org.apache.any23.http.HTTPClient;
 import org.apache.any23.source.DocumentSource;
 import org.apache.any23.source.FileDocumentSource;
 import org.apache.any23.source.StringDocumentSource;
 import org.apache.any23.util.StringUtils;
-import org.junit.Assert;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mortbay.jetty.testing.HttpTester;
 import org.mortbay.jetty.testing.ServletTester;
 
-import java.io.File;
-import java.io.IOException;
-import java.net.URISyntaxException;
-import java.net.URLEncoder;
-
 /**
  * Test case for {@link Servlet} class.
  */
@@ -395,12 +394,12 @@ public class ServletTest {
         HttpTester response = doPostRequest("/json", body, "application/n-quads");
         Assert.assertEquals(200, response.getStatus());
         final String EXPECTED_JSON =
-                "[" +
-                "{ \"type\" : \"uri\", \"value\" : \"http://sub/1\"}, " +
-                "\"http://pred/1\", " +
-                "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, " +
-                "\"http://graph/1\"" +
-                "]";
+                "["
+                + "{\"@graph\":"
+                + "["
+                + "{\"@id\":\"http://sub/1\","
+                + "\"http://pred/1\":[{\"@type\":\"http://datatype\",\"@value\":\"123\"}]}],"
+                + "\"@id\":\"http://graph/1\"}]";
         assertContains(EXPECTED_JSON, response.getContent());
     }
 


[2/6] any23 git commit: reset json writer

Posted by le...@apache.org.
reset json writer


Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/27dac032
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/27dac032
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/27dac032

Branch: refs/heads/master
Commit: 27dac032a5b07b5db243771b168f26621a41e472
Parents: f8e94d1
Author: Julio Caguano <ju...@gmail.com>
Authored: Sun May 6 15:45:55 2018 -0500
Committer: Julio Caguano <ju...@gmail.com>
Committed: Sun May 6 15:45:55 2018 -0500

----------------------------------------------------------------------
 .../org/apache/any23/writer/JSONWriter.java     | 213 ++++++++++++++++++-
 .../org/apache/any23/writer/JSONWriterTest.java |  84 +++++---
 .../org/apache/any23/servlet/ServletTest.java   |  64 +++---
 3 files changed, 295 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/27dac032/core/src/main/java/org/apache/any23/writer/JSONWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONWriter.java b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
index 567c014..a4cce5a 100644
--- a/core/src/main/java/org/apache/any23/writer/JSONWriter.java
+++ b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
@@ -14,20 +14,225 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.any23.writer;
 
+import java.io.BufferedOutputStream;
 import java.io.OutputStream;
-import org.eclipse.rdf4j.rio.RDFFormat;
-import org.eclipse.rdf4j.rio.Rio;
+import java.io.PrintStream;
+import java.util.Optional;
+import org.apache.any23.extractor.ExtractionContext;
+import org.eclipse.rdf4j.model.BNode;
+import org.eclipse.rdf4j.model.IRI;
+import org.eclipse.rdf4j.model.Literal;
+import org.eclipse.rdf4j.model.Resource;
+import org.eclipse.rdf4j.model.Value;
 
 /**
  * Implementation of <i>JSON</i> format writer.
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
-public class JSONWriter extends RDFWriterTripleHandler implements FormatWriter {
+public class JSONWriter implements FormatWriter {
+
+    private final PrintStream ps;
+
+    private boolean documentStarted = false;
+
+    private boolean firstArrayElemWritten = false;
+    private boolean firstObjectWritten    = false;
 
     public JSONWriter(OutputStream os) {
-        super(Rio.createWriter(RDFFormat.JSONLD, os));
+        if(os == null) {
+            throw new NullPointerException("Output stream cannot be null.");
+        }
+        this.ps = new PrintStream(new BufferedOutputStream(os));
+    }
+
+    @Override
+    public void startDocument(IRI documentIRI) throws TripleHandlerException {
+        if(documentStarted) {
+            throw new IllegalStateException("Document already started.");
+        }
+        documentStarted = true;
+
+        firstArrayElemWritten = false;
+        ps.print("{ \"quads\" : [");
+    }
+
+    @Override
+    public void openContext(ExtractionContext context) throws TripleHandlerException {
+        // Empty.
+    }
+
+    @Override
+    public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
+    throws TripleHandlerException {
+        validateDocumentStarted();
+
+        if(firstArrayElemWritten) {
+            ps.print(", ");
+        } else {
+            firstArrayElemWritten = true;
+        }
+        firstObjectWritten    = false;
+        
+        ps.print('[');
+
+        if(s instanceof IRI) {
+            printExplicitIRI(s.stringValue(), ps);
+        } else {
+            printBNode(s.stringValue(), ps);
+        }
+
+        printIRI(p.stringValue(), ps);
+
+         if(o instanceof IRI) {
+            printExplicitIRI(o.stringValue(), ps);
+        } else if(o instanceof BNode) {
+            printBNode(o.stringValue(), ps);
+        } else {
+            printLiteral((Literal) o, ps);
+        }
+
+        printIRI(g == null ? null : g.stringValue(), ps);
+
+        ps.print(']');
+    }
+
+    @Override
+    public void receiveNamespace(String prefix, String uri, ExtractionContext context)
+    throws TripleHandlerException {
+        // Empty.
+    }
+
+    @Override
+    public void closeContext(ExtractionContext context) throws TripleHandlerException {
+        // Empty.
+    }
+
+    @Override
+    public void endDocument(IRI documentIRI) throws TripleHandlerException {
+        validateDocumentStarted();
+        ps.print("]}");
+        documentStarted = false;
+    }
+
+    @Override
+    public void setContentLength(long contentLength) {
+        // Empty.
+    }
+
+    @Override
+    public void close() throws TripleHandlerException {
+    	if(documentStarted) {
+    		endDocument(null);
+    	}
+        ps.close();
+    }
+
+    private void validateDocumentStarted() {
+       if(!documentStarted) {
+            throw new IllegalStateException("Document didn't start.");
+        }
+    }
+
+    private void printIRI(String uri, PrintStream ps) {
+        printValue(uri, ps);
+    }
+
+    private void printExplicitIRI(String uri, PrintStream ps) {
+        printValue("uri", uri, ps);
+    }
+
+    private void printBNode(String bnode, PrintStream ps) {
+        printValue("bnode", bnode, ps);
+    }
+
+    private void printCommaIfNeeded(PrintStream ps) {
+        if(firstObjectWritten) {
+            ps.print(", ");
+        } else {
+            firstObjectWritten = true;
+        }
+    }
+
+    private void printLiteral(Literal literal, PrintStream ps) {
+        printCommaIfNeeded(ps);
+
+        ps.print('{');
+
+        ps.print("\"type\" : \"literal\"");
+
+        ps.print(", ");
+
+        ps.print("\"value\" : ");
+        ps.print('"');
+        ps.print(literal.stringValue());
+        ps.print('"');
+
+        ps.print(", ");
+
+        ps.print("\"lang\" : ");
+        final Optional<String> language = literal.getLanguage();
+        if (language.isPresent()) {
+            ps.print('"');
+            ps.print(literal.getLanguage().get());
+            ps.print('"');
+        } else {
+            ps.print("null");
+        }
+
+        ps.print(", ");
+
+        ps.print("\"datatype\" : ");
+        final IRI datatype = literal.getDatatype();
+        if(datatype != null) {
+        ps.print('"');
+        ps.print(datatype.stringValue());
+        ps.print('"');
+        } else {
+            ps.print("null");
+        }
+
+        ps.print('}');
+    }
+
+    private void printValue(String type, String value, PrintStream ps) {
+        printCommaIfNeeded(ps);
+
+        ps.print("{ \"type\" : \"");
+        ps.print(type);
+        ps.print("\", \"value\" : ");
+        if (value != null) {
+            ps.print('"');
+            ps.print(value);
+            ps.print('"');
+        } else {
+            ps.print("null");
+        }
+        ps.print('}');
+    }
+
+    private void printValue(String value, PrintStream ps) {
+        printCommaIfNeeded(ps);
+
+        if (value != null) {
+            ps.print('"');
+            ps.print(value);
+            ps.print('"');
+        } else {
+            ps.print("null");
+        }
+    }
+
+    @Override
+    public boolean isAnnotated() {
+        return false; // TODO: add annotation support.
+    }
+
+    @Override
+    public void setAnnotated(boolean f) {
+        // Empty.
     }
 }

http://git-wip-us.apache.org/repos/asf/any23/blob/27dac032/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
index 1b1d846..44bb5c0 100644
--- a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
+++ b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
@@ -14,10 +14,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.any23.writer;
 
 import java.io.ByteArrayOutputStream;
-import org.apache.any23.extractor.ExtractionContext;
 import org.eclipse.rdf4j.model.IRI;
 import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
 import org.junit.Assert;
@@ -33,41 +33,57 @@ public class JSONWriterTest {
     @Test
     public void testWriting() throws TripleHandlerException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try (JSONWriter jsonWriter = new JSONWriter(baos);) {
-            final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
-            ExtractionContext extractionContext = new ExtractionContext("rdf-nq", SimpleValueFactory.getInstance().createIRI("http://any23.org/tmp/"));
-           
-            jsonWriter.startDocument(documentIRI);
-            jsonWriter.receiveTriple(
-                    SimpleValueFactory.getInstance().createBNode("bn1"),
-                    SimpleValueFactory.getInstance().createIRI("http://pred/1"),
-                    SimpleValueFactory.getInstance().createIRI("http://value/1"),
-                    SimpleValueFactory.getInstance().createIRI("http://graph/1"),
-                    null
-            );
-            jsonWriter.receiveTriple(
-                    SimpleValueFactory.getInstance().createIRI("http://sub/2"),
-                    SimpleValueFactory.getInstance().createIRI("http://pred/2"),
-                    SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
-                    SimpleValueFactory.getInstance().createIRI("http://graph/2"),
-                    null
-            );
-            jsonWriter.receiveTriple(
-                    SimpleValueFactory.getInstance().createIRI("http://sub/3"),
-                    SimpleValueFactory.getInstance().createIRI("http://pred/3"),
-                    SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
-                    null,
-                    extractionContext
-            );
-            jsonWriter.endDocument(documentIRI);
+        try(JSONWriter jsonWriter = new JSONWriter(baos);) {
+	        final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
+	        jsonWriter.startDocument(documentIRI);
+	        jsonWriter.receiveTriple(
+	                SimpleValueFactory.getInstance().createBNode("bn1"),
+	                SimpleValueFactory.getInstance().createIRI("http://pred/1"),
+	                SimpleValueFactory.getInstance().createIRI("http://value/1"),
+	                SimpleValueFactory.getInstance().createIRI("http://graph/1"),
+	                null
+	        );
+	        jsonWriter.receiveTriple(
+	                SimpleValueFactory.getInstance().createIRI("http://sub/2"),
+	                SimpleValueFactory.getInstance().createIRI("http://pred/2"),
+	                SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
+	                SimpleValueFactory.getInstance().createIRI("http://graph/2"),
+	                null
+	        );
+	        jsonWriter.receiveTriple(
+	                SimpleValueFactory.getInstance().createIRI("http://sub/3"),
+	                SimpleValueFactory.getInstance().createIRI("http://pred/3"),
+	                SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
+	                null,
+	                null
+	        );
+	        jsonWriter.endDocument(documentIRI);
         }
 
-        final String expected
-                = "["
-                + "{\"@graph\":[{\"@id\":\"http://sub/3\",\"http://pred/3\":[{\"@type\":\"http://datatype\",\"@value\":\"123\"}]}],\"@id\":\"http://any23.org/tmp/\"},"
-                + "{\"@graph\":[{\"@id\":\"_:bn1\",\"http://pred/1\":[{\"@id\":\"http://value/1\"}]}],\"@id\":\"http://graph/1\"},"
-                + "{\"@graph\":[{\"@id\":\"http://sub/2\",\"http://pred/2\":[{\"@language\":\"en\",\"@value\":\"language literal\"}]}],\"@id\":\"http://graph/2\"}"
-                + "]";
+        final String expected =
+            "{ " +
+            "\"quads\" : " +
+            "[" +
+            "[" +
+            "{ \"type\" : \"bnode\", \"value\" : \"bn1\"}, " +
+            "\"http://pred/1\", " +
+            "{ \"type\" : \"uri\", \"value\" : \"http://value/1\"}, " +
+            "\"http://graph/1\"" +
+            "], " +
+            "[" +
+            "{ \"type\" : \"uri\", \"value\" : \"http://sub/2\"}, " +
+            "\"http://pred/2\", " +
+            "{\"type\" : \"literal\", \"value\" : \"language literal\", \"lang\" : \"en\", \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"}, " +
+            "\"http://graph/2\"" +
+            "], " +
+            "[" +
+            "{ \"type\" : \"uri\", \"value\" : \"http://sub/3\"}, " +
+            "\"http://pred/3\", " +
+            "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, " +
+            "null" +
+            "]" +
+            "]" +
+            "}";
         Assert.assertEquals(expected, baos.toString());
     }
 }

http://git-wip-us.apache.org/repos/asf/any23/blob/27dac032/service/src/test/java/org/apache/any23/servlet/ServletTest.java
----------------------------------------------------------------------
diff --git a/service/src/test/java/org/apache/any23/servlet/ServletTest.java b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
index 02c0603..2359e90 100644
--- a/service/src/test/java/org/apache/any23/servlet/ServletTest.java
+++ b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.any23.servlet;
 
 import java.io.File;
@@ -223,7 +222,9 @@ public class ServletTest {
     }
 
     /**
-     * This test has been disabled in order to avoid external resources dependencies
+     * This test has been disabled in order to avoid external resources
+     * dependencies
+     *
      * @throws Exception
      */
     @Test
@@ -233,8 +234,10 @@ public class ServletTest {
         Assert.assertEquals(200, response.getStatus());
     }
 
-     /**
-     * This test has been disabled in order to avoid external resources dependencies
+    /**
+     * This test has been disabled in order to avoid external resources
+     * dependencies
+     *
      * @throws Exception
      */
     @Test
@@ -244,8 +247,10 @@ public class ServletTest {
         Assert.assertEquals(200, response.getStatus());
     }
 
-     /**
-     * This test has been disabled in order to avoid external resources dependencies
+    /**
+     * This test has been disabled in order to avoid external resources
+     * dependencies
+     *
      * @throws Exception
      */
     @Test
@@ -375,16 +380,16 @@ public class ServletTest {
         HttpTester response = doGetRequest("/best/http://foo.com?validation-mode=validate-fix&report=on");
         Assert.assertEquals(200, response.getStatus());
         final String content = response.getContent();
-        assertContainsTag("response"        , content);
-        assertContainsTag("extractors"      , content);
-        assertContainsTag("report"          , content);
-        assertContainsTag("message", true, 1 , content);
-        assertContainsTag("error"  , true, 1 , content);
-        assertContainsTag("error"  , true, 1 , content);
+        assertContainsTag("response", content);
+        assertContainsTag("extractors", content);
+        assertContainsTag("report", content);
+        assertContainsTag("message", true, 1, content);
+        assertContainsTag("error", true, 1, content);
+        assertContainsTag("error", true, 1, content);
         assertContainsTag("validationReport", content);
-        assertContainsTag("errors"          , content);
-        assertContainsTag("issues"          , content);
-        assertContainsTag("ruleActivations" , content);
+        assertContainsTag("errors", content);
+        assertContainsTag("issues", content);
+        assertContainsTag("ruleActivations", content);
         assertContainsTag("data", content);
     }
 
@@ -393,13 +398,13 @@ public class ServletTest {
         String body = "<http://sub/1> <http://pred/1> \"123\"^^<http://datatype> <http://graph/1>.";
         HttpTester response = doPostRequest("/json", body, "application/n-quads");
         Assert.assertEquals(200, response.getStatus());
-        final String EXPECTED_JSON =
+        final String EXPECTED_JSON = 
                 "["
-                + "{\"@graph\":"
-                + "["
-                + "{\"@id\":\"http://sub/1\","
-                + "\"http://pred/1\":[{\"@type\":\"http://datatype\",\"@value\":\"123\"}]}],"
-                + "\"@id\":\"http://graph/1\"}]";
+                + "{ \"type\" : \"uri\", \"value\" : \"http://sub/1\"}, "
+                + "\"http://pred/1\", "
+                + "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, "
+                + "\"http://graph/1\""
+                + "]";
         assertContains(EXPECTED_JSON, response.getContent());
     }
 
@@ -409,8 +414,8 @@ public class ServletTest {
         HttpTester response = doPostRequest("/trix", body, "application/n-quads");
         Assert.assertEquals(200, response.getStatus());
         final String content = response.getContent();
-        assertContainsTag("graph" , false, 1, content);
-        assertContainsTag("uri"   , false, 3, content);
+        assertContainsTag("graph", false, 1, content);
+        assertContainsTag("uri", false, 3, content);
         assertContainsTag("triple", false, 1, content);
     }
 
@@ -451,9 +456,12 @@ public class ServletTest {
     }
 
     private void assertContains(String expected, String container) {
-        if(expected.length() == 0)
+        if (expected.length() == 0) {
             throw new IllegalArgumentException("expected string must contains at lease one char.");
-        if (container.contains(expected)) return;
+        }
+        if (container.contains(expected)) {
+            return;
+        }
         Assert.fail("expected '" + expected + "' to be contained in '" + container + "'");
     }
 
@@ -468,8 +476,8 @@ public class ServletTest {
             Assert.assertEquals(
                     String.format("Cannot find open tag %s %d times", tag, occurrences),
                     occurrences,
-                    StringUtils.countOccurrences(container, "<" + tag + ">") +
-                    StringUtils.countOccurrences(container, "<" + tag + " ")
+                    StringUtils.countOccurrences(container, "<" + tag + ">")
+                    + StringUtils.countOccurrences(container, "<" + tag + " ")
             );
             Assert.assertEquals(
                     String.format("Cannot find close tag %s %d times", tag, occurrences),
@@ -492,7 +500,7 @@ public class ServletTest {
         protected DocumentSource createHTTPDocumentSource(HTTPClient httpClient, String uri)
                 throws IOException, URISyntaxException {
             requestedIRI = uri;
-            if(content != null) {
+            if (content != null) {
                 return new StringDocumentSource(content, uri);
             } else {
                 return super.createHTTPDocumentSource(httpClient, uri);


[6/6] any23 git commit: Update the site documentation - Supported Formats (JSON-LD)

Posted by le...@apache.org.
Update the site documentation - Supported Formats (JSON-LD)

Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/ed662bae
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/ed662bae
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/ed662bae

Branch: refs/heads/master
Commit: ed662baee6363bc9d6c92e8aebbff662002d4dde
Parents: b4e5005
Author: dev <ju...@gmail.com>
Authored: Wed May 9 23:27:22 2018 -0500
Committer: dev <ju...@gmail.com>
Committed: Wed May 9 23:27:22 2018 -0500

----------------------------------------------------------------------
 src/site/apt/supported-formats.apt | 2 ++
 1 file changed, 2 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/ed662bae/src/site/apt/supported-formats.apt
----------------------------------------------------------------------
diff --git a/src/site/apt/supported-formats.apt b/src/site/apt/supported-formats.apt
index 0f6ea22..239d36f 100644
--- a/src/site/apt/supported-formats.apt
+++ b/src/site/apt/supported-formats.apt
@@ -58,6 +58,8 @@ Supported Formats in Apache Any23
 
    * <<RDF/XML>> <<Apache Any23>> is able to produce output in {{{http://www.w3.org/TR/rdf-syntax-grammar/}RDF/XML}}.
 
+   * <<JSON-LD>> <<Apache Any23>> is able to produce output in {{{http://www.w3.org/TR/json-ld/}JSON-LD}}.
+
    * <<JSON Statements>> <<Apache Any23>> is able to produce output in {{{http://www.json.org/}JSON}} . See the specific {{{json-statements}format}}.
 
    * <<XML Report>> <<Apache Any23>> is able to produce a detailed report of the latest document extraction if required. See further details {{{./service.html#report-format}here}}.


[4/6] any23 git commit: use jackson for parsing json

Posted by le...@apache.org.
use jackson for parsing json


Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/ff4ad701
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/ff4ad701
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/ff4ad701

Branch: refs/heads/master
Commit: ff4ad701f8a047b0921e4eea35a8c1f88128e487
Parents: 2cb1242
Author: Julio Caguano <ju...@gmail.com>
Authored: Sun May 6 19:36:28 2018 -0500
Committer: Julio Caguano <ju...@gmail.com>
Committed: Sun May 6 19:36:28 2018 -0500

----------------------------------------------------------------------
 .../org/apache/any23/writer/JSONWriter.java     | 203 ++++++++-----------
 .../org/apache/any23/writer/JSONWriterTest.java |   3 +-
 2 files changed, 82 insertions(+), 124 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/ff4ad701/core/src/main/java/org/apache/any23/writer/JSONWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONWriter.java b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
index a4cce5a..d274871 100644
--- a/core/src/main/java/org/apache/any23/writer/JSONWriter.java
+++ b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
@@ -14,12 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.any23.writer;
 
-import java.io.BufferedOutputStream;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
+import java.io.IOException;
 import java.io.OutputStream;
-import java.io.PrintStream;
 import java.util.Optional;
 import org.apache.any23.extractor.ExtractionContext;
 import org.eclipse.rdf4j.model.BNode;
@@ -35,29 +36,35 @@ import org.eclipse.rdf4j.model.Value;
  */
 public class JSONWriter implements FormatWriter {
 
-    private final PrintStream ps;
-
+    private JsonGenerator ps;
     private boolean documentStarted = false;
 
-    private boolean firstArrayElemWritten = false;
-    private boolean firstObjectWritten    = false;
-
     public JSONWriter(OutputStream os) {
-        if(os == null) {
+        if (os == null) {
             throw new NullPointerException("Output stream cannot be null.");
         }
-        this.ps = new PrintStream(new BufferedOutputStream(os));
+        JsonFactory factory = new JsonFactory();
+        try {
+            this.ps = factory.createGenerator(os)
+                    .setPrettyPrinter(new DefaultPrettyPrinter());
+        } catch (IOException ex) {
+        }
     }
 
     @Override
     public void startDocument(IRI documentIRI) throws TripleHandlerException {
-        if(documentStarted) {
+        if (documentStarted) {
             throw new IllegalStateException("Document already started.");
         }
         documentStarted = true;
 
-        firstArrayElemWritten = false;
-        ps.print("{ \"quads\" : [");
+        try {
+            ps.writeStartObject();
+            ps.writeFieldName("quads");
+            ps.writeStartArray();
+        } catch (IOException ex) {
+            throw new TripleHandlerException("IO Error while starting document.", ex);
+        }
     }
 
     @Override
@@ -67,42 +74,38 @@ public class JSONWriter implements FormatWriter {
 
     @Override
     public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
-    throws TripleHandlerException {
+            throws TripleHandlerException {
         validateDocumentStarted();
-
-        if(firstArrayElemWritten) {
-            ps.print(", ");
-        } else {
-            firstArrayElemWritten = true;
-        }
-        firstObjectWritten    = false;
-        
-        ps.print('[');
-
-        if(s instanceof IRI) {
-            printExplicitIRI(s.stringValue(), ps);
-        } else {
-            printBNode(s.stringValue(), ps);
-        }
-
-        printIRI(p.stringValue(), ps);
-
-         if(o instanceof IRI) {
-            printExplicitIRI(o.stringValue(), ps);
-        } else if(o instanceof BNode) {
-            printBNode(o.stringValue(), ps);
-        } else {
-            printLiteral((Literal) o, ps);
+        try {
+            ps.writeStartArray();
+
+            if (s instanceof IRI) {
+                printExplicitIRI(s.stringValue());
+            } else {
+                printBNode(s.stringValue());
+            }
+
+            printIRI(p.stringValue());
+
+            if (o instanceof IRI) {
+                printExplicitIRI(o.stringValue());
+            } else if (o instanceof BNode) {
+                printBNode(o.stringValue());
+            } else {
+                printLiteral((Literal) o);
+            }
+
+            printIRI(g == null ? null : g.stringValue());
+
+            ps.writeEndArray();
+        } catch (IOException ex) {
+            throw new TripleHandlerException("IO Error while writing triple", ex);
         }
-
-        printIRI(g == null ? null : g.stringValue(), ps);
-
-        ps.print(']');
     }
 
     @Override
     public void receiveNamespace(String prefix, String uri, ExtractionContext context)
-    throws TripleHandlerException {
+            throws TripleHandlerException {
         // Empty.
     }
 
@@ -114,8 +117,14 @@ public class JSONWriter implements FormatWriter {
     @Override
     public void endDocument(IRI documentIRI) throws TripleHandlerException {
         validateDocumentStarted();
-        ps.print("]}");
-        documentStarted = false;
+
+        try {
+            ps.writeEndArray();
+            ps.writeEndObject();
+            documentStarted = false;
+        } catch (IOException ex) {
+            throw new TripleHandlerException("IO Error while closing document.", ex);
+        }
     }
 
     @Override
@@ -125,105 +134,53 @@ public class JSONWriter implements FormatWriter {
 
     @Override
     public void close() throws TripleHandlerException {
-    	if(documentStarted) {
-    		endDocument(null);
-    	}
-        ps.close();
+        if (documentStarted) {
+            endDocument(null);
+        }
+        try {
+            ps.close();
+        } catch (IOException ex) {
+            throw new TripleHandlerException("IO Error while closing stream.", ex);
+        }
     }
 
     private void validateDocumentStarted() {
-       if(!documentStarted) {
+        if (!documentStarted) {
             throw new IllegalStateException("Document didn't start.");
         }
     }
 
-    private void printIRI(String uri, PrintStream ps) {
-        printValue(uri, ps);
+    private void printIRI(String uri) throws IOException {
+        printValue(uri);
     }
 
-    private void printExplicitIRI(String uri, PrintStream ps) {
-        printValue("uri", uri, ps);
+    private void printExplicitIRI(String uri) throws IOException {
+        printValue("uri", uri);
     }
 
-    private void printBNode(String bnode, PrintStream ps) {
-        printValue("bnode", bnode, ps);
+    private void printBNode(String bnode) throws IOException {
+        printValue("bnode", bnode);
     }
 
-    private void printCommaIfNeeded(PrintStream ps) {
-        if(firstObjectWritten) {
-            ps.print(", ");
-        } else {
-            firstObjectWritten = true;
-        }
-    }
-
-    private void printLiteral(Literal literal, PrintStream ps) {
-        printCommaIfNeeded(ps);
-
-        ps.print('{');
-
-        ps.print("\"type\" : \"literal\"");
-
-        ps.print(", ");
-
-        ps.print("\"value\" : ");
-        ps.print('"');
-        ps.print(literal.stringValue());
-        ps.print('"');
-
-        ps.print(", ");
+    private void printLiteral(Literal literal) throws IOException {
+        ps.writeStartObject();
+        ps.writeStringField("type", "literal");
+        ps.writeStringField("value", literal.stringValue());
 
-        ps.print("\"lang\" : ");
         final Optional<String> language = literal.getLanguage();
-        if (language.isPresent()) {
-            ps.print('"');
-            ps.print(literal.getLanguage().get());
-            ps.print('"');
-        } else {
-            ps.print("null");
-        }
-
-        ps.print(", ");
+        ps.writeStringField("lang", language.isPresent() ? literal.getLanguage().get() : null);
 
-        ps.print("\"datatype\" : ");
         final IRI datatype = literal.getDatatype();
-        if(datatype != null) {
-        ps.print('"');
-        ps.print(datatype.stringValue());
-        ps.print('"');
-        } else {
-            ps.print("null");
-        }
-
-        ps.print('}');
+        ps.writeStringField("datatype", datatype != null ? datatype.stringValue() : null);
+        ps.writeEndObject();
     }
 
-    private void printValue(String type, String value, PrintStream ps) {
-        printCommaIfNeeded(ps);
-
-        ps.print("{ \"type\" : \"");
-        ps.print(type);
-        ps.print("\", \"value\" : ");
-        if (value != null) {
-            ps.print('"');
-            ps.print(value);
-            ps.print('"');
-        } else {
-            ps.print("null");
-        }
-        ps.print('}');
+    private void printValue(String type, String value) throws IOException {
+        ps.writeString(value);
     }
 
-    private void printValue(String value, PrintStream ps) {
-        printCommaIfNeeded(ps);
-
-        if (value != null) {
-            ps.print('"');
-            ps.print(value);
-            ps.print('"');
-        } else {
-            ps.print("null");
-        }
+    private void printValue(String value) throws IOException {
+        ps.writeString(value);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/any23/blob/ff4ad701/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
index 8660716..93d0558 100644
--- a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
+++ b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
@@ -17,6 +17,7 @@
 package org.apache.any23.writer;
 
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import org.apache.any23.extractor.ExtractionContext;
 import org.eclipse.rdf4j.model.IRI;
 import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
@@ -32,7 +33,7 @@ import org.junit.Test;
 public class JSONWriterTest {
 
     @Test
-    public void testJSONWriting() throws TripleHandlerException {
+    public void testJSONWriting() throws TripleHandlerException, IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         writeContent(new JSONWriter(baos));
 


[5/6] any23 git commit: Fix some tests and add jsonld to Live Service Demo.

Posted by le...@apache.org.
Fix some tests and add jsonld to Live Service Demo.

Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/b4e50050
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/b4e50050
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/b4e50050

Branch: refs/heads/master
Commit: b4e50050c885487465af184e7073f3a879db2453
Parents: ff4ad70
Author: dev <ju...@gmail.com>
Authored: Mon May 7 01:05:39 2018 -0500
Committer: dev <ju...@gmail.com>
Committed: Mon May 7 01:05:39 2018 -0500

----------------------------------------------------------------------
 .../org/apache/any23/writer/JSONWriter.java     |  5 +-
 .../org/apache/any23/writer/JSONWriterTest.java | 50 ++++++++++----------
 service/src/main/resources/form.html            |  3 ++
 .../org/apache/any23/servlet/ServletTest.java   | 15 +++---
 4 files changed, 42 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/b4e50050/core/src/main/java/org/apache/any23/writer/JSONWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONWriter.java b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
index d274871..70e2700 100644
--- a/core/src/main/java/org/apache/any23/writer/JSONWriter.java
+++ b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
@@ -176,7 +176,10 @@ public class JSONWriter implements FormatWriter {
     }
 
     private void printValue(String type, String value) throws IOException {
-        ps.writeString(value);
+        ps.writeStartObject();
+        ps.writeStringField("type", type);
+        ps.writeStringField("value", value);
+        ps.writeEndObject();
     }
 
     private void printValue(String value) throws IOException {

http://git-wip-us.apache.org/repos/asf/any23/blob/b4e50050/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
index 93d0558..f6cdfeb 100644
--- a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
+++ b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
@@ -37,30 +37,32 @@ public class JSONWriterTest {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         writeContent(new JSONWriter(baos));
 
-        final String expected
-                = "{ "
-                + "\"quads\" : "
-                + "["
-                + "["
-                + "{ \"type\" : \"bnode\", \"value\" : \"bn1\"}, "
-                + "\"http://pred/1\", "
-                + "{ \"type\" : \"uri\", \"value\" : \"http://value/1\"}, "
-                + "\"http://graph/1\""
-                + "], "
-                + "["
-                + "{ \"type\" : \"uri\", \"value\" : \"http://sub/2\"}, "
-                + "\"http://pred/2\", "
-                + "{\"type\" : \"literal\", \"value\" : \"language literal\", \"lang\" : \"en\", \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"}, "
-                + "\"http://graph/2\""
-                + "], "
-                + "["
-                + "{ \"type\" : \"uri\", \"value\" : \"http://sub/3\"}, "
-                + "\"http://pred/3\", "
-                + "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, "
-                + "null"
-                + "]"
-                + "]"
-                + "}";
+        final String expected 
+            = "{\n"
+            + "  \"quads\" : [ [ {\n"
+            + "    \"type\" : \"bnode\",\n"
+            + "    \"value\" : \"bn1\"\n"
+            + "  }, \"http://pred/1\", {\n"
+            + "    \"type\" : \"uri\",\n"
+            + "    \"value\" : \"http://value/1\"\n"
+            + "  }, \"http://graph/1\" ], [ {\n"
+            + "    \"type\" : \"uri\",\n"
+            + "    \"value\" : \"http://sub/2\"\n"
+            + "  }, \"http://pred/2\", {\n"
+            + "    \"type\" : \"literal\",\n"
+            + "    \"value\" : \"language literal\",\n"
+            + "    \"lang\" : \"en\",\n"
+            + "    \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"\n"
+            + "  }, \"http://graph/2\" ], [ {\n"
+            + "    \"type\" : \"uri\",\n"
+            + "    \"value\" : \"http://sub/3\"\n"
+            + "  }, \"http://pred/3\", {\n"
+            + "    \"type\" : \"literal\",\n"
+            + "    \"value\" : \"123\",\n"
+            + "    \"lang\" : null,\n"
+            + "    \"datatype\" : \"http://datatype\"\n"
+            + "  }, null ] ]\n"
+            + "}";
         Assert.assertEquals(expected, baos.toString());
     }
 

http://git-wip-us.apache.org/repos/asf/any23/blob/b4e50050/service/src/main/resources/form.html
----------------------------------------------------------------------
diff --git a/service/src/main/resources/form.html b/service/src/main/resources/form.html
index a5cf937..7cac3e4 100644
--- a/service/src/main/resources/form.html
+++ b/service/src/main/resources/form.html
@@ -76,6 +76,7 @@ function showModal( id )
             <option value="nquads">nquads</option>
             <option value="trix">trix</option>
             <option value="json">json</option>
+            <option value="jsonld">json-ld</option>
           </select>/<input type="text" size="50" name="uri" value="http://twitter.com/cygri" />
         </div>
       </div>
@@ -154,6 +155,7 @@ function showModal( id )
             <option value="nquads">nquads</option>
             <option value="trix">trix</option>
             <option value="json">json</option>
+            <option value="jsonld">json-ld</option>
           </select>
         </div>
       </div>
@@ -317,6 +319,7 @@ Content-Length: 174
       <li><code>rdfxml</code>, <code>rdf</code>, <code>xml</code> for
         <a href="http://www.w3.org/TR/rdf-syntax-grammar/" target="_blank">RDF/XML</a></li>
       <li><code>json</code> for <a href="http://json.org/" target="_blank">JSON</a></li>
+      <li><code>jsonld</code> for <a href="https://json-ld.org/" target="_blank">JSON-LD</a></li>
     </ul>
 
     <h3>Error reporting</h3>

http://git-wip-us.apache.org/repos/asf/any23/blob/b4e50050/service/src/test/java/org/apache/any23/servlet/ServletTest.java
----------------------------------------------------------------------
diff --git a/service/src/test/java/org/apache/any23/servlet/ServletTest.java b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
index 1f7044f..3d14f98 100644
--- a/service/src/test/java/org/apache/any23/servlet/ServletTest.java
+++ b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
@@ -399,12 +399,15 @@ public class ServletTest {
         HttpTester response = doPostRequest("/json", body, "application/n-quads");
         Assert.assertEquals(200, response.getStatus());
         final String EXPECTED_JSON
-                = "["
-                + "{ \"type\" : \"uri\", \"value\" : \"http://sub/1\"}, "
-                + "\"http://pred/1\", "
-                + "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, "
-                + "\"http://graph/1\""
-                + "]";
+            = "[ {\n"
+            + "    \"type\" : \"uri\",\n"
+            + "    \"value\" : \"http://sub/1\"\n"
+            + "  }, \"http://pred/1\", {\n"
+            + "    \"type\" : \"literal\",\n"
+            + "    \"value\" : \"123\",\n"
+            + "    \"lang\" : null,\n"
+            + "    \"datatype\" : \"http://datatype\"\n"
+            + "  }, \"http://graph/1\" ]";
         assertContains(EXPECTED_JSON, response.getContent());
     }
 


[3/6] any23 git commit: Add support for json-ld

Posted by le...@apache.org.
Add support for json-ld


Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/2cb12425
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/2cb12425
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/2cb12425

Branch: refs/heads/master
Commit: 2cb12425b0f8f91a072d627d3f0ee5036d8276fd
Parents: 27dac03
Author: Julio Caguano <ju...@gmail.com>
Authored: Sun May 6 19:00:51 2018 -0500
Committer: Julio Caguano <ju...@gmail.com>
Committed: Sun May 6 19:00:51 2018 -0500

----------------------------------------------------------------------
 .../org/apache/any23/writer/JSONLDWriter.java   |  33 +++++
 .../any23/writer/JSONLDWriterFactory.java       |  51 +++++++
 .../org.apache.any23.writer.WriterFactory       |   1 +
 .../org/apache/any23/writer/JSONWriterTest.java | 134 +++++++++++--------
 .../apache/any23/writer/WriterRegistryTest.java |   7 +-
 .../org/apache/any23/servlet/WebResponder.java  |  21 +--
 .../org/apache/any23/servlet/ServletTest.java   |  19 ++-
 7 files changed, 197 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/core/src/main/java/org/apache/any23/writer/JSONLDWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONLDWriter.java b/core/src/main/java/org/apache/any23/writer/JSONLDWriter.java
new file mode 100644
index 0000000..50d3900
--- /dev/null
+++ b/core/src/main/java/org/apache/any23/writer/JSONLDWriter.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.any23.writer;
+
+import java.io.OutputStream;
+import org.eclipse.rdf4j.rio.RDFFormat;
+import org.eclipse.rdf4j.rio.Rio;
+
+/**
+ * Implementation of <i>JSON-LD</i> format writer.
+ *
+ * @author Julio Caguano
+ */
+public class JSONLDWriter extends RDFWriterTripleHandler implements FormatWriter {
+
+    public JSONLDWriter(OutputStream os) {
+        super(Rio.createWriter(RDFFormat.JSONLD, os));
+    }
+}

http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/core/src/main/java/org/apache/any23/writer/JSONLDWriterFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONLDWriterFactory.java b/core/src/main/java/org/apache/any23/writer/JSONLDWriterFactory.java
new file mode 100644
index 0000000..df20279
--- /dev/null
+++ b/core/src/main/java/org/apache/any23/writer/JSONLDWriterFactory.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.any23.writer;
+
+import java.io.OutputStream;
+import org.eclipse.rdf4j.rio.RDFFormat;
+
+/**
+ *
+ * @author Julio Caguano.
+ */
+public class JSONLDWriterFactory implements WriterFactory {
+
+    public static final String MIME_TYPE = RDFFormat.JSONLD.getDefaultMIMEType();
+    public static final String IDENTIFIER = "jsonld";
+
+    @Override
+    public RDFFormat getRdfFormat() {
+        return RDFFormat.JSONLD;
+    }
+
+    @Override
+    public String getIdentifier() {
+        return JSONLDWriterFactory.IDENTIFIER;
+    }
+
+    @Override
+    public String getMimeType() {
+        return JSONLDWriterFactory.MIME_TYPE;
+    }
+
+    @Override
+    public FormatWriter getRdfWriter(OutputStream os) {
+        return new JSONLDWriter(os);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/core/src/main/resources/META-INF/services/org.apache.any23.writer.WriterFactory
----------------------------------------------------------------------
diff --git a/core/src/main/resources/META-INF/services/org.apache.any23.writer.WriterFactory b/core/src/main/resources/META-INF/services/org.apache.any23.writer.WriterFactory
index 03f32cd..3da561d 100644
--- a/core/src/main/resources/META-INF/services/org.apache.any23.writer.WriterFactory
+++ b/core/src/main/resources/META-INF/services/org.apache.any23.writer.WriterFactory
@@ -1,3 +1,4 @@
+org.apache.any23.writer.JSONLDWriterFactory
 org.apache.any23.writer.JSONWriterFactory
 org.apache.any23.writer.NQuadsWriterFactory
 org.apache.any23.writer.NTriplesWriterFactory

http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
index 44bb5c0..8660716 100644
--- a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
+++ b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
@@ -14,76 +14,104 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.any23.writer;
 
 import java.io.ByteArrayOutputStream;
+import org.apache.any23.extractor.ExtractionContext;
 import org.eclipse.rdf4j.model.IRI;
 import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
 import org.junit.Assert;
 import org.junit.Test;
 
 /**
- * Test case for {@link JSONWriter} class.
+ * Test case for {@link JSONWriter} and {@link JSONLDWriter} class.
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
+ * @author Julio Caguano
  */
 public class JSONWriterTest {
 
     @Test
-    public void testWriting() throws TripleHandlerException {
+    public void testJSONWriting() throws TripleHandlerException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try(JSONWriter jsonWriter = new JSONWriter(baos);) {
-	        final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
-	        jsonWriter.startDocument(documentIRI);
-	        jsonWriter.receiveTriple(
-	                SimpleValueFactory.getInstance().createBNode("bn1"),
-	                SimpleValueFactory.getInstance().createIRI("http://pred/1"),
-	                SimpleValueFactory.getInstance().createIRI("http://value/1"),
-	                SimpleValueFactory.getInstance().createIRI("http://graph/1"),
-	                null
-	        );
-	        jsonWriter.receiveTriple(
-	                SimpleValueFactory.getInstance().createIRI("http://sub/2"),
-	                SimpleValueFactory.getInstance().createIRI("http://pred/2"),
-	                SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
-	                SimpleValueFactory.getInstance().createIRI("http://graph/2"),
-	                null
-	        );
-	        jsonWriter.receiveTriple(
-	                SimpleValueFactory.getInstance().createIRI("http://sub/3"),
-	                SimpleValueFactory.getInstance().createIRI("http://pred/3"),
-	                SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
-	                null,
-	                null
-	        );
-	        jsonWriter.endDocument(documentIRI);
-        }
+        writeContent(new JSONWriter(baos));
+
+        final String expected
+                = "{ "
+                + "\"quads\" : "
+                + "["
+                + "["
+                + "{ \"type\" : \"bnode\", \"value\" : \"bn1\"}, "
+                + "\"http://pred/1\", "
+                + "{ \"type\" : \"uri\", \"value\" : \"http://value/1\"}, "
+                + "\"http://graph/1\""
+                + "], "
+                + "["
+                + "{ \"type\" : \"uri\", \"value\" : \"http://sub/2\"}, "
+                + "\"http://pred/2\", "
+                + "{\"type\" : \"literal\", \"value\" : \"language literal\", \"lang\" : \"en\", \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"}, "
+                + "\"http://graph/2\""
+                + "], "
+                + "["
+                + "{ \"type\" : \"uri\", \"value\" : \"http://sub/3\"}, "
+                + "\"http://pred/3\", "
+                + "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, "
+                + "null"
+                + "]"
+                + "]"
+                + "}";
+        Assert.assertEquals(expected, baos.toString());
+    }
 
-        final String expected =
-            "{ " +
-            "\"quads\" : " +
-            "[" +
-            "[" +
-            "{ \"type\" : \"bnode\", \"value\" : \"bn1\"}, " +
-            "\"http://pred/1\", " +
-            "{ \"type\" : \"uri\", \"value\" : \"http://value/1\"}, " +
-            "\"http://graph/1\"" +
-            "], " +
-            "[" +
-            "{ \"type\" : \"uri\", \"value\" : \"http://sub/2\"}, " +
-            "\"http://pred/2\", " +
-            "{\"type\" : \"literal\", \"value\" : \"language literal\", \"lang\" : \"en\", \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"}, " +
-            "\"http://graph/2\"" +
-            "], " +
-            "[" +
-            "{ \"type\" : \"uri\", \"value\" : \"http://sub/3\"}, " +
-            "\"http://pred/3\", " +
-            "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, " +
-            "null" +
-            "]" +
-            "]" +
-            "}";
+    @Test
+    public void testJSONLDWriting() throws TripleHandlerException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        writeContent(new JSONLDWriter(baos));
+        final String expected
+                = "["
+                + "{\"@graph\":[{\"@id\":\"http://sub/3\",\"http://pred/3\":[{\"@type\":\"http://datatype\",\"@value\":\"123\"}]}],\"@id\":\"http://any23.org/tmp/\"},"
+                + "{\"@graph\":[{\"@id\":\"_:bn1\",\"http://pred/1\":[{\"@id\":\"http://value/1\"}]}],\"@id\":\"http://graph/1\"},"
+                + "{\"@graph\":[{\"@id\":\"http://sub/2\",\"http://pred/2\":[{\"@language\":\"en\",\"@value\":\"language literal\"}]}],\"@id\":\"http://graph/2\"}"
+                + "]";
         Assert.assertEquals(expected, baos.toString());
     }
+
+    private void writeContent(FormatWriter writer) throws TripleHandlerException {
+        final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
+        writer.startDocument(documentIRI);
+        writer.receiveTriple(
+                SimpleValueFactory.getInstance().createBNode("bn1"),
+                SimpleValueFactory.getInstance().createIRI("http://pred/1"),
+                SimpleValueFactory.getInstance().createIRI("http://value/1"),
+                SimpleValueFactory.getInstance().createIRI("http://graph/1"),
+                null
+        );
+        writer.receiveTriple(
+                SimpleValueFactory.getInstance().createIRI("http://sub/2"),
+                SimpleValueFactory.getInstance().createIRI("http://pred/2"),
+                SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
+                SimpleValueFactory.getInstance().createIRI("http://graph/2"),
+                null
+        );
+        if (writer instanceof JSONWriter) {
+            writer.receiveTriple(
+                    SimpleValueFactory.getInstance().createIRI("http://sub/3"),
+                    SimpleValueFactory.getInstance().createIRI("http://pred/3"),
+                    SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
+                    null,
+                    null
+            );
+        } else if (writer instanceof JSONLDWriter) {
+            ExtractionContext extractionContext = new ExtractionContext("rdf-nq", SimpleValueFactory.getInstance().createIRI("http://any23.org/tmp/"));
+            writer.receiveTriple(
+                    SimpleValueFactory.getInstance().createIRI("http://sub/3"),
+                    SimpleValueFactory.getInstance().createIRI("http://pred/3"),
+                    SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
+                    null,
+                    extractionContext
+            );
+        }
+        writer.endDocument(documentIRI);
+        writer.close();
+    }
 }

http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/core/src/test/java/org/apache/any23/writer/WriterRegistryTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/WriterRegistryTest.java b/core/src/test/java/org/apache/any23/writer/WriterRegistryTest.java
index e89822e..ec0ccf0 100644
--- a/core/src/test/java/org/apache/any23/writer/WriterRegistryTest.java
+++ b/core/src/test/java/org/apache/any23/writer/WriterRegistryTest.java
@@ -17,14 +17,13 @@
 
 package org.apache.any23.writer;
 
-import org.junit.Assert;
-import org.junit.Test;
-
 import java.io.ByteArrayOutputStream;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import org.junit.Assert;
+import org.junit.Test;
 
 /**
  * Test case for {@link WriterRegistry}.
@@ -33,7 +32,7 @@ import java.util.Set;
  */
 public class WriterRegistryTest {
 
-    private static final int NUM_OF_WRITERS = 7;
+    private static final int NUM_OF_WRITERS = 8;
 
     private final WriterFactoryRegistry target = WriterFactoryRegistry.getInstance();
 

http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/service/src/main/java/org/apache/any23/servlet/WebResponder.java
----------------------------------------------------------------------
diff --git a/service/src/main/java/org/apache/any23/servlet/WebResponder.java b/service/src/main/java/org/apache/any23/servlet/WebResponder.java
index 5b16070..b9641d2 100644
--- a/service/src/main/java/org/apache/any23/servlet/WebResponder.java
+++ b/service/src/main/java/org/apache/any23/servlet/WebResponder.java
@@ -17,6 +17,15 @@
 
 package org.apache.any23.servlet;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
 import org.apache.any23.Any23;
 import org.apache.any23.ExtractionReport;
 import org.apache.any23.extractor.ExtractionException;
@@ -38,16 +47,6 @@ import org.apache.any23.writer.WriterFactory;
 import org.apache.any23.writer.WriterFactoryRegistry;
 import sun.security.validator.ValidatorException;
 
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletResponse;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.PrintStream;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
 /**
  * This class is responsible for building the {@link Servlet}
  * web response.
@@ -358,6 +357,8 @@ class WebResponder {
             finalFormat = "trix";
         } else if("json".equals(format)) {
             finalFormat = "json";
+        } else if("jsonld".equals(format)){
+            finalFormat = "jsonld";        
         } else {
             return null;
         }

http://git-wip-us.apache.org/repos/asf/any23/blob/2cb12425/service/src/test/java/org/apache/any23/servlet/ServletTest.java
----------------------------------------------------------------------
diff --git a/service/src/test/java/org/apache/any23/servlet/ServletTest.java b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
index 2359e90..1f7044f 100644
--- a/service/src/test/java/org/apache/any23/servlet/ServletTest.java
+++ b/service/src/test/java/org/apache/any23/servlet/ServletTest.java
@@ -398,8 +398,8 @@ public class ServletTest {
         String body = "<http://sub/1> <http://pred/1> \"123\"^^<http://datatype> <http://graph/1>.";
         HttpTester response = doPostRequest("/json", body, "application/n-quads");
         Assert.assertEquals(200, response.getStatus());
-        final String EXPECTED_JSON = 
-                "["
+        final String EXPECTED_JSON
+                = "["
                 + "{ \"type\" : \"uri\", \"value\" : \"http://sub/1\"}, "
                 + "\"http://pred/1\", "
                 + "{\"type\" : \"literal\", \"value\" : \"123\", \"lang\" : null, \"datatype\" : \"http://datatype\"}, "
@@ -409,6 +409,21 @@ public class ServletTest {
     }
 
     @Test
+    public void testJSONLDResponseFormat() throws Exception {
+        String body = "<http://sub/1> <http://pred/1> \"123\"^^<http://datatype> <http://graph/1>.";
+        HttpTester response = doPostRequest("/jsonld", body, "application/n-quads");
+        Assert.assertEquals(200, response.getStatus());
+        final String EXPECTED_JSON
+                = "["
+                + "{\"@graph\":"
+                + "["
+                + "{\"@id\":\"http://sub/1\","
+                + "\"http://pred/1\":[{\"@type\":\"http://datatype\",\"@value\":\"123\"}]}],"
+                + "\"@id\":\"http://graph/1\"}]";
+        assertContains(EXPECTED_JSON, response.getContent());
+    }
+
+    @Test
     public void testTriXResponseFormat() throws Exception {
         String body = "<http://sub/1> <http://pred/1> \"123\"^^<http://datatype> <http://graph/1>.";
         HttpTester response = doPostRequest("/trix", body, "application/n-quads");