Skip to content
Open
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
34 changes: 34 additions & 0 deletions core/model-api/src/main/java/org/eclipse/rdf4j/model/Literal.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@
* @see <a href="https://www.w3.org/TR/xmlschema11-2">XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes</a>
*/
public interface Literal extends Value {
String LTR_SUFFIX = "--ltr";
String RTL_SUFFIX = "--rtl";
String BASE_DIR_SEPARATOR = "--";

enum BaseDirection {
NONE(""),
LTR(LTR_SUFFIX),
RTL(RTL_SUFFIX);

private final String suffix;

BaseDirection(final String suffix) {
this.suffix = suffix;
}

@Override
public String toString() {
return suffix;
}

public static BaseDirection fromString(final String dir) {
if (dir == null || dir.isEmpty())
return NONE;
if (dir.equals(LTR_SUFFIX))
return LTR;
if (dir.equals(RTL_SUFFIX))
return RTL;
throw new IllegalArgumentException("Unknown BaseDirection: " + dir);
}
}

@Override
default boolean isLiteral() {
Expand All @@ -61,6 +91,10 @@ default boolean isLiteral() {
*/
Optional<String> getLanguage();

default BaseDirection getBaseDirection() {
return BaseDirection.NONE;
}

/**
* Gets the datatype for this literal.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public interface ValueFactory {
*/
Literal createLiteral(String label, String language);

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

/**
* Creates a new literal with the supplied label and datatype.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ public abstract class AbstractLiteral implements Literal {
private static final long serialVersionUID = -1286527360744086451L;

static boolean reserved(IRI datatype) {
return CoreDatatype.RDF.LANGSTRING.getIri().equals(datatype);
return CoreDatatype.RDF.LANGSTRING.getIri().equals(datatype)
|| CoreDatatype.RDF.DIRLANGSTRING.getIri().equals(datatype);
}

static boolean reserved(CoreDatatype datatype) {
return CoreDatatype.RDF.LANGSTRING == datatype;
return CoreDatatype.RDF.LANGSTRING == datatype || CoreDatatype.RDF.DIRLANGSTRING == datatype;
}

/**
Expand Down Expand Up @@ -186,7 +187,7 @@ public String toString() {

return getLanguage()

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

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

private final String label;
private final String language;
private final BaseDirection baseDirection;

TaggedLiteral(String label, String language) {
this(label, language, BaseDirection.NONE);
}

TaggedLiteral(String label, String language, BaseDirection baseDirection) {
this.label = label;
this.language = language;
this.baseDirection = baseDirection;
}

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

@Override
public BaseDirection getBaseDirection() {
return baseDirection;
}

@Override
public IRI getDatatype() {
return CoreDatatype.RDF.LANGSTRING.getIri();
return baseDirection == BaseDirection.NONE ? CoreDatatype.RDF.LANGSTRING.getIri()
: CoreDatatype.RDF.DIRLANGSTRING.getIri();
}

@Override
public CoreDatatype.RDF getCoreDatatype() {
return CoreDatatype.RDF.LANGSTRING;
return baseDirection == BaseDirection.NONE ? CoreDatatype.RDF.LANGSTRING : CoreDatatype.RDF.DIRLANGSTRING;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,21 @@ public Literal createLiteral(String label, IRI datatype, CoreDatatype coreDataty

@Override
public Literal createLiteral(String label, String language) {
return createLiteral(label, language, Literal.BaseDirection.NONE);
}

@Override
public Literal createLiteral(String label, String language, Literal.BaseDirection baseDirection) {

Objects.requireNonNull(label, "null label");
Objects.requireNonNull(language, "null language");
Objects.requireNonNull(baseDirection, "null baseDirection");

if (language.isEmpty()) {
throw new IllegalArgumentException("empty language tag");
}

return new TaggedLiteral(label, language);
return new TaggedLiteral(label, language, baseDirection);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ public String toString() {
enum RDF implements CoreDatatype {

HTML(iri("HTML")),
JSON(iri("JSON")),
XMLLITERAL(iri("XMLLiteral")),
DIRLANGSTRING(iri("dirLangString")),
LANGSTRING(iri("langString"));

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