| 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.event.ReceiverOptions; |
| 11 | import net.sf.saxon.expr.oper.OperandArray; |
| 12 | import net.sf.saxon.expr.parser.RebindingMap; |
| 13 | import net.sf.saxon.om.*; |
| 14 | import net.sf.saxon.trace.ExpressionPresenter; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.ItemType; |
| 17 | import net.sf.saxon.type.Type; |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * An abstract implementation of Expression designed to make it easy to implement new expressions, |
| 24 | * in particular, expressions to support extension instructions. |
| 25 | * <p>An implementation of this class must supply the {@link Callable#call(XPathContext, net.sf.saxon.om.Sequence[])} |
| 26 | * method to evaluate the expression and return its result.</p> |
| 27 | */ |
| 28 | |
| 29 | public abstract class SimpleExpression extends Expression implements Callable { |
| 30 | |
| 31 | private OperandArray operanda; |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | */ |
| 36 | |
| 37 | public SimpleExpression() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Set the data structure for the operands of this expression. This must be created during initialisation of the |
| 42 | * expression and must not be subsequently changed |
| 43 | * @param operanda the data structure for expression operands |
| 44 | */ |
| 45 | |
| 46 | protected void setOperanda(OperandArray operanda) { |
| 47 | this.operanda = operanda; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the data structure holding the operands of this expression. |
| 52 | * @return the data structure holding expression operands |
| 53 | */ |
| 54 | |
| 55 | protected OperandArray getOperanda() { |
| 56 | return operanda; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Iterable<Operand> operands() { |
| 61 | return operanda.operands(); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Set the immediate sub-expressions of this expression. |
| 67 | * |
| 68 | * @param sub an array containing the sub-expressions of this expression |
| 69 | */ |
| 70 | |
| 71 | public void setArguments(Expression[] sub) { |
| 72 | if (getOperanda() != null && getOperanda().getNumberOfOperands() > 0) { |
| 73 | throw new IllegalArgumentException("Cannot replace existing argument array"); |
| 74 | } |
| 75 | Expression[] sub2 = Arrays.copyOf(sub, sub.length); |
| 76 | OperandRole[] roles = new OperandRole[sub.length]; |
| 77 | Arrays.fill(roles, OperandRole.NAVIGATE); |
| 78 | setOperanda(new OperandArray(this, sub2, roles)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Copy an expression. This makes a deep copy. |
| 83 | * |
| 84 | * @return the copy of the original expression |
| 85 | * @param rebindings variables that need to be re-bound |
| 86 | */ |
| 87 | |
| 88 | /*@NotNull*/ |
| 89 | public Expression copy(RebindingMap rebindings) { |
| 90 | |
| 91 | try { |
| 92 | SimpleExpression se2 = getClass().newInstance(); |
| 93 | Expression[] a2 = new Expression[operanda.getNumberOfOperands()]; |
| 94 | int i = 0; |
| 95 | for (Operand o : operands()) { |
| 96 | a2[i++] = o.getChildExpression().copy(rebindings); |
| 97 | } |
| 98 | OperandArray o2 = new OperandArray(se2, a2, operanda.getRoles()); |
| 99 | se2.setOperanda(o2); |
| 100 | return se2; |
| 101 | } catch (InstantiationException | IllegalAccessException e) { |
| 102 | throw new UnsupportedOperationException(getClass().getName() + ".copy()"); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Helper method for subclasses to implement the copy() operation: this method can be applied |
| 108 | * to the new expression to copy operands from the old expressions |
| 109 | * @param se1 the expression being copied |
| 110 | * @return the target object of this method (for convenience) |
| 111 | */ |
| 112 | |
| 113 | protected SimpleExpression copyOperandsFrom(SimpleExpression se1) { |
| 114 | Expression[] a2 = new Expression[se1.operanda.getNumberOfOperands()]; |
| 115 | int i = 0; |
| 116 | for (Operand o : se1.operands()) { |
| 117 | a2[i++] = o.getChildExpression().copy(new RebindingMap()); |
| 118 | } |
| 119 | OperandArray o2 = new OperandArray(this, a2, se1.operanda.getRoles()); |
| 120 | setOperanda(o2); |
| 121 | return this; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Determine the data type of the items returned by this expression. This implementation |
| 126 | * returns "item()", which can be overridden in a subclass. |
| 127 | * |
| 128 | * @return the data type |
| 129 | */ |
| 130 | |
| 131 | /*@NotNull*/ |
| 132 | public ItemType getItemType() { |
| 133 | return Type.ITEM_TYPE; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Determine the static cardinality of the expression. This implementation |
| 138 | * returns "zero or more", which can be overridden in a subclass. |
| 139 | */ |
| 140 | |
| 141 | public int computeCardinality() { |
| 142 | if ((getImplementationMethod() & Expression.EVALUATE_METHOD) == 0) { |
| 143 | return StaticProperty.ALLOWS_ONE_OR_MORE; |
| 144 | } else { |
| 145 | return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Evaluate an expression as a single item. This always returns either a single Item or |
| 151 | * null (denoting the empty sequence). No conversion is done. This method should not be |
| 152 | * used unless the static type of the expression is a subtype of "item" or "item?": that is, |
| 153 | * it should not be called if the expression may return a sequence. There is no guarantee that |
| 154 | * this condition will be detected. |
| 155 | * |
| 156 | * @param context The context in which the expression is to be evaluated |
| 157 | * @return the node or atomic value that results from evaluating the |
| 158 | * expression; or null to indicate that the result is an empty |
| 159 | * sequence |
| 160 | * @throws XPathException if any dynamic error occurs evaluating the |
| 161 | * expression |
| 162 | */ |
| 163 | |
| 164 | public final Item evaluateItem(XPathContext context) throws XPathException { |
| 165 | return call(context, evaluateArguments(context)).head(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Return an Iterator to iterate over the values of a sequence. The value of every |
| 170 | * expression can be regarded as a sequence, so this method is supported for all |
| 171 | * expressions. This default implementation handles iteration for expressions that |
| 172 | * return singleton values: for non-singleton expressions, the subclass must |
| 173 | * provide its own implementation. |
| 174 | * |
| 175 | * @param context supplies the context for evaluation |
| 176 | * @return a SequenceIterator that can be used to iterate over the result |
| 177 | * of the expression |
| 178 | * @throws XPathException if any dynamic error occurs evaluating the |
| 179 | * expression |
| 180 | */ |
| 181 | |
| 182 | /*@NotNull*/ |
| 183 | public final SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 184 | return call(context, evaluateArguments(context)).iterate(); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Process the instruction, without returning any tail calls |
| 189 | * |
| 190 | * @param context The dynamic context, giving access to the current node, |
| 191 | * the current variables, etc. |
| 192 | */ |
| 193 | |
| 194 | public final void process(XPathContext context) throws XPathException { |
| 195 | SequenceIterator<?> iter = call(context, evaluateArguments(context)).iterate(); |
| 196 | iter.forEachOrFail( |
| 197 | it -> context.getReceiver().append(it, getLocation(), ReceiverOptions.ALL_NAMESPACES) |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Internal method to evaluate the arguments prior to calling the generic call() method |
| 203 | * |
| 204 | * @param context the XPath dynamic context |
| 205 | * @return the values of the (evaluated) arguments |
| 206 | * @throws XPathException if a dynamic error occurs |
| 207 | */ |
| 208 | |
| 209 | private Sequence<?>[] evaluateArguments(XPathContext context) throws XPathException { |
| 210 | Sequence<?>[] iters = |
| 211 | SequenceTool.makeSequenceArray(getOperanda().getNumberOfOperands()); |
| 212 | int i=0; |
| 213 | for (Operand o : operands()) { |
| 214 | iters[i++] = SequenceTool.toLazySequence(o.getChildExpression().iterate(context)); |
| 215 | } |
| 216 | return iters; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Diagnostic print of expression structure. The abstract expression tree |
| 221 | * is written to the supplied output destination. |
| 222 | */ |
| 223 | |
| 224 | public void export(ExpressionPresenter destination) throws XPathException { |
| 225 | throw new XPathException("In general, stylesheets using extension instructions cannot be exported"); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Return a distinguishing name for the expression, for use in diagnostics. |
| 230 | * By default the class name is used. |
| 231 | * |
| 232 | * @return a distinguishing name for the expression (defaults to the name of the implementation class) |
| 233 | */ |
| 234 | |
| 235 | public String getExpressionType() { |
| 236 | return getClass().getName(); |
| 237 | } |
| 238 | |
| 239 | } |
| 240 |