| 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.instruct.ForEachGroup; |
| 12 | import net.sf.saxon.expr.parser.RebindingMap; |
| 13 | import net.sf.saxon.expr.sort.GroupIterator; |
| 14 | import net.sf.saxon.om.*; |
| 15 | import net.sf.saxon.trace.ExpressionPresenter; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.type.AnyItemType; |
| 18 | import net.sf.saxon.type.ItemType; |
| 19 | import net.sf.saxon.value.AtomicValue; |
| 20 | |
| 21 | /** |
| 22 | * Implements the XSLT function current-group() |
| 23 | */ |
| 24 | |
| 25 | public class CurrentGroupCall extends Expression implements Callable { |
| 26 | |
| 27 | private boolean isInLoop = false; |
| 28 | private ItemType itemType = AnyItemType.getInstance(); |
| 29 | private ForEachGroup controllingInstruction = null; // may be unknown, when current group has dynamic scope |
| 30 | |
| 31 | /** |
| 32 | * Get the innermost scoping expression of this expression, for expressions that directly |
| 33 | * depend on something in the dynamic context. For example, in the case of a local variable |
| 34 | * reference this is the expression that causes the relevant variable to be bound; for a |
| 35 | * context item expression it is the innermost focus-setting container. For expressions |
| 36 | * that have no intrinsic dependency on the dynamic context, the value returned is null; |
| 37 | * the scoping container for such an expression is the innermost scoping container of its |
| 38 | * operands. |
| 39 | * |
| 40 | * @return the innermost scoping container of this expression |
| 41 | */ |
| 42 | @Override |
| 43 | public Expression getScopingExpression() { |
| 44 | return getControllingInstruction(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Set the containing xsl:for-each-group instruction, if there is one |
| 49 | * @param instruction the (innermost) containing xsl:for-each-group instruction |
| 50 | * @param itemType the statically inferred item type of the grouping population |
| 51 | * @param isInLoop true if the current-group() expression is evaluated more than once during |
| 52 | * evaluation of the body of the for-each-group instruction |
| 53 | */ |
| 54 | |
| 55 | public void setControllingInstruction(ForEachGroup instruction, ItemType itemType, boolean isInLoop) { |
| 56 | resetLocalStaticProperties(); |
| 57 | this.controllingInstruction = instruction; |
| 58 | this.isInLoop = isInLoop; |
| 59 | this.itemType = itemType; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void resetLocalStaticProperties() { |
| 64 | super.resetLocalStaticProperties(); |
| 65 | this.controllingInstruction = null; |
| 66 | this.itemType = AnyItemType.getInstance(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the innermost containing xsl:for-each-group instruction, if there is one |
| 71 | * @return the innermost containing xsl:for-each-group instruction |
| 72 | */ |
| 73 | |
| 74 | public ForEachGroup getControllingInstruction() { |
| 75 | if (controllingInstruction == null) { |
| 76 | controllingInstruction = findControllingInstruction(this); |
| 77 | } |
| 78 | return controllingInstruction; |
| 79 | } |
| 80 | |
| 81 | public static ForEachGroup findControllingInstruction(Expression exp) { |
| 82 | Expression child = exp; |
| 83 | Expression parent = exp.getParentExpression(); |
| 84 | while (parent != null) { |
| 85 | if (parent instanceof ForEachGroup && |
| 86 | (child == ((ForEachGroup) parent).getActionExpression() || child == ((ForEachGroup) parent).getSortKeyDefinitionList())) { |
| 87 | return (ForEachGroup)parent; |
| 88 | } |
| 89 | child = parent; |
| 90 | parent = parent.getParentExpression(); |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Determine whether the current-group() function is executed repeatedly within a single iteration |
| 97 | * of the containing xsl:for-each-group |
| 98 | * @return true if it is evaluated repeatedly |
| 99 | */ |
| 100 | |
| 101 | public boolean isInLoop() { |
| 102 | return isInLoop; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Determine the item type of the value returned by the function |
| 107 | */ |
| 108 | |
| 109 | @Override |
| 110 | public ItemType getItemType() { |
| 111 | if (itemType == AnyItemType.getInstance() && controllingInstruction != null) { |
| 112 | itemType = controllingInstruction.getSelectExpression().getItemType(); |
| 113 | } |
| 114 | return itemType; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Determine the dependencies |
| 119 | */ |
| 120 | |
| 121 | public int getIntrinsicDependencies() { |
| 122 | return StaticProperty.DEPENDS_ON_CURRENT_GROUP; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Compute the static cardinality of this expression |
| 127 | * |
| 128 | * @return the computed cardinality, as one of the values {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_ONE}, |
| 129 | * {@link net.sf.saxon.expr.StaticProperty#EXACTLY_ONE}, {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ONE_OR_MORE}, |
| 130 | * {@link net.sf.saxon.expr.StaticProperty#ALLOWS_ZERO_OR_MORE} |
| 131 | */ |
| 132 | @Override |
| 133 | protected int computeCardinality() { |
| 134 | return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process(). |
| 139 | * This method indicates which of these methods is provided directly. The other methods will always be available |
| 140 | * indirectly, using an implementation that relies on one of the other methods. |
| 141 | * |
| 142 | * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or |
| 143 | * {@link #PROCESS_METHOD} |
| 144 | */ |
| 145 | @Override |
| 146 | public int getImplementationMethod() { |
| 147 | return ITERATE_METHOD; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Diagnostic print of expression structure. The abstract expression tree |
| 152 | * is written to the supplied output destination. |
| 153 | * |
| 154 | * @param out the expression presenter used to display the structure |
| 155 | */ |
| 156 | @Override |
| 157 | public void export(ExpressionPresenter out) throws XPathException { |
| 158 | out.startElement("currentGroup"); |
| 159 | out.endElement(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Determine the special properties of this expression. The properties such as document-ordering are the same as |
| 164 | * the properties of the grouping population as a whole. |
| 165 | * |
| 166 | * @return {@link net.sf.saxon.expr.StaticProperty#NO_NODES_NEWLY_CREATED} (unless the variable is assignable using saxon:assign) |
| 167 | */ |
| 168 | @Override |
| 169 | public int computeSpecialProperties() { |
| 170 | if (getControllingInstruction() == null) { |
| 171 | return 0; |
| 172 | } else { |
| 173 | return controllingInstruction.getSelectExpression().getSpecialProperties(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Expression copy(RebindingMap rebindings) { |
| 179 | CurrentGroupCall cg = new CurrentGroupCall(); |
| 180 | cg.isInLoop = isInLoop; |
| 181 | cg.itemType = itemType; |
| 182 | cg.controllingInstruction = controllingInstruction; |
| 183 | return cg; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Return an iteration over the result sequence |
| 188 | */ |
| 189 | |
| 190 | /*@NotNull*/ |
| 191 | public SequenceIterator<?> iterate(XPathContext c) throws XPathException { |
| 192 | GroupIterator gi = c.getCurrentGroupIterator(); |
| 193 | if (gi == null) { |
| 194 | XPathException err = new XPathException("There is no current group", "XTDE1061"); |
| 195 | err.setLocation(getLocation()); |
| 196 | throw err; |
| 197 | } |
| 198 | return gi.iterateCurrentGroup(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Call the Callable (used from generated bytecode). |
| 203 | * |
| 204 | * @param context the dynamic evaluation context |
| 205 | * @param arguments the values of the arguments, supplied as Sequences. |
| 206 | * <p>Generally it is advisable, if calling iterate() to process a supplied sequence, to |
| 207 | * call it only once; if the value is required more than once, it should first be converted |
| 208 | * to a {@link GroundedValue} by calling the utility method |
| 209 | * SequenceTool.toGroundedValue().</p> |
| 210 | * <p>If the expected value is a single item, the item should be obtained by calling |
| 211 | * Sequence.head(): it cannot be assumed that the item will be passed as an instance of |
| 212 | * {@link Item} or {@link AtomicValue}.</p> |
| 213 | * <p>It is the caller's responsibility to perform any type conversions required |
| 214 | * to convert arguments to the type expected by the callee. An exception is where |
| 215 | * this Callable is explicitly an argument-converting wrapper around the original |
| 216 | * Callable.</p> |
| 217 | * @return the result of the evaluation, in the form of a Sequence. It is the responsibility |
| 218 | * of the callee to ensure that the type of result conforms to the expected result type. |
| 219 | * @throws XPathException if a dynamic error occurs during the evaluation of the expression |
| 220 | */ |
| 221 | public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 222 | return SequenceTool.toLazySequence(iterate(context)); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * <p>The toString() method for an expression attempts to give a representation of the expression |
| 227 | * in an XPath-like form.</p> |
| 228 | * <p>For subclasses of Expression that represent XPath expressions, the result should always be a string that |
| 229 | * parses as an XPath 3.0 expression. The expression produced should be equivalent to the original making certain |
| 230 | * assumptions about the static context. In general the expansion will make no assumptions about namespace bindings, |
| 231 | * except that (a) the prefix "xs" is used to refer to the XML Schema namespace, and (b) the default funcion namespace |
| 232 | * is assumed to be the "fn" namespace.</p> |
| 233 | * <p>In the case of XSLT instructions and XQuery expressions, the toString() method gives an abstracted view of the syntax |
| 234 | * that is not designed in general to be parseable.</p> |
| 235 | * |
| 236 | * @return a representation of the expression as a string |
| 237 | */ |
| 238 | @Override |
| 239 | public String toString() { |
| 240 | return "current-group()"; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Produce a short string identifying the expression for use in error messages |
| 245 | * |
| 246 | * @return a short string, sufficient to identify the expression |
| 247 | */ |
| 248 | @Override |
| 249 | public String toShortString() { |
| 250 | return toString(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Get the (partial) name of a class that supports streaming of this kind of expression |
| 255 | * |
| 256 | * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE, |
| 257 | * or null if there is no such class |
| 258 | */ |
| 259 | @Override |
| 260 | public String getStreamerName() { |
| 261 | return "CurrentGroup"; |
| 262 | } |
| 263 | |
| 264 | } |
| 265 |