Search in sources :

Example 6 with NumericType

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

the class FsMinus method fs_minus_unary.

/**
 * Unary operation on the values of the arguments.
 *
 * @param args
 *            input arguments.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the operation.
 */
public static ResultSequence fs_minus_unary(Collection args) throws DynamicError {
    // make sure we got only one arg
    if (args.size() != 1)
        DynamicError.throw_type_error();
    ResultSequence arg = (org.eclipse.wst.xml.xpath2.api.ResultSequence) args.iterator().next();
    // make sure we got only one numeric atom
    if (arg.size() != 1)
        DynamicError.throw_type_error();
    Item at = arg.first();
    if (!(at instanceof NumericType))
        DynamicError.throw_type_error();
    NumericType nt = (NumericType) at;
    return nt.unary_minus();
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) Item(org.eclipse.wst.xml.xpath2.api.Item) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence)

Example 7 with NumericType

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

the class TestBugs method testNumberAggregationWithNill2.

public void testNumberAggregationWithNill2() throws Exception {
    URL fileURL = bundle.getEntry("/bugTestFiles/bugNilledNoSchema.xml");
    loadDOMDocument(fileURL);
    XSModel schema = getGrammar();
    setupDynamicContext(schema);
    String xpath = null;
    ResultSequence rs = null;
    String actual = null;
    // a
    xpath = "fn:count(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("3", actual);
    // b
    xpath = "fn:sum(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((NumericType) rs.first()).getStringValue();
    assertEquals("43", actual);
    // b2
    xpath = "fn:sum(( /root/element1, /root/element2, /root/element3 ), 100)";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((NumericType) rs.first()).getStringValue();
    assertEquals("143", actual);
    // c
    xpath = "fn:avg(( /root/element1, /root/element2, /root/element3, 1 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((NumericType) rs.first()).getStringValue();
    assertEquals("11", actual);
    // d
    xpath = "fn:max(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((NumericType) rs.first()).getStringValue();
    assertEquals("42", actual);
    // e
    xpath = "fn:min(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((NumericType) rs.first()).getStringValue();
    assertEquals("1", actual);
}
Also used : 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 8 with NumericType

use of org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType 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)

Example 9 with NumericType

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

the class FnRoundHalfToEven method fn_round_half_to_even.

public static ResultSequence fn_round_half_to_even(Collection args) throws DynamicError {
    if (args.size() > 2 || args.size() <= 1) {
        throw new DynamicError(TypeError.invalid_type(null));
    }
    Iterator argIt = args.iterator();
    ResultSequence rsArg1 = (ResultSequence) argIt.next();
    ResultSequence rsPrecision = (ResultSequence) argIt.next();
    NumericType nt = FnAbs.get_single_numeric_arg(rsArg1);
    // empty arg
    if (nt == null)
        return ResultBuffer.EMPTY;
    NumericType ntPrecision = (NumericType) rsPrecision.first();
    return nt.round_half_to_even(Integer.parseInt(ntPrecision.getStringValue()));
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Iterator(java.util.Iterator) DynamicError(org.eclipse.wst.xml.xpath2.processor.DynamicError)

Example 10 with NumericType

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

the class FsConvertOperand method convert_operand.

/**
 * Convert-Operand operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fs: operation.
 */
public static ResultSequence convert_operand(Collection args) throws DynamicError {
    assert args.size() == 2;
    Iterator iter = args.iterator();
    ResultSequence actual = (ResultSequence) iter.next();
    ResultSequence expected = (ResultSequence) iter.next();
    if (expected.size() != 1)
        DynamicError.throw_type_error();
    Item at = expected.first();
    if (!(at instanceof AnyAtomicType))
        DynamicError.throw_type_error();
    AnyAtomicType exp_aat = (AnyAtomicType) at;
    ResultBuffer result = new ResultBuffer();
    // 1
    if (actual.empty())
        return result.getSequence();
    // convert sequence
    for (Iterator i = actual.iterator(); i.hasNext(); ) {
        AnyType item = (AnyType) i.next();
        // 2
        if (item instanceof XSUntypedAtomic) {
            // a
            if (exp_aat instanceof XSUntypedAtomic)
                result.add(new XSString(item.getStringValue()));
            else // b
            if (exp_aat instanceof NumericType)
                result.add(new XSDouble(item.getStringValue()));
            else // c
            {
                assert exp_aat instanceof CtrType;
                CtrType cons = (CtrType) exp_aat;
                result.concat(cons.constructor(new XSString(item.getStringValue())));
            }
        } else
            // 4
            result.add(item);
    }
    return result.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) CtrType(org.eclipse.wst.xml.xpath2.processor.internal.types.CtrType) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSUntypedAtomic(org.eclipse.wst.xml.xpath2.processor.internal.types.XSUntypedAtomic) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Aggregations

NumericType (org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType)10 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)7 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)6 Iterator (java.util.Iterator)5 Item (org.eclipse.wst.xml.xpath2.api.Item)4 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)4 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)4 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)4 AnyAtomicType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)3 XSUntypedAtomic (org.eclipse.wst.xml.xpath2.processor.internal.types.XSUntypedAtomic)3 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)2 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)2 XSBoolean (org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 XSModel (org.apache.xerces.xs.XSModel)1 CtrType (org.eclipse.wst.xml.xpath2.processor.internal.types.CtrType)1 QName (org.eclipse.wst.xml.xpath2.processor.internal.types.QName)1