Search in sources :

Example 11 with XSDouble

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

the class XSDouble method div.

/**
 * Mathematical division operator between this XSDouble and the supplied
 * ResultSequence.
 *
 * @param arg
 *            The ResultSequence to perform an division with
 * @return A XSDouble consisting of the result of the mathematical division.
 */
public ResultSequence div(ResultSequence arg) throws DynamicError {
    ResultSequence carg = convertResultSequence(arg);
    XSDouble val = (XSDouble) get_single_type(carg, XSDouble.class);
    return ResultSequenceFactory.create_new(new XSDouble(double_value() / val.double_value()));
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence)

Example 12 with XSDouble

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

the class XSYearMonthDuration method div.

/**
 * Mathematical division between this duration stored and the supplied
 * duration of time (of type XSYearMonthDuration)
 *
 * @param arg
 *            The duration of time to divide by
 * @return New XSYearMonthDuration representing the resulting duration
 *         after the division
 * @throws DynamicError
 */
public ResultSequence div(ResultSequence arg) throws DynamicError {
    if (arg.size() != 1)
        DynamicError.throw_type_error();
    Item at = arg.first();
    if (at instanceof XSDouble) {
        XSDouble dt = (XSDouble) at;
        int ret = 0;
        if (!dt.zero())
            ret = (int) Math.round(monthValue() / dt.double_value());
        return ResultSequenceFactory.create_new(new XSYearMonthDuration(ret));
    } else if (at instanceof XSDecimal) {
        XSDecimal dt = (XSDecimal) at;
        int ret = 0;
        if (!dt.zero())
            ret = (int) Math.round(monthValue() / dt.getValue().doubleValue());
        return ResultSequenceFactory.create_new(new XSYearMonthDuration(ret));
    } else if (at instanceof XSYearMonthDuration) {
        XSYearMonthDuration md = (XSYearMonthDuration) at;
        double res = (double) monthValue() / md.monthValue();
        return ResultSequenceFactory.create_new(new XSDecimal(new BigDecimal(res)));
    } else {
        DynamicError.throw_type_error();
        // unreach
        return null;
    }
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) BigDecimal(java.math.BigDecimal)

Example 13 with XSDouble

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

the class XSYearMonthDuration method times.

/**
 * Mathematical multiplication between this duration stored and the supplied
 * duration of time (of type XSYearMonthDuration)
 *
 * @param arg
 *            The duration of time to multiply by
 * @return New XSYearMonthDuration representing the resulting duration
 *         after the multiplication
 * @throws DynamicError
 */
public ResultSequence times(ResultSequence arg) throws DynamicError {
    ResultSequence convertedRS = arg;
    if (arg.size() == 1) {
        Item argValue = arg.first();
        if (argValue instanceof XSDecimal) {
            convertedRS = ResultSequenceFactory.create_new(new XSDouble(argValue.getStringValue()));
        }
    }
    XSDouble val = (XSDouble) NumericType.get_single_type(convertedRS, XSDouble.class);
    if (val.nan()) {
        throw DynamicError.nan();
    }
    if (val.infinite()) {
        throw DynamicError.overflowDateTime();
    }
    int res = (int) Math.round(monthValue() * val.double_value());
    return ResultSequenceFactory.create_new(new XSYearMonthDuration(res));
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence)

Example 14 with XSDouble

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

the class TestBugs method testNegativeZeroDouble.

public void testNegativeZeroDouble() throws Exception {
    // Bug 279406
    bundle = Platform.getBundle("org.w3c.xqts.testsuite");
    URL fileURL = bundle.getEntry("/TestSources/emptydoc.xml");
    loadDOMDocument(fileURL);
    // Get XML Schema Information for the Document
    XSModel schema = getGrammar();
    setupDynamicContext(schema);
    String xpath = "-(xs:double('0'))";
    compileXPath(xpath);
    ResultSequence rs = evaluate(domDoc);
    XSDouble result = (XSDouble) rs.first();
    String actual = result.getStringValue();
    assertEquals("-0", actual);
}
Also used : XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) XSModel(org.apache.xerces.xs.XSModel) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) URL(java.net.URL)

Example 15 with XSDouble

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

the class FnSubsequence method subsequence.

/**
 * Subsequence operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:subsequence operation.
 */
public static ResultSequence subsequence(Collection args) throws DynamicError {
    ResultBuffer rs = new ResultBuffer();
    // get args
    Iterator citer = args.iterator();
    ResultSequence seq = (ResultSequence) citer.next();
    if (seq.empty())
        return ResultBuffer.EMPTY;
    ResultSequence startLoc = (ResultSequence) citer.next();
    ResultSequence length = null;
    if (citer.hasNext()) {
        length = (ResultSequence) citer.next();
    }
    Item at = startLoc.first();
    if (!(at instanceof NumericType)) {
        DynamicError.throw_type_error();
    }
    at = new XSDouble(at.getStringValue());
    int start = (int) ((XSDouble) at).double_value();
    // no of items beyond index >= 1 that are added to the result
    int effectiveNoItems = 0;
    if (length != null) {
        // the 3rd argument is present
        if (length.size() != 1)
            DynamicError.throw_type_error();
        at = length.first();
        if (!(at instanceof NumericType)) {
            DynamicError.throw_type_error();
        }
        at = new XSDouble(at.getStringValue());
        int len = (int) ((XSDouble) at).double_value();
        if (len < 0) {
            DynamicError.throw_type_error();
        }
        if (start <= 0) {
            effectiveNoItems = start + len - 1;
            start = 1;
        } else {
            effectiveNoItems = len;
        }
    } else {
        // 3rd argument is absent
        if (start <= 0) {
            start = 1;
            effectiveNoItems = seq.size();
        } else {
            effectiveNoItems = seq.size() - start + 1;
        }
    }
    // index running parallel to the iterator
    int pos = 1;
    int addedItems = 0;
    if (effectiveNoItems > 0) {
        for (Iterator seqIter = seq.iterator(); seqIter.hasNext(); ) {
            at = (AnyType) seqIter.next();
            if (start <= pos && addedItems < effectiveNoItems) {
                rs.add(at);
                addedItems++;
            }
            pos++;
        }
    }
    return rs.getSequence();
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) Item(org.eclipse.wst.xml.xpath2.api.Item) ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Iterator(java.util.Iterator)

Aggregations

ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)17 Item (org.eclipse.wst.xml.xpath2.api.Item)12 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)12 Iterator (java.util.Iterator)9 AnyAtomicType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)6 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)6 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)5 XSFloat (org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat)5 Collection (java.util.Collection)4 NumericType (org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType)4 XSUntypedAtomic (org.eclipse.wst.xml.xpath2.processor.internal.types.XSUntypedAtomic)4 TypePromoter (org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter)4 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)3 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)2 ComparableTypePromoter (org.eclipse.wst.xml.xpath2.processor.internal.utils.ComparableTypePromoter)2 ScalarTypePromoter (org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1