Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RdfConsumer interface #386

Merged
merged 7 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ JsonLd.compact("https://example/expanded.jsonld", "https://example/context.jsonl
// Flattening
JsonLd.flatten("https://example/document.jsonld").get();

// JSON-LD to RDF
// JSON-LD to RDF
JsonLd.toRdf("https://example/document.jsonld").get();
// or, since 1.6.0
JsonLd.toRdf("https://example/document.jsonld").process(RdfConsumer);

// RDF to JSON-LD
JsonLd.fromRdf("https://example/document.nq").options(options).get();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.apicatalog</groupId>
<artifactId>titanium</artifactId>
<version>1.5.0</version>
<version>1.6.0-SNAPSHOT</version>
<relativePath>pom_parent.xml</relativePath>
</parent>
<artifactId>titanium-json-ld</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom_jre8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.apicatalog</groupId>
<artifactId>titanium</artifactId>
<version>1.5.0</version>
<version>1.6.0-SNAPSHOT</version>
<relativePath>pom_parent.xml</relativePath>
</parent>
<artifactId>titanium-json-ld-jre8</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom_parent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.apicatalog</groupId>
<artifactId>titanium</artifactId>
<version>1.5.0</version>
<version>1.6.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Titanium JSON-LD 1.1</name>
Expand Down
40 changes: 29 additions & 11 deletions src/main/java/com/apicatalog/jsonld/api/ToRdfApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import com.apicatalog.jsonld.loader.DocumentLoader;
import com.apicatalog.jsonld.processor.ToRdfProcessor;
import com.apicatalog.jsonld.uri.UriUtils;
import com.apicatalog.rdf.RdfConsumer;
import com.apicatalog.rdf.RdfDataset;
import com.apicatalog.rdf.RdfDatasetConsumer;

import jakarta.json.JsonStructure;

public final class ToRdfApi implements CommonApi<ToRdfApi>, LoaderApi<ToRdfApi>, ContextApi<ToRdfApi>{
public final class ToRdfApi implements CommonApi<ToRdfApi>, LoaderApi<ToRdfApi>, ContextApi<ToRdfApi> {

// required
private final Document document;
Expand Down Expand Up @@ -98,7 +100,9 @@ public ToRdfApi context(Document context) {
}

/**
* If set to true, the JSON-LD processor may emit blank nodes for triple predicates, otherwise they will be omitted.
* If set to true, the JSON-LD processor may emit blank nodes for triple
* predicates, otherwise they will be omitted.
*
* @param enable
* @return builder instance
*/
Expand All @@ -117,7 +121,8 @@ public ToRdfApi produceGeneralizedRdf() {
}

/**
* Determines how value objects containing a base direction are transformed to and from RDF.
* Determines how value objects containing a base direction are transformed to
* and from RDF.
*
* @param direction
* @return builder instance
Expand Down Expand Up @@ -154,23 +159,36 @@ public ToRdfApi ordered(boolean enable) {
/**
* Transform provided <code>JSON-LD</code> document into {@link RdfDataset}.
*
* @return {@link RdfDataset} representing provided <code>JSON-LD</code> document
* @return {@link RdfDataset} representing provided <code>JSON-LD</code>
* document
* @throws JsonLdError
*/
public RdfDataset get() throws JsonLdError {
final RdfDatasetConsumer consumer = new RdfDatasetConsumer();
process(consumer);
return consumer.dataset();
}

/**
* Emit transformed <code>JSON-LD</code> as RDF statements.
*
* @param consumer that accepts emitted RDF statements
* @throws JsonLdError
*/
public void process(RdfConsumer consumer) throws JsonLdError {
if (documentUri != null) {
return ToRdfProcessor.toRdf(documentUri, options);
}
ToRdfProcessor.toRdf(consumer, documentUri, options);

if (document != null) {
return ToRdfProcessor.toRdf(document, options);
}
} else if (document != null) {
ToRdfProcessor.toRdf(consumer, document, options);

throw new IllegalArgumentException();
} else {
throw new IllegalArgumentException();
}
}

/**
* Experimental: Accept numeric @id. Disabled by default.
* Experimental: Accept numeric <code>@id</code>. Disabled by default.
*
* @return builder instance
*/
Expand Down
150 changes: 66 additions & 84 deletions src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.apicatalog.jsonld.deseralization;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -31,10 +29,9 @@
import com.apicatalog.jsonld.uri.UriUtils;
import com.apicatalog.jsonld.uri.UriValidationPolicy;
import com.apicatalog.rdf.Rdf;
import com.apicatalog.rdf.RdfConsumer;
import com.apicatalog.rdf.RdfDataset;
import com.apicatalog.rdf.RdfResource;
import com.apicatalog.rdf.RdfTriple;
import com.apicatalog.rdf.RdfValue;
import com.apicatalog.rdf.RdfDatasetConsumer;
import com.apicatalog.rdf.lang.RdfConstants;

import jakarta.json.JsonString;
Expand All @@ -46,13 +43,15 @@ public final class JsonLdToRdf {

// required
private final NodeMap nodeMap;
private final RdfDataset dataset;

// optional
private boolean produceGeneralizedRdf;
private RdfDirection rdfDirection;
private UriValidationPolicy uriValidation;

// deprecated
private RdfDataset dataset;

private JsonLdToRdf(NodeMap nodeMap, RdfDataset dataset) {
this.nodeMap = nodeMap;
this.dataset = dataset;
Expand All @@ -62,10 +61,15 @@ private JsonLdToRdf(NodeMap nodeMap, RdfDataset dataset) {
this.uriValidation = JsonLdOptions.DEFAULT_URI_VALIDATION;
}

@Deprecated
public static final JsonLdToRdf with(NodeMap nodeMap, RdfDataset dataset) {
return new JsonLdToRdf(nodeMap, dataset);
}

public static final JsonLdToRdf with(NodeMap nodeMap) {
return new JsonLdToRdf(nodeMap, null);
}

public JsonLdToRdf produceGeneralizedRdf(boolean enable) {
this.produceGeneralizedRdf = enable;
return this;
Expand All @@ -76,135 +80,113 @@ public JsonLdToRdf rdfDirection(RdfDirection rdfDirection) {
return this;
}

public RdfDataset build() throws JsonLdError {
public void process(RdfConsumer consumer) throws JsonLdError {

// 1.
for (final String graphName : Utils.index(nodeMap.graphs(), true)) {

// 1.2.
final RdfResource rdfGraphName;

if (Keywords.DEFAULT.equals(graphName)) {
rdfGraphName = null;

} else {

// 1.1.
if (BlankNode.isWellFormed(graphName)) {
consumer.defaultGraph();

rdfGraphName = Rdf.createBlankNode(graphName);
} else if (BlankNode.isWellFormed(graphName)) {
consumer.namedGraph(graphName, true);

} else if (UriUtils.isAbsoluteUri(graphName, uriValidation)) {
} else if (UriUtils.isAbsoluteUri(graphName, uriValidation)) {
consumer.namedGraph(graphName, false);

rdfGraphName = Rdf.createIRI(graphName);

} else {
continue;
}
} else {
continue;
}

// 1.3.
for (final String subject : Utils.index(nodeMap.subjects(graphName), true)) {

final RdfResource rdfSubject;
boolean blankSubject = false;

// 1.3.1.
if (BlankNode.isWellFormed(subject)) {
rdfSubject = Rdf.createBlankNode(subject);

} else if (UriUtils.isAbsoluteUri(subject, uriValidation)) {
rdfSubject = Rdf.createIRI(subject);
blankSubject = true;

} else {
} else if (UriUtils.isNotAbsoluteUri(subject, uriValidation)) {
LOGGER.log(Level.WARNING, "Non well-formed subject [{0}] has been skipped.", subject);
continue;
}

// 1.3.2.
for (final String property : Utils.index(nodeMap.properties(graphName, subject), true)) {

// 1.3.2.1.
if (Keywords.TYPE.equals(property)) {

for (JsonValue type : nodeMap.get(graphName, subject, property).asJsonArray()) {
for (final JsonValue type : nodeMap.get(graphName, subject, property).asJsonArray()) {

if (JsonUtils.isNotString(type)) {
continue;
}

final String typeString = ((JsonString)type).getString();
final String typeString = ((JsonString) type).getString();

final RdfValue rdfObject;
boolean blankType = false;

if (BlankNode.isWellFormed(typeString)) {
rdfObject = Rdf.createBlankNode(typeString);
blankType = true;

} else if (UriUtils.isAbsoluteUri(typeString, uriValidation)) {
rdfObject = Rdf.createIRI(typeString);

} else {
} else if (UriUtils.isNotAbsoluteUri(typeString, uriValidation)) {
continue;
}

dataset.add(Rdf.createNQuad(
rdfSubject,
Rdf.createIRI(RdfConstants.TYPE),
rdfObject,
rdfGraphName
));
consumer.accept(
subject,
blankSubject,
RdfConstants.TYPE,
false,
typeString,
blankType);
}

// 1.3.2.2.
} else if (!Keywords.contains(property)) {

final RdfResource rdfProperty;

if (BlankNode.isWellFormed(property)) {
rdfProperty = !produceGeneralizedRdf ? Rdf.createBlankNode(property) : null;
boolean blankProperty = false;

} else if (UriUtils.isAbsoluteUri(property, uriValidation)) {
rdfProperty = Rdf.createIRI(property);
if (BlankNode.isWellFormed(property) && !produceGeneralizedRdf) {
blankProperty = true;

} else {
rdfProperty = null;
} else if (UriUtils.isNotAbsoluteUri(property, uriValidation)) {
continue;
}

if (rdfProperty != null) {

// 1.3.2.5.
for (JsonValue item : nodeMap.get(graphName, subject, property).asJsonArray()) {

// 1.3.2.5.1.
final List<RdfTriple> listTriples = new ArrayList<>();

// 1.3.2.5.2.
ObjectToRdf
.with(item.asJsonObject(), listTriples, nodeMap)
.rdfDirection(rdfDirection)
.uriValidation(uriValidation)
.build()
.ifPresent(rdfObject ->
dataset.add(Rdf.createNQuad(
rdfSubject,
rdfProperty,
rdfObject,
rdfGraphName
)));
// 1.3.2.5.3.
listTriples.stream()
.map(triple -> Rdf.createNQuad(triple, rdfGraphName))
.forEach(dataset::add);
}
for (final JsonValue item : nodeMap.get(graphName, subject, property).asJsonArray()) {

ObjectToRdf
.with(item.asJsonObject(), consumer, nodeMap)
.rdfDirection(rdfDirection)
.uriValidation(uriValidation)
.produce(
subject,
blankSubject,
property,
blankProperty);
}
}
}
}
}
}

/**
* @deprecated since 1.6.0, use {@link #process(RdfConsumer)}.
* @return
* @throws JsonLdError
*/
@Deprecated
public RdfDataset build() throws JsonLdError {

if (dataset == null) {
dataset = Rdf.createDataset();
}

process(new RdfDatasetConsumer(dataset));

return dataset;
}

/**
* @deprecated since 1.5.0, use <code>JsonLdToRdf#uriValidation(com.apicatalog.jsonld.uri.UriValidationPolicy)</code>
* @deprecated since 1.5.0, use {@link #uriValidation(UriValidationPolicy)}.
*/
@Deprecated
public JsonLdToRdf uriValidation(boolean enabled) {
Expand Down
Loading
Loading