Skip to content

Commit

Permalink
Reflect latest feedback on RdfConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
filip26 committed Feb 23, 2025
1 parent a6c36d9 commit e190cf1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 66 deletions.
48 changes: 20 additions & 28 deletions src/main/java/com/apicatalog/jsonld/deseralization/JsonLdToRdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void provide(RdfConsumer consumer) throws JsonLdError {
continue;
}

consumer.accept(
consumer.triple(
subject,
blankSubject,
RdfConstants.TYPE,
Expand Down Expand Up @@ -247,10 +247,10 @@ private void fromObject(
String idString = ((JsonString) id).getString();

if (BlankNode.isWellFormed(idString)) {
consumer.accept(subject, blankSubject, predicate, blankPredicate, idString, true);
consumer.triple(subject, blankSubject, predicate, blankPredicate, idString, true);

} else if (UriUtils.isAbsoluteUri(idString, uriValidation)) {
consumer.accept(subject, blankSubject, predicate, blankPredicate, idString, false);
consumer.triple(subject, blankSubject, predicate, blankPredicate, idString, false);
}

return;
Expand Down Expand Up @@ -365,70 +365,62 @@ private void fromObject(
: "";
// 13.2.
if (RdfDirection.I18N_DATATYPE == rdfDirection) {
datatype = "https://www.w3.org/ns/i18n#"
.concat(language)
.concat("_")
.concat(item.getString(Keywords.DIRECTION));

consumer.accept(
consumer.triple(
subject, blankSubject,
predicate, blankPredicate,
valueString, datatype, null);
valueString, language, item.getString(Keywords.DIRECTION));

// 13.3.
} else if (RdfDirection.COMPOUND_LITERAL == rdfDirection) {

final String blankNodeId = nodeMap.createIdentifier();

// 13.3.2.
consumer.accept(
consumer.triple(
blankNodeId,
true,
RdfConstants.VALUE,
false,
valueString,
XsdConstants.STRING,
null);
XsdConstants.STRING);

// 13.3.3.
if (item.containsKey(Keywords.LANGUAGE) && JsonUtils.isString(item.get(Keywords.LANGUAGE))) {
consumer.accept(
consumer.triple(
blankNodeId,
true,
RdfConstants.LANGUAGE,
false,
item.getString(Keywords.LANGUAGE).toLowerCase(),
XsdConstants.STRING,
null);
XsdConstants.STRING);
}

// 13.3.4.
consumer.accept(
consumer.triple(
blankNodeId,
true,
RdfConstants.DIRECTION,
false,
item.getString(Keywords.DIRECTION),
XsdConstants.STRING,
null);
XsdConstants.STRING);

consumer.accept(subject, blankSubject, predicate, blankPredicate, blankNodeId, true);
consumer.triple(subject, blankSubject, predicate, blankPredicate, blankNodeId, true);
return;
}

// 14.
} else {
if (item.containsKey(Keywords.LANGUAGE) && JsonUtils.isString(item.get(Keywords.LANGUAGE))) {
consumer.accept(
consumer.triple(
subject, blankSubject,
predicate, blankPredicate,
valueString, RdfConstants.LANG_STRING, item.getString(Keywords.LANGUAGE));
valueString, item.getString(Keywords.LANGUAGE), null);

} else {
consumer.accept(
consumer.triple(
subject, blankSubject,
predicate, blankPredicate,
valueString, datatype, null);
valueString, datatype);
}
}
}
Expand All @@ -447,7 +439,7 @@ private void fromList(

// 1.
if (JsonUtils.isEmptyArray(list)) {
consumer.accept(subject, blankSubject, predicate, blankPredicate, RdfConstants.NIL, false);
consumer.triple(subject, blankSubject, predicate, blankPredicate, RdfConstants.NIL, false);
return;
}

Expand All @@ -456,7 +448,7 @@ private void fromList(

IntStream.range(0, bnodes.length).forEach(i -> bnodes[i] = nodeMap.createIdentifier());

consumer.accept(subject, blankSubject, predicate, blankPredicate, bnodes[0], true);
consumer.triple(subject, blankSubject, predicate, blankPredicate, bnodes[0], true);

// 3.
int index = 0;
Expand All @@ -475,10 +467,10 @@ private void fromList(

// 3.4.
if (index < bnodes.length) {
consumer.accept(blankNodeSubject, true, RdfConstants.REST, false, bnodes[index], true);
consumer.triple(blankNodeSubject, true, RdfConstants.REST, false, bnodes[index], true);

} else {
consumer.accept(blankNodeSubject, true, RdfConstants.REST, false, RdfConstants.NIL, false);
consumer.triple(blankNodeSubject, true, RdfConstants.REST, false, RdfConstants.NIL, false);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/apicatalog/rdf/Rdf.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,22 @@ public static RdfLiteral createLangString(String lexicalForm, String langTag) {
return RdfProvider.provider().createLangString(lexicalForm, langTag);
}

public static RdfLiteral createLangString(String lexicalForm, String language, String direction) {
return direction != null
? createTypedString(lexicalForm, "https://www.w3.org/ns/i18n#"
.concat(language)
.concat("_")
.concat(direction))
: createLangString(lexicalForm, language);
}

/**
* Create a new {@link RdfResource}.
*
* @param resource is an absolute IRI or blank node identifier
* @return RDF resource
* @throws IllegalArgumentException if the resource is not an absolute IRI or blank node identifier
* @throws IllegalArgumentException if the resource is not an absolute IRI or
* blank node identifier
*/
public static RdfResource createResource(String resource) {

Expand Down
94 changes: 62 additions & 32 deletions src/main/java/com/apicatalog/rdf/RdfConsumer.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,100 @@
package com.apicatalog.rdf;

/**
* RDF dataset consumer interface designed for
* speed, streamlined processing, and efficiency.
*
* This interface minimizes unnecessary object instantiation
* and facilitates seamless integration with third-party libraries,
* enabling efficient RDF data processing.
* RDF dataset consumer interface designed for speed, streamlined processing,
* and efficiency.
*
* This interface minimizes unnecessary object instantiation and facilitates
* seamless integration with third-party libraries, enabling efficient RDF data
* processing.
*/
public interface RdfConsumer {

/**
* Sets the default graph as the active scope.
* Invoked when triples belong to the unnamed, default graph.
* Sets the default graph as the active scope. Invoked when triples belong to
* the unnamed, default graph.
*/
void defaultGraph();

/**
* Sets a named graph as the active scope.
* Ensures that subsequent triples are associated with the specified graph.
* Sets a named graph as the active scope. Ensures that subsequent triples are
* associated with the specified graph.
*
* @param graph The name of the graph (IRI or blank node identifier).
* @param graph The name of the graph (IRI or blank node identifier
* prefixed with "_:").
* @param blankGraph {@code true} if the graph name is a blank node identifier.
*/
void namedGraph(String graph, boolean blankGraph);

/**
* Accepts a new RDF triple where the object is an IRI or a blank node.
* The triple is processed within the currently active graph scope.
* Accepts a new RDF triple where the object is an IRI or a blank node. The
* triple is processed within the currently active graph scope.
*
* @param subject The subject of the triple (IRI or blank node identifier).
* @param subject The subject of the triple (IRI or blank node identifier
* prefixed with "_:").
* @param blankSubject {@code true} if the subject is a blank node identifier.
* @param predicate The predicate of the triple (IRI or blank node identifier).
* @param blankPredicate {@code true} if the predicate is a blank node identifier.
* @param object The object of the triple (IRI or blank node identifier).
* @param predicate The predicate of the triple (IRI or blank node
* identifier prefixed with "_:").
* @param blankPredicate {@code true} if the predicate is a blank node
* identifier.
* @param object The object of the triple (IRI or blank node identifier
* prefixed with "_:").
* @param blankObject {@code true} if the object is a blank node identifier.
*/
void accept(
void triple(
String subject,
boolean blankSubject,
String predicate,
boolean blankPredicate,
String object,
boolean blankObject
);
boolean blankObject);

/**
* Accepts a new RDF triple where the object is a literal value.
* The triple is processed within the currently active graph scope.
* Optimized for efficient handling of typed and language-tagged literals.
* Accepts a new RDF triple where the object is a literal value. The triple is
* processed within the currently active graph scope. Optimized for efficient
* handling of typed and language-tagged literals.
*
* @param subject The subject of the triple (IRI or blank node identifier).
* @param subject The subject of the triple (IRI or blank node identifier
* prefixed with "_:").
* @param blankSubject {@code true} if the subject is a blank node identifier.
* @param predicate The predicate of the triple (IRI or blank node identifier).
* @param blankPredicate {@code true} if the predicate is a blank node identifier.
* @param predicate The predicate of the triple (IRI or blank node
* identifier prefixed with "_:").
* @param blankPredicate {@code true} if the predicate is a blank node
* identifier.
* @param literal The literal value of the object.
* @param datatype The datatype IRI of the literal, never {@code null}.
* @param language The language tag of the literal (optional, may be {@code null}).
* @param datatype The datatype IRI of the literal (never {@code null}).
*/
void accept(
void triple(
String subject,
boolean blankSubject,
String predicate,
boolean blankPredicate,
String literal,
String datatype,
String language
);
String datatype);

/**
* Accepts a new RDF triple where the object is a literal value. The triple is
* processed within the currently active graph scope. Optimized for efficient
* handling of typed and language-tagged literals.
*
* @param subject The subject of the triple (IRI or blank node identifier
* prefixed with "_:").
* @param blankSubject {@code true} if the subject is a blank node identifier.
* @param predicate The predicate of the triple (IRI or blank node
* identifier prefixed with "_:").
* @param blankPredicate {@code true} if the predicate is a blank node
* identifier.
* @param literal The literal value of the object.
* @param language The language code of the literal (never {@code null}).
* @param direction The direction of the literal (optional, may be
* {@code null}).
*/
void triple(
String subject,
boolean blankSubject,
String predicate,
boolean blankPredicate,
String literal,
String language,
String direction);
}
16 changes: 11 additions & 5 deletions src/main/java/com/apicatalog/rdf/RdfNQuadConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ public void defaultGraph() {
}

@Override
public void accept(String subject, boolean blankSubject, String predicate, boolean blankPredicate, String literal, String datatype, String language) {
public void triple(String subject, boolean blankSubject, String predicate, boolean blankPredicate, String literal, String language, String direction) {
accept(getResource(subject, blankSubject),
getResource(predicate, blankPredicate),
language != null
? Rdf.createLangString(literal, language)
: Rdf.createTypedString(literal, datatype),
Rdf.createLangString(literal, language, direction),
graphName);
}

@Override
public void accept(String subject, boolean blankSubject, String predicate, boolean blankPredicate, String object, boolean blankObject) {
public void triple(String subject, boolean blankSubject, String predicate, boolean blankPredicate, String literal, String datatype) {
accept(getResource(subject, blankSubject),
getResource(predicate, blankPredicate),
Rdf.createTypedString(literal, datatype),
graphName);
}

@Override
public void triple(String subject, boolean blankSubject, String predicate, boolean blankPredicate, String object, boolean blankObject) {
accept(getResource(subject, blankSubject),
getResource(predicate, blankPredicate),
getResource(object, blankObject),
Expand Down

0 comments on commit e190cf1

Please sign in to comment.