| 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.UnfailingErrorListener; |
| 16 | import net.sf.saxon.om.*; |
| 17 | import net.sf.saxon.regex.RegexIterator; |
| 18 | import net.sf.saxon.trace.InstructionInfo; |
| 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.CalendarValue; |
| 23 | import net.sf.saxon.value.DateTimeValue; |
| 24 | |
| 25 | import javax.xml.transform.URIResolver; |
| 26 | import java.util.Collections; |
| 27 | import java.util.Iterator; |
| 28 | |
| 29 | /** |
| 30 | * This class is an implementation of XPathContext used when evaluating constant sub-expressions at |
| 31 | * compile time. |
| 32 | */ |
| 33 | |
| 34 | public class EarlyEvaluationContext implements XPathContext { |
| 35 | |
| 36 | private Configuration config; |
| 37 | |
| 38 | /** |
| 39 | * Create an early evaluation context, used for evaluating constant expressions at compile time |
| 40 | * |
| 41 | * @param config the Saxon configuration |
| 42 | * //@param map the available collations |
| 43 | */ |
| 44 | |
| 45 | public EarlyEvaluationContext(Configuration config) { |
| 46 | this.config = config; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the value of a local variable, identified by its slot number |
| 51 | */ |
| 52 | |
| 53 | /*@Nullable*/ |
| 54 | public Sequence<?> evaluateLocalVariable(int slotnumber) { |
| 55 | notAllowed(); |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the calling XPathContext (the next one down the stack). This will be null if unknown, or |
| 61 | * if the bottom of the stack has been reached. |
| 62 | */ |
| 63 | |
| 64 | public XPathContext getCaller() { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the URI resolver. This gets the local URIResolver set in the XPathContext if there |
| 70 | * is one; if not, it gets the URIResolver from the Controller (which itself defaults to the |
| 71 | * one set in the Configuration). |
| 72 | * |
| 73 | * @return the user-supplied URI resolver if there is one, or null otherwise. |
| 74 | * @since 9.6 |
| 75 | */ |
| 76 | public URIResolver getURIResolver() { |
| 77 | return config.getURIResolver(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the error listener. If no ErrorListener |
| 82 | * has been set locally, the ErrorListener in the Controller is returned; this in turn defaults |
| 83 | * to the ErrorListener set in the Configuration. |
| 84 | * |
| 85 | * @return the ErrorListener in use. This will always be an UnfailingErrorListener, |
| 86 | * which is a Saxon subclass of ErrorListener that throws no exceptions. |
| 87 | * @since 9.6 |
| 88 | */ |
| 89 | public UnfailingErrorListener getErrorListener() { |
| 90 | return config.getErrorListener(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the current component |
| 95 | */ |
| 96 | public Component getCurrentComponent() { |
| 97 | notAllowed(); |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the Configuration |
| 103 | */ |
| 104 | |
| 105 | public Configuration getConfiguration() { |
| 106 | return config; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the context item |
| 111 | * |
| 112 | * @return the context item, or null if the context item is undefined |
| 113 | */ |
| 114 | |
| 115 | public Item getContextItem() { |
| 116 | return null; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the Controller. May return null when running outside XSLT or XQuery |
| 121 | */ |
| 122 | |
| 123 | public Controller getController() { |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the current group iterator. This supports the current-group() and |
| 129 | * current-grouping-key() functions in XSLT 2.0 |
| 130 | * |
| 131 | * @return the current grouped collection |
| 132 | */ |
| 133 | |
| 134 | public GroupIterator getCurrentGroupIterator() { |
| 135 | notAllowed(); |
| 136 | return null; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the current merge group iterator. This supports the current-merge-group() and |
| 141 | * current-merge-key() functions in XSLT 3.0 |
| 142 | * |
| 143 | * @return the current grouped collection |
| 144 | */ |
| 145 | |
| 146 | public GroupIterator getCurrentMergeGroupIterator() { |
| 147 | notAllowed(); |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Get the current iterator. |
| 154 | * This encapsulates the context item, context position, and context size. |
| 155 | * |
| 156 | * @return the current iterator, or null if there is no current iterator |
| 157 | * (which means the context item, position, and size are undefined). |
| 158 | */ |
| 159 | |
| 160 | public FocusTrackingIterator<?> getCurrentIterator() { |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get the current mode. |
| 166 | * |
| 167 | * @return the current mode |
| 168 | */ |
| 169 | |
| 170 | public Component.M getCurrentMode() { |
| 171 | notAllowed(); |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the current regex iterator. This supports the functionality of the regex-group() |
| 177 | * function in XSLT 2.0. |
| 178 | * |
| 179 | * @return the current regular expressions iterator |
| 180 | */ |
| 181 | |
| 182 | public RegexIterator getCurrentRegexIterator() { |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Get the current template. This is used to support xsl:apply-imports |
| 188 | * |
| 189 | * @return the current template |
| 190 | */ |
| 191 | |
| 192 | public Rule getCurrentTemplateRule() { |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the context size (the position of the last item in the current node list) |
| 198 | * |
| 199 | * @return the context size |
| 200 | * @throws net.sf.saxon.trans.XPathException |
| 201 | * if the context position is undefined |
| 202 | */ |
| 203 | |
| 204 | public int getLast() throws XPathException { |
| 205 | XPathException err = new XPathException("The context item is absent"); |
| 206 | err.setErrorCode("XPDY0002"); |
| 207 | throw err; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get the local (non-tunnel) parameters that were passed to the current function or template |
| 212 | * |
| 213 | * @return a ParameterSet containing the local parameters |
| 214 | */ |
| 215 | |
| 216 | public ParameterSet getLocalParameters() { |
| 217 | notAllowed(); |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the Name Pool |
| 223 | */ |
| 224 | |
| 225 | public NamePool getNamePool() { |
| 226 | return config.getNamePool(); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get information about the creating expression or other construct. |
| 231 | */ |
| 232 | |
| 233 | public InstructionInfo getOrigin() { |
| 234 | return null; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the type of location from which this context was created. |
| 239 | */ |
| 240 | |
| 241 | public int getOriginatingConstructType() { |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get the Receiver to which output is currently being written. |
| 247 | * |
| 248 | * @return the current Receiver |
| 249 | */ |
| 250 | public Receiver getReceiver() { |
| 251 | notAllowed(); |
| 252 | return null; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get a reference to the local stack frame for variables. Note that it's |
| 257 | * the caller's job to make a local copy of this. This is used for creating |
| 258 | * a Closure containing a retained copy of the variables for delayed evaluation. |
| 259 | * |
| 260 | * @return array of variables. |
| 261 | */ |
| 262 | |
| 263 | public StackFrame getStackFrame() { |
| 264 | notAllowed(); |
| 265 | return null; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get the tunnel parameters that were passed to the current function or template. This includes all |
| 270 | * active tunnel parameters whether the current template uses them or not. |
| 271 | * |
| 272 | * @return a ParameterSet containing the tunnel parameters |
| 273 | */ |
| 274 | |
| 275 | public ParameterSet getTunnelParameters() { |
| 276 | notAllowed(); |
| 277 | return null; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Determine whether the context position is the same as the context size |
| 282 | * that is, whether position()=last() |
| 283 | */ |
| 284 | |
| 285 | public boolean isAtLast() throws XPathException { |
| 286 | XPathException err = new XPathException("The context item is absent"); |
| 287 | err.setErrorCode("XPDY0002"); |
| 288 | throw err; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Construct a new context without copying (used for the context in a function call) |
| 293 | */ |
| 294 | |
| 295 | public XPathContextMajor newCleanContext() { |
| 296 | notAllowed(); |
| 297 | return null; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Construct a new context as a copy of another. The new context is effectively added |
| 302 | * to the top of a stack, and contains a pointer to the previous context |
| 303 | */ |
| 304 | |
| 305 | public XPathContextMajor newContext() { |
| 306 | Controller controller = new Controller(config); |
| 307 | return controller.newXPathContext(); |
| 308 | // notAllowed(); |
| 309 | // return null; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Construct a new minor context. A minor context can only hold new values of the focus |
| 314 | * (currentIterator) and current output destination. |
| 315 | */ |
| 316 | |
| 317 | public XPathContextMinor newMinorContext() { |
| 318 | return newContext().newMinorContext(); |
| 319 | // notAllowed(); |
| 320 | // return null; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Set the calling XPathContext |
| 325 | */ |
| 326 | |
| 327 | public void setCaller(XPathContext caller) { |
| 328 | // no-op |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Set a new sequence iterator. |
| 333 | */ |
| 334 | |
| 335 | public void setCurrentIterator(FocusIterator<?> iter) { |
| 336 | notAllowed(); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public <T extends Item<?>> FocusIterator<T> trackFocus(SequenceIterator<T> iter) { |
| 341 | notAllowed(); |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Set the value of a local variable, identified by its slot number |
| 347 | */ |
| 348 | |
| 349 | public void setLocalVariable(int slotNumber, Sequence<?> value) { |
| 350 | notAllowed(); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Change the Receiver to which output is written |
| 355 | */ |
| 356 | |
| 357 | public void setReceiver(Receiver receiver) { |
| 358 | notAllowed(); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Use local parameter. This is called when a local xsl:param element is processed. |
| 363 | * If a parameter of the relevant name was supplied, it is bound to the xsl:param element. |
| 364 | * Otherwise the method returns false, so the xsl:param default will be evaluated |
| 365 | * |
| 366 | * @param parameterId Globally-unique parameter identifier |
| 367 | * @param slotNumber Slot number of the parameter within the stack frame of the called template |
| 368 | * @param isTunnel True if a tunnel parameter is required, else false @return true if a parameter of this name was supplied, false if not |
| 369 | */ |
| 370 | |
| 371 | public int useLocalParameter(StructuredQName parameterId, int slotNumber, boolean isTunnel) { |
| 372 | return ParameterSet.NOT_SUPPLIED; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Get the current date and time. This implementation always throws a |
| 377 | * NoDynamicContextException. |
| 378 | * |
| 379 | * @return the current date and time. All calls within a single query or transformation |
| 380 | * will return the same value |
| 381 | */ |
| 382 | |
| 383 | public DateTimeValue getCurrentDateTime() throws NoDynamicContextException { |
| 384 | throw new NoDynamicContextException("current-dateTime"); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Get the implicit timezone, as a positive or negative offset from UTC in minutes. |
| 389 | * The range is -14hours to +14hours. This implementation returns {@link CalendarValue#NO_TIMEZONE}, |
| 390 | * meaning the value is unknown. |
| 391 | * |
| 392 | * @return the implicit timezone, as an offset from UTC in minutes |
| 393 | */ |
| 394 | |
| 395 | public int getImplicitTimezone() { |
| 396 | return CalendarValue.MISSING_TIMEZONE; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /** |
| 401 | * Get the context stack. This method returns an iterator whose items are instances of |
| 402 | * {@link net.sf.saxon.trace.ContextStackFrame}, starting with the top-most stackframe and |
| 403 | * ending at the point the query or transformation was invoked by a calling application. |
| 404 | * |
| 405 | * @return an iterator over a copy of the run-time call stack |
| 406 | */ |
| 407 | |
| 408 | public Iterator iterateStackFrames() { |
| 409 | return Collections.EMPTY_LIST.iterator(); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get the current exception (in saxon:catch) |
| 414 | * |
| 415 | * @return the current exception, or null if there is none defined |
| 416 | */ |
| 417 | |
| 418 | public XPathException getCurrentException() { |
| 419 | return null; |
| 420 | } |
| 421 | |
| 422 | public void waitForChildThreads() throws XPathException { |
| 423 | getCaller().waitForChildThreads(); |
| 424 | } |
| 425 | |
| 426 | public void setTemporaryOutputState(int temporary) { |
| 427 | // no action |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Ask whether the XSLT output state is "temporary" or "final" |
| 432 | * |
| 433 | * @return non-zero in temporary output state; zero in final output state |
| 434 | */ |
| 435 | public int getTemporaryOutputState() { |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
| 441 | * Set the current output URI |
| 442 | * @param uri the current output URI, or null if in temporary output state |
| 443 | */ |
| 444 | |
| 445 | public void setCurrentOutputUri(String uri) { |
| 446 | // no action |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Get the current output URI |
| 451 | * @return the current output URI, or null if in temporary output state |
| 452 | */ |
| 453 | |
| 454 | public String getCurrentOutputUri() { |
| 455 | return null; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Throw an error for operations that aren't supported when doing early evaluation of constant |
| 460 | * subexpressions |
| 461 | */ |
| 462 | |
| 463 | private void notAllowed() { |
| 464 | throw new UnsupportedOperationException( |
| 465 | new NoDynamicContextException("Internal error: early evaluation of subexpression with no context")); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Get the thread manager used to process asynchronous xsl:result-document threads. |
| 470 | * |
| 471 | * @return the current thread manager; or null if multithreading is not supported |
| 472 | */ |
| 473 | public XPathContextMajor.ThreadManager getThreadManager() { |
| 474 | return null; |
| 475 | } |
| 476 | |
| 477 | public Component getTargetComponent(int bindingSlot) { |
| 478 | return null; |
| 479 | } |
| 480 | } |