| 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; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.Controller; |
| 12 | import net.sf.saxon.event.Receiver; |
| 13 | import net.sf.saxon.expr.instruct.ParameterSet; |
| 14 | import net.sf.saxon.expr.sort.GroupIterator; |
| 15 | import net.sf.saxon.lib.DelegatingErrorListener; |
| 16 | import net.sf.saxon.lib.UnfailingErrorListener; |
| 17 | import net.sf.saxon.om.*; |
| 18 | import net.sf.saxon.regex.RegexIterator; |
| 19 | import net.sf.saxon.trans.NoDynamicContextException; |
| 20 | import net.sf.saxon.trans.XPathException; |
| 21 | import net.sf.saxon.trans.rules.Rule; |
| 22 | import net.sf.saxon.value.DateTimeValue; |
| 23 | |
| 24 | import javax.xml.transform.URIResolver; |
| 25 | import java.util.Iterator; |
| 26 | |
| 27 | /** |
| 28 | * This class represents a context in which an XPath expression is evaluated. |
| 29 | */ |
| 30 | |
| 31 | public interface XPathContext { |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Construct a new context as a copy of another. The new context is effectively added |
| 36 | * to the top of a stack, and contains a pointer to the previous context |
| 37 | * |
| 38 | * @return a new context, created as a copy of this context |
| 39 | */ |
| 40 | |
| 41 | XPathContextMajor newContext(); |
| 42 | |
| 43 | /** |
| 44 | * Construct a new context without copying (used for the context in a function call) |
| 45 | * |
| 46 | * @return a new clean context |
| 47 | */ |
| 48 | |
| 49 | XPathContextMajor newCleanContext(); |
| 50 | |
| 51 | /** |
| 52 | * Construct a new minor context. A minor context can only hold new values of the focus |
| 53 | * (currentIterator) and current output destination. |
| 54 | * |
| 55 | * @return a new minor context |
| 56 | */ |
| 57 | |
| 58 | XPathContextMinor newMinorContext(); |
| 59 | |
| 60 | /** |
| 61 | * Get the local (non-tunnel) parameters that were passed to the current function or template |
| 62 | * |
| 63 | * @return a ParameterSet containing the local parameters |
| 64 | */ |
| 65 | |
| 66 | ParameterSet getLocalParameters(); |
| 67 | |
| 68 | /** |
| 69 | * Get the tunnel parameters that were passed to the current function or template. This includes all |
| 70 | * active tunnel parameters whether the current template uses them or not. |
| 71 | * |
| 72 | * @return a ParameterSet containing the tunnel parameters |
| 73 | */ |
| 74 | |
| 75 | ParameterSet getTunnelParameters(); |
| 76 | |
| 77 | /** |
| 78 | * Get the Controller. May return null when running outside XSLT or XQuery |
| 79 | * |
| 80 | * @return the controller for this query or transformation |
| 81 | */ |
| 82 | |
| 83 | /*@Nullable*/ |
| 84 | Controller getController(); |
| 85 | |
| 86 | /** |
| 87 | * Get the Configuration |
| 88 | * |
| 89 | * @return the Saxon configuration object |
| 90 | */ |
| 91 | |
| 92 | Configuration getConfiguration(); |
| 93 | |
| 94 | /** |
| 95 | * Get the Name Pool |
| 96 | * |
| 97 | * @return the name pool |
| 98 | */ |
| 99 | |
| 100 | NamePool getNamePool(); |
| 101 | |
| 102 | /** |
| 103 | * Set the calling XPathContext |
| 104 | * |
| 105 | * @param caller the XPathContext of the calling expression |
| 106 | */ |
| 107 | |
| 108 | void setCaller(XPathContext caller); |
| 109 | |
| 110 | /** |
| 111 | * Get the calling XPathContext (the next one down the stack). This will be null if unknown, or |
| 112 | * if the bottom of the stack has been reached. |
| 113 | * |
| 114 | * @return the XPathContext of the calling expression |
| 115 | */ |
| 116 | |
| 117 | XPathContext getCaller(); |
| 118 | |
| 119 | /** |
| 120 | * Create, set, and return a focus tracking iterator that wraps a supplied sequence iterator. |
| 121 | * |
| 122 | * @param iter the current iterator. The context item, position, and size are determined by reference |
| 123 | * to the current iterator. |
| 124 | */ |
| 125 | |
| 126 | <T extends Item<?>> FocusIterator<T> trackFocus(SequenceIterator<T> iter); |
| 127 | |
| 128 | /** |
| 129 | * Set a new sequence iterator. |
| 130 | * |
| 131 | * @param iter the current iterator. The context item, position, and size are determined by reference |
| 132 | * to the current iterator. |
| 133 | */ |
| 134 | |
| 135 | void setCurrentIterator(FocusIterator<?> iter); |
| 136 | |
| 137 | /** |
| 138 | * Get the current iterator. |
| 139 | * This encapsulates the context item, context position, and context size. |
| 140 | * |
| 141 | * @return the current iterator, or null if there is no current iterator |
| 142 | * (which means the context item, position, and size are undefined). |
| 143 | */ |
| 144 | |
| 145 | FocusIterator<?> getCurrentIterator(); |
| 146 | |
| 147 | /** |
| 148 | * Get the context item |
| 149 | * |
| 150 | * @return the context item, or null if the context item is undefined |
| 151 | */ |
| 152 | |
| 153 | Item<?> getContextItem(); |
| 154 | |
| 155 | /** |
| 156 | * Get the context size (the position of the last item in the current node list) |
| 157 | * |
| 158 | * @return the context size |
| 159 | * @throws XPathException if the context position is undefined |
| 160 | */ |
| 161 | |
| 162 | int getLast() throws XPathException; |
| 163 | |
| 164 | /** |
| 165 | * Determine whether the context position is the same as the context size |
| 166 | * that is, whether position()=last(). In many cases this has better performance |
| 167 | * than a direct comparison, because it does not require reading to the end of the |
| 168 | * sequence. |
| 169 | * |
| 170 | * @return true if the context position is the same as the context size. |
| 171 | */ |
| 172 | |
| 173 | boolean isAtLast() throws XPathException; |
| 174 | |
| 175 | /** |
| 176 | * Get the URI resolver. This gets the local URIResolver set in the XPathContext if there |
| 177 | * is one; if not, it gets the URIResolver from the Controller (which itself defaults to the |
| 178 | * one set in the Configuration). |
| 179 | * |
| 180 | * @return the user-supplied URI resolver if there is one, or null otherwise. |
| 181 | * @since 9.6 |
| 182 | */ |
| 183 | |
| 184 | URIResolver getURIResolver(); |
| 185 | |
| 186 | /** |
| 187 | * Get the error listener. If no ErrorListener |
| 188 | * has been set locally, the ErrorListener in the Controller is returned; this in turn defaults |
| 189 | * to the ErrorListener set in the Configuration. |
| 190 | * |
| 191 | * @return the ErrorListener in use. Note that this is not necessarily the ErrorListener that was supplied |
| 192 | * by the caller; if that was not an {@link UnfailingErrorListener}, |
| 193 | * it will have been wrapped in a {@link DelegatingErrorListener}, and it is the DelegatingErrorListener |
| 194 | * that this method returns. |
| 195 | * @since 9.6 |
| 196 | */ |
| 197 | |
| 198 | UnfailingErrorListener getErrorListener(); |
| 199 | |
| 200 | /** |
| 201 | * Get the current component |
| 202 | */ |
| 203 | |
| 204 | Component getCurrentComponent(); |
| 205 | |
| 206 | /** |
| 207 | * Use local parameter. This is called when a local xsl:param element is processed. |
| 208 | * If a parameter of the relevant name was supplied, it is bound to the xsl:param element. |
| 209 | * Otherwise the method returns false, so the xsl:param default will be evaluated |
| 210 | * |
| 211 | * |
| 212 | * @param parameterId Globally-unique parameter identifier |
| 213 | * @param slotNumber Slot number of the parameter within the stack frame of the called template |
| 214 | * @param isTunnel True if a tunnel parameter is required, else false |
| 215 | * @return ParameterSet.NOT_SUPPLIED, ParameterSet.SUPPLIED, or ParameterSet.SUPPLIED_AND_CHECKED |
| 216 | */ |
| 217 | |
| 218 | int useLocalParameter( |
| 219 | StructuredQName parameterId, int slotNumber, boolean isTunnel) throws XPathException; |
| 220 | |
| 221 | /** |
| 222 | * Get a reference to the local stack frame for variables. Note that it's |
| 223 | * the caller's job to make a local copy of this. This is used for creating |
| 224 | * a Closure containing a retained copy of the variables for delayed evaluation. |
| 225 | * |
| 226 | * @return array of variables. |
| 227 | */ |
| 228 | |
| 229 | StackFrame getStackFrame(); |
| 230 | |
| 231 | /** |
| 232 | * Get the value of a local variable, identified by its slot number |
| 233 | * |
| 234 | * @param slotnumber the slot number allocated at compile time to the variable, |
| 235 | * which identifies its position within the local stack frame |
| 236 | * @return the value of the variable. |
| 237 | */ |
| 238 | |
| 239 | Sequence<?> evaluateLocalVariable(int slotnumber); |
| 240 | |
| 241 | /** |
| 242 | * Set the value of a local variable, identified by its slot number |
| 243 | * @param slotNumber the slot number allocated at compile time to the variable, |
| 244 | * which identifies its position within the local stack frame |
| 245 | * @param value the value of the variable |
| 246 | */ |
| 247 | |
| 248 | void setLocalVariable(int slotNumber, Sequence<?> value) throws XPathException; |
| 249 | |
| 250 | /** |
| 251 | * Set the XSLT output state to "temporary" or "final" |
| 252 | * |
| 253 | * @param temporary set non-zero to set temporary output state; zero to set final output state |
| 254 | * |
| 255 | */ |
| 256 | |
| 257 | void setTemporaryOutputState(int temporary); |
| 258 | |
| 259 | /** |
| 260 | * Ask whether the XSLT output state is "temporary" or "final" |
| 261 | * |
| 262 | * @return non-zero if in temporary output state (integer identifies the state); zero if in final output state |
| 263 | */ |
| 264 | int getTemporaryOutputState(); |
| 265 | |
| 266 | /** |
| 267 | * Set the current output URI |
| 268 | * @param uri the current output URI, or null if in temporary output state |
| 269 | */ |
| 270 | |
| 271 | void setCurrentOutputUri(String uri); |
| 272 | |
| 273 | /** |
| 274 | * Get the current output URI |
| 275 | * @return the current output URI, or null if in temporary output state |
| 276 | */ |
| 277 | |
| 278 | String getCurrentOutputUri(); |
| 279 | |
| 280 | /** |
| 281 | * Change the SequenceReceiver to which output is written |
| 282 | * |
| 283 | * @param receiver the SequenceReceiver to be used |
| 284 | */ |
| 285 | |
| 286 | void setReceiver(Receiver receiver); |
| 287 | |
| 288 | /** |
| 289 | * Get the Receiver to which output is currently being written. |
| 290 | * |
| 291 | * @return the current SequenceReceiver |
| 292 | */ |
| 293 | Receiver getReceiver(); |
| 294 | |
| 295 | /** |
| 296 | * Get the current mode. |
| 297 | * |
| 298 | * @return the current mode |
| 299 | */ |
| 300 | |
| 301 | Component.M getCurrentMode(); |
| 302 | |
| 303 | /** |
| 304 | * Get the current template rule. This is used to support xsl:apply-imports and xsl:next-match |
| 305 | * |
| 306 | * @return the current template rule |
| 307 | */ |
| 308 | |
| 309 | Rule getCurrentTemplateRule(); |
| 310 | |
| 311 | /** |
| 312 | * Get the current group iterator. This supports the current-group() and |
| 313 | * current-grouping-key() functions in XSLT 2.0 |
| 314 | * |
| 315 | * @return the current grouped collection |
| 316 | */ |
| 317 | |
| 318 | GroupIterator getCurrentGroupIterator(); |
| 319 | |
| 320 | /** |
| 321 | * Get the current merge group iterator. This supports the current-merge-group() and |
| 322 | * current-merge-key() functions in XSLT 2.0 |
| 323 | * |
| 324 | * @return the current merge group |
| 325 | */ |
| 326 | |
| 327 | GroupIterator getCurrentMergeGroupIterator(); |
| 328 | |
| 329 | /** |
| 330 | * Get the current regex iterator. This supports the functionality of the regex-group() |
| 331 | * function in XSLT 2.0. |
| 332 | * |
| 333 | * @return the current regular expressions iterator |
| 334 | */ |
| 335 | |
| 336 | RegexIterator getCurrentRegexIterator(); |
| 337 | |
| 338 | /** |
| 339 | * Get the current date and time |
| 340 | * |
| 341 | * @return the current date and time. All calls within a single query or transformation |
| 342 | * will return the same value |
| 343 | */ |
| 344 | |
| 345 | DateTimeValue getCurrentDateTime() throws NoDynamicContextException; |
| 346 | |
| 347 | /** |
| 348 | * Get the implicit timezone |
| 349 | * |
| 350 | * @return the implicit timezone. This will be the timezone of the current date and time, and |
| 351 | * all calls within a single query or transformation will return the same value. The result is |
| 352 | * expressed as an offset from UTC in minutes. If the implicit timezone is unknown (which is |
| 353 | * the case when this is called on an EarlyEvaluationContext), return |
| 354 | * {@link net.sf.saxon.value.CalendarValue#NO_TIMEZONE} |
| 355 | */ |
| 356 | |
| 357 | int getImplicitTimezone(); |
| 358 | |
| 359 | /** |
| 360 | * Get the context stack. This method returns an iterator whose items are instances of |
| 361 | * {@link net.sf.saxon.trace.ContextStackFrame}, starting with the top-most stackframe and |
| 362 | * ending at the point the query or transformation was invoked by a calling application. |
| 363 | * |
| 364 | * @return an iterator over a copy of the run-time call stack |
| 365 | */ |
| 366 | |
| 367 | Iterator iterateStackFrames(); |
| 368 | |
| 369 | /** |
| 370 | * Get the current exception (in saxon:catch) |
| 371 | * |
| 372 | * @return the current exception, or null if there is none defined |
| 373 | */ |
| 374 | |
| 375 | XPathException getCurrentException(); |
| 376 | |
| 377 | /** |
| 378 | * Get the thread manager used to process asynchronous xsl:result-document threads. |
| 379 | * @return the current thread manager; or null if multithreading is not supported |
| 380 | */ |
| 381 | |
| 382 | XPathContextMajor.ThreadManager getThreadManager(); |
| 383 | |
| 384 | /** |
| 385 | * Wait for child threads started under the control of this context to finish. |
| 386 | * This is called at the end of the (main thread of a) transformation, and also |
| 387 | * at the end of the "try" part of a try/catch. The threads affected are those |
| 388 | * used to implement xsl:result-document instructions. |
| 389 | * @throws XPathException if any of the child threads have failed with a dynamic |
| 390 | * error. |
| 391 | */ |
| 392 | |
| 393 | void waitForChildThreads() throws XPathException; |
| 394 | |
| 395 | /** |
| 396 | * Bind a component reference to a component. This is used for binding component references |
| 397 | * (such as function calls, global variable references, or xsl:call-template) across package |
| 398 | * boundaries. The binding is done dynamically because, in the presence of overridden components, |
| 399 | * the choice among different components with the same name depends on which package the caller |
| 400 | * is in. |
| 401 | * @param bindingSlot Binding slots are allocated statically to the external component references |
| 402 | * in every component: for example, in the case of a template, to all global |
| 403 | * variable references, named function calls, and named template calls within |
| 404 | * that template. The binding slot therefore identifies the name of the |
| 405 | * component that is required; and the selection of an actual component is |
| 406 | * done by selection from the binding vector of the component currently being |
| 407 | * executed |
| 408 | * @return the component to be invoked |
| 409 | */ |
| 410 | |
| 411 | Component getTargetComponent(int bindingSlot); |
| 412 | |
| 413 | } |
| 414 |