Search in sources :

Example 1 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class FnAdjustTimeToTimeZone method adjustTime.

/**
 * Evaluate the function using the arguments passed.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @param sc
 *            Result of static context operation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the fn:dateTime operation.
 */
public static ResultSequence adjustTime(Collection args, DynamicContext dc) throws DynamicError {
    Collection cargs = Function.convert_arguments(args, expectedArgs());
    // get args
    Iterator argiter = cargs.iterator();
    ResultSequence arg1 = (ResultSequence) argiter.next();
    if (arg1.empty()) {
        return ResultBuffer.EMPTY;
    }
    ResultSequence arg2 = ResultBuffer.EMPTY;
    if (argiter.hasNext()) {
        arg2 = (ResultSequence) argiter.next();
    }
    XSTime time = (XSTime) arg1.first();
    XSDayTimeDuration timezone = null;
    if (arg2.empty()) {
        if (time.timezoned()) {
            XSTime localized = new XSTime(time.calendar(), null);
            return localized;
        } else {
            return arg1;
        }
    }
    XMLGregorianCalendar xmlCalendar = null;
    if (time.tz() != null) {
        xmlCalendar = _datatypeFactory.newXMLGregorianCalendar((GregorianCalendar) time.normalizeCalendar(time.calendar(), time.tz()));
    } else {
        xmlCalendar = _datatypeFactory.newXMLGregorianCalendarTime(time.hour(), time.minute(), (int) time.second(), 0);
    }
    timezone = (XSDayTimeDuration) arg2.first();
    if (timezone.lt(minDuration, dc) || timezone.gt(maxDuration, dc)) {
        throw DynamicError.invalidTimezone();
    }
    if (time.tz() == null) {
        return new XSTime(time.calendar(), timezone);
    }
    Duration duration = _datatypeFactory.newDuration(timezone.getStringValue());
    xmlCalendar.add(duration);
    return new XSTime(xmlCalendar.toGregorianCalendar(), timezone);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Iterator(java.util.Iterator) XSDayTimeDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Collection(java.util.Collection) Duration(javax.xml.datatype.Duration) XSDayTimeDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration) XSTime(org.eclipse.wst.xml.xpath2.processor.internal.types.XSTime)

Example 2 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class FnAdjustTimeToTimeZone method expectedArgs.

/**
 * Obtain a list of expected arguments.
 *
 * @return Result of operation.
 */
public static synchronized Collection expectedArgs() {
    if (_expected_args == null) {
        _expected_args = new ArrayList();
        _expected_args.add(new SeqType(new XSTime(), SeqType.OCC_QMARK));
        _expected_args.add(new SeqType(new XSDayTimeDuration(), SeqType.OCC_QMARK));
    }
    return _expected_args;
}
Also used : SeqType(org.eclipse.wst.xml.xpath2.processor.internal.SeqType) ArrayList(java.util.ArrayList) XSDayTimeDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration) XSTime(org.eclipse.wst.xml.xpath2.processor.internal.types.XSTime)

Example 3 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class FnAdjustDateTimeToTimeZone method expectedArgs.

/**
 * Obtain a list of expected arguments.
 *
 * @return Result of operation.
 */
public static synchronized Collection expectedArgs() {
    if (_expected_args == null) {
        _expected_args = new ArrayList();
        _expected_args.add(new SeqType(new XSDateTime(), SeqType.OCC_QMARK));
        _expected_args.add(new SeqType(new XSDayTimeDuration(), SeqType.OCC_QMARK));
    }
    return _expected_args;
}
Also used : XSDateTime(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDateTime) SeqType(org.eclipse.wst.xml.xpath2.processor.internal.SeqType) ArrayList(java.util.ArrayList) XSDayTimeDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)

Example 4 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class FnAdjustDateToTimeZone method adjustDate.

/**
 * Evaluate the function using the arguments passed.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @param sc
 *            Result of static context operation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the fn:dateTime operation.
 */
public static ResultSequence adjustDate(Collection args, DynamicContext dc) throws DynamicError {
    Collection cargs = Function.convert_arguments(args, expectedArgs());
    // get args
    Iterator argiter = cargs.iterator();
    ResultSequence arg1 = (ResultSequence) argiter.next();
    if (arg1.empty()) {
        return ResultBuffer.EMPTY;
    }
    ResultSequence arg2 = ResultBuffer.EMPTY;
    if (argiter.hasNext()) {
        arg2 = (ResultSequence) argiter.next();
    }
    XSDate date = (XSDate) arg1.item(0);
    XSDayTimeDuration timezone = null;
    if (arg2.empty()) {
        if (date.timezoned()) {
            XSDate localized = new XSDate(date.calendar(), null);
            return localized;
        }
        return arg1;
    }
    timezone = (XSDayTimeDuration) arg2.item(0);
    if (timezone.lt(minDuration, dc) || timezone.gt(maxDuration, dc)) {
        throw DynamicError.invalidTimezone();
    }
    if (date.tz() == null) {
        return new XSDate(date.calendar(), timezone);
    }
    XMLGregorianCalendar xmlCalendar = _datatypeFactory.newXMLGregorianCalendar((GregorianCalendar) date.normalizeCalendar(date.calendar(), date.tz()));
    Duration duration = _datatypeFactory.newDuration(timezone.getStringValue());
    xmlCalendar.add(duration);
    return new XSDate(xmlCalendar.toGregorianCalendar(), timezone);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) XSDate(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDate) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Iterator(java.util.Iterator) XSDayTimeDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration) Collection(java.util.Collection) Duration(javax.xml.datatype.Duration) XSDayTimeDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)

Example 5 with XSDayTimeDuration

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration in project webtools.sourceediting by eclipse.

the class XSDate method plus.

/**
 * Mathematical addition operator between this XSDate and a supplied result
 * sequence (XDTYearMonthDuration and XDTDayTimeDuration are only valid
 * ones).
 *
 * @param arg
 *            The supplied ResultSequence that is on the right of the minus
 *            operator. If arg is an XDTYearMonthDuration or an
 *            XDTDayTimeDuration the result will be a XSDate of the result
 *            of the current date minus the duration of time supplied.
 * @return New ResultSequence consisting of the result of the mathematical
 *         minus operation.
 */
public ResultSequence plus(ResultSequence arg) throws DynamicError {
    if (arg.size() != 1)
        DynamicError.throw_type_error();
    Item at = arg.first();
    try {
        if (at instanceof XSYearMonthDuration) {
            XSYearMonthDuration val = (XSYearMonthDuration) at;
            XSDate res = (XSDate) clone();
            res.calendar().add(Calendar.MONTH, val.monthValue());
            return ResultSequenceFactory.create_new(res);
        } else if (at instanceof XSDayTimeDuration) {
            XSDayTimeDuration val = (XSDayTimeDuration) at;
            XSDate res = (XSDate) clone();
            // We only need to add the Number of days dropping the rest.
            int days = val.days();
            if (val.negative()) {
                days *= -1;
            }
            res.calendar().add(Calendar.DAY_OF_MONTH, days);
            res.calendar().add(Calendar.MILLISECOND, (int) (val.time_value() * 1000.0));
            return ResultSequenceFactory.create_new(res);
        } else {
            DynamicError.throw_type_error();
            // unreach
            return null;
        }
    } catch (CloneNotSupportedException err) {
        assert false;
        return null;
    }
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item)

Aggregations

XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)9 Duration (javax.xml.datatype.Duration)6 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)4 Item (org.eclipse.wst.xml.xpath2.api.Item)4 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Iterator (java.util.Iterator)3 SeqType (org.eclipse.wst.xml.xpath2.processor.internal.SeqType)3 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)3 XSDate (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDate)3 XSDateTime (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDateTime)3 XSTime (org.eclipse.wst.xml.xpath2.processor.internal.types.XSTime)3 GregorianCalendar (java.util.GregorianCalendar)2 BigDecimal (java.math.BigDecimal)1 CalendarType (org.eclipse.wst.xml.xpath2.processor.internal.types.CalendarType)1