| 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.expr.StaticContext; |
| 11 | import net.sf.saxon.om.StructuredQName; |
| 12 | import net.sf.saxon.s9api.streams.Step; |
| 13 | import net.sf.saxon.sxpath.IndependentContext; |
| 14 | import net.sf.saxon.sxpath.XPathExpression; |
| 15 | import net.sf.saxon.sxpath.XPathVariable; |
| 16 | |
| 17 | import java.util.Iterator; |
| 18 | import java.util.LinkedHashMap; |
| 19 | import java.util.Map; |
| 20 | import java.util.stream.Stream; |
| 21 | |
| 22 | /** |
| 23 | * An XPathExecutable represents the compiled form of an XPath expression. |
| 24 | * To evaluate the expression, it must first be loaded to form an {@link XPathSelector}. |
| 25 | * <p>An XPathExecutable is immutable, and therefore thread-safe. It is simplest to load |
| 26 | * a new XPathSelector each time the expression is to be evaluated. However, the XPathSelector |
| 27 | * is serially reusable within a single thread.</p> |
| 28 | * <p>An XPathExecutable is created by using the {@link XPathCompiler#compile} method |
| 29 | * on the {@link XPathCompiler} class.</p> |
| 30 | */ |
| 31 | |
| 32 | public class XPathExecutable { |
| 33 | |
| 34 | private XPathExpression exp; |
| 35 | private Processor processor; |
| 36 | private IndependentContext env; |
| 37 | |
| 38 | // protected constructor |
| 39 | |
| 40 | protected XPathExecutable(XPathExpression exp, Processor processor, IndependentContext env) { |
| 41 | this.exp = exp; |
| 42 | this.processor = processor; |
| 43 | this.env = env; |
| 44 | //this.declaredVariables = declaredVariables; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Load the compiled XPath expression to prepare it for execution. |
| 49 | * |
| 50 | * @return An XPathSelector. The returned XPathSelector can be used to set up the |
| 51 | * dynamic context, and then to evaluate the expression. |
| 52 | */ |
| 53 | |
| 54 | public XPathSelector load() { |
| 55 | Map<StructuredQName, XPathVariable> declaredVariables = new LinkedHashMap<StructuredQName, XPathVariable>(); |
| 56 | for (Iterator iter = env.iterateExternalVariables(); iter.hasNext(); ) { |
| 57 | XPathVariable var = (XPathVariable) iter.next(); |
| 58 | declaredVariables.put(var.getVariableQName(), var); |
| 59 | } |
| 60 | return new XPathSelector(exp, declaredVariables); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Treat the compiled XPath expression as a {@link Step}, so that it can be used in a call on |
| 65 | * {@link XdmValue#select}. |
| 66 | * |
| 67 | * <p>Example usage: <code>node.select(c.compile(".//x[a=b+2]").asStep()).asXdmValue()</code></p> |
| 68 | * @return a Step, whose effect is to evaluate the XPath expression (with the origin item as the |
| 69 | * initial context item) |
| 70 | */ |
| 71 | |
| 72 | public Step<XdmItem> asStep() { |
| 73 | return new Step<XdmItem>() { |
| 74 | @Override |
| 75 | public Stream<? extends XdmItem> apply(XdmItem item) { |
| 76 | try { |
| 77 | XPathSelector selector = load(); |
| 78 | selector.setContextItem(item); |
| 79 | XdmSequenceIterator<XdmItem> result = selector.iterator(); |
| 80 | return result.stream(); |
| 81 | } catch (SaxonApiException e) { |
| 82 | throw new SaxonApiUncheckedException(e); |
| 83 | } |
| 84 | } |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Get the ItemType of the items in the result of the expression, as determined by static analysis. This |
| 92 | * is the most precise ItemType that the processor is able to determine from static examination of the |
| 93 | * expression; the actual items in the expression result are guaranteed to belong to this ItemType or to a subtype |
| 94 | * of this ItemType. |
| 95 | * |
| 96 | * @return the statically-determined ItemType of the result of the expression |
| 97 | * @since 9.1 |
| 98 | */ |
| 99 | |
| 100 | public ItemType getResultItemType() { |
| 101 | net.sf.saxon.type.ItemType it = |
| 102 | exp.getInternalExpression().getItemType(); |
| 103 | return new ConstructedItemType(it, processor); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the statically-determined cardinality of the result of the expression. This is the most precise cardinality |
| 108 | * that the processor is able to determine from static examination of the expression. |
| 109 | * |
| 110 | * @return the statically-determined cardinality of the result of the expression |
| 111 | * @since 9.1 |
| 112 | */ |
| 113 | |
| 114 | public OccurrenceIndicator getResultCardinality() { |
| 115 | int card = exp.getInternalExpression().getCardinality(); |
| 116 | return OccurrenceIndicator.getOccurrenceIndicator(card); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get an iterator over the names of all the external variables. This includes both variables that have |
| 121 | * been explicitly declared using a call to <tt>declareVariable()</tt>, and variables that are implicitly |
| 122 | * declared by reference in the case where the <tt>allowUndeclaredVariables</tt> option is set. It does |
| 123 | * not include range variables bound in a <tt>for</tt>, <tt>some</tt>, or <tt>every</tt> expression. |
| 124 | * <p>If the <tt>allowUndeclaredVariables</tt> option is set, this method allows discovery of the variable |
| 125 | * references that appear in the compiled expression.</p> |
| 126 | * |
| 127 | * @return an iterator over the names of the external variables defined in the XPath expression |
| 128 | * @since 9.2 |
| 129 | */ |
| 130 | |
| 131 | public Iterator<QName> iterateExternalVariables() { |
| 132 | final Iterator varIterator = env.iterateExternalVariables(); |
| 133 | return new Iterator<QName>() { |
| 134 | public boolean hasNext() { |
| 135 | return varIterator.hasNext(); |
| 136 | } |
| 137 | |
| 138 | public QName next() { |
| 139 | return new QName(((XPathVariable) varIterator.next()).getVariableQName()); |
| 140 | } |
| 141 | |
| 142 | public void remove() { |
| 143 | throw new UnsupportedOperationException("remove"); |
| 144 | } |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get the required item type of a declared variable in the static context of the expression. |
| 150 | * |
| 151 | * @param variableName the name of a declared variable |
| 152 | * @return the required item type. |
| 153 | * <p>If the variable was explicitly declared, this will be the item type that was set when the |
| 154 | * variable was declared. If no item type was set, it will be {@link ItemType#ANY_ITEM}.</p> |
| 155 | * <p>If the variable was implicitly declared by reference (which can happen only when the |
| 156 | * <tt>allowUndeclaredVariables</tt> option is set), the returned type will be {@link ItemType#ANY_ITEM}.</p> |
| 157 | * <p>If no variable with the specified QName has been declared either explicitly or implicitly, |
| 158 | * the method returns null.</p> |
| 159 | * @since 9.2 |
| 160 | */ |
| 161 | |
| 162 | /*@Nullable*/ |
| 163 | public ItemType getRequiredItemTypeForVariable(QName variableName) { |
| 164 | XPathVariable var = env.getExternalVariable(variableName.getStructuredQName()); |
| 165 | if (var == null) { |
| 166 | return null; |
| 167 | } else { |
| 168 | return new ConstructedItemType(var.getRequiredType().getPrimaryType(), processor); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Get the required cardinality of a declared variable in the static context of the expression. |
| 174 | * |
| 175 | * @param variableName the name of a declared variable |
| 176 | * @return the required cardinality. |
| 177 | * <p>If the variable was explicitly declared, this will be the occurrence indicator that was set when the |
| 178 | * variable was declared. If no item type was set, it will be {@link OccurrenceIndicator#ZERO_OR_MORE}.</p> |
| 179 | * <p>If the variable was implicitly declared by reference (which can happen only when the |
| 180 | * <tt>allowUndeclaredVariables</tt> option is set), the returned type will be |
| 181 | * {@link OccurrenceIndicator#ZERO_OR_MORE}.</p> |
| 182 | * <p>If no variable with the specified QName has been declared either explicitly or implicitly, |
| 183 | * the method returns null.</p> |
| 184 | * @since 9.2 |
| 185 | */ |
| 186 | |
| 187 | public OccurrenceIndicator getRequiredCardinalityForVariable(QName variableName) { |
| 188 | XPathVariable var = env.getExternalVariable(variableName.getStructuredQName()); |
| 189 | if (var == null) { |
| 190 | return null; |
| 191 | } else { |
| 192 | return OccurrenceIndicator.getOccurrenceIndicator(var.getRequiredType().getCardinality()); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the underlying implementation object representing the compiled XPath expression. |
| 198 | * This method provides access to lower-level Saxon classes and methods which may be subject to change |
| 199 | * from one release to the next. |
| 200 | * |
| 201 | * @return the underlying compiled XPath expression. |
| 202 | */ |
| 203 | |
| 204 | public XPathExpression getUnderlyingExpression() { |
| 205 | return exp; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get the underlying implementation object representing the static context of the compiled |
| 210 | * XPath expression. This method provides access to lower-level Saxon classes and methods which may be |
| 211 | * subject to change from one release to the next. |
| 212 | * |
| 213 | * @return the underlying static context. |
| 214 | */ |
| 215 | |
| 216 | public StaticContext getUnderlyingStaticContext() { |
| 217 | return env; |
| 218 | } |
| 219 | |
| 220 | } |
| 221 |