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);
}
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);
}
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;
}
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;
}
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());
}
Aggregations