| 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.instruct; |
| 9 | |
| 10 | import net.sf.saxon.expr.ContextItemExpression; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.expr.Operand; |
| 13 | import net.sf.saxon.expr.StaticProperty; |
| 14 | import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 15 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 16 | import net.sf.saxon.expr.parser.PathMap; |
| 17 | import net.sf.saxon.trans.XPathException; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * An xsl:apply-imports or xsl:next-match element in the stylesheet. |
| 25 | */ |
| 26 | |
| 27 | public abstract class ApplyNextMatchingTemplate extends Instruction implements ITemplateCall { |
| 28 | |
| 29 | private WithParam[] actualParams; |
| 30 | private WithParam[] tunnelParams; |
| 31 | |
| 32 | public ApplyNextMatchingTemplate() { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 37 | * This method indicates which of these methods is prefered. For instructions this is the process() method. |
| 38 | */ |
| 39 | |
| 40 | public int getImplementationMethod() { |
| 41 | return super.getImplementationMethod() | Expression.WATCH_METHOD; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Get the actual parameters passed to the called template |
| 47 | * |
| 48 | * @return the non-tunnel parameters |
| 49 | */ |
| 50 | |
| 51 | |
| 52 | public WithParam[] getActualParams() { |
| 53 | return actualParams; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the tunnel parameters passed to the called template |
| 58 | * |
| 59 | * @return the tunnel parameters |
| 60 | */ |
| 61 | |
| 62 | |
| 63 | public WithParam[] getTunnelParams() { |
| 64 | return tunnelParams; |
| 65 | } |
| 66 | |
| 67 | public void setActualParams(WithParam[] params) { |
| 68 | this.actualParams = params; |
| 69 | } |
| 70 | |
| 71 | public void setTunnelParams(WithParam[] params) { |
| 72 | this.tunnelParams = params; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Iterable<Operand> operands() { |
| 77 | List<Operand> operanda = new ArrayList<Operand>(actualParams.length + tunnelParams.length); |
| 78 | WithParam.gatherOperands(this, actualParams, operanda); |
| 79 | WithParam.gatherOperands(this, tunnelParams, operanda); |
| 80 | return operanda; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Simplify an expression. This performs any static optimization (by rewriting the expression |
| 85 | * as a different expression). |
| 86 | * |
| 87 | * |
| 88 | * |
| 89 | * @return the simplified expression |
| 90 | * @throws XPathException |
| 91 | * if an error is discovered during expression |
| 92 | * rewriting |
| 93 | */ |
| 94 | |
| 95 | /*@NotNull*/ |
| 96 | public Expression simplify() throws XPathException { |
| 97 | WithParam.simplify(getActualParams()); |
| 98 | WithParam.simplify(getTunnelParams()); |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | /*@NotNull*/ |
| 103 | public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 104 | WithParam.typeCheck(actualParams, visitor, contextInfo); |
| 105 | WithParam.typeCheck(tunnelParams, visitor, contextInfo); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | /*@NotNull*/ |
| 110 | public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 111 | WithParam.optimize(visitor, actualParams, contextInfo); |
| 112 | WithParam.optimize(visitor, tunnelParams, contextInfo); |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | public int getIntrinsicDependencies() { |
| 117 | return StaticProperty.DEPENDS_ON_CONTEXT_ITEM; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Determine whether this instruction creates new nodes. |
| 122 | * This implementation returns true (which is almost invariably the case, so it's not worth |
| 123 | * doing any further analysis to find out more precisely). |
| 124 | */ |
| 125 | |
| 126 | public final boolean mayCreateNewNodes() { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited |
| 132 | * by an expression in a source tree. |
| 133 | * <p>The default implementation of this method assumes that an expression does no navigation other than |
| 134 | * the navigation done by evaluating its subexpressions, and that the subexpressions are evaluated in the |
| 135 | * same context as the containing expression. The method must be overridden for any expression |
| 136 | * where these assumptions do not hold. For example, implementations exist for AxisExpression, ParentExpression, |
| 137 | * and RootExpression (because they perform navigation), and for the doc(), document(), and collection() |
| 138 | * functions because they create a new navigation root. Implementations also exist for PathExpression and |
| 139 | * FilterExpression because they have subexpressions that are evaluated in a different context from the |
| 140 | * calling expression.</p> |
| 141 | * |
| 142 | * @param pathMap the PathMap to which the expression should be added |
| 143 | * @param pathMapNodeSet the PathMapNodeSet to which the paths embodied in this expression should be added |
| 144 | * @return the pathMapNodeSet representing the points in the source document that are both reachable by this |
| 145 | * expression, and that represent possible results of this expression. For an expression that does |
| 146 | * navigation, it represents the end of the arc in the path map that describes the navigation route. For other |
| 147 | * expressions, it is the same as the input pathMapNode. |
| 148 | */ |
| 149 | |
| 150 | public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 151 | // This logic is assuming the mode is streamable (so called templates can't return streamed nodes) |
| 152 | //PathMap.PathMapNodeSet result = super.addToPathMap(pathMap, pathMapNodeSet); |
| 153 | //result.setReturnable(false); |
| 154 | if (pathMapNodeSet == null) { |
| 155 | ContextItemExpression cie = new ContextItemExpression(); |
| 156 | //cie.setContainer(getContainer()); |
| 157 | pathMapNodeSet = new PathMap.PathMapNodeSet(pathMap.makeNewRoot(cie)); |
| 158 | } |
| 159 | pathMapNodeSet.addDescendants(); |
| 160 | return new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this)); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | } |
| 165 |