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 2021/10/28 16:24:13 UTC
[jena] branch main updated: JENA-2187: Handle @context array
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/main by this push:
new 74f65a6 JENA-2187: Handle @context array
new 7eff6b0 Merge pull request #1094 from afs/jena2187-jsonld11-prefixes
74f65a6 is described below
commit 74f65a68428ffecdce55285bc3e1a1cb78130101
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Wed Oct 27 21:40:54 2021 +0100
JENA-2187: Handle @context array
---
.../org/apache/jena/riot/lang/LangJSONLD11.java | 72 ++++++++++++++--------
.../org/apache/jena/system/TestJenaTitanium.java | 25 +++++++-
jena-arq/testing/RIOT/jsonld11/doc-1.jsonld11 | 1 +
jena-arq/testing/RIOT/jsonld11/doc-2.jsonld11 | 12 ++++
jena-arq/testing/RIOT/jsonld11/doc-3.jsonld11 | 16 +++++
5 files changed, 99 insertions(+), 27 deletions(-)
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/lang/LangJSONLD11.java b/jena-arq/src/main/java/org/apache/jena/riot/lang/LangJSONLD11.java
index d73aac4..7a2ec07 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/lang/LangJSONLD11.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/lang/LangJSONLD11.java
@@ -35,7 +35,8 @@ import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import org.apache.jena.atlas.logging.Log;
import org.apache.jena.atlas.web.ContentType;
-import org.apache.jena.riot.*;
+import org.apache.jena.riot.ReaderRIOT;
+import org.apache.jena.riot.RiotException;
import org.apache.jena.riot.system.JenaTitanium;
import org.apache.jena.riot.system.StreamRDF;
import org.apache.jena.riot.system.SysJSONLD11;
@@ -77,7 +78,7 @@ public class LangJSONLD11 implements ReaderRIOT {
}
/**
- * JSONLD does not define prefixes.
+ * JSON-LD does not define prefixes.
* <p>
* The use of "prefix:localname" happens for any definition of "prefix" in the
* {@literal @context} even if intended for a URI e.g a property.
@@ -89,36 +90,55 @@ public class LangJSONLD11 implements ReaderRIOT {
* </p>
* <p>
* In addition, {@literal @vocab} becomes prefix "".
+ * </p>
*/
private static void extractPrefixes(Document document, BiConsumer<String, String> action) {
try {
JsonStructure js = document.getJsonContent().get();
JsonValue jv = js.asJsonObject().get(Keywords.CONTEXT);
- JsonObject jCxt = jv.asJsonObject();
- Set<String> keys = jCxt.keySet();
- keys.stream().forEach(k->{
- // "@vocab" : "uri"
- // "shortName" : "uri"
- JsonValue jvx = jCxt.get(k);
- if ( JsonValue.ValueType.STRING != jvx.getValueType() )
- return;
- if ( Keywords.VOCAB.equals(k) ) {
- action.accept("", jvx.toString());
- return;
- }
- if ( k.startsWith("@") )
- // Keyword, not @vocab.
- return;
- // Avoid single property aliases.
- // Pragmatic filter: URI ends in "#" or "/" or ":"
- String uri = JsonString.class.cast(jvx).getString();
- if ( uri.endsWith("#") || uri.endsWith("/") || uri.endsWith(":") ) {
- action.accept(k, uri);
- return;
- }
- });
+ extractPrefixes(jv, action);
} catch(Throwable ex) {
- Log.warn(LangJSONLD11.class, "Unexpected problem while extracting prefixes: "+ex.getMessage(), ex);
+ Log.warn(LangJSONLD11.class, "Unexpected problem while extracting prefixes: "+ex.getMessage(), ex);
+ }
+ }
+
+ private static void extractPrefixes(JsonValue jsonValue, BiConsumer<String, String> action) {
+ // JSON-LD 1.1 section 9.4
+ switch(jsonValue.getValueType()) {
+ case ARRAY :
+ jsonValue.asJsonArray().forEach(jv -> extractPrefixes(jv, action));
+ break;
+ case OBJECT :
+ extractPrefixesCxtDefn(jsonValue.asJsonObject(), action);
+ break;
+ case NULL: break; // We are only interested in prefixes
+ case STRING: break; // We are only interested in prefixes
+ default :
+ break;
}
}
+
+ private static void extractPrefixesCxtDefn(JsonObject jCxt, BiConsumer<String, String> action) {
+ Set<String> keys = jCxt.keySet();
+ keys.stream().forEach(k->{
+ // "@vocab" : "uri"
+ // "shortName" : "uri"
+ // "shortName" : { "@type":"@id" , "@id": "uri" } -- presumed to be a single property aliases, not a prefix.
+ JsonValue jvx = jCxt.get(k);
+ if ( JsonValue.ValueType.STRING != jvx.getValueType() )
+ return;
+ String prefix = k;
+ if ( Keywords.VOCAB.equals(k) )
+ prefix = "";
+ else if ( k.startsWith("@") )
+ // Keyword, not @vocab.
+ return;
+ // Pragmatic filter: URI ends in "#" or "/" or ":"
+ String uri = JsonString.class.cast(jvx).getString();
+ if ( uri.endsWith("#") || uri.endsWith("/") || uri.endsWith(":") ) {
+ action.accept(prefix, uri);
+ return;
+ }
+ });
+ }
}
diff --git a/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java b/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java
index 39fa9d9..05ef59e 100644
--- a/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java
+++ b/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java
@@ -18,6 +18,7 @@
package org.apache.jena.system;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -41,10 +42,32 @@ import org.junit.Test;
public class TestJenaTitanium {
@Test
- public final void readContextVersion() {
+ public final void readContext_1() {
DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
RDFParser.source("testing/RIOT/jsonld11/doc-1.jsonld11").parse(dsg);
assertFalse(dsg.isEmpty());
+ // "@vocab" : "http://example.org/vocab" -- not a prefix - does not end in "/" "#" or ":"
+ assertEquals(0, dsg.prefixes().size());
+ }
+
+ @Test
+ public final void readContextArrayPrefixes_1() {
+ DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
+ RDFParser.source("testing/RIOT/jsonld11/doc-2.jsonld11").parse(dsg);
+ assertTrue(dsg.prefixes().containsPrefix("foaf"));
+ assertEquals(1, dsg.prefixes().size());
+ }
+
+ @Test
+ public final void readContextArrayPrefixes_2() {
+ DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
+ RDFParser.source("testing/RIOT/jsonld11/doc-3.jsonld11").parse(dsg);
+ assertTrue(dsg.prefixes().containsPrefix("foaf"));
+ assertEquals(3, dsg.prefixes().size());
+ assertTrue(dsg.prefixes().containsPrefix("foaf"));
+ assertTrue(dsg.prefixes().containsPrefix("foo"));
+ assertTrue(dsg.prefixes().containsPrefix(""));
+ assertFalse(dsg.prefixes().containsPrefix("bar"));
}
@Test public void convertDataset() throws IOException, RdfWriterException {
diff --git a/jena-arq/testing/RIOT/jsonld11/doc-1.jsonld11 b/jena-arq/testing/RIOT/jsonld11/doc-1.jsonld11
index e466248..2d7fa68 100644
--- a/jena-arq/testing/RIOT/jsonld11/doc-1.jsonld11
+++ b/jena-arq/testing/RIOT/jsonld11/doc-1.jsonld11
@@ -1,6 +1,7 @@
{
"@context": {
"@version": 1.1,
+ "@vocab" : "http://example.org/vocab",
"p" : {
"@id" : "http://example/p",
"@type" : "http://www.w3.org/2001/XMLSchema#integer"
diff --git a/jena-arq/testing/RIOT/jsonld11/doc-2.jsonld11 b/jena-arq/testing/RIOT/jsonld11/doc-2.jsonld11
new file mode 100644
index 0000000..84d35a9
--- /dev/null
+++ b/jena-arq/testing/RIOT/jsonld11/doc-2.jsonld11
@@ -0,0 +1,12 @@
+{
+ "@context": [
+ {
+ "pic": {
+ "@id": "http://example/picture",
+ "@type": "@id"
+ },
+ "foaf": "http://xmlns.com/foaf/0.1/"
+ }
+ ],
+ "foaf:depication": "http://twitter.com/"
+}
diff --git a/jena-arq/testing/RIOT/jsonld11/doc-3.jsonld11 b/jena-arq/testing/RIOT/jsonld11/doc-3.jsonld11
new file mode 100644
index 0000000..3b2d73e
--- /dev/null
+++ b/jena-arq/testing/RIOT/jsonld11/doc-3.jsonld11
@@ -0,0 +1,16 @@
+{
+ "@context": [
+ {
+ "@version": 1.1,
+ "@vocab": "http://example.org/vocab/",
+ "foo": "http://example.org/foo#"
+ }, {
+ "foaf": "http://xmlns.com/foaf/0.1/"
+ }, {
+ "term": {
+ "@context": { "id": "@id" }
+ }
+ }
+ ],
+ "foaf:depication": "http://twitter.com/picture"
+}