Package net.sf.saxon.type

Examples of net.sf.saxon.type.ItemType


        operand0 = TypeChecker.staticTypeCheck(operand0, atomicType, false, role0, env);

        RoleLocator role1 = new RoleLocator(RoleLocator.BINARY_EXPR, Token.tokens[operator], 1);
        operand1 = TypeChecker.staticTypeCheck(operand1, atomicType, false, role1, env);

        ItemType t0 = operand0.getItemType();
        ItemType t1 = operand1.getItemType();

        int c0 = operand0.getCardinality();
        int c1 = operand1.getCardinality();

        if (!backwardsCompatible) {
            if (t0 == Type.ANY_ATOMIC_TYPE || t0 == Type.UNTYPED_ATOMIC_TYPE ||
                    t1 == Type.ANY_ATOMIC_TYPE || t1 == Type.UNTYPED_ATOMIC_TYPE ) {
                // then no static type checking is possible
            } else {
                int pt0 = t0.getPrimitiveType();
                int pt1 = t1.getPrimitiveType();
                if (pt0 != pt1 &&
                    !(Type.isSubType(t0, Type.NUMBER_TYPE) &&
                        Type.isSubType(t1, Type.NUMBER_TYPE))) {
                    StaticError err = new StaticError(
                            "Cannot compare " + t0.toString(env.getNamePool()) +
                            " to " + t1.toString(env.getNamePool()));
                    err.setIsTypeError(true);
                    throw err;
                }
            }
        }
View Full Code Here


    * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER, Type.NODE,
    * or Type.ITEM (meaning not known in advance)
    */

  public ItemType getItemType() {
        ItemType in = operand.getItemType();
        if (Type.isSubType(in, Type.ANY_ATOMIC_TYPE)) {
            return in;
        }
        if (in instanceof ContentTypeTest) {
            SchemaType schemaType = ((ContentTypeTest)in).getSchemaType();
View Full Code Here

    * Type-check the expression
    */

    public Expression analyze(StaticContext env, ItemType contextItemType) throws XPathException {
        operand = operand.analyze(env, contextItemType);
        ItemType type = operand.getItemType();
        if (type instanceof NodeTest) {
            return this;
        }
        if (type==Type.ANY_ATOMIC_TYPE || type instanceof AnyItemType ||
                type==Type.UNTYPED_ATOMIC_TYPE) {
View Full Code Here

        // System.err.println("Static Type Check on expression (requiredType = " + req + "):"); supplied.display(10);

        Expression exp = supplied;

        ItemType reqItemType = req.getPrimaryType();
        int reqCard = req.getCardinality();
        boolean allowsMany = Cardinality.allowsMany(reqCard);

        ItemType suppliedItemType = null;
            // item type of the supplied expression: null means not yet calculated
        int suppliedCard = -1;
            // cardinality of the supplied expression: -1 means not yet calculated

        boolean cardOK = (reqCard == StaticProperty.ALLOWS_ZERO_OR_MORE);
        // Unless the required cardinality is zero-or-more (no constraints).
        // check the static cardinality of the supplied expression
        if (!cardOK) {
            suppliedCard = exp.getCardinality();
            cardOK = Cardinality.subsumes(reqCard, suppliedCard);
        }

        boolean itemTypeOK = req.getPrimaryType() instanceof AnyItemType;
        // Unless the required item type and content type are ITEM (no constraints)
        // check the static item type against the supplied expression.
        // NOTE: we don't currently do any static inference regarding the content type
        if (!itemTypeOK) {
            suppliedItemType = exp.getItemType();
            int relation = Type.relationship(reqItemType, suppliedItemType);
            itemTypeOK = relation == Type.SAME_TYPE || relation == Type.SUBSUMES;
            if (!itemTypeOK) {
                // Handle numeric promotion decimal -> float -> double
                if (Type.isPromotable(suppliedItemType, reqItemType)) {
                    itemTypeOK = true;
                    ComputedExpression cexp;
                    if (allowsMany) {
                        cexp = new AtomicSequenceConverter(exp, (AtomicType)reqItemType);
                    } else {
                        cexp = new CastExpression(exp, (AtomicType)reqItemType, true);
                    }
                    if (exp instanceof Value) {
                        try {
                            exp = ExpressionTool.eagerEvaluate(cexp, null);
                        } catch (XPathException err) {
                            throw new StaticError(err);
                        }
                    } else {
                        cexp.adoptChildExpression(exp);
                        exp = cexp;
                    }
                    suppliedItemType = reqItemType;
                }
                // Handle conversion of untypedAtomic to the required type
                if (suppliedItemType.getPrimitiveType() == Type.UNTYPED_ATOMIC) {
                    itemTypeOK = true;
                    ComputedExpression cexp;
                    if (allowsMany) {
                        cexp = new AtomicSequenceConverter(exp, (AtomicType)reqItemType);
                    } else {
                        cexp = new CastExpression(exp, (AtomicType)reqItemType, true);
                    }
                    if (exp instanceof Value) {
                        try {
                            exp = ExpressionTool.eagerEvaluate(cexp, null);
                        } catch (XPathException err) {
                            throw new StaticError(err);
                        }
                    } else {
                        cexp.adoptChildExpression(exp);
                        exp = cexp;
                    }
                    suppliedItemType = reqItemType;
                }
            }
        }

        // If both the cardinality and item type are statically OK, return now.
        if (itemTypeOK && cardOK) {
            return exp;
        }

        // If we haven't evaluated the cardinality of the supplied expression, do it now
        if (suppliedCard == -1) {
            suppliedCard = exp.getCardinality();
            if (!cardOK) {
                cardOK = Cardinality.subsumes(reqCard, suppliedCard);
            }
        }

        // If an empty sequence was explicitly supplied, and empty sequence is allowed,
        // then the item type doesn't matter
        if (cardOK && suppliedCard==StaticProperty.EMPTY) {
            return exp;
        }

        // If we haven't evaluated the item type of the supplied expression, do it now
        if (suppliedItemType == null) {
            suppliedItemType = exp.getItemType();
        }

        if (suppliedCard==StaticProperty.EMPTY && ((reqCard & StaticProperty.ALLOWS_ZERO) == 0) ) {
            StaticError err = new StaticError(
                        "An empty sequence is not allowed as the " + role.getMessage(),
                        ExpressionTool.getLocator(supplied));
            err.setErrorCode(role.getErrorCode());
            err.setIsTypeError(true);
            throw err;
        }

        // Handle the special rules for 1.0 compatibility mode
        if (backwardsCompatible && !allowsMany) {
            if (Type.isSubType(reqItemType, Type.STRING_TYPE)) {
                if (!Type.isSubType(suppliedItemType, Type.ANY_ATOMIC_TYPE)) {
                    ComputedExpression cexp = new Atomizer(exp);
                    cexp.adoptChildExpression(exp);
                    exp = cexp;
                }
                ComputedExpression cexp2 = new ConvertToString(exp);
                if (exp instanceof Value) {
                    try {
                        exp = ExpressionTool.eagerEvaluate(cexp2, null);
                    } catch (XPathException err) {
                        throw new StaticError(err);
                    }
                } else {
                    cexp2.adoptChildExpression(exp);
                    exp = cexp2;
                }
                suppliedItemType = Type.STRING_TYPE;
                suppliedCard = StaticProperty.EXACTLY_ONE;
                cardOK = Cardinality.subsumes(reqCard, suppliedCard);
            } else if (reqItemType == Type.NUMBER_TYPE || Type.isSubType(reqItemType, Type.DOUBLE_TYPE)) {
                // TODO: in the Nov 2003 draft, the rules have changed so that number() is called
                // only if the supplied value doesn't match the expected type. We're currently
                // returning different results for round(()) depending on whether the arg value
                // is known statically or not.
                NumberFn fn = (NumberFn)SystemFunction.makeSystemFunction("number", env.getNamePool());
                Expression[] args = new Expression[1];
                args[0] = exp;
                fn.setArguments(args);
                if (exp instanceof Value) {
                    try {
                        exp = ExpressionTool.eagerEvaluate(fn, null);
                    } catch (XPathException err) {
                        throw new StaticError(err);
                    }
                } else {
                    fn.adoptChildExpression(exp);
                    exp = fn;
                }
                suppliedItemType = Type.DOUBLE_TYPE;
                suppliedCard = StaticProperty.EXACTLY_ONE;
                cardOK = Cardinality.subsumes(reqCard, suppliedCard);
            } else if (reqItemType instanceof AnyNodeTest ||
                    reqItemType instanceof AnyItemType
                        || reqItemType == Type.ANY_ATOMIC_TYPE ) {
                                // TODO: this last condition isn't in the rules for function calls,
                                // but is needed for arithmetic expressions
                if (Cardinality.allowsMany(suppliedCard)) {
                    ComputedExpression cexp = new FirstItemExpression(exp);
                    cexp.adoptChildExpression(exp);
                    exp = cexp;
                }
                suppliedCard = StaticProperty.ALLOWS_ZERO_OR_ONE;
                cardOK = Cardinality.subsumes(reqCard, suppliedCard);
            }
        }

        // If the required type is atomic, and the supplied type is not statically
        // guaranteed to be atomic, add an Atomizer

        if ((reqItemType instanceof AtomicType) &&
                !(suppliedItemType instanceof AtomicType) &&
                !(suppliedCard == StaticProperty.EMPTY)) {
            ComputedExpression cexp = new Atomizer(exp);
            exp = cexp;
            suppliedItemType = exp.getItemType();
            suppliedCard = exp.getCardinality();
            cardOK = Cardinality.subsumes(reqCard, suppliedCard);
        }

        // If the required type is a subtype of ATOMIC, and the supplied type is
        // capable of producing untyped atomic values, add an Untyped Atomic Converter

        if (reqItemType != Type.ANY_ATOMIC_TYPE &&
                (reqItemType instanceof AtomicType) &&
                (suppliedItemType instanceof AtomicType) &&
                (suppliedItemType == Type.ANY_ATOMIC_TYPE ||
                    suppliedItemType == Type.UNTYPED_ATOMIC_TYPE) &&
                !(suppliedCard == StaticProperty.EMPTY)) {
            ComputedExpression cexp = new UntypedAtomicConverter(exp, (AtomicType)reqItemType);
            cexp.adoptChildExpression(exp);
            exp = cexp;
            suppliedItemType = exp.getItemType();
            suppliedCard = exp.getCardinality();
            cardOK = Cardinality.subsumes(reqCard, suppliedCard);
        }

        // Try a static type check. We only throw it out if the call cannot possibly succeed.

        int relation = Type.relationship(suppliedItemType, reqItemType);
        if (relation == Type.DISJOINT) {
            // The item types may be disjoint, but if both the supplied and required types permit
            // an empty sequence, we can't raise a static error. Raise a warning instead.
            if (Cardinality.allowsZero(suppliedCard) &&
                    Cardinality.allowsZero(reqCard)) {
                if (suppliedCard != StaticProperty.EMPTY) {
                    String msg = "Required item type of " + role.getMessage() +
                            " is " + reqItemType.toString(env.getNamePool()) +
                            "; supplied value has item type " +
                            suppliedItemType.toString(env.getNamePool()) +
                            ". The expression can succeed only if the supplied value is an empty sequence.";
                    env.issueWarning(msg);
                }
            } else {
                    StaticError err = new StaticError(
                        "Required item type of " + role.getMessage() +
                            " is " + reqItemType.toString(env.getNamePool()) +
                            "; supplied value has item type " +
                            suppliedItemType.toString(env.getNamePool()),
                        ExpressionTool.getLocator(supplied));
                    err.setErrorCode(role.getErrorCode());
                    err.setIsTypeError(true);
                    throw err;
            }
View Full Code Here

                Atomizer atomizer = new Atomizer(select);
                ExpressionTool.copyLocationInfo(select, atomizer);
                select = visitor.simplify(atomizer);
                select = visitor.typeCheck(atomizer, contextItemType);
            }
            ItemType itemType = select.getItemType(th);
            if (!(th.isSubType(itemType, BuiltInAtomicType.STRING) ||
                    th.isSubType(itemType, BuiltInAtomicType.UNTYPED_ATOMIC))) {
                select = new AtomicSequenceConverter(select, BuiltInAtomicType.STRING);
            }
            // Don't bother converting untypedAtomic to string
View Full Code Here

        separator = visitor.typeCheck(separator, contextItemType);
        if (!Cardinality.allowsMany(select.getCardinality())) {
            isSingleton = true;
        }
        final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
        ItemType itemType = select.getItemType(th);
        if (itemType.isAtomicType()) {
            isAtomic = true;
        }
        select.setFlattened(true);
        if (select instanceof Literal && separator instanceof Literal) {
            XPathContext c = visitor.getStaticContext().makeEarlyEvaluationContext();
View Full Code Here

            }
        }
        Expression expr = ExpressionTool.make(expression, staticContext,
                0, Token.EOF, getDocumentLocator().getLineNumber(locationId), false);
        expr.setContainer(staticContext);
        ItemType contextItemType = Type.ITEM_TYPE;
        ExpressionVisitor visitor = ExpressionVisitor.make(staticContext);
        expr = visitor.typeCheck(expr, contextItemType);
        SlotManager stackFrameMap = getPipelineConfiguration().getConfiguration().makeSlotManager();
        ExpressionTool.allocateSlots(expr, stackFrameMap.getNumberOfVariables(), stackFrameMap);
        Controller controller = new Controller(getConfiguration());
View Full Code Here

            trace.setContainer(compiledTemplate);
            exp = trace;
            compiledTemplate.setBody(exp);
        }

        ItemType contextItemType = Type.ITEM_TYPE;
        if (getObjectName() == null) {
            // the template can't be called by name, so the context item must match the match pattern
            contextItemType = match.getNodeTest();
        }
View Full Code Here

    }

    public XQSequenceType getStaticResultType() throws XQException {
        checkNotClosed();
        Expression exp = expression.getExpression();    // unwrap two layers!
        ItemType itemType = exp.getItemType(connection.getConfiguration().getTypeHierarchy());
        int cardinality = exp.getCardinality();
        SequenceType staticType = SequenceType.makeSequenceType(itemType, cardinality);
        return new SaxonXQSequenceType(staticType, connection.getConfiguration());
    }
View Full Code Here

        return XQConstants.CONSTRUCTION_MODE_STRIP;
    }


    public XQItemType getContextItemStaticType() {
        ItemType type = sqc.getRequiredContextItemType();
        return new SaxonXQItemType(type, sqc.getConfiguration());
    }
View Full Code Here

TOP

Related Classes of net.sf.saxon.type.ItemType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.