use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class ModelCom method createTypedLiteral.
/**
* Build a typed literal from its value form.
*
* @param value the value of the literal
* @param typeURI the URI of the type of the literal, null for old style "plain" literals
*/
@Override
public Literal createTypedLiteral(Object value, String typeURI) {
RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(typeURI);
LiteralLabel ll = LiteralLabelFactory.createByValue(value, "", dt);
return new LiteralImpl(NodeFactory.createLiteral(ll), this);
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class NodeUtils method stringLiteral.
/** Get lexical for of anything that looks like a string literal.
* Returns the string value of plain literal (simple literal
* or lang string) or XSD string.
*/
public static String stringLiteral(Node literal) {
if (!literal.isLiteral())
return null;
RDFDatatype dType = literal.getLiteralDatatype();
String langTag = literal.getLiteralLanguage();
// Language?
if (langTag != null && !langTag.equals(""))
return literal.getLiteralLexicalForm();
if (dType == null || dType.equals(XSDDatatype.XSDstring))
return literal.getLiteralLexicalForm();
return null;
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class NodeValue method _setByValue.
// Returns null for unrecognized literal.
private static NodeValue _setByValue(Node node) {
// nodeToNodeValue should have dealt with it.
if (NodeUtils.hasLang(node))
return new NodeValueLang(node);
LiteralLabel lit = node.getLiteral();
String lex = lit.getLexicalForm();
RDFDatatype datatype = lit.getDatatype();
// Quick check.
// Only XSD supported.
// And (for testing) roman numerals.
String datatypeURI = datatype.getURI();
if (!datatypeURI.startsWith(xsdNamespace) && !SystemARQ.EnableRomanNumerals) {
// Not XSD.
return null;
}
try {
// DatatypeFormatException - should not happen
if (XSDstring.isValidLiteral(lit))
// String - plain or xsd:string, or derived datatype.
return new NodeValueString(lit.getLexicalForm(), node);
if (!datatype.equals(XSDdecimal)) {
// XSD integer and derived types
if (XSDinteger.isValidLiteral(lit)) {
// .trim() implements the facet of whitespace collapse.
// BigInteger does not accept such whitespace.
String s = node.getLiteralLexicalForm().trim();
if (s.startsWith("+"))
// BigInteger does not accept leading "+"
s = s.substring(1);
// Includes subtypes (int, byte, postiveInteger etc).
// NB Known to be valid for type by now
BigInteger integer = new BigInteger(s);
return new NodeValueInteger(integer, node);
}
}
if (datatype.equals(XSDdecimal) && XSDdecimal.isValidLiteral(lit)) {
BigDecimal decimal = new BigDecimal(lit.getLexicalForm());
return new NodeValueDecimal(decimal, node);
}
if (datatype.equals(XSDfloat) && XSDfloat.isValidLiteral(lit)) {
// NB If needed, call to floatValue, then assign to double.
// Gets 1.3f != 1.3d right
float f = ((Number) lit.getValue()).floatValue();
return new NodeValueFloat(f, node);
}
if (datatype.equals(XSDdouble) && XSDdouble.isValidLiteral(lit)) {
double d = ((Number) lit.getValue()).doubleValue();
return new NodeValueDouble(d, node);
}
if ((datatype.equals(XSDdateTime) || datatype.equals(XSDdateTimeStamp)) && XSDdateTime.isValid(lex)) {
XSDDateTime dateTime = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDdate) && XSDdate.isValidLiteral(lit)) {
// Jena datatype support works on masked dataTimes.
XSDDateTime dateTime = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDtime) && XSDtime.isValidLiteral(lit)) {
// Jena datatype support works on masked dataTimes.
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgYear) && XSDgYear.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgYearMonth) && XSDgYearMonth.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgMonth) && XSDgMonth.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgMonthDay) && XSDgMonthDay.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgDay) && XSDgDay.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDduration) && XSDduration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDyearMonthDuration) && XSDyearMonthDuration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDdayTimeDuration) && XSDdayTimeDuration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDboolean) && XSDboolean.isValidLiteral(lit)) {
boolean b = (Boolean) lit.getValue();
return new NodeValueBoolean(b, node);
}
// Not wired in
if (SystemARQ.EnableRomanNumerals) {
if (lit.getDatatypeURI().equals(RomanNumeralDatatype.get().getURI())) {
Object obj = RomanNumeralDatatype.get().parse(lit.getLexicalForm());
if (obj instanceof Integer)
return new NodeValueInteger(((Integer) obj).longValue());
if (obj instanceof RomanNumeral)
return new NodeValueInteger(((RomanNumeral) obj).intValue());
throw new ARQInternalErrorException("DatatypeFormatException: Roman numeral is unknown class");
}
}
} catch (DatatypeFormatException ex) {
// Should have been caught earlier by special test in nodeToNodeValue
throw new ARQInternalErrorException("DatatypeFormatException: " + lit, ex);
}
return null;
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class XSDFuncOp method substring.
public static NodeValue substring(NodeValue nvString, NodeValue nvStart, NodeValue nvLength) {
Node n = checkAndGetStringLiteral("substring", nvString);
RDFDatatype dt = n.getLiteralDatatype();
String lang = n.getLiteralLanguage();
// XSD F&O:
try {
// NaN, float and double.
String string = n.getLiteralLexicalForm();
int start = intValueStr(nvStart, string.length() + 1);
int length;
if (nvLength != null)
length = intValueStr(nvLength, 0);
else {
length = string.length();
if (start < 0)
// Address to end of string.
length = length - start;
}
int finish = start + length;
// java needs indexes in-bounds.
if (start <= 0)
start = 1;
start--;
finish--;
if (finish > string.length())
// Java index must be within bounds.
finish = string.length();
if (finish < start)
finish = start;
if (finish < 0)
finish = 0;
if (string.length() == 0)
return calcReturn("", n);
String lex2 = string.substring(start, string.offsetByCodePoints(start, finish - start));
return calcReturn(lex2, n);
} catch (IndexOutOfBoundsException ex) {
throw new ExprEvalException("IndexOutOfBounds", ex);
}
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class TestNode method assertLangString.
private static void assertLangString(Node n) {
RDFDatatype dt = n.getLiteralDatatype();
// "" is not legal.
assertDiffer("", n.getLiteralLanguage());
if (JenaRuntime.isRDF11)
assertEquals(RDF.dtLangString, dt);
else
assertEquals(null, dt);
}
Aggregations