You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2017/01/17 16:16:48 UTC

[6/9] jena git commit: Definition of Symbol cste moved from JsonLDReader to RIOT, and test to check what happens if overriding the @context by a URI

Definition of Symbol cste moved from JsonLDReader to RIOT, and test to
check what happens if overriding the @context by a URI

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

Branch: refs/heads/master
Commit: 4cdb77ffec7ba8b65ec4d174d85e65d176211fc4
Parents: e5885b3
Author: Franc\u0327ois-Paul Servant <fp...@semanlink.net>
Authored: Mon Jan 9 15:53:31 2017 +0100
Committer: Franc\u0327ois-Paul Servant <fp...@semanlink.net>
Committed: Mon Jan 9 15:53:31 2017 +0100

----------------------------------------------------------------------
 .../main/java/org/apache/jena/riot/RIOT.java    |  8 +++
 .../org/apache/jena/riot/lang/JsonLDReader.java | 40 ++++++-----
 .../org/apache/jena/riot/TestJsonLDReader.java  | 72 ++++++++++++++------
 3 files changed, 81 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/4cdb77ff/jena-arq/src/main/java/org/apache/jena/riot/RIOT.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/RIOT.java b/jena-arq/src/main/java/org/apache/jena/riot/RIOT.java
index 86d0ae0..2bd31c3 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/RIOT.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/RIOT.java
@@ -106,4 +106,12 @@ public class RIOT {
     public static String getBuildDate() {
         return ARQ.BUILD_DATE ;
     }
+    
+    /**
+     * Symbol to use to pass (in a Context object) the "@context" to be used when reading jsonld
+     * (overriding the actual @context in the jsonld)
+     * Expected value: the value of the "@context", 
+     * as expected by the JSONLD-java API (a Map) */
+    public static final Symbol JSONLD_CONTEXT = Symbol.create("http://jena.apache.org/riot/jsonld#JSONLD_CONTEXT");
+
 }

http://git-wip-us.apache.org/repos/asf/jena/blob/4cdb77ff/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
index bd1d081..e567ce1 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/JsonLDReader.java
@@ -26,11 +26,6 @@ import java.util.Map ;
 import java.util.Map.Entry;
 import java.util.Objects;
 
-import com.fasterxml.jackson.core.JsonLocation ;
-import com.fasterxml.jackson.core.JsonProcessingException ;
-import com.github.jsonldjava.core.* ;
-import com.github.jsonldjava.utils.JsonUtils ;
-
 import org.apache.jena.atlas.io.IO ;
 import org.apache.jena.atlas.lib.InternalErrorException ;
 import org.apache.jena.atlas.web.ContentType ;
@@ -40,25 +35,34 @@ import org.apache.jena.graph.Node ;
 import org.apache.jena.graph.NodeFactory ;
 import org.apache.jena.graph.Triple ;
 import org.apache.jena.riot.RDFLanguages ;
+import org.apache.jena.riot.RIOT;
 import org.apache.jena.riot.ReaderRIOT ;
 import org.apache.jena.riot.RiotException ;
-import org.apache.jena.riot.system.* ;
+import org.apache.jena.riot.system.ErrorHandler;
+import org.apache.jena.riot.system.ErrorHandlerFactory;
+import org.apache.jena.riot.system.ParserProfile;
+import org.apache.jena.riot.system.RiotLib;
+import org.apache.jena.riot.system.StreamRDF;
+import org.apache.jena.riot.system.SyntaxLabels;
 import org.apache.jena.sparql.core.Quad ;
 import org.apache.jena.sparql.util.Context ;
-import org.apache.jena.sparql.util.Symbol;
 
+import com.fasterxml.jackson.core.JsonLocation ;
+import com.fasterxml.jackson.core.JsonProcessingException ;
+import com.github.jsonldjava.core.JsonLdError;
+import com.github.jsonldjava.core.JsonLdOptions;
+import com.github.jsonldjava.core.JsonLdProcessor;
+import com.github.jsonldjava.core.JsonLdTripleCallback;
+import com.github.jsonldjava.core.RDFDataset;
+import com.github.jsonldjava.utils.JsonUtils ;
+
+/**
+ * Note: it is possible to override jsonld's "@context" value by providing one,
+ * using a {@link org.apache.jena.sparql.util.Context}, and setting {@link org.apache.jena.query.ARQ.RIOT#JSONLD_CONTEXT} Symbol's value
+ * to the data expected by JSON-LD java API (a Map)
+ */
 public class JsonLDReader implements ReaderRIOT
 {
-    private static final String SYMBOLS_NS = "http://jena.apache.org/riot/jsonld#" ;
-    /**
-     * Symbol to use to pass the "@context" to be used when reading the jsonld
-     * (overriding the actual @context in the jsonld)
-     * Expected value: the value of the "@context", 
-     * as expected by the JSONLD-java API (a Map) */
-    public static final Symbol JSONLD_CONTEXT = Symbol.create(SYMBOLS_NS + "JSONLD_CONTEXT");
-
-    //
-    
     private ErrorHandler errorHandler = ErrorHandlerFactory.getDefaultErrorHandler() ;
     private ParserProfile parserProfile = null ; 
     
@@ -94,7 +98,7 @@ public class JsonLDReader implements ReaderRIOT
             Object jsonObject = JsonUtils.fromInputStream(in) ;
             
             if (context != null) {
-                Object jsonldCtx = context.get(JSONLD_CONTEXT);
+                Object jsonldCtx = context.get(RIOT.JSONLD_CONTEXT);
                 if (jsonldCtx != null) {
                     if (jsonObject instanceof Map) {
                         ((Map) jsonObject).put("@context", jsonldCtx);

http://git-wip-us.apache.org/repos/asf/jena/blob/4cdb77ff/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java b/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
index 6b12aac..b5f9373 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
@@ -30,7 +30,6 @@ import org.apache.jena.query.Dataset;
 import org.apache.jena.query.DatasetFactory;
 import org.apache.jena.rdf.model.Model;
 import org.apache.jena.rdf.model.ModelFactory;
-import org.apache.jena.riot.lang.JsonLDReader;
 import org.apache.jena.riot.system.StreamRDFLib;
 import org.apache.jena.sparql.util.Context;
 import org.apache.jena.vocabulary.RDF;
@@ -41,44 +40,75 @@ import com.github.jsonldjava.utils.JsonUtils;
 
 public class TestJsonLDReader {
 
-    @Test public final void simpleReadtest() {
-       try {
+    @Test public final void simpleReadTest() {
+        try {
             String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"http://schema.org/Person\",\"name\":\"John Doe\",\"@context\":\"http://schema.org/\"}";
             StringReader reader = new StringReader(jsonld);
             Model m = ModelFactory.createDefaultModel();
             m.read(reader, null, "JSON-LD");
             assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person")));
             assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe"));
-       } catch (RiotException e) {
-           // cf. org.apache.jena.riot.RiotException: loading remote context failed: http://schema.org/
-           // There's a line printed anyway
-           // e.printStackTrace();
-       }
+        } catch (RiotException e) {
+            // cf. org.apache.jena.riot.RiotException: loading remote context failed: http://schema.org/
+            // There's a line printed anyway
+            // e.printStackTrace();
+        }
     }
-   
-    /** test using the jena Context mechanism to pass the jsonld "@context" */
-    @Test public final void replaceAtContextTest() throws JsonGenerationException, IOException {
-         // some jsonld using schema.org's URI as "@context"
+
+    /** Test using the jena Context mechanism to pass the jsonld "@context" */
+    @Test public final void overrideAtContextTest() throws JsonGenerationException, IOException {
+        // some jsonld using schema.org's URI as "@context"
         String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://schema.org/\"}";
-        
+
         // a subset of schema.org that can be used as @context for jsonld
         String jsonldContext = "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
-        
+
         // pass the jsonldContext to the read using a jena Context
         Context jenaCtx = new Context();
         Object jsonldContextAsMap = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8)));
-        jenaCtx.set(JsonLDReader.JSONLD_CONTEXT, jsonldContextAsMap);
-        
+        jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsMap);
+
+        // read the jsonld, replacing its "@context"
         Dataset ds = DatasetFactory.create();
         ReaderRIOT reader = RDFDataMgr.createReader(Lang.JSONLD);
-        // read the jsonld, replacing its "@context"
         try (InputStream in = new ByteArrayInputStream(jsonld.getBytes(StandardCharsets.UTF_8))) {
             reader.read(in, null, null, StreamRDFLib.dataset(ds.asDatasetGraph()), jenaCtx);
         }
-
-         
+        
         Model m = ds.getDefaultModel();
         assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person")));
         assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe"));
-     }
- }
+    }
+
+    /** Not really useful, but one can replace the @context by a URI: in this case, this URI is used when expanding the json
+     * (letting JSON-LD java API taking care of downloading the context. */
+    // well, as of this writing, it doesn't work, as we get a "loading remote context failed"
+    // But it is about the replacing URI, not the replaced one, showing that the mechanism does work
+    @Test public final void overrideAtContextByURITest() throws JsonGenerationException, IOException {
+        // some jsonld using a (fake) pseudo.schema.org's URI as "@context"
+        String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://pseudo.schema.org/\"}";
+
+        // a subset of schema.org that can be used as @context for jsonld
+        String jsonldContext = "\"http://schema.org\"";
+
+        // pass the jsonldContext to the read using a jena Context
+        Context jenaCtx = new Context();
+        Object jsonldContextAsObject = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8)));
+        jenaCtx.set(RIOT.JSONLD_CONTEXT, jsonldContextAsObject);
+
+        // read the jsonld, replacing its "@context"
+        Dataset ds = DatasetFactory.create();
+        ReaderRIOT reader = RDFDataMgr.createReader(Lang.JSONLD);
+        try (InputStream in = new ByteArrayInputStream(jsonld.getBytes(StandardCharsets.UTF_8))) {
+            reader.read(in, null, null, StreamRDFLib.dataset(ds.asDatasetGraph()), jenaCtx);
+            Model m = ds.getDefaultModel();
+            assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person")));
+            assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe"));
+        } catch (RiotException e) {
+            // cf. org.apache.jena.riot.RiotException: loading remote context failed: http://schema.org/
+            // There's a line printed anyway
+            // e.printStackTrace();
+        }
+    }
+
+}