Debian Codegraph @

net/sf/saxon/query/DynamicQueryContext.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.query;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.Controller;
12 import net.sf.saxon.expr.instruct.GlobalParameterSet;
13 import net.sf.saxon.functions.AccessorFn;
14 import net.sf.saxon.lib.*;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.DateTimeValue;
18
19 import javax.xml.transform.ErrorListener;
20 import javax.xml.transform.URIResolver;
21
22 /**
23 * This object represents a dynamic context for query execution. This class is used
24 * by the application writer to set up aspects of the dynamic context; it is not used
25 * operationally (or modified) by the XQuery processor itself, which copies all required
26 * information into its own internal representation.
27 */
28
29 public class DynamicQueryContext {
30
31 /*@Nullable*/ private Item contextItem;
32 /*@Nullable*/ private GlobalParameterSet parameters = new GlobalParameterSet();
33 private Configuration config;
34 private URIResolver uriResolver;
35 private ErrorListener errorListener;
36 /*@Nullable*/ private TraceListener traceListener;
37 private UnparsedTextURIResolver unparsedTextURIResolver;
38 private DateTimeValue currentDateTime;
39 private Logger traceFunctionDestination;
40 private int validationMode = Validation.DEFAULT;
41 private boolean applyConversionRules = true;
42
43 /**
44 * Create the dynamic context for a query
45 *
46 * @param config the Saxon configuration
47 * @since 8.4.
48 */
49
50 public DynamicQueryContext(/*@NotNull*/ Configuration config) {
51 this.config = config;
52 uriResolver = config.getURIResolver();
53 errorListener = config.getErrorListener();
54 if (errorListener instanceof StandardErrorListener) {
55 errorListener = ((StandardErrorListener) errorListener).makeAnother(Configuration.XQUERY);
56 ((StandardErrorListener) errorListener).setRecoveryPolicy(Configuration.DO_NOT_RECOVER);
57 }
58 traceFunctionDestination = config.getLogger();
59 }
60
61 /**
62 * Ask whether source documents loaded using the doc(), document(), and collection()
63 * functions, or supplied as a StreamSource or SAXSource to the transform() or addParameter() method
64 * should be subjected to schema validation
65 *
66 * @return the schema validation mode previously set using setSchemaValidationMode(),
67 * or the default mode {@link net.sf.saxon.lib.Validation#DEFAULT} otherwise.
68 */
69
70 public int getSchemaValidationMode() {
71 return validationMode;
72 }
73
74 /**
75 * Say whether source documents loaded using the doc(), document(), and collection()
76 * functions, or supplied as a StreamSource or SAXSource to the transform() or addParameter() method,
77 * should be subjected to schema validation. The default value is taken
78 * from the corresponding property of the Configuration.
79 *
80 * @param validationMode the validation (or construction) mode to be used for source documents.
81 * One of {@link net.sf.saxon.lib.Validation#STRIP}, {@link net.sf.saxon.lib.Validation#PRESERVE}, {@link net.sf.saxon.lib.Validation#STRICT},
82 * {@link net.sf.saxon.lib.Validation#LAX}
83 * @since 9.2
84 */
85
86 public void setSchemaValidationMode(int validationMode) {
87 this.validationMode = validationMode;
88 }
89
90 /**
91 * Say whether the function conversion rules should be applied to supplied
92 * parameter values. For example, this allows an integer to be supplied as the value
93 * for a parameter where the expected type is xs:double. The default is true.
94 * The value "false" is allowed because XQJ requires that no conversion is attempted.
95 *
96 * @param convert true if function conversion rules are to be applied to supplied
97 * values; if false, the supplied value must match the required type exactly.
98 * @since 9.3
99 */
100
101 public void setApplyFunctionConversionRulesToExternalVariables(boolean convert) {
102 applyConversionRules = convert;
103 }
104
105 /**
106 * Ask whether the function conversion rules should be applied to supplied
107 * parameter values. For example, this allows an integer to be supplied as the value
108 * for a parameter where the expected type is xs:double. The default is true.
109 *
110 * @return true if function conversion rules are to be applied to supplied
111 * values; if false, the supplied value must match the required type exactly.
112 * @since 9.3
113 */
114
115 public boolean isApplyFunctionConversionRulesToExternalVariables() {
116 return applyConversionRules;
117 }
118
119 /**
120 * Set the context item for evaluating the expression. If this method is not called,
121 * the context node will be undefined. The context item is available as the value of
122 * the expression ".".
123 * To obtain a node by parsing a source document, see the method
124 * {@link net.sf.saxon.Configuration#buildDocumentTree} in the Configuration class.
125 *
126 * @param item The item that is to be the context item for the query
127 * @throws IllegalArgumentException if the supplied item is a node that was built under the wrong
128 * Saxon Configuration
129 * @throws NullPointerException if the supplied item is null
130 * @since 8.4
131 */
132
133 public void setContextItem(Item item) {
134 if (item == null) {
135 throw new NullPointerException("Context item cannot be null");
136 }
137 if (item instanceof DocumentInfo) {
138 // for legacy reasons
139 item = ((DocumentInfo)item).getUnderlyingNode();
140 }
141 if (item instanceof NodeInfo) {
142 if (!((NodeInfo) item).getConfiguration().isCompatible(config)) {
143 throw new IllegalArgumentException(
144 "Supplied node must be built using the same or a compatible Configuration");
145 }
146 }
147 contextItem = item;
148 //parameters.put(StandardNames.SAXON_CONTEXT_ITEM, item);
149 }
150
151 /**
152 * Get the context item for the query, as set using setContextItem().
153 *
154 * @return the context item if set, or null otherwise.
155 * @since 8.4
156 */
157
158 /*@Nullable*/
159 public Item getContextItem() {
160 return contextItem;
161 }
162
163 /**
164 * Set a parameter for the query.
165 *
166 * @param expandedName The name of the parameter.
167 * It is not an error to supply a value for a parameter that has not been
168 * declared, the parameter will simply be ignored. If the parameter has
169 * been declared in the query (as an external global variable) then it
170 * will be initialized with the value supplied.
171 * @param value The value of the parameter.
172 * @since 8.4. Changed in 9.6 (a) to require a Sequence value, thus making the alternative
173 * method setParameterValue redundant, and (b) to accept the name as a StructuredQName
174 * Changed in 9.9 to require the second argument to be a GroundedValue
175 */
176
177 public void setParameter(StructuredQName expandedName, GroundedValue<?> value) {
178 if (parameters == null) {
179 parameters = new GlobalParameterSet();
180 }
181 parameters.put(expandedName, value);
182 }
183
184 /**
185 * Reset the parameters to an empty list.
186 */
187
188 public void clearParameters() {
189 parameters = new GlobalParameterSet();
190 }
191
192 /**
193 * Get the actual value of a parameter to the query.
194 *
195 * @param expandedName the name of the required parameter
196 * @return the value of the parameter, if it exists, or null otherwise
197 * @since 8.4. Changed in 9.6 to take a StructuredQName for the name. Changed in 9.9.1.1
198 * to return a generified GroundedValue.
199 */
200
201 /*@Nullable*/
202 public GroundedValue<?> getParameter(StructuredQName expandedName) {
203 if (parameters == null) {
204 return null;
205 }
206 return parameters.get(expandedName);
207 }
208
209
210 /**
211 * Get all the supplied parameters.
212 *
213 * @return a structure containing all the parameters
214 */
215
216 public GlobalParameterSet getParameters() {
217 if (parameters == null) {
218 return new GlobalParameterSet();
219 } else {
220 return parameters;
221 }
222 }
223
224 /**
225 * Set an object that will be used to resolve URIs used in
226 * fn:document() and related functions.
227 *
228 * @param resolver An object that implements the URIResolver interface, or
229 * null.
230 * @since 8.4
231 */
232
233 public void setURIResolver(URIResolver resolver) {
234 // System.err.println("Setting uriresolver to " + resolver + " on " + this);
235 uriResolver = resolver;
236 }
237
238 /**
239 * Get the URI resolver.
240 *
241 * @return the user-supplied URI resolver if there is one, or the
242 * system-defined one otherwise
243 * @since 8.4
244 */
245
246 public URIResolver getURIResolver() {
247 return uriResolver;
248 }
249
250 /**
251 * Set an object that will be used to resolve URIs used in
252 * fn:unparsed-text() and related functions.
253 *
254 * @param resolver An object that implements the UnparsedTextURIResolver interface, or
255 * null.
256 * @since 9.5
257 */
258
259 public void setUnparsedTextURIResolver(UnparsedTextURIResolver resolver) {
260 // System.err.println("Setting uriresolver to " + resolver + " on " + this);
261 unparsedTextURIResolver = resolver;
262 }
263
264 /**
265 * Get the URI resolver for unparsed text.
266 *
267 * @return the user-supplied unparsed text URI resolver if there is one, or the
268 * system-defined one otherwise
269 * @since 8.4
270 */
271
272 public UnparsedTextURIResolver getUnparsedTextURIResolver() {
273 return unparsedTextURIResolver;
274 }
275
276
277 /**
278 * Set the error listener. The error listener receives reports of all run-time
279 * errors and can decide how to report them.
280 *
281 * @param listener the ErrorListener to be used
282 * @since 8.4
283 */
284
285 public void setErrorListener(ErrorListener listener) {
286 errorListener = listener;
287 }
288
289 /**
290 * Get the error listener.
291 *
292 * @return the ErrorListener in use
293 * @since 8.4
294 */
295
296 public ErrorListener getErrorListener() {
297 return errorListener;
298 }
299
300 /**
301 * Set the trace listener. The trace listener receives reports of all run-time
302 * expression evaluation.
303 *
304 * @param listener the TraceListener to be used
305 * @since 9.0
306 */
307
308 public void setTraceListener(/*@Nullable*/ TraceListener listener) {
309 traceListener = listener;
310 }
311
312 /**
313 * Get the trace listener.
314 *
315 * @return the TraceListener in use, or null if none is in use
316 * @since 9.0
317 */
318
319 /*@Nullable*/
320 public TraceListener getTraceListener() {
321 return traceListener;
322 }
323
324 /**
325 * Set the destination for output from the fn:trace() function.
326 * By default, the destination is System.err. If a TraceListener is in use,
327 * this is ignored, and the trace() output is sent to the TraceListener.
328 *
329 * @param stream the PrintStream to which trace output will be sent. If set to
330 * null, trace output is suppressed entirely. It is the caller's responsibility
331 * to close the stream after use.
332 * @since 9.1. Changed in 9.6 to use a Logger.
333 */
334
335 public void setTraceFunctionDestination(Logger stream) {
336 traceFunctionDestination = stream;
337 }
338
339 /**
340 * Get the destination for output from the fn:trace() function.
341 *
342 * @return the PrintStream to which trace output will be sent. If no explicitly
343 * destination has been set, returns System.err. If the destination has been set
344 * to null to suppress trace output, returns null.
345 * @since 9.1. Changed in 9.6 to use a Logger
346 */
347
348 public Logger getTraceFunctionDestination() {
349 return traceFunctionDestination;
350 }
351
352
353 /**
354 * Get the date and time set previously using {@link #setCurrentDateTime(net.sf.saxon.value.DateTimeValue)}
355 * or null if none has been set.
356 *
357 * @return the current date and time, if it has been set.
358 * @since 8.5
359 */
360
361 public DateTimeValue getCurrentDateTime() {
362 return currentDateTime;
363 }
364
365 /**
366 * Set a value to be used as the current date and time for the query. By default, the "real" current date and
367 * time are used. The main purpose of this method is that it allows repeatable results to be achieved when
368 * testing queries.
369 * <p>This method also has the effect of setting the implicit timezone.</p>
370 *
371 * @param dateTime The value to be used as the current date and time. This must include a timezone. The timezone
372 * from this value will also be used as the implicit timezone
373 * @throws net.sf.saxon.trans.XPathException
374 * if the dateTime does not include a timezone
375 * @since 8.5
376 */
377
378 public void setCurrentDateTime(/*@NotNull*/ DateTimeValue dateTime) throws XPathException {
379 currentDateTime = dateTime;
380 if (dateTime.getComponent(AccessorFn.Component.TIMEZONE) == null) {
381 throw new XPathException("Supplied date/time must include a timezone");
382 }
383 }
384
385 /**
386 * Get the Configuration associated with this dynamic query context
387 *
388 * @return the Configuration
389 * @since 8.8
390 */
391
392 public Configuration getConfiguration() {
393 return config;
394 }
395
396 /**
397 * Apply the settings from this DynamicQueryContext to a Controller
398 *
399 * @param controller the Controller whose settings are to be initialized
400 */
401
402 public void initializeController(/*@NotNull*/ Controller controller) throws XPathException {
403 controller.setURIResolver(getURIResolver());
404 controller.setErrorListener(getErrorListener());
405 controller.addTraceListener(getTraceListener());
406 if (unparsedTextURIResolver != null) {
407 controller.setUnparsedTextURIResolver(unparsedTextURIResolver);
408 }
409 controller.setTraceFunctionDestination(getTraceFunctionDestination());
410 controller.setSchemaValidationMode(getSchemaValidationMode());
411 DateTimeValue currentDateTime = getCurrentDateTime();
412 if (currentDateTime != null) {
413 try {
414 controller.setCurrentDateTime(currentDateTime);
415 } catch (XPathException e) {
416 throw new AssertionError(e); // the value should already have been checked
417 }
418 }
419 controller.setGlobalContextItem(contextItem);
420 controller.initializeController(parameters);
421 controller.setApplyFunctionConversionRulesToExternalVariables(applyConversionRules);
422 //controller.getExecutable().checkAllRequiredParamsArePresent(parameters);
423 //controller.getBindery().defineGlobalParameters(parameters);
424 }
425
426 }
427