Search in sources :

Example 71 with Duration

use of javax.xml.datatype.Duration in project jena by apache.

the class NodeValueOps method subtractionNV.

// NodeFunctions
public static NodeValue subtractionNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numSubtract(nv1, nv2);
    if (vs1.equals(VSPACE_DURATION) && vs2.equals(VSPACE_DURATION)) {
        // A lot of testing to keep it as derived types.
        boolean isDTDur = dtXSDdayTimeDuration.equals(nv1.getDatatypeURI()) && dtXSDdayTimeDuration.equals(nv2.getDatatypeURI());
        boolean isYMDur = dtXSDyearMonthDuration.equals(nv1.getDatatypeURI()) && dtXSDyearMonthDuration.equals(nv2.getDatatypeURI());
        Duration d3 = nv1.getDuration().subtract(nv2.getDuration());
        String lex = d3.toString();
        Node n;
        if (isDTDur)
            n = NodeFactoryExtra.createLiteralNode(lex, null, dtXSDdayTimeDuration);
        else if (isYMDur)
            n = NodeFactoryExtra.createLiteralNode(lex, null, dtXSDyearMonthDuration);
        else
            n = org.apache.jena.graph.NodeFactory.createLiteral(lex, XSDDatatype.XSDduration);
        return NodeValue.makeNodeDuration(d3, n);
    }
    if (isDT(vs1) && isDT(vs2)) {
        XMLGregorianCalendar cal1 = nv1.getDateTime();
        XMLGregorianCalendar cal2 = nv2.getDateTime();
        boolean isDef1 = (cal1.getTimezone() == FIELD_UNDEFINED);
        boolean isDef2 = (cal2.getTimezone() == FIELD_UNDEFINED);
        if ((isDef1 && !isDef2) || (!isDef1 && isDef2))
            throw new ExprEvalTypeException("Operator '-': can't substract timezone/non-timezone values");
        // Inspect duration and force to better type? xsd:dayTimeDuration
        return NodeValue.makeDuration(xsd_substract(cal1, cal2));
    }
    // Loose style. Subtract any duration to any date or time value.
    if (vs1.equals(VSPACE_DATETIME) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        // add-negation
        XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDateTime(result);
        return r;
    }
    if (vs1.equals(VSPACE_DATE) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        // add-negation
        XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDate(result);
        return r;
    }
    if (vs1.equals(VSPACE_TIME) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        // add-negation
        XMLGregorianCalendar result = xsd_subtract(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeNode(result.toXMLFormat(), XSDDatatype.XSDtime);
        return r;
    }
    throw new ExprEvalTypeException("Operator '-' : Undefined subtraction: " + nv1 + " and " + nv2);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) NodeValue(org.apache.jena.sparql.expr.NodeValue) ExprEvalTypeException(org.apache.jena.sparql.expr.ExprEvalTypeException) Node(org.apache.jena.graph.Node) Duration(javax.xml.datatype.Duration) ValueSpaceClassification(org.apache.jena.sparql.expr.ValueSpaceClassification)

Example 72 with Duration

use of javax.xml.datatype.Duration in project jena by apache.

the class NodeValueOps method multiplicationNV.

public static NodeValue multiplicationNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numMultiply(nv1, nv2);
    if (vs1.equals(VSPACE_DURATION) && vs2.equals(VSPACE_NUM)) {
        // ONLY defined for dayTime.
        Duration dur = nv1.getDuration();
        boolean valid = XSDFuncOp.isDayTime(dur);
        if (!valid)
            throw new ExprEvalTypeException("Operator '*': only dayTime duration.  Got: " + nv1);
        BigDecimal dec = nv2.getDecimal();
        Duration r = dur.multiply(dec);
        Node n = NodeFactoryExtra.createLiteralNode(r.toString(), null, dtXSDdayTimeDuration);
        return NodeValue.makeNodeDuration(r, n);
    }
    throw new ExprEvalTypeException("Operator '*' : Undefined multiply: " + nv1 + " and " + nv2);
}
Also used : ExprEvalTypeException(org.apache.jena.sparql.expr.ExprEvalTypeException) Node(org.apache.jena.graph.Node) Duration(javax.xml.datatype.Duration) ValueSpaceClassification(org.apache.jena.sparql.expr.ValueSpaceClassification) BigDecimal(java.math.BigDecimal)

Example 73 with Duration

use of javax.xml.datatype.Duration in project jena by apache.

the class NodeFunctions method duration.

/**
 * A duration, tided
 */
public static Duration duration(int seconds) {
    if (seconds == 0)
        return XSDFuncOp.zeroDuration;
    Duration dur = NodeValue.xmlDatatypeFactory.newDuration(1000 * seconds);
    // Neaten the duration. Not all the fields ar zero.
    dur = NodeValue.xmlDatatypeFactory.newDuration(dur.getSign() >= 0, field(dur, DatatypeConstants.YEARS), field(dur, DatatypeConstants.MONTHS), field(dur, DatatypeConstants.DAYS), field(dur, DatatypeConstants.HOURS), field(dur, DatatypeConstants.MINUTES), field2(dur, DatatypeConstants.SECONDS));
    return dur;
}
Also used : Duration(javax.xml.datatype.Duration)

Example 74 with Duration

use of javax.xml.datatype.Duration in project jena by apache.

the class XSDFuncOp method localTimezone.

public static NodeValue localTimezone() {
    Duration dur = localTimezoneDuration();
    NodeValue nv = NodeValue.makeDuration(dur);
    return nv;
}
Also used : Duration(javax.xml.datatype.Duration)

Example 75 with Duration

use of javax.xml.datatype.Duration in project jena by apache.

the class NodeValueRewriterTest method visitNodeValueDurationNodeTest.

@Test
public void visitNodeValueDurationNodeTest() throws DatatypeConfigurationException {
    Duration dur = DatatypeFactory.newInstance().newDuration(true, 1, 2, 3, 4, 5, 6);
    Node n = NodeFactory.createLiteral(dur.toString(), XSDDatatype.XSDduration);
    NodeValue nv = new NodeValueDuration(dur, n);
    nv.visit(rewriter);
    NodeValue result = rewriter.getResult();
    assertEquals(nv, result);
    assertEquals(nv.getClass(), result.getClass());
}
Also used : NodeValue(org.apache.jena.sparql.expr.NodeValue) NodeValueDuration(org.apache.jena.sparql.expr.nodevalue.NodeValueDuration) NodeValueNode(org.apache.jena.sparql.expr.nodevalue.NodeValueNode) Node(org.apache.jena.graph.Node) NodeValueDuration(org.apache.jena.sparql.expr.nodevalue.NodeValueDuration) Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Aggregations

Duration (javax.xml.datatype.Duration)137 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)57 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)12 Date (java.util.Date)12 BigDecimal (java.math.BigDecimal)9 Calendar (java.util.Calendar)9 GregorianCalendar (java.util.GregorianCalendar)9 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)8 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)7 CleanupPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType)6 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)5 IOException (java.io.IOException)5 XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)5 FileNotFoundException (java.io.FileNotFoundException)4 Collection (java.util.Collection)4 DatatypeFactory (javax.xml.datatype.DatatypeFactory)4 NotNull (org.jetbrains.annotations.NotNull)4 PrismObject (com.evolveum.midpoint.prism.PrismObject)3