| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.event.PipelineConfiguration; |
| 12 | import net.sf.saxon.query.XQueryExpression; |
| 13 | import net.sf.saxon.trace.ExpressionPresenter; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | |
| 16 | /** |
| 17 | * An XQueryExecutable represents the compiled form of a query. |
| 18 | * To execute the query, it must first be loaded to form an {@link net.sf.saxon.s9api.XQueryEvaluator}. |
| 19 | * <p>An XQueryExecutable is immutable, and therefore thread-safe. |
| 20 | * It is simplest to load a new XQueryEvaluator each time the query is to be run. |
| 21 | * However, the XQueryEvaluator is serially reusable within a single thread. </p> |
| 22 | * <p>An XQueryExecutable is created by using one of the <code>compile</code> methods on the |
| 23 | * {@link net.sf.saxon.s9api.XQueryCompiler} class.</p> |
| 24 | * |
| 25 | * @since 9.0 |
| 26 | */ |
| 27 | public class XQueryExecutable { |
| 28 | |
| 29 | Processor processor; |
| 30 | XQueryExpression exp; |
| 31 | |
| 32 | protected XQueryExecutable(Processor processor, XQueryExpression exp) { |
| 33 | this.processor = processor; |
| 34 | this.exp = exp; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Load the stylesheet to prepare it for execution. |
| 39 | * |
| 40 | * @return An XsltTransformer. The returned XsltTransformer can be used to set up the |
| 41 | * dynamic context for stylesheet evaluation, and to run the stylesheet. |
| 42 | */ |
| 43 | |
| 44 | public XQueryEvaluator load() { |
| 45 | return new XQueryEvaluator(processor, exp); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the ItemType of the items in the result of the query, as determined by static analysis. This |
| 50 | * is the most precise ItemType that the processor is able to determine from static examination of the |
| 51 | * query; the actual items in the query result are guaranteed to belong to this ItemType or to a subtype |
| 52 | * of this ItemType. |
| 53 | * |
| 54 | * @return the statically-determined ItemType of the result of the query |
| 55 | * @since 9.1 |
| 56 | */ |
| 57 | |
| 58 | public ItemType getResultItemType() { |
| 59 | net.sf.saxon.type.ItemType it = |
| 60 | exp.getExpression().getItemType(); |
| 61 | return new ConstructedItemType(it, processor); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the statically-determined cardinality of the result of the query. This is the most precise cardinality |
| 66 | * that the processor is able to determine from static examination of the query. |
| 67 | * |
| 68 | * @return the statically-determined cardinality of the result of the query |
| 69 | * @since 9.1 |
| 70 | */ |
| 71 | |
| 72 | /*@NotNull*/ |
| 73 | public OccurrenceIndicator getResultCardinality() { |
| 74 | int card = exp.getExpression().getCardinality(); |
| 75 | return OccurrenceIndicator.getOccurrenceIndicator(card); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Ask whether the query is an updating query: that is, whether it returns a Pending Update List |
| 80 | * rather than a Value. Note that queries using the XUpdate copy-modify syntax are not considered |
| 81 | * to be updating queries. |
| 82 | * |
| 83 | * @return true if the query is an updating query, false if not |
| 84 | * @since 9.1 |
| 85 | */ |
| 86 | |
| 87 | public boolean isUpdateQuery() { |
| 88 | return exp.isUpdateQuery(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Produce a diagnostic representation of the compiled query, in XML form. |
| 93 | * <p><i>The detailed form of this representation is not stable (or even documented).</i></p> |
| 94 | * |
| 95 | * @param destination the destination for the XML document containing the diagnostic representation |
| 96 | * of the compiled stylesheet |
| 97 | * @since 9.6 |
| 98 | */ |
| 99 | |
| 100 | public void explain(Destination destination) throws SaxonApiException { |
| 101 | Configuration config = processor.getUnderlyingConfiguration(); |
| 102 | try { |
| 103 | PipelineConfiguration pipe = config.makePipelineConfiguration(); |
| 104 | exp.explain(new ExpressionPresenter(config, destination.getReceiver(pipe, config.obtainDefaultSerializationProperties()))); |
| 105 | } catch (XPathException e) { |
| 106 | throw new SaxonApiException(e); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the underlying implementation object representing the compiled stylesheet. This provides |
| 112 | * an escape hatch into lower-level APIs. The object returned by this method may change from release |
| 113 | * to release. |
| 114 | * |
| 115 | * @return the underlying implementation of the compiled stylesheet |
| 116 | */ |
| 117 | |
| 118 | public XQueryExpression getUnderlyingCompiledQuery() { |
| 119 | return exp; |
| 120 | } |
| 121 | } |
| 122 |