Search in sources :

Example 11 with AnyAtomicType

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

the class FnSum method sum.

/**
 * Sum operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:sum operation.
 */
public static ResultSequence sum(ResultSequence arg, AnyAtomicType zero) throws DynamicError {
    if (arg.empty())
        return ResultSequenceFactory.create_new(zero);
    MathPlus total = null;
    TypePromoter tp = new ScalarTypePromoter();
    tp.considerSequence(arg);
    for (Iterator i = arg.iterator(); i.hasNext(); ) {
        AnyAtomicType conv = tp.promote((AnyType) i.next());
        if (conv == null) {
            conv = zero;
        }
        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();
        }
    }
    return ResultSequenceFactory.create_new((AnyType) total);
}
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) 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 12 with AnyAtomicType

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

the class DefaultEvaluator method visit.

/**
 * visit cast expression
 *
 * @param cexp
 *            is the cast expression.
 * @return a new function
 */
public Object visit(CastExpr cexp) {
    ResultSequence rs = (ResultSequence) cexp.left().accept(this);
    SingleType st = (SingleType) cexp.right();
    rs = FnData.atomize(rs);
    if (rs.size() > 1)
        report_error(TypeError.invalid_type(null));
    if (rs.empty()) {
        if (st.qmark())
            return rs;
        else
            report_error(TypeError.invalid_type(null));
    }
    AnyType at = (AnyType) rs.item(0);
    if (!(at instanceof AnyAtomicType))
        report_error(TypeError.invalid_type(null));
    AnyAtomicType aat = (AnyAtomicType) at;
    QName type = st.type();
    // prepare args from function
    Collection args = new ArrayList();
    args.add(ResultSequenceFactory.create_new(aat));
    try {
        Function function = cexp.function();
        if (function == null) {
            function = _sc.resolveFunction(type.asQName(), args.size());
            cexp.set_function(function);
        }
        if (function == null)
            report_error(TypeError.invalid_type(null));
        return function.evaluate(args, _ec);
    } catch (DynamicError err) {
        report_error(err);
        // unreach
        return null;
    }
}
Also used : Function(org.eclipse.wst.xml.xpath2.api.Function) SingleType(org.eclipse.wst.xml.xpath2.processor.internal.ast.SingleType) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) ArrayList(java.util.ArrayList) Collection(java.util.Collection) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 13 with AnyAtomicType

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

the class DefaultEvaluator method do_step.

// this will evaluate the step expression for the whole focus and return
// the result.
// 
// i.e. It will execute the step expression for each item in the focus
// [each time changing the context item].
private ResultSequence do_step(StepExpr se) {
    ResultBuffer rs = new ResultBuffer();
    ArrayList results = new ArrayList();
    // 0: don't know yet
    int type = 0;
    // 1: atomic
    // 2: node
    Focus focus = focus();
    int original_pos = focus.position();
    // execute step for all items in focus
    while (true) {
        results.add(se.accept(this));
        // go to next
        if (!focus.advance_cp())
            break;
    }
    // make sure we didn't change focus
    focus.set_position(original_pos);
    boolean node_types = false;
    // check the results
    for (Iterator i = results.iterator(); i.hasNext(); ) {
        ResultSequence result = (ResultSequence) i.next();
        // make sure results are of same type, and add them in
        for (Iterator j = result.iterator(); j.hasNext(); ) {
            AnyType item = (AnyType) j.next();
            // first item
            if (type == 0) {
                if (item instanceof AnyAtomicType)
                    type = 1;
                else if (item instanceof NodeType)
                    type = 2;
                else
                    assert false;
            }
            // make sure we got coherent types
            switch(type) {
                // atomic... just concat
                case 1:
                    if (!(item instanceof AnyAtomicType))
                        report_error(TypeError.mixed_vals(null));
                    rs.add(item);
                    break;
                case 2:
                    node_types = true;
                    if (!(item instanceof NodeType))
                        report_error(TypeError.mixed_vals(null));
                    rs.add(item);
                    break;
                default:
                    assert false;
            }
        }
    }
    // XXX lame
    if (node_types) {
        rs = NodeType.linarize(rs);
    }
    return rs.getSequence();
}
Also used : ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) Focus(org.eclipse.wst.xml.xpath2.processor.internal.Focus) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) NodeType(org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType) ArrayList(java.util.ArrayList) ListIterator(java.util.ListIterator) 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 14 with AnyAtomicType

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

the class XercesFloatUserDefined method constructor.

public ResultSequence constructor(ResultSequence arg) throws DynamicError {
    ResultSequence rs = ResultSequenceFactory.create_new();
    if (arg.empty())
        return rs;
    // AnyAtomicType aat = (AnyAtomicType) arg.first();
    AnyType aat = arg.first();
    XSSimpleTypeDefinition simpletype = (XSSimpleTypeDefinition) typeInfo;
    if (simpletype != null) {
        if (simpletype.isDefinedFacet(XSSimpleTypeDefinition.FACET_MININCLUSIVE)) {
            String minValue = simpletype.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MININCLUSIVE);
            float iminValue = Float.parseFloat(minValue);
            float actualValue = Float.parseFloat(aat.getStringValue());
            if (actualValue < iminValue) {
                throw DynamicError.invalidForCastConstructor();
            }
        }
        if (simpletype.isDefinedFacet(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE)) {
            String maxValue = simpletype.getLexicalFacetValue(XSSimpleTypeDefinition.FACET_MAXINCLUSIVE);
            float imaxValue = Float.parseFloat(maxValue);
            float actualValue = Float.parseFloat(aat.getStringValue());
            if (actualValue > imaxValue) {
                throw DynamicError.invalidForCastConstructor();
            }
        }
    }
    rs.add(new XercesFloatUserDefined(Float.parseFloat(aat.getStringValue())));
    return rs;
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType) XSSimpleTypeDefinition(org.apache.xerces.xs.XSSimpleTypeDefinition)

Example 15 with AnyAtomicType

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

the class XercesQNameUserDefined method constructor.

public ResultSequence constructor(ResultSequence arg) throws DynamicError {
    ResultSequence rs = ResultSequenceFactory.create_new();
    if (arg.empty())
        return rs;
    // AnyAtomicType aat = (AnyAtomicType) arg.first();
    AnyType aat = arg.first();
    QName qname = QName.parse_QName(aat.getStringValue());
    rs.add(qname);
    return rs;
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

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