Search in sources :

Example 6 with ExprEvalException

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);
}
Also used : Node(org.apache.jena.graph.Node) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 7 with ExprEvalException

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;
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) Node(org.apache.jena.graph.Node) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 8 with ExprEvalException

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);
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 9 with ExprEvalException

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);
}
Also used : Node(org.apache.jena.graph.Node) BigInteger(java.math.BigInteger) Duration(javax.xml.datatype.Duration) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException) BigDecimal(java.math.BigDecimal)

Example 10 with ExprEvalException

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);
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Aggregations

ExprEvalException (org.apache.jena.sparql.expr.ExprEvalException)32 Node (org.apache.jena.graph.Node)14 NodeValue (org.apache.jena.sparql.expr.NodeValue)13 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)3 BigInteger (java.math.BigInteger)2 GNode (org.apache.jena.sparql.util.graph.GNode)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Duration (javax.xml.datatype.Duration)1 ArgDecl (jena.cmd.ArgDecl)1 CmdException (jena.cmd.CmdException)1 CmdLineArgs (jena.cmd.CmdLineArgs)1 TerminationException (jena.cmd.TerminationException)1 Graph (org.apache.jena.graph.Graph)1 IRI (org.apache.jena.iri.IRI)1 Violation (org.apache.jena.iri.Violation)1 QueryExecException (org.apache.jena.query.QueryExecException)1 QueryParseException (org.apache.jena.query.QueryParseException)1 SortCondition (org.apache.jena.query.SortCondition)1 PrefixMapping (org.apache.jena.shared.PrefixMapping)1