Skip to content

Commit d1e25c4

Browse files
committed
GH-5327 RDF 1.2 implementation for JSON and dirLangString literals
1 parent e611e16 commit d1e25c4

File tree

44 files changed

+1090
-465
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1090
-465
lines changed

core/model-api/src/main/java/org/eclipse/rdf4j/model/Literal.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@
4141
* @see <a href="https://www.w3.org/TR/xmlschema11-2">XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes</a>
4242
*/
4343
public interface Literal extends Value {
44+
String LTR_SUFFIX = "--ltr";
45+
String RTL_SUFFIX = "--rtl";
46+
String BASE_DIR_SEPARATOR = "--";
47+
48+
enum BaseDirection {
49+
NONE(""),
50+
LTR(LTR_SUFFIX),
51+
RTL(RTL_SUFFIX);
52+
53+
private final String suffix;
54+
55+
BaseDirection(final String suffix) {
56+
this.suffix = suffix;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return suffix;
62+
}
63+
64+
public static BaseDirection fromString(final String dir) {
65+
if (dir == null || dir.isEmpty())
66+
return NONE;
67+
if (dir.equals(LTR_SUFFIX))
68+
return LTR;
69+
if (dir.equals(RTL_SUFFIX))
70+
return RTL;
71+
throw new IllegalArgumentException("Unknown BaseDirection: " + dir);
72+
}
73+
}
4474

4575
@Override
4676
default boolean isLiteral() {
@@ -61,6 +91,10 @@ default boolean isLiteral() {
6191
*/
6292
Optional<String> getLanguage();
6393

94+
default BaseDirection getBaseDirection() {
95+
return BaseDirection.NONE;
96+
}
97+
6498
/**
6599
* Gets the datatype for this literal.
66100
* <p>

core/model-api/src/main/java/org/eclipse/rdf4j/model/ValueFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ public interface ValueFactory {
8686
*/
8787
Literal createLiteral(String label, String language);
8888

89+
/**
90+
* Creates a new literal with the supplied label and language attribute. The return value of
91+
* {@link Literal#getDatatype()} for the returned object must be
92+
* <a href="http://www.w3.org/1999/02/22-rdf-syntax-ns#langString">{@code rdf:langString}</a>.
93+
*
94+
* @param label The literal's label, must not be <var>null</var>.
95+
* @param language The literal's language attribute, must not be <var>null</var>.
96+
* @param baseDirection The literal's base direction, either "", "--ltr", or "--rtl".
97+
* @return A literal for the specified value and language attribute.
98+
*/
99+
Literal createLiteral(String label, String language, Literal.BaseDirection baseDirection);
100+
89101
/**
90102
* Creates a new literal with the supplied label and datatype.
91103
*

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/AbstractLiteral.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ public abstract class AbstractLiteral implements Literal {
7777
private static final long serialVersionUID = -1286527360744086451L;
7878

7979
static boolean reserved(IRI datatype) {
80-
return CoreDatatype.RDF.LANGSTRING.getIri().equals(datatype);
80+
return CoreDatatype.RDF.LANGSTRING.getIri().equals(datatype)
81+
|| CoreDatatype.RDF.DIRLANGSTRING.getIri().equals(datatype);
8182
}
8283

8384
static boolean reserved(CoreDatatype datatype) {
84-
return CoreDatatype.RDF.LANGSTRING == datatype;
85+
return CoreDatatype.RDF.LANGSTRING == datatype || CoreDatatype.RDF.DIRLANGSTRING == datatype;
8586
}
8687

8788
/**
@@ -186,7 +187,7 @@ public String toString() {
186187

187188
return getLanguage()
188189

189-
.map(language -> label + '@' + language)
190+
.map(language -> label + '@' + language + getBaseDirection())
190191

191192
.orElseGet(() -> CoreDatatype.XSD.STRING == getCoreDatatype() ? label
192193
: label + "^^<" + getDatatype().stringValue() + ">");
@@ -268,10 +269,16 @@ static class TaggedLiteral extends AbstractLiteral {
268269

269270
private final String label;
270271
private final String language;
272+
private final BaseDirection baseDirection;
271273

272274
TaggedLiteral(String label, String language) {
275+
this(label, language, BaseDirection.NONE);
276+
}
277+
278+
TaggedLiteral(String label, String language, BaseDirection baseDirection) {
273279
this.label = label;
274280
this.language = language;
281+
this.baseDirection = baseDirection;
275282
}
276283

277284
@Override
@@ -284,14 +291,20 @@ public Optional<String> getLanguage() {
284291
return Optional.of(language);
285292
}
286293

294+
@Override
295+
public BaseDirection getBaseDirection() {
296+
return baseDirection;
297+
}
298+
287299
@Override
288300
public IRI getDatatype() {
289-
return CoreDatatype.RDF.LANGSTRING.getIri();
301+
return baseDirection == BaseDirection.NONE ? CoreDatatype.RDF.LANGSTRING.getIri()
302+
: CoreDatatype.RDF.DIRLANGSTRING.getIri();
290303
}
291304

292305
@Override
293306
public CoreDatatype.RDF getCoreDatatype() {
294-
return CoreDatatype.RDF.LANGSTRING;
307+
return baseDirection == BaseDirection.NONE ? CoreDatatype.RDF.LANGSTRING : CoreDatatype.RDF.DIRLANGSTRING;
295308
}
296309
}
297310

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/AbstractValueFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,21 @@ public Literal createLiteral(String label, IRI datatype, CoreDatatype coreDataty
146146

147147
@Override
148148
public Literal createLiteral(String label, String language) {
149+
return createLiteral(label, language, Literal.BaseDirection.NONE);
150+
}
151+
152+
@Override
153+
public Literal createLiteral(String label, String language, Literal.BaseDirection baseDirection) {
149154

150155
Objects.requireNonNull(label, "null label");
151156
Objects.requireNonNull(language, "null language");
157+
Objects.requireNonNull(baseDirection, "null baseDirection");
152158

153159
if (language.isEmpty()) {
154160
throw new IllegalArgumentException("empty language tag");
155161
}
156162

157-
return new TaggedLiteral(label, language);
163+
return new TaggedLiteral(label, language, baseDirection);
158164
}
159165

160166
@Override

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/CoreDatatype.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ public String toString() {
283283
enum RDF implements CoreDatatype {
284284

285285
HTML(iri("HTML")),
286+
JSON(iri("JSON")),
286287
XMLLITERAL(iri("XMLLiteral")),
288+
DIRLANGSTRING(iri("dirLangString")),
287289
LANGSTRING(iri("langString"));
288290

289291
public static final String NAMESPACE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

0 commit comments

Comments
 (0)