use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.
the class namespace method exec.
@Override
public NodeValue exec(NodeValue v) {
Node n = v.asNode();
if (!n.isURI())
throw new ExprEvalException("Not a URI: " + FmtUtils.stringForNode(n));
String str = n.getNameSpace();
return NodeValue.makeString(str);
}
use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.
the class bnode method exec.
@Override
public NodeValue exec(NodeValue v) {
Node n = v.asNode();
if (!n.isBlank())
throw new ExprEvalException("bnode: not a blank node");
NodeValue nv = NodeValue.makeString(n.getBlankNodeId().getLabelString());
return nv;
}
use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.
the class FN_StrSubstring method exec.
@Override
public NodeValue exec(List<NodeValue> args) {
if (args.size() != 2 && args.size() != 3)
throw new ExprEvalException("substring: Wrong number of arguments: " + args.size() + " : [wanted 2 or 3]");
NodeValue v1 = args.get(0);
NodeValue v2 = args.get(1);
NodeValue v3 = null;
if (args.size() == 3) {
v3 = args.get(2);
return XSDFuncOp.substring(v1, v2, v3);
}
return XSDFuncOp.substring(v1, v2);
}
use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.
the class CastXSD method cast.
/** Cast a NodeValue to an XSD datatype.
* This includes "by value" so 1e0 (an xsd:double) casts to 1 (an xsd:intger)
* @param nv
* @param castType
* @return NodeValue
* @throws ExprEvalException
*/
public static NodeValue cast(NodeValue nv, XSDDatatype castType) {
// http://www.w3.org/TR/xpath-functions/#casting
Node n = nv.asNode();
if (n.isBlank())
throw exception("Can't cast blank nodes: " + nv);
if (n.isURI()) {
if (castType.equals(XSDDatatype.XSDstring))
return cast$(n.getURI(), castType);
else
throw exception("Can't cast URIs to " + castType.getURI());
}
if (!n.isLiteral())
throw exception("Can't cast (not a literal, nor URI to string) " + nv + " : " + castType.getURI());
// Cast to self but may be an invalid lexical form.
if (Objects.equals(nv.getNode().getLiteralDatatype(), castType)) {
String lex = nv.getNode().getLiteralLexicalForm();
if (castType.isValid(lex))
return nv;
throw exception("Invalid lexical form for " + castType.getURI());
}
// To a temporal
if (isTemporalDatatype(castType)) {
return XSDFuncOp.dateTimeCast(nv, castType);
}
if (isDurationDatatype(castType)) {
if (nv.isDuration()) {
Duration d = nv.getDuration();
if (castType.equals(XSDDatatype.XSDyearMonthDuration)) {
// Include xsd:duration only covering year-month.
if (nv.isDayTimeDuration())
return NodeValue.makeNode("P0M", castType);
Duration d2 = NodeValue.xmlDatatypeFactory.newDuration(d.getSign() >= 0, (BigInteger) d.getField(DatatypeConstants.YEARS), (BigInteger) d.getField(DatatypeConstants.MONTHS), null, null, null, null);
return NodeValue.makeNode(d2.toString(), castType);
}
if (castType.equals(XSDDatatype.XSDdayTimeDuration)) {
if (nv.isYearMonthDuration())
return NodeValue.makeNode("PT0S", castType);
Duration d2 = NodeValue.xmlDatatypeFactory.newDuration(d.getSign() >= 0, null, null, (BigInteger) d.getField(DatatypeConstants.DAYS), (BigInteger) d.getField(DatatypeConstants.HOURS), (BigInteger) d.getField(DatatypeConstants.MINUTES), (BigDecimal) d.getField(DatatypeConstants.SECONDS));
// return NodeValue.makeDuration(d2) ;
return NodeValue.makeNode(d2.toString(), castType);
}
}
}
// From number, can consider value.
if (nv.isNumber()) {
if (castType.equals(XSDDatatype.XSDdecimal)) {
// Number to decimal.
if (isDouble(nv) || isFloat(nv)) {
// FP to decimal.
double d = nv.getDouble();
if (Double.isNaN(d))
throw exception("Can't cast NaN to xsd:decimal");
if (Double.isInfinite(d))
throw exception("Can't cast Inf or -Inf to xsd:decimal");
// BigDecimal.valueOf(d) can lead to trailing zeros
// BigDecimal.valueOf(d) goes via strings.
String lex = doubleToDecimalString(d);
return NodeValue.makeDecimal(lex);
}
// Integer, or derived type -> decimal.
return castByLex(nv, castType);
}
if (XSDFuncOp.isIntegerType(castType)) {
// Number to integer
if (isDouble(nv) || isFloat(nv)) {
// FP to integer
double d = nv.getDouble();
boolean isIntegerValue = (Math.rint(d) == d);
if (isIntegerValue) {
String lex = doubleIntegerToString(d);
if (lex != null)
return castByLex(lex, castType);
}
throw exception(nv, castType);
} else if (isDecimal(nv)) {
// Decimal to integer
BigDecimal bd = nv.getDecimal();
try {
// Exception on fraction.
BigInteger bi = bd.toBigIntegerExact();
return castByLex(bi.toString(), castType);
} catch (ArithmeticException ex) {
throw new ExprEvalException("CastXSD: Not a valid cast: '" + nv + "'");
}
} else {
// Integer derived type -> integer derived type.
return castByLex(nv, castType);
}
}
}
// Boolean -> xsd:
if (nv.isBoolean()) {
boolean b = nv.getBoolean();
// Boolean to boolean covered above.
String lex;
if (XSDDatatype.XSDfloat.equals(castType) || XSDDatatype.XSDdouble.equals(castType))
return cast$((b ? "1.0E0" : "0.0E0"), castType);
else if (XSDDatatype.XSDdecimal.equals(castType))
return cast$((b ? "1.0" : "0.0"), castType);
else if (XSDFuncOp.isIntegerType(castType))
return cast$((b ? "1" : "0"), castType);
else if (XSDDatatype.XSDstring.equals(castType))
return cast$(nv.getNode().getLiteralLexicalForm(), castType);
throw exception("Can't cast xsd:boolean to " + castType);
}
// Try by lexical
return castByLex(nv, castType);
}
use of org.apache.jena.sparql.expr.ExprEvalException in project jena by apache.
the class FunctionBase2 method exec.
@Override
public final NodeValue exec(List<NodeValue> args) {
if (args == null)
// The contract on the function interface is that this should not happen.
throw new ARQInternalErrorException(Lib.className(this) + ": Null args list");
if (args.size() != 2)
throw new ExprEvalException(Lib.className(this) + ": Wrong number of arguments: Wanted 2, got " + args.size());
NodeValue v1 = args.get(0);
NodeValue v2 = args.get(1);
return exec(v1, v2);
}
Aggregations