Package net.sf.saxon.type

Examples of net.sf.saxon.type.TypeHierarchy


            return sequence;
        }

        if (requiredType != null) {
            // if declaration is null, we've already done the type checking in a previous pass
            final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
            SequenceType decl = requiredType;
            SequenceType sequenceType = SequenceType.makeSequenceType(
                    decl.getPrimaryType(), StaticProperty.ALLOWS_ZERO_OR_MORE);
            RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, variableName, 0
            );
View Full Code Here


                Expression e2 = ExpressionTool.tryToFactorOutDot(this, contextItemType);
                if (e2 != null) {
                    return e2;
                }
            }
            final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
            final Optimizer opt = visitor.getConfiguration().getOptimizer();
            Expression head = null;
            Expression selection = sequence;
            ItemType selectionContextItemType = contextItemType;
            if (sequence instanceof PathExpression) {
                if (((PathExpression)sequence).isAbsolute(th)) {
                    head = ((PathExpression)sequence).getFirstStep();
                    selection = ((PathExpression)sequence).getRemainingSteps();
                    selectionContextItemType = head.getItemType(th);
                } else {
                    PathExpression p = ((PathExpression)sequence).tryToMakeAbsolute(th);
                    if (p != null) {
                        sequence = p;
                        adoptChildExpression(p);
                        head = ((PathExpression)sequence).getFirstStep();
                        selection = ((PathExpression)sequence).getRemainingSteps();
                        selectionContextItemType = head.getItemType(th);
                    }
                }
            }

            boolean changed = false;
            Expression condition = ((Choose)action).getConditions()[0];
            List list = new ArrayList(4);
            BooleanExpression.listAndComponents(condition, list);
            for (int t=list.size()-1; t>=0; t--) {
                // Process each term in the where clause independently
                Expression term = (Expression)list.get(t);

                if (positionVariable != null &&
                        (term instanceof ValueComparison || term instanceof SingletonComparison)) {
                    BinaryExpression comp = (BinaryExpression)term;
                    Expression[] operands = comp.getOperands();
                    for (int op=0; op<2; op++) {

                        // If the where clause is a simple test on the position variable, for example
                        //    for $x at $p in EXPR where $p = 5 return A
                        // then absorb the where condition into a predicate, rewriting it as
                        //    for $x in EXPR[position() = 5] return A
                        // This takes advantage of the optimizations applied to positional filter expressions
                        // Only do this if the sequence expression has not yet been changed, because
                        // the position in a predicate after the first is different.
                        Binding[] thisVar = {this};
                        if (positionVariable != null && operands[op] instanceof VariableReference && !changed) {
                            List varRefs = new ArrayList();
                            ExpressionTool.gatherVariableReferences(action, positionVariable, varRefs);
                            if (varRefs.size() == 1 && varRefs.get(0) == operands[op] &&
                                    !ExpressionTool.dependsOnFocus(operands[1-op]) &&
                                    !ExpressionTool.dependsOnVariable(operands[1-op], thisVar)) {
                                FunctionCall position =
                                        SystemFunction.makeSystemFunction("position", SimpleExpression.NO_ARGUMENTS);
                                Expression predicate;
                                if (term instanceof ValueComparison) {
                                    if (op==0) {
                                        predicate = new ValueComparison(position, comp.getOperator(), operands[1]);
                                    } else {
                                        predicate = new ValueComparison(operands[0], comp.getOperator(), position);
                                    }
                                } else { // term instanceof SingletonComparison
                                    boolean checkTypes = ((SingletonComparison)term).needsRuntimeComparabilityCheck();
                                    if (op==0) {
                                        predicate = new SingletonComparison(
                                                position, comp.getOperator(), operands[1], checkTypes);
                                    } else {
                                        predicate = new SingletonComparison(
                                                operands[0], comp.getOperator(), position, checkTypes);
                                    }
                                }
                                selection = new FilterExpression(selection, predicate);
                                ExpressionTool.copyLocationInfo(this, selection);
                                selection = visitor.typeCheck(selection, selectionContextItemType);
                                positionVariable = null;
                                list.remove(t);
                                changed = true;
                                break;
                            }
                        }
                    }
                }

                if (positionVariable == null) {
                    Binding[] thisVar = {this};
                    if (opt.isVariableReplaceableByDot(term, thisVar) && !ExpressionTool.dependsOnFocus(term)) {
                        boolean useDotDirectly = opt.isVariableReplaceableByDot(term, thisVar);
                        Expression replacement;
                        // When rewriting the where expression as a filter, we have to replace references to the
                        // range variable by references to the context item. If we can do this directly, we do. But
                        // if the reference to the range variable occurs inside a predicate, or on the rhs of slash,
                        // we have to bind a new variable to the context item. So for example "for $x in S where
                        // T[abc = $x]" gets rewritten as "for $x in S[let $dot := . return T[abc = $dot]]"
                        if (useDotDirectly) {
                            replacement = new ContextItemExpression();
                        } else {
                            LetExpression let = new LetExpression();
                            let.setVariableQName(
                                    new StructuredQName("saxon", NamespaceConstant.SAXON, "dot" + hashCode()));
                            let.setRequiredType(SequenceType.makeSequenceType(contextItemType, StaticProperty.EXACTLY_ONE));
                            let.setSequence(new ContextItemExpression());
                            let.setAction(term);
                            term = let;
                            replacement = new VariableReference(let);
                        }

                        PromotionOffer offer = new PromotionOffer(visitor.getConfiguration().getOptimizer());
                        offer.action = PromotionOffer.INLINE_VARIABLE_REFERENCES;
                        offer.bindingList = thisVar;
                        offer.containingExpression = replacement;
                        Expression newTerm = term.promote(offer, this);
                        if (newTerm != null && offer.accepted) {
                            Expression predicate = visitor.typeCheck(newTerm, sequence.getItemType(th));
                            // If the result of the predicate might be a number, wrap it in a call of boolean()
                            int rel = th.relationship(predicate.getItemType(th), BuiltInAtomicType.INTEGER);
                            if (rel != TypeHierarchy.DISJOINT) {
                                predicate = SystemFunction.makeSystemFunction("boolean", new Expression[]{predicate});
                            }
                            selection = new FilterExpression(selection, predicate);
                            ExpressionTool.copyLocationInfo(this, selection);
View Full Code Here

    * is statically known and constant, in which case it is the actual type of the value.
    */

    public SequenceType getRequiredType() {
        // System.err.println("Get required type of $" + getVariableName());
        final TypeHierarchy th = getConfiguration().getTypeHierarchy();
        SequenceType defaultType = (requiredType==null ? SequenceType.ANY_SEQUENCE : requiredType);
        if (assignable) {
            return defaultType;
        } else if (requiredType != null) {
            return requiredType;
View Full Code Here

        Executable exec = visitor.getExecutable();
        if (exec == null) {
            // happens during use-when evaluation
            return;
        }
        TypeHierarchy th = exec.getConfiguration().getTypeHierarchy();
        ItemType oldItemType = getItemType(th);
        ItemType newItemType = oldItemType;
        if (th.isSubType(type, oldItemType)) {
            newItemType = type;
        }
        int newcard = cardinality & getCardinality();
        if (newcard==0) {
            // this will probably lead to a type error later
View Full Code Here

    public boolean isId() {
        if (getFingerprint() == StandardNames.XML_ID) {
            return true;
        }
        TypeHierarchy th = getConfiguration().getTypeHierarchy();
        return th.isIdCode(getTypeAnnotation());
    }
View Full Code Here

     * Determine whether this node has the is-idref property
     * @return true if the node is an IDREF or IDREFS element or attribute
     */

    public boolean isIdref() {
        TypeHierarchy th = getConfiguration().getTypeHierarchy();
        return th.isIdrefsCode(getTypeAnnotation());
    }
View Full Code Here

    * Type-check the expression
    */

    public Expression typeCheck(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException {

        final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();

        // The order of events is critical here. First we ensure that the type of the
        // sequence expression is established. This is used to establish the type of the variable,
        // which in turn is required when type-checking the action part.

View Full Code Here

     * @throws XPathException if compile-time errors are found.
     */

    public GlobalVariable compile(Executable exec, int slot) throws XPathException {

        TypeHierarchy th = exec.getConfiguration().getTypeHierarchy();
        GlobalVariable var;
        if (isParameter) {
            var = new GlobalParam();
            var.setExecutable(exec);
            var.setRequiredParam(value==null);
        } else {
            var = new GlobalVariable();
            var.setExecutable(exec);
        }

        var.setSelectExpression(value);
        var.setRequiredType(requiredType);
        var.setVariableQName(variableName);
        var.setSlotNumber(slot);

        int loc = exec.getLocationMap().allocateLocationId(systemId, lineNumber);
        var.setLocationId(loc);
        var.setContainer(var);

        final SequenceType type = getRequiredType();
        Iterator iter = references.iterator();
        while (iter.hasNext()) {
            BindingReference ref = (BindingReference)iter.next();
            ref.fixup(var);
            Value constantValue = null;
            int properties = 0;
            Expression select = value;
            if (select instanceof Literal && !isParameter) {
                // we can't rely on the constant value because it hasn't yet been type-checked,
                // which could change it (eg by numeric promotion). Rather than attempt all the type-checking
                // now, we do a quick check. See test bug64
                int relation = th.relationship(select.getItemType(th), type.getPrimaryType());
                if (relation == TypeHierarchy.SAME_TYPE || relation == TypeHierarchy.SUBSUMED_BY) {
                    constantValue = ((Literal)select).getValue();
                }
            }
            if (select != null) {
View Full Code Here

            }

            if (var.getRequiredType() == SequenceType.ANY_SEQUENCE && !(var instanceof GlobalParam)) {
                // no type was declared; try to deduce a type from the value
                try {
                    final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
                    final ItemType itemType = value.getItemType(th);
                    final int cardinality = value.getCardinality();
                    var.setRequiredType(SequenceType.makeSequenceType(itemType, cardinality));
                    Value constantValue = null;
                    if (value2 instanceof Literal) {
View Full Code Here

     * Arithmetic: anyAtomicType - AnyAtomicType
     */

    private static class AnyMinusAny extends Calculator {
        public AtomicValue compute(AtomicValue a, AtomicValue b, XPathContext c) throws XPathException {
            TypeHierarchy th = c.getConfiguration().getTypeHierarchy();
            Calculator calc = getCalculator(
                    a.getItemType(th).getPrimitiveType(), b.getItemType(th).getPrimitiveType(), MINUS, true);
            if (calc == null) {
                throw new XPathException("Unsuitable types for - operation (" +
                        Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ")", "XPTY0004", c);
View Full Code Here

TOP

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

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.