Debian Codegraph @

net/sf/saxon/functions/CurrentMergeGroup.java

raw symbols SCIP
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.Expression;
11 import net.sf.saxon.expr.SystemFunctionCall;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.sort.GroupIterator;
14 import net.sf.saxon.expr.sort.MergeGroupingIterator;
15 import net.sf.saxon.expr.sort.MergeInstr;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.om.SequenceIterator;
18 import net.sf.saxon.om.SequenceTool;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.AnyItemType;
21 import net.sf.saxon.type.ItemType;
22
23 import java.util.HashSet;
24 import java.util.Set;
25
26 /**
27 * Implements the XSLT 3.0 function current-merge-group()
28 */
29
30 public class CurrentMergeGroup extends SystemFunction {
31
32 private boolean isInLoop = false;
33 private MergeInstr controllingInstruction = null; // may be unknown, when current group has dynamic scope
34 private Set<String> allowedNames = new HashSet<>();
35
36 /**
37 * Set the containing xsl:merge instruction, if there is one
38 * @param instruction the (innermost) containing xsl:merge instruction
39 * @param isInLoop true if the current-merge-group() expression is evaluated more than once during
40 * evaluation of the body of the for-each-group instruction
41 */
42
43 public void setControllingInstruction(MergeInstr instruction, boolean isInLoop) {
44 this.controllingInstruction = instruction;
45 this.isInLoop = isInLoop;
46 for (MergeInstr.MergeSource m : instruction.getMergeSources()) {
47 String name = m.sourceName;
48 if (name != null) {
49 allowedNames.add(name);
50 }
51 }
52 }
53
54 /**
55 * Get the innermost containing xsl:merge instruction, if there is one
56 * @return the innermost containing xsl:merge instruction
57 */
58
59 public MergeInstr getControllingInstruction() {
60 return controllingInstruction;
61 }
62
63 /**
64 * Determine whether the current-group() function is executed repeatedly within a single iteration
65 * of the containing xsl:for-each-group
66 * @return true if it is evaluated repeatedly
67 */
68
69 public boolean isInLoop() {
70 return isInLoop;
71 }
72
73 /**
74 * Determine the item type of the value returned by the function
75 */
76
77 @Override
78 public ItemType getResultItemType() {
79 return AnyItemType.getInstance();
80 }
81
82 /**
83 * Determine the special properties of this expression.
84 *
85 * @return {@link net.sf.saxon.expr.StaticProperty#NO_NODES_NEWLY_CREATED} (unless the variable is assignable using saxon:assign)
86 * @param arguments the actual arguments supplied to the function call
87 */
88 @Override
89 public int getSpecialProperties(Expression[] arguments) {
90 return 0;
91 }
92
93 /**
94 * Make an expression that either calls this function, or that is equivalent to a call
95 * on this function
96 *
97 * @param arguments the supplied arguments to the function call
98 * @return either a function call on this function, or an expression that delivers
99 * the same result
100 */
101 @Override
102 public Expression makeFunctionCall(Expression... arguments) {
103 return new SystemFunctionCall(this, arguments) {
104 @Override
105 public Expression getScopingExpression() {
106 return getControllingInstruction();
107 }
108 };
109 }
110
111 /**
112 * Evaluate the expression
113 *
114 * @param context the dynamic evaluation context
115 * @param arguments the values of the arguments, supplied as SequenceIterators
116 * @return the result of the evaluation, in the form of a SequenceIterator
117 * @throws net.sf.saxon.trans.XPathException
118 * if a dynamic error occurs during the evaluation of the expression
119 */
120 public Sequence<?> call(XPathContext context, Sequence[] arguments /*@NotNull*/) throws XPathException {
121 String source = null;
122 if (arguments.length > 0) {
123 source = arguments[0].head().getStringValue();
124 }
125 return SequenceTool.toLazySequence(currentGroup(source, context));
126 }
127
128 /**
129 * Private method to perform the evaluation
130 * @param source the first argument to the function, the name of the merge source; or null if not supplied
131 * @param c the dynamic evaluation context
132 * @return an iterator over the selected items
133 * @throws XPathException if a dynamic error occurs
134 */
135
136 private SequenceIterator<?> currentGroup(String source, XPathContext c) throws XPathException {
137 GroupIterator gi = c.getCurrentMergeGroupIterator();
138 if (gi == null) {
139 throw new XPathException("There is no current merge group", "XTDE3480");
140 }
141 if (source == null) {
142 return gi.iterateCurrentGroup();
143 } else {
144 if (!allowedNames.contains(source)) {
145 throw new XPathException("Supplied argument (" + source +
146 ") is not the name of any xsl:merge-source in the containing xsl:merge instruction", "XTDE3490");
147 }
148 return ((MergeGroupingIterator)gi).iterateCurrentGroup(source);
149 }
150
151 }
152
153 public String getStreamerName() {
154 return "CurrentMergeGroup";
155 }
156
157 }
158
159