Search in sources :

Example 6 with AnyAtomicType

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

the class FnMin method min.

/**
 * Min operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @param dynamic
 *            Dynamic context
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:min operation.
 */
public static ResultSequence min(Collection args, DynamicContext context) throws DynamicError {
    ResultSequence arg = FnMax.get_arg(args, CmpLt.class);
    if (arg.empty())
        return ResultSequenceFactory.create_new();
    CmpLt max = null;
    TypePromoter tp = new ComparableTypePromoter();
    tp.considerSequence(arg);
    for (Iterator i = arg.iterator(); i.hasNext(); ) {
        AnyAtomicType conv = tp.promote((AnyType) i.next());
        if (conv != null) {
            if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
                return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
            }
            if (max == null || ((CmpLt) conv).lt((AnyType) max, context)) {
                max = (CmpLt) conv;
            }
        }
    }
    return ResultSequenceFactory.create_new((AnyType) max);
}
Also used : XSFloat(org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat) XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Iterator(java.util.Iterator) TypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter) ComparableTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ComparableTypePromoter) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) ComparableTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ComparableTypePromoter) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 7 with AnyAtomicType

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

the class FnIndexOf method index_of.

/**
 * Index-Of operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @param dynamicContext
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:index-of operation.
 */
public static ResultSequence index_of(Collection args, DynamicContext dc) {
    Function.convert_arguments(args, expected_args());
    // get args
    Iterator citer = args.iterator();
    ResultSequence arg1 = (ResultSequence) citer.next();
    ResultSequence arg2 = (ResultSequence) citer.next();
    if (arg1.empty()) {
        return ResultBuffer.EMPTY;
    }
    // sanity chex
    if (arg2.size() != 1)
        DynamicError.throw_type_error();
    String collationUri = dc.getCollationProvider().getDefaultCollation();
    if (citer.hasNext()) {
        ResultSequence arg3 = (ResultSequence) citer.next();
        if (!arg3.empty()) {
            XSString collation = (XSString) arg3.first();
            collationUri = collation.getStringValue();
        }
    }
    ResultBuffer rb = new ResultBuffer();
    AnyAtomicType at = (AnyAtomicType) arg2.first();
    get_comparable(at);
    int index = 1;
    for (Iterator i = arg1.iterator(); i.hasNext(); ) {
        AnyType cmptype = (AnyType) i.next();
        get_comparable(cmptype);
        if (!(at instanceof CmpEq))
            continue;
        if (isBoolean(cmptype, at)) {
            XSBoolean boolat = (XSBoolean) cmptype;
            if (boolat.eq(at, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (isNumeric(cmptype, at)) {
            NumericType numericat = (NumericType) at;
            if (numericat.eq(cmptype, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (isDuration(cmptype, at)) {
            XSDuration durat = (XSDuration) at;
            if (durat.eq(cmptype, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (at instanceof QName && cmptype instanceof QName) {
            QName qname = (QName) at;
            if (qname.eq(cmptype, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (needsStringComparison(cmptype, at)) {
            XSString xstr1 = new XSString(cmptype.getStringValue());
            XSString itemStr = new XSString(at.getStringValue());
            if (FnCompare.compare_string(collationUri, xstr1, itemStr, dc).equals(BigInteger.ZERO)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        }
        index++;
    }
    return rb.getSequence();
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) XSDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration) ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSBoolean(org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean) XSInteger(org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger) Iterator(java.util.Iterator) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 8 with AnyAtomicType

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

the class FnAvg method avg.

/**
 * Average value operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:avg operation.
 */
public static ResultSequence avg(Collection args) throws DynamicError {
    ResultSequence arg = (ResultSequence) args.iterator().next();
    if (arg.empty())
        return ResultSequenceFactory.create_new();
    int elems = 0;
    MathPlus total = null;
    TypePromoter tp = new ScalarTypePromoter();
    tp.considerSequence(arg);
    for (Iterator i = arg.iterator(); i.hasNext(); ) {
        ++elems;
        AnyAtomicType conv = tp.promote((AnyType) i.next());
        if (conv != null) {
            if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
                return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
            }
            if (total == null) {
                total = (MathPlus) conv;
            } else {
                total = (MathPlus) total.plus(ResultSequenceFactory.create_new(conv)).first();
            }
        }
    }
    if (!(total instanceof MathDiv))
        DynamicError.throw_type_error();
    return ((MathDiv) total).div(ResultSequenceFactory.create_new(new XSInteger(BigInteger.valueOf(elems))));
}
Also used : XSFloat(org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat) ScalarTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter) XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) XSInteger(org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger) Iterator(java.util.Iterator) TypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter) ScalarTypePromoter(org.eclipse.wst.xml.xpath2.processor.internal.utils.ScalarTypePromoter) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)

Example 9 with AnyAtomicType

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

the class DefaultStaticContext method make_atomic.

/**
 * Creates an atomic from a specific type name initialized with a default
 * value.
 *
 * @param name
 *            name of type to create
 * @return Atomic type of desired type.
 */
public AnyAtomicType make_atomic(QName name) {
    String ns = name.namespace();
    if (!_functions.containsKey(ns))
        return null;
    FunctionLibrary fl = (FunctionLibrary) _functions.get(ns);
    if (!(fl instanceof ConstructorFL))
        return null;
    ConstructorFL cfl = (ConstructorFL) fl;
    return cfl.atomic_type(name);
}
Also used : FnFunctionLibrary(org.eclipse.wst.xml.xpath2.processor.function.FnFunctionLibrary) FunctionLibrary(org.eclipse.wst.xml.xpath2.processor.internal.function.FunctionLibrary) ConstructorFL(org.eclipse.wst.xml.xpath2.processor.internal.function.ConstructorFL)

Example 10 with AnyAtomicType

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

the class SeqType method make_atomic.

private AnyAtomicType make_atomic(StaticContext sc, QName qname) {
    String ns = qname.namespace();
    Map functionLibraries = sc.getFunctionLibraries();
    if (!functionLibraries.containsKey(ns))
        return null;
    FunctionLibrary fl = (FunctionLibrary) functionLibraries.get(ns);
    if (!(fl instanceof ConstructorFL))
        return null;
    ConstructorFL cfl = (ConstructorFL) fl;
    return cfl.atomic_type(qname);
}
Also used : FunctionLibrary(org.eclipse.wst.xml.xpath2.processor.internal.function.FunctionLibrary) ConstructorFL(org.eclipse.wst.xml.xpath2.processor.internal.function.ConstructorFL) Map(java.util.Map)

Aggregations

AnyAtomicType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)11 Iterator (java.util.Iterator)10 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)10 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)9 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)6 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)5 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)5 NumericType (org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType)4 QName (org.eclipse.wst.xml.xpath2.processor.internal.types.QName)4 XSFloat (org.eclipse.wst.xml.xpath2.processor.internal.types.XSFloat)4 TypePromoter (org.eclipse.wst.xml.xpath2.processor.internal.utils.TypePromoter)4 Item (org.eclipse.wst.xml.xpath2.api.Item)3 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)3 ArrayList (java.util.ArrayList)2 XSSimpleTypeDefinition (org.apache.xerces.xs.XSSimpleTypeDefinition)2 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)2 ConstructorFL (org.eclipse.wst.xml.xpath2.processor.internal.function.ConstructorFL)2 FunctionLibrary (org.eclipse.wst.xml.xpath2.processor.internal.function.FunctionLibrary)2 XSInteger (org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger)2 XSUntypedAtomic (org.eclipse.wst.xml.xpath2.processor.internal.types.XSUntypedAtomic)2