| 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.functions; |
| 9 | |
| 10 | import net.sf.saxon.expr.*; |
| 11 | import net.sf.saxon.expr.parser.RebindingMap; |
| 12 | import net.sf.saxon.expr.sort.GroupIterator; |
| 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.BuiltInAtomicType; |
| 17 | import net.sf.saxon.type.ItemType; |
| 18 | import net.sf.saxon.value.AtomicValue; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * Implements the XSLT function current-grouping-key() |
| 23 | */ |
| 24 | |
| 25 | public class CurrentGroupingKeyCall extends Expression implements Callable { |
| 26 | |
| 27 | /** |
| 28 | * Get the innermost scoping expression of this expression, for expressions that directly |
| 29 | * depend on something in the dynamic context. For example, in the case of a local variable |
| 30 | * reference this is the expression that causes the relevant variable to be bound; for a |
| 31 | * context item expression it is the innermost focus-setting container. For expressions |
| 32 | * that have no intrinsic dependency on the dynamic context, the value returned is null; |
| 33 | * the scoping container for such an expression is the innermost scoping container of its |
| 34 | * operands. |
| 35 | * |
| 36 | * @return the innermost scoping container of this expression |
| 37 | */ |
| 38 | @Override |
| 39 | public Expression getScopingExpression() { |
| 40 | return CurrentGroupCall.findControllingInstruction(this); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Compute the static cardinality of this expression |
| 45 | * |
| 46 | * @return the computed cardinality, as one of the values {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_ONE}, |
| 47 | * {@link net.sf.saxon.expr.StaticProperty#EXACTLY_ONE}, {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ONE_OR_MORE}, |
| 48 | * {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_MORE} |
| 49 | */ |
| 50 | @Override |
| 51 | protected int computeCardinality() { |
| 52 | // Can return an empty sequence in 2.0 mode |
| 53 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 58 | * This method indicates which of these methods is provided directly. The other methods will always be available |
| 59 | * indirectly, using an implementation that relies on one of the other methods. |
| 60 | * |
| 61 | * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or |
| 62 | * {@link #PROCESS_METHOD} |
| 63 | */ |
| 64 | @Override |
| 65 | public int getImplementationMethod() { |
| 66 | return ITERATE_METHOD; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Determine the data type of the expression, if possible. All expression return |
| 71 | * sequences, in general; this method determines the type of the items within the |
| 72 | * sequence, assuming that (a) this is known in advance, and (b) it is the same for |
| 73 | * all items in the sequence. |
| 74 | * <p>This method should always return a result, though it may be the best approximation |
| 75 | * that is available at the time.</p> |
| 76 | * |
| 77 | * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER, |
| 78 | * Type.NODE, or Type.ITEM (meaning not known at compile time) |
| 79 | */ |
| 80 | @Override |
| 81 | public ItemType getItemType() { |
| 82 | return BuiltInAtomicType.ANY_ATOMIC; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Diagnostic print of expression structure. The abstract expression tree |
| 87 | * is written to the supplied output destination. |
| 88 | * |
| 89 | * @param out the expression presenter used to display the structure |
| 90 | */ |
| 91 | @Override |
| 92 | public void export(ExpressionPresenter out) throws XPathException { |
| 93 | out.startElement("currentGroupingKey"); |
| 94 | out.endElement(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Copy an expression. This makes a deep copy. |
| 99 | * |
| 100 | * @return the copy of the original expression |
| 101 | * @param rebindings |
| 102 | */ |
| 103 | @Override |
| 104 | public Expression copy(RebindingMap rebindings) { |
| 105 | return new CurrentGroupingKeyCall(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Determine the dependencies |
| 110 | */ |
| 111 | |
| 112 | public int getIntrinsicDependencies() { |
| 113 | return StaticProperty.DEPENDS_ON_CURRENT_GROUP; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Evaluate the expression |
| 118 | */ |
| 119 | |
| 120 | /*@NotNull*/ |
| 121 | @Override |
| 122 | public SequenceIterator<?> iterate(XPathContext c) throws XPathException { |
| 123 | GroupIterator gi = c.getCurrentGroupIterator(); |
| 124 | AtomicSequence result = gi==null ? null : gi.getCurrentGroupingKey(); |
| 125 | if (result == null) { |
| 126 | XPathException err = new XPathException("There is no current grouping key", "XTDE1071"); |
| 127 | err.setLocation(getLocation()); |
| 128 | throw err; |
| 129 | } |
| 130 | return result.iterate(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Call the Callable. |
| 135 | * |
| 136 | * @param context the dynamic evaluation context |
| 137 | * @param arguments the values of the arguments, supplied as Sequences. |
| 138 | * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to |
| 139 | * call it only once; if the value is required more than once, it should first be converted |
| 140 | * to a {@link GroundedValue} by calling the utility method |
| 141 | * SequenceTool.toGroundedValue().</p> |
| 142 | * <p>If the expected value is a single item, the item should be obtained by calling |
| 143 | * Sequence.head(): it cannot be assumed that the item will be passed as an instance of |
| 144 | * {@link Item} or {@link AtomicValue}.</p> |
| 145 | * <p>It is the caller's responsibility to perform any type conversions required |
| 146 | * to convert arguments to the type expected by the callee. An exception is where |
| 147 | * this Callable is explicitly an argument-converting wrapper around the original |
| 148 | * Callable.</p> |
| 149 | * @return the result of the evaluation, in the form of a Sequence. It is the responsibility |
| 150 | * of the callee to ensure that the type of result conforms to the expected result type. |
| 151 | * @throws XPathException if a dynamic error occurs during the evaluation of the expression |
| 152 | */ |
| 153 | public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 154 | return SequenceTool.toLazySequence(iterate(context)); |
| 155 | } |
| 156 | } |
| 157 |