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 2016/10/03 10:35:45 UTC

[13/41] jena git commit: JSONLD_CONTEXT_SUBSTITUTION to replace the @context in the output

JSONLD_CONTEXT_SUBSTITUTION to replace the @context in the output


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

Branch: refs/heads/master
Commit: 3c2a469ae2140eba04d3c941d0e23b11e646b404
Parents: 66f604c
Author: Franc\u0327ois-Paul Servant <fp...@semanlink.net>
Authored: Fri May 6 16:03:26 2016 +0200
Committer: Franc\u0327ois-Paul Servant <fp...@semanlink.net>
Committed: Fri May 6 16:03:26 2016 +0200

----------------------------------------------------------------------
 .../org/apache/jena/riot/out/JsonLDWriter.java  | 29 ++++++++++++++++++--
 .../jena/riot/writer/TestJsonLDWriter.java      | 22 +++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/3c2a469a/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java b/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
index 1b7a94e..0bbba9f 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/out/JsonLDWriter.java
@@ -83,7 +83,19 @@ public class JsonLDWriter extends WriterDatasetRIOTBase
 {
 		/** Expected value: the value of the "@context" (a JSON String) */
 		public static final Symbol JSONLD_CONTEXT = Symbol.create("JSONLD_CONTEXT");
-		
+		/**
+		 * Expected value: the value of the "@context" to be put in final output (a JSON String) 
+		 * This is NOT the context used to produce the output (given by JSONLD_CONTEXT,
+		 * or computed from the input RDF. It is something that will replace the @content content
+		 * This is useful 1) for the cases you want to have a URI as value of @context,
+		 * without having JSON-LD java to download it and 2) as a trick to
+		 * change the URIs in your result. 
+		 * 
+		 * Only for compact and flatten formats.
+		 * 
+		 * Note that it is supposed to be a JSON String: to set the value of @context to a URI,
+		 * the String to use must be quoted.*/
+		public static final Symbol JSONLD_CONTEXT_SUBSTITUTION = Symbol.create("JSONLD_CONTEXT_SUBSTITUTION");		
 		/** value: the frame object expected by JsonLdProcessor.frame */
 		public static final Symbol JSONLD_FRAME = Symbol.create("JSONLD_FRAME");
 		/** value: the option object expected by JsonLdProcessor (instance of JsonLdOptions) */
@@ -181,9 +193,22 @@ public class JsonLDWriter extends WriterDatasetRIOTBase
       	      } else {
       	      	throw new IllegalArgumentException("Unexpected output form " + outputForm);
       	      }
+      	  		
+      	  		// replace @context in output?
+      	  		if (jenaContext != null) {
+	      	  		Object ctxReplacement = jenaContext.get(JSONLD_CONTEXT_SUBSTITUTION);
+	      	  		if (ctxReplacement != null) {
+	      	  			if (obj instanceof Map) {
+	      	  				Map map = (Map) obj;
+	      	  				if (map.containsKey("@context")) {
+	      	  					map.put("@context", JsonUtils.fromString((String) ctxReplacement));
+	      	  				}
+	      	  			}
+	      	  		}
+      	  		}
       	    }
 
-            if ( isPretty() )
+      	    if ( isPretty() )
                 JsonUtils.writePrettyPrint(writer, obj) ;
             else
                 JsonUtils.write(writer, obj) ;

http://git-wip-us.apache.org/repos/asf/jena/blob/3c2a469a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
index 551c33e..440d39a 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
@@ -329,6 +329,28 @@ java.lang.NoSuchMethodError: org.apache.http.impl.client.cache.CacheConfig.custo
 }
 
 /**
+ * Test using a context to compute the output, and replacing the @context with a given value
+ */
+@Test public void testSubstitutingContext() {
+	Model m = ModelFactory.createDefaultModel();
+	String ns = "http://schema.org/";
+	Resource person = m.createResource(ns + "Person");
+	Resource s = m.createResource();
+	m.add(s, m.createProperty(ns + "name"), "Jane Doe");
+	m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
+	m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
+	m.add(s, RDF.type, person);
+	
+	Context jenaCtx = new Context();
+	jenaCtx.set(JsonLDWriter.JSONLD_CONTEXT_SUBSTITUTION, "\"" + ns + "\"");
+
+	String jsonld = toString(m, RDFFormat.JSONLD_COMPACT_FLAT, jenaCtx);
+	String c = "\"@context\":\"http://schema.org/\"";
+	assertTrue(jsonld.indexOf(c) > -1);
+}
+
+
+/**
  * Checking frames
  */
 @Test public final void testFrames() throws JsonParseException, IOException {