Add support for json-ld
authorJulio Caguano <julio.caguanob@gmail.com>
Mon, 7 May 2018 00:00:51 +0000 (19:00 -0500)
committerJulio Caguano <julio.caguanob@gmail.com>
Mon, 7 May 2018 00:00:51 +0000 (19:00 -0500)
core/src/main/java/org/apache/any23/writer/JSONLDWriter.java [new file with mode: 0644]
core/src/main/java/org/apache/any23/writer/JSONLDWriterFactory.java [new file with mode: 0644]
core/src/main/resources/META-INF/services/org.apache.any23.writer.WriterFactory
core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
core/src/test/java/org/apache/any23/writer/WriterRegistryTest.java
service/src/main/java/org/apache/any23/servlet/WebResponder.java
service/src/test/java/org/apache/any23/servlet/ServletTest.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 (file)
index 0000000..50d3900
--- /dev/null
@@ -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));
+    }
+}
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 (file)
index 0000000..df20279
--- /dev/null
@@ -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);
+    }
+
+}
index 03f32cd..3da561d 100644 (file)
@@ -1,3 +1,4 @@
+org.apache.any23.writer.JSONLDWriterFactory
 org.apache.any23.writer.JSONWriterFactory
 org.apache.any23.writer.NQuadsWriterFactory
 org.apache.any23.writer.NTriplesWriterFactory
index 44bb5c0..8660716 100644 (file)
  * 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();
+    }
 }
index e89822e..ec0ccf0 100644 (file)
 
 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();
 
index 5b16070..b9641d2 100644 (file)
 
 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;
         }
index 2359e90..1f7044f 100644 (file)
@@ -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");