Debian Codegraph @

net/sf/saxon/expr/accum/Accumulator.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.accum;
9
10 import net.sf.saxon.expr.Component;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.instruct.Actor;
14 import net.sf.saxon.expr.instruct.Block;
15 import net.sf.saxon.expr.instruct.SlotManager;
16 import net.sf.saxon.lib.NamespaceConstant;
17 import net.sf.saxon.om.StandardNames;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.trace.ExpressionPresenter;
20 import net.sf.saxon.trace.LocationKind;
21 import net.sf.saxon.trans.*;
22 import net.sf.saxon.trans.rules.Rule;
23 import net.sf.saxon.value.SequenceType;
24
25 import java.util.Map;
26
27 /**
28 * Represents a single accumulator declared in an XSLT 3.0 stylesheet
29 */
30 public class Accumulator extends Actor {
31
32 private StructuredQName accumulatorName;
33
34 private SimpleMode preDescentRules;
35 private SimpleMode postDescentRules;
36
37 private Expression initialValueExpression;
38 private SequenceType type;
39 private boolean streamable;
40 private boolean universallyApplicable;
41 private int importPrecedence;
42 private boolean tracing;
43
44 private SlotManager slotManagerForInitialValueExpression;
45
46
47
48 public Accumulator() {
49 preDescentRules = new SimpleMode(new StructuredQName("saxon", NamespaceConstant.SAXON, "preDescent"));
50 postDescentRules = new SimpleMode(new StructuredQName("saxon", NamespaceConstant.SAXON, "postDescent"));
51 // The "body" of an accumulator is an artificial expression that contains all the constituent expressions, for ease of management.
52 body = Literal.makeEmptySequence();
53 }
54
55 /**
56 * Get the symbolic name of the component
57 *
58 * @return the symbolic name
59 */
60 @Override
61 public SymbolicName getSymbolicName() {
62 return new SymbolicName(StandardNames.XSL_ACCUMULATOR, getAccumulatorName());
63 }
64
65 /**
66 * Ask whether the accumulator is declared streamable
67 *
68 * @return true if it is declared with streamable="yes"
69 */
70
71 public boolean isDeclaredStreamable() {
72 return streamable;
73 }
74
75 /**
76 * Say whether the accumulator is declared streamable
77 *
78 * @param streamable true if it is declared with streamable="yes"
79 */
80
81 public void setDeclaredStreamable(boolean streamable) {
82 this.streamable = streamable;
83 }
84
85 /**
86 * Get the name of the accumulator
87 *
88 * @return the accumulator name
89 */
90
91 public StructuredQName getAccumulatorName() {
92 return accumulatorName;
93 }
94
95 /**
96 * Set the name of the pre-descent function
97 *
98 * @param firstName the function name (always present)
99 */
100
101 public void setAccumulatorName(StructuredQName firstName) {
102 this.accumulatorName = firstName;
103 }
104
105 /**
106 * Get the import precedence
107 * @return the import precedence of the accumulator
108 */
109
110 public int getImportPrecedence() {
111 return importPrecedence;
112 }
113
114 /**
115 * Set the import precedence
116 * @param importPrecedence the import precedence of the accumulator
117 */
118
119 public void setImportPrecedence(int importPrecedence) {
120 this.importPrecedence = importPrecedence;
121 }
122
123 /**
124 * Say whether this accumulator is universally appicable to all documents
125 * @param universal true if this accumulator is universally applicable
126 */
127
128 public void setUniversallyApplicable(boolean universal) {
129 this.universallyApplicable = universal;
130 }
131
132 /**
133 * Ask whether this accumulator is universally appicable to all documents
134 * @return true if this accumulator is universally applicable
135 */
136
137 public boolean isUniversallyApplicable() {
138 return universallyApplicable;
139 }
140
141 /**
142 * Ask whether diagnostic tracing is enabled for this accumulator
143 * @return true if diagnostic tracing is enabled
144 */
145
146 public boolean isTracing() {
147 return tracing;
148 }
149
150 /**
151 * Say whether diagnostic tracing is to be enabled for this accumulator
152 * @param tracing true if diagnostic tracing is to be enabled
153 */
154
155 public void setTracing(boolean tracing) {
156 this.tracing = tracing;
157 }
158
159 /**
160 * Get the slotManager to be used for evaluating the initial-value expression
161 * @return the slotManager for the initial-value expression (defining any local variables declared and used
162 * within this expression)
163 */
164
165 public SlotManager getSlotManagerForInitialValueExpression() {
166 return slotManagerForInitialValueExpression;
167 }
168
169 /**
170 * Set the slotManager to be used for evaluating the initial-value expression
171 * @param slotManagerForInitialValueExpression the slotManager for the initial-value expression
172 * (defining any local variables declared and used within this expression)
173 */
174
175 public void setSlotManagerForInitialValueExpression(SlotManager slotManagerForInitialValueExpression) {
176 this.slotManagerForInitialValueExpression = slotManagerForInitialValueExpression;
177 }
178
179 /**
180 * Get the set of rules for phase="start", held in the form of a Mode object
181 *
182 * @return the Mode object containing all the rules that apply to phase="start"
183 */
184
185 public SimpleMode getPreDescentRules() {
186 return preDescentRules;
187 }
188
189 /**
190 * Set the set of rules for phase="start", held in the form of a Mode object
191 *
192 * @param preDescentRules the Mode object containing all the rules that apply to phase="start"
193 */
194
195 public void setPreDescentRules(SimpleMode preDescentRules) {
196 this.preDescentRules = preDescentRules;
197 }
198
199 /**
200 * Get the set of rules for phase="end", held in the form of a Mode object
201 *
202 * @return the Mode object containing all the rules that apply to phase="end"
203 */
204
205 public SimpleMode getPostDescentRules() {
206 return postDescentRules;
207 }
208
209 /**
210 * Set the set of rules for phase="end", held in the form of a Mode object
211 *
212 * @param postDescentRules the Mode object containing all the rules that apply to phase="end"
213 */
214
215
216 public void setPostDescentRules(SimpleMode postDescentRules) {
217 this.postDescentRules = postDescentRules;
218 }
219
220 /**
221 * Get the expression that computes the initial value of the accumulator
222 *
223 * @return the initial value expression
224 */
225
226 public Expression getInitialValueExpression() {
227 return initialValueExpression;
228 }
229
230 /**
231 * Set the expression that computes the initial value of the accumulator
232 *
233 * @param initialValueExpression the initial value expression
234 */
235
236 public void setInitialValueExpression(Expression initialValueExpression) {
237 this.initialValueExpression = initialValueExpression;
238 }
239
240 /**
241 * Add an expression to the list of expressions used by this accumulator
242 * @param expression the expression to be added to the list
243 */
244
245 public void addChildExpression(Expression expression) {
246 Expression e = Block.makeBlock(getBody(), expression);
247 setBody(e);
248 }
249
250 /**
251 * Get the declared type of the accumulator. The initial value and all intermediate values
252 * must conform to this type
253 *
254 * @return the declared type of the accumulator
255 */
256
257 public SequenceType getType() {
258 return type;
259 }
260
261 /**
262 * Set the declared type of the accumulator. The initial value and all intermediate values
263 * must conform to this type
264 *
265 * @param type the declared type of the accumulator
266 */
267
268 public void setType(SequenceType type) {
269 this.type = type;
270 }
271
272 /**
273 * Determine whether this accumulator is compatible with one that it overrides
274 * @param other the accumlator that this one overrides
275 * @return true if they are compatible, as defined in the XSLT 3.0 specification
276 */
277
278 public boolean isCompatible(Accumulator other) {
279 return getAccumulatorName().equals(other.getAccumulatorName());
280 }
281
282 /**
283 * Get the kind of component that this represents, using integer constants such as
284 * {@link net.sf.saxon.om.StandardNames#XSL_FUNCTION}
285 */
286 @Override
287 public int getComponentKind() {
288 return StandardNames.XSL_ACCUMULATOR;
289 }
290
291 /**
292 * Get the type of construct. This will either be the fingerprint of a standard XSLT instruction name
293 * (values in {@link net.sf.saxon.om.StandardNames}: all less than 1024)
294 * or it will be a constant in class {@link LocationKind}.
295 *
296 * @return an integer identifying the kind of construct
297 */
298 public int getConstructType() {
299 return StandardNames.XSL_ACCUMULATOR;
300 }
301
302 /**
303 * Get a name identifying the object of the expression, for example a function name, template name,
304 * variable name, key name, element name, etc. This is used only where the name is known statically.
305 *
306 * @return the QName of the object declared or manipulated by this instruction or expression
307 */
308 public StructuredQName getObjectName() {
309 return accumulatorName;
310 }
311
312 /**
313 * Export expression structure. The abstract expression tree
314 * is written to the supplied outputstream.
315 *
316 * @param presenter the expression presenter used to generate the XML representation of the structure
317 */
318 @Override
319 public void export(ExpressionPresenter presenter) throws XPathException {
320 export(presenter, null);
321 }
322
323 /**
324 * Export expression structure. The abstract expression tree
325 * is written to the supplied outputstream.
326 *
327 * @param out the expression presenter used to display the structure
328 */
329
330 public void export(final ExpressionPresenter out, Map<Component, Integer> componentIdMap) throws XPathException {
331 // if ("JS".equals(out.getOption("target"))) {
332 // throw new XPathException("xsl:accumulator is not supported in Saxon-JS", SaxonErrorCode.SXJS0001);
333 // }
334 out.startElement("accumulator");
335 out.emitAttribute("name", getObjectName());
336 out.emitAttribute("line", getLineNumber() + "");
337 out.emitAttribute("module", getSystemId());
338 out.emitAttribute("as", type.toExportString());
339 out.emitAttribute("streamable", streamable ? "1" : "0");
340 out.emitAttribute("slots", getSlotManagerForInitialValueExpression().getNumberOfVariables()+"");
341 if (componentIdMap != null) {
342 out.emitAttribute("binds", "" + getDeclaringComponent().listComponentReferences(componentIdMap));
343 }
344 if (isUniversallyApplicable()) {
345 out.emitAttribute("flags", "u");
346 }
347 out.setChildRole("init");
348 initialValueExpression.export(out);
349
350 SimpleMode.RuleAction action = new SimpleMode.RuleAction() {
351 public void processRule(Rule r) throws XPathException {
352 out.startElement("accRule");
353 out.emitAttribute("slots", ((AccumulatorRule)r.getAction()).getStackFrameMap().getNumberOfVariables()+"");
354 out.emitAttribute("rank", ""+r.getRank());
355 if (((AccumulatorRule) r.getAction()).isCapturing()) {
356 out.emitAttribute("flags", "c");
357 }
358 r.getPattern().export(out);
359 r.getAction().export(out);
360 out.endElement();
361 }
362 };
363 try {
364 out.startElement("pre");
365 out.emitAttribute("slots", preDescentRules.getStackFrameSlotsNeeded()+"");
366 preDescentRules.processRules(action);
367 out.endElement();
368 out.startElement("post");
369 out.emitAttribute("slots", postDescentRules.getStackFrameSlotsNeeded() + "");
370 postDescentRules.processRules(action);
371 out.endElement();
372 } catch (XPathException e) {
373 throw new AssertionError(e);
374 }
375 out.endElement();
376 }
377 }