Debian Codegraph @

net/sf/saxon/expr/instruct/WithParam.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.expr.Expression;
11 import net.sf.saxon.expr.Operand;
12 import net.sf.saxon.expr.OperandRole;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.parser.*;
15 import net.sf.saxon.om.Sequence;
16 import net.sf.saxon.om.StandardNames;
17 import net.sf.saxon.om.StructuredQName;
18 import net.sf.saxon.trace.ExpressionPresenter;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.value.SequenceType;
21
22 import java.util.List;
23
24 /**
25 * An object derived from a xsl:with-param element in the stylesheet. <br>
26 */
27
28 public class WithParam {
29
30 public static WithParam[] EMPTY_ARRAY = new WithParam[0];
31
32 private Operand selectOp;
33 //private int parameterId;
34 private boolean typeChecked = false;
35 private int slotNumber = -1;
36 private SequenceType requiredType;
37 private StructuredQName variableQName;
38 private Evaluator evaluator = null;
39
40 public WithParam() {
41 }
42
43 /**
44 * Set the expression to which this variable is bound
45 * @param parent the parent expression
46 * @param select the initializing expression
47 */
48
49 public void setSelectExpression(Expression parent, Expression select) {
50 selectOp = new Operand(parent, select, OperandRole.NAVIGATE);
51 }
52
53 /**
54 * Get the expression to which this variable is bound
55 *
56 * @return the initializing expression
57 */
58
59 public Expression getSelectExpression() {
60 return selectOp.getChildExpression();
61 }
62
63 /**
64 * Set the required type of this variable
65 *
66 * @param required the required type
67 */
68
69 public void setRequiredType(SequenceType required) {
70 requiredType = required;
71 }
72
73 /**
74 * Get the required type of this variable
75 *
76 * @return the required type
77 */
78
79 public SequenceType getRequiredType() {
80 return requiredType;
81 }
82
83 /**
84 * Get the slot number allocated to this variable. This is used only for xsl:iterate and xsl:evaluate;
85 * template parameters are identified by name, not by slot number.
86 *
87 * @return the slot number, that is the position allocated to the variable on its stack frame
88 */
89
90 public int getSlotNumber() {
91 return slotNumber;
92 }
93
94 /**
95 * Set the slot number of this variable
96 *
97 * @param s the slot number, that is, the position allocated to this variable on its stack frame
98 */
99
100 public void setSlotNumber(int s) {
101 slotNumber = s;
102 }
103
104 /**
105 * Set the name of the variable
106 *
107 * @param s the name of the variable (a QName)
108 */
109
110 public void setVariableQName(StructuredQName s) {
111 variableQName = s;
112 }
113
114 /**
115 * Get the name of this variable
116 *
117 * @return the name of this variable (a QName)
118 */
119
120 public StructuredQName getVariableQName() {
121 return variableQName;
122 }
123
124 /**
125 * Say whether this parameter will have been typechecked by the caller to ensure it satisfies
126 * the required type, in which case the callee need not do a dynamic type check
127 *
128 * @param checked true if the caller has done static type checking against the required type
129 */
130
131 public void setTypeChecked(boolean checked) {
132 typeChecked = checked;
133 }
134
135
136 public int getInstructionNameCode() {
137 return StandardNames.XSL_WITH_PARAM;
138 }
139
140 /**
141 * Static method to simplify a set of with-param elements
142 *
143 * @param params the set of parameters to be simplified
144 * @throws XPathException if a static error is found
145 */
146
147 public static void simplify(WithParam[] params) throws XPathException {
148 if (params != null) {
149 for (WithParam param : params) {
150 param.selectOp.setChildExpression(param.selectOp.getChildExpression().simplify());
151 }
152 }
153 }
154
155 /**
156 * Static method to typecheck a set of with-param elements
157 *
158 * @param params the set of parameters to be checked
159 * @param visitor the expression visitor
160 * @param contextItemType static information about the context item type and existence
161 * @throws XPathException if a static error is found
162 */
163
164
165 public static void typeCheck(WithParam[] params, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
166 if (params != null) {
167 for (WithParam param : params) {
168 param.selectOp.typeCheck(visitor, contextItemType);
169 }
170 }
171 }
172
173 /**
174 * Static method to optimize a set of with-param elements
175 *
176 * @param params the set of parameters to be optimized
177 * @param visitor the expression visitor
178 * @param contextItemType static information about the context item type and existence
179 * @throws XPathException if a static error is found
180 */
181
182 public static void optimize(ExpressionVisitor visitor, WithParam[] params, ContextItemStaticInfo contextItemType) throws XPathException {
183 if (params != null) {
184 for (WithParam param : params) {
185 param.selectOp.optimize(visitor, contextItemType);
186 param.computeEvaluator();
187 }
188 }
189 }
190
191 /**
192 * Get the evaluation mode of the variable
193 *
194 * @return the evaluation mode (a constant in {@link ExpressionTool}
195 */
196
197 public int getEvaluationMode() {
198 if (evaluator == null) {
199 computeEvaluator();
200 }
201 return evaluator.getCode();
202 }
203
204
205
206 private void computeEvaluator() {
207 evaluator = ExpressionTool.lazyEvaluator(selectOp.getChildExpression(), true);
208 }
209
210
211 /**
212 * Static method to copy a set of parameters
213 * @param parent the new parent expression
214 * @param params the parameters to be copied
215 * @return the resulting copy
216 */
217
218 public static WithParam[] copy(Expression parent, WithParam[] params, RebindingMap rebindings) {
219 if (params == null) {
220 return null;
221 }
222 WithParam[] result = new WithParam[params.length];
223 for (int i = 0; i < params.length; i++) {
224 result[i] = new WithParam();
225 //result[i].parameterId = params[i].parameterId;
226 result[i].slotNumber = params[i].slotNumber;
227 result[i].typeChecked = params[i].typeChecked;
228 result[i].selectOp = new Operand(parent, params[i].selectOp.getChildExpression().copy(rebindings), OperandRole.NAVIGATE);
229 result[i].requiredType = params[i].requiredType;
230 result[i].variableQName = params[i].variableQName;
231 }
232 return result;
233 }
234
235 /**
236 * Static method to gather the XPath expressions used in an array of WithParam parameters (add them to the supplied list)
237 *
238 * @param parent the containing expression
239 * @param params the set of with-param elements to be searched
240 * @param list the list to which the subexpressions will be added
241 */
242
243 public static void gatherOperands(Expression parent, WithParam[] params, List<Operand> list) {
244 if (params != null) {
245 for (WithParam param : params) {
246 list.add(param.selectOp);
247 }
248 }
249 }
250
251
252 /**
253 * Static method to export a set of parameters
254 *
255 * @param params the set of parameters to be exported
256 * @param out the destination for the output
257 * @param tunnel true if these are tunnel parameters
258 */
259
260 public static void exportParameters(WithParam[] params, ExpressionPresenter out, boolean tunnel) throws XPathException{
261 if (params != null) {
262 for (WithParam param : params) {
263 out.startElement("withParam");
264 out.emitAttribute("name", param.variableQName);
265 String flags = "";
266 if (tunnel) {
267 flags += "t";
268 }
269 if (param.isTypeChecked()) {
270 flags += "c";
271 }
272 if (!flags.isEmpty()) {
273 out.emitAttribute("flags", flags);
274 }
275 ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions();
276 if (param.getRequiredType() != SequenceType.ANY_SEQUENCE) {
277 out.emitAttribute("as", param.getRequiredType().toExportString());
278 if (options.target.equals("JS") && options.targetVersion == 2) {
279 out.emitAttribute("asJ", param.getRequiredType().toExportString2());
280 }
281 }
282 if (param.getSlotNumber() != -1) {
283 out.emitAttribute("slot", param.getSlotNumber() + "");
284 }
285 param.selectOp.getChildExpression().export(out);
286 out.endElement();
287 }
288 }
289 }
290
291 /**
292 * Evaluate the variable. That is,
293 * get the value of the select expression if present or the content
294 * of the element otherwise, either as a tree or as a sequence
295 *
296 * @param context the XPath dynamic context
297 * @return the result of evaluating the variable
298 * @throws net.sf.saxon.trans.XPathException
299 * if evaluation of the select expression fails
300 * with a dynamic error
301 */
302
303 public Sequence<?> getSelectValue(XPathContext context) throws XPathException {
304 // There is a select attribute: do a lazy evaluation of the expression,
305 // which will already contain any code to force conversion to the required type.
306 if (evaluator == null) {
307 computeEvaluator();
308 }
309 int savedOutputState = context.getTemporaryOutputState();
310 context.setTemporaryOutputState(StandardNames.XSL_WITH_PARAM);
311 Sequence<?> result = evaluator.evaluate(selectOp.getChildExpression(), context);
312 context.setTemporaryOutputState(savedOutputState);
313 return result;
314 }
315
316
317 /**
318 * Ask whether static type checking has been done
319 *
320 * @return true if the caller has done static type checking against the type required by the callee
321 */
322
323 public boolean isTypeChecked() {
324 return typeChecked;
325 }
326 }
327