| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 11 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.value.Cardinality; |
| 14 | |
| 15 | /** |
| 16 | * A SingleItemFilter is an expression that selects zero or one items from a supplied sequence |
| 17 | */ |
| 18 | |
| 19 | public abstract class SingleItemFilter extends UnaryExpression { |
| 20 | |
| 21 | public SingleItemFilter(Expression base) { |
| 22 | super(base); |
| 23 | } |
| 24 | |
| 25 | protected OperandRole getOperandRole() { |
| 26 | return OperandRole.SAME_FOCUS_ACTION; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Perform optimisation of an expression and its subexpressions. |
| 31 | * <p>This method is called after all references to functions and variables have been resolved |
| 32 | * to the declaration of the function or variable, and after all type checking has been done.</p> |
| 33 | * |
| 34 | * @param visitor an expresion visitor |
| 35 | * @param contextInfo the static type of "." at the point where this expression is invoked. |
| 36 | * The parameter is set to null if it is known statically that the context item will be undefined. |
| 37 | * If the type of the context item is not known statically, the argument is set to |
| 38 | * {@link net.sf.saxon.type.Type#ITEM_TYPE} |
| 39 | * @return the original expression, rewritten if appropriate to optimize execution |
| 40 | * @throws net.sf.saxon.trans.XPathException |
| 41 | * if an error is discovered during this phase |
| 42 | * (typically a type error) |
| 43 | */ |
| 44 | |
| 45 | /*@NotNull*/ |
| 46 | public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 47 | getOperand().optimize(visitor, contextInfo); |
| 48 | Expression base = getBaseExpression(); |
| 49 | if (!Cardinality.allowsMany(base.getCardinality())) { |
| 50 | return base; |
| 51 | } |
| 52 | return super.optimize(visitor, contextInfo); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the static cardinality: this implementation is appropriate for [1] and [last()] which will always |
| 57 | * return something if the input is non-empty |
| 58 | */ |
| 59 | |
| 60 | public int computeCardinality() { |
| 61 | return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 62 | } |
| 63 | |
| 64 | } |
| 65 |