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 <[email protected]>
Authored: Sun May 6 19:00:51 2018 -0500
Committer: Julio Caguano <[email protected]>
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 ([email protected])
+ * @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");

Reply via email to