Search in sources :

Example 26 with Duration

use of javax.xml.datatype.Duration in project podam by devopsfolks.

the class XMLDatatypeUnitTest method testDurationManufacturing.

@Test
@Title("When given an external factory, Podam should be able to create instances of the Duration class")
public void testDurationManufacturing() throws Exception {
    PodamFactory podamFactory = podamFactorySteps.givenAPodamFactoryWithXmlTypesExternalFactory();
    Duration pojo = podamInvocationSteps.whenIInvokeTheFactoryForClass(Duration.class, podamFactory);
    podamValidationSteps.theObjectShouldNotBeNull(pojo);
}
Also used : PodamFactory(uk.co.jemos.podam.api.PodamFactory) Duration(javax.xml.datatype.Duration) Test(org.junit.Test) Title(net.thucydides.core.annotations.Title)

Example 27 with Duration

use of javax.xml.datatype.Duration 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);
        // Otherwise xsd:string is like any other unknown datatype.
        // Ditto literals with language tags (which are handled by nodeToNodeValue)
        // isValidLiteral is a value test - not a syntactic test.
        // This makes a difference in that "1"^^xsd:decimal" is a
        // valid literal for xsd:integer (all other cases are subtypes of xsd:integer)
        // which we want to become integer anyway).
        // Order here is promotion order integer-decimal-float-double
        // XSD allows whitespace. Java String.trim removes too much
        // so must test for validity on the untrimmed lexical form.
        String lexTrimmed = lex.trim();
        if (!datatype.equals(XSDdecimal)) {
            // XSD integer and derived types
            if (XSDinteger.isValidLiteral(lit)) {
                // BigInteger does not accept such whitespace.
                String s = lexTrimmed;
                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(lexTrimmed);
            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(XSDboolean) && XSDboolean.isValidLiteral(lit)) {
            boolean b = (Boolean) lit.getValue();
            return new NodeValueBoolean(b, node);
        }
        if ((datatype.equals(XSDdateTime) || datatype.equals(XSDdateTimeStamp)) && XSDdateTime.isValid(lex)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDdate) && XSDdate.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDtime) && XSDtime.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgYear) && XSDgYear.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgYearMonth) && XSDgYearMonth.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgMonth) && XSDgMonth.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgMonthDay) && XSDgMonthDay.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDgDay) && XSDgDay.isValidLiteral(lit)) {
            return NodeValueDateTime.create(lexTrimmed, node);
        }
        if (datatype.equals(XSDduration) && XSDduration.isValid(lex)) {
            Duration duration = xmlDatatypeFactory.newDuration(lexTrimmed);
            return new NodeValueDuration(duration, node);
        }
        if (datatype.equals(XSDyearMonthDuration) && XSDyearMonthDuration.isValid(lex)) {
            Duration duration = xmlDatatypeFactory.newDuration(lexTrimmed);
            return new NodeValueDuration(duration, node);
        }
        if (datatype.equals(XSDdayTimeDuration) && XSDdayTimeDuration.isValid(lex)) {
            Duration duration = xmlDatatypeFactory.newDuration(lexTrimmed);
            return new NodeValueDuration(duration, node);
        }
        // Not wired in
        if (SystemARQ.EnableRomanNumerals) {
            if (lit.getDatatypeURI().equals(RomanNumeralDatatype.get().getURI())) {
                Object obj = RomanNumeralDatatype.get().parse(lexTrimmed);
                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;
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) Duration(javax.xml.datatype.Duration) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 28 with Duration

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

the class NodeValueOps method additionNV.

public static NodeValue additionNV(NodeValue nv1, NodeValue nv2) {
    ValueSpaceClassification vs1 = nv1.getValueSpace();
    ValueSpaceClassification vs2 = nv2.getValueSpace();
    if (vs1.equals(VSPACE_NUM) && vs2.equals(VSPACE_NUM))
        return XSDFuncOp.numAdd(nv1, nv2);
    if (vs1.equals(VSPACE_STRING) && vs2.equals(VSPACE_STRING))
        return NodeValue.makeString(nv1.asString() + nv2.asString());
    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().add(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);
    }
    // Loose style. Add any duration to any date or time value.
    if (vs1.equals(VSPACE_DATETIME) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDateTime(result);
        return r;
    }
    // Loose style. Add any duration to any date or time value.
    if (vs1.equals(VSPACE_DATE) && vs2.equals(VSPACE_DURATION)) {
        XMLGregorianCalendar cal = nv1.getDateTime();
        XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeDate(result);
        return r;
    }
    // Loose style. Add any duration to any date or time value.
    if (vs1.equals(VSPACE_TIME) && vs2.equals(VSPACE_DURATION)) {
        // ONLY dayTime.
        XMLGregorianCalendar cal = nv1.getDateTime();
        XMLGregorianCalendar result = xsd_add(cal, nv2.getDuration());
        NodeValue r = NodeValue.makeNode(result.toXMLFormat(), XSDDatatype.XSDtime);
        return r;
    }
    if (isDT(vs2) && vs1.equals(VSPACE_DURATION))
        // Carefully ...
        return additionNV(nv2, nv1);
    throw new ExprEvalTypeException("Operator '+' : Undefined addition: " + 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 29 with Duration

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

the class XMLGregorianCalendarImpl method normalizeToTimezone.

/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
private XMLGregorianCalendar normalizeToTimezone(XMLGregorianCalendar cal, int timezone) {
    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) cal.clone();
    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new // isPositive
    DurationImpl(// isPositive
    minutes >= 0, // years
    0, // months
    0, // days
    0, // hours
    0, // absolute
    minutes < 0 ? -minutes : minutes, // seconds
    0);
    result.add(d);
    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Example 30 with Duration

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

the class NodeValueRewriterTest method visitNodeValueDurationTest.

@Test
public void visitNodeValueDurationTest() throws DatatypeConfigurationException {
    Duration dur = DatatypeFactory.newInstance().newDuration(true, 1, 2, 3, 4, 5, 6);
    NodeValue nv = new NodeValueDuration(dur);
    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) NodeValueDuration(org.apache.jena.sparql.expr.nodevalue.NodeValueDuration) Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Aggregations

Duration (javax.xml.datatype.Duration)110 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)50 Test (org.junit.Test)17 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)12 ArrayList (java.util.ArrayList)11 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)8 GregorianCalendar (java.util.GregorianCalendar)8 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)7 Date (java.util.Date)7 CleanupPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType)6 Calendar (java.util.Calendar)6 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)5 XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)5 Collection (java.util.Collection)4 NodeValue (org.apache.jena.sparql.expr.NodeValue)4 NotNull (org.jetbrains.annotations.NotNull)4 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)3 Task (com.evolveum.midpoint.task.api.Task)3