
be.ugent.rml.term.Literal Maven / Gradle / Ivy
package be.ugent.rml.term;
public class Literal extends AbstractTerm {
private String language;
private Term datatype;
public Literal(String value) {
super(value);
}
public Literal(String value, String language) {
this(value);
this.language = language;
}
public Literal(String value, Term datatype) {
this(value);
this.datatype = datatype;
}
public String getLanguage() {
return language;
}
public Term getDatatype() {
return datatype;
}
@Override
public String toString() {
String temp = "\"" + escapeValue(this.getValue()) + "\"";
if (this.language != null && !this.language.equals("")) {
temp += "@" + this.language;
} else if (this.datatype != null) {
temp += "^^" + this.datatype;
}
return temp;
}
@Override
public boolean equals(Object o) {
if (o instanceof Literal) {
return o.toString().equals(toString());
} else {
return false;
}
}
/**
* Escapes a Unicode string to an N-Triples compatible character sequence. Any special characters are
* escaped using backslashes (" becomes \", etc.), and non-ascii/non-printable
* characters are escaped using Unicode escapes (\uxxxx and \Uxxxxxxxx) if the
* option is selected.
*/
private String escapeValue(String label) {
String result = "";
for (int i = 0; i < label.length(); i++) {
char c = label.charAt(i);
if (c == '\\') {
result += "\\\\";
} else if (c == '"') {
result += "\\\"";
} else if (c == '\n') {
result += "\\n";
} else if (c == '\r') {
result += "\\r";
} else if (c == '\t') {
result += "\\t";
} else {
result += c;
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy