Debian Codegraph @

net/sf/saxon/expr/instruct/NextMatch.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.expr.instruct;
9
10 import net.sf.saxon.Controller;
11 import net.sf.saxon.expr.Component;
12 import net.sf.saxon.expr.Expression;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.XPathContextMajor;
15 import net.sf.saxon.expr.parser.ExpressionTool;
16 import net.sf.saxon.expr.parser.RebindingMap;
17 import net.sf.saxon.om.Item;
18 import net.sf.saxon.om.StandardNames;
19 import net.sf.saxon.trace.ExpressionPresenter;
20 import net.sf.saxon.trans.Mode;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.trans.rules.Rule;
23
24 import java.util.Arrays;
25
26
27 /**
28 * An xsl:next-match element in the stylesheet
29 */
30
31 public class NextMatch extends ApplyNextMatchingTemplate {
32
33 boolean useTailRecursion;
34
35 public NextMatch(boolean useTailRecursion) {
36 super();
37 this.useTailRecursion = useTailRecursion;
38 }
39
40 /**
41 * Get the name of this instruction for diagnostic and tracing purposes
42 */
43
44 public int getInstructionNameCode() {
45 return StandardNames.XSL_NEXT_MATCH;
46 }
47
48 /**
49 * Copy an expression. This makes a deep copy.
50 *
51 * @return the copy of the original expression
52 * @param rebindings
53 */
54
55 /*@NotNull*/
56 public Expression copy(RebindingMap rebindings) {
57 NextMatch nm2 = new NextMatch(useTailRecursion);
58 nm2.setActualParams(WithParam.copy(nm2, getActualParams(), rebindings));
59 nm2.setTunnelParams(WithParam.copy(nm2, getTunnelParams(), rebindings));
60 ExpressionTool.copyLocationInfo(this, nm2);
61 return nm2;
62 }
63
64
65 /*@Nullable*/
66 public TailCall processLeavingTail(XPathContext context) throws XPathException {
67
68 Controller controller = context.getController();
69 assert controller != null;
70
71 // handle parameters if any
72
73 ParameterSet params = assembleParams(context, getActualParams());
74 ParameterSet tunnels = assembleTunnelParams(context, getTunnelParams());
75
76 Rule currentRule = context.getCurrentTemplateRule();
77 if (currentRule == null) {
78 XPathException e = new XPathException("There is no current template rule", "XTDE0560");
79 e.setXPathContext(context);
80 e.setLocation(getLocation());
81 throw e;
82 }
83 Component.M modeComponent = context.getCurrentMode();
84 if (modeComponent == null) {
85 throw new AssertionError("Current mode is null");
86 }
87 Mode mode = modeComponent.getActor();
88
89 Item currentItem = context.getCurrentIterator().current();
90
91 Rule rule = mode.getNextMatchRule(currentItem, currentRule, context);
92 //Rule rule = controller.getRuleManager().getNextMatchHandler(currentItem, mode.getCode(), currentRule, context);
93
94 if (rule == null) { // use the default action for the node
95 mode.getBuiltInRuleSet().process(currentItem, params, tunnels, context, getLocation());
96 } else if (useTailRecursion) {
97 // clear all the local variables: they are no longer needed
98 Arrays.fill(context.getStackFrame().getStackFrameValues(), null);
99 ((XPathContextMajor) context).setCurrentComponent(modeComponent); // bug 2818
100 return new NextMatchPackage(rule, params, tunnels, context);
101 } else {
102 TemplateRule nh = (TemplateRule) rule.getAction();
103 nh.initialize();
104 XPathContextMajor c2 = context.newContext();
105 c2.setOrigin(this);
106 //c2.setOriginatingConstructType(LocationKind.TEMPLATE);
107 c2.openStackFrame(nh.getStackFrameMap());
108 c2.setLocalParameters(params);
109 c2.setTunnelParameters(tunnels);
110 c2.setCurrentTemplateRule(rule);
111 c2.setCurrentComponent(modeComponent); // needed in the case where next-match is called from a named template
112 nh.apply(c2);
113 }
114 return null;
115 }
116
117 /**
118 * Diagnostic print of expression structure. The abstract expression tree
119 * is written to the supplied output destination.
120 */
121
122 public void export(ExpressionPresenter out) throws XPathException {
123 out.startElement("nextMatch", this);
124 String flags = "i";
125 if (useTailRecursion) {
126 flags = "t";
127 }
128 out.emitAttribute("flags", flags);
129 if (getActualParams().length != 0) {
130 WithParam.exportParameters(getActualParams(), out, false);
131 }
132 if (getTunnelParams().length != 0) {
133 WithParam.exportParameters(getTunnelParams(), out, true);
134 }
135 out.endElement();
136 }
137
138 /**
139 * A NextMatchPackage is an object that encapsulates the name of a template to be called,
140 * the parameters to be supplied, and the execution context. This object can be returned as a tail
141 * call, so that the actual call is made from a lower point on the stack, allowing a tail-recursive
142 * template to execute in a finite stack size
143 */
144
145 private class NextMatchPackage implements TailCall {
146
147 private Rule rule;
148 private ParameterSet params;
149 private ParameterSet tunnelParams;
150 private XPathContext evaluationContext;
151
152 /**
153 * Construct a NextMatchPackage that contains information about a call.
154 *
155 * @param rule the rule identifying the Template to be called
156 * @param params the parameters to be supplied to the called template
157 * @param tunnelParams the tunnel parameter supplied to the called template
158 * @param evaluationContext saved context information from the Controller (current mode, etc)
159 * which must be reset to ensure that the template is called with all the context information
160 * intact
161 */
162
163 public NextMatchPackage(Rule rule,
164 ParameterSet params,
165 ParameterSet tunnelParams,
166 XPathContext evaluationContext) {
167 this.rule = rule;
168 this.params = params;
169 this.tunnelParams = tunnelParams;
170 this.evaluationContext = evaluationContext;
171 }
172
173 /**
174 * Process the template call encapsulated by this package.
175 *
176 * @return another TailCall. This will never be the original call, but it may be the next
177 * recursive call. For example, if A calls B which calls C which calls D, then B may return
178 * a TailCall to A representing the call from B to C; when this is processed, the result may be
179 * a TailCall representing the call from C to D.
180 * @throws XPathException if a dynamic error occurs
181 */
182
183 public TailCall processLeavingTail() throws XPathException {
184 TemplateRule nh = (TemplateRule) rule.getAction();
185 nh.initialize();
186 XPathContextMajor c2 = evaluationContext.newContext();
187 c2.setOrigin(NextMatch.this);
188 //c2.setOriginatingConstructType(LocationKind.TEMPLATE);
189 c2.setLocalParameters(params);
190 c2.setTunnelParameters(tunnelParams);
191 c2.openStackFrame(nh.getStackFrameMap());
192 c2.setCurrentTemplateRule(rule);
193 c2.setCurrentComponent(evaluationContext.getCurrentComponent());
194
195 // System.err.println("Tail call on template");
196
197 return nh.applyLeavingTail(c2);
198 }
199 }
200
201 /**
202 * Get the (partial) name of a class that supports streaming of this kind of expression
203 *
204 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
205 * or null if there is no such class
206 */
207 @Override
208 public String getStreamerName() {
209 return "NextMatch";
210 }
211
212
213 }
214