//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2018 Saxonica Limited. // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// package net.sf.saxon.s9api; import net.sf.saxon.expr.StaticContext; import net.sf.saxon.om.StructuredQName; import net.sf.saxon.s9api.streams.Step; import net.sf.saxon.sxpath.IndependentContext; import net.sf.saxon.sxpath.XPathExpression; import net.sf.saxon.sxpath.XPathVariable; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Stream; /** * An XPathExecutable represents the compiled form of an XPath expression. * To evaluate the expression, it must first be loaded to form an {@link XPathSelector}. *
An XPathExecutable is immutable, and therefore thread-safe. It is simplest to load * a new XPathSelector each time the expression is to be evaluated. However, the XPathSelector * is serially reusable within a single thread.
*An XPathExecutable is created by using the {@link XPathCompiler#compile} method * on the {@link XPathCompiler} class.
*/ public class XPathExecutable { private XPathExpression exp; private Processor processor; private IndependentContext env; // protected constructor protected XPathExecutable(XPathExpression exp, Processor processor, IndependentContext env) { this.exp = exp; this.processor = processor; this.env = env; //this.declaredVariables = declaredVariables; } /** * Load the compiled XPath expression to prepare it for execution. * * @return An XPathSelector. The returned XPathSelector can be used to set up the * dynamic context, and then to evaluate the expression. */ public XPathSelector load() { MapExample usage: node.select(c.compile(".//x[a=b+2]").asStep()).asXdmValue()
If the allowUndeclaredVariables option is set, this method allows discovery of the variable * references that appear in the compiled expression.
* * @return an iterator over the names of the external variables defined in the XPath expression * @since 9.2 */ public IteratorIf the variable was explicitly declared, this will be the item type that was set when the * variable was declared. If no item type was set, it will be {@link ItemType#ANY_ITEM}.
*If the variable was implicitly declared by reference (which can happen only when the * allowUndeclaredVariables option is set), the returned type will be {@link ItemType#ANY_ITEM}.
*If no variable with the specified QName has been declared either explicitly or implicitly, * the method returns null.
* @since 9.2 */ /*@Nullable*/ public ItemType getRequiredItemTypeForVariable(QName variableName) { XPathVariable var = env.getExternalVariable(variableName.getStructuredQName()); if (var == null) { return null; } else { return new ConstructedItemType(var.getRequiredType().getPrimaryType(), processor); } } /** * Get the required cardinality of a declared variable in the static context of the expression. * * @param variableName the name of a declared variable * @return the required cardinality. *If the variable was explicitly declared, this will be the occurrence indicator that was set when the * variable was declared. If no item type was set, it will be {@link OccurrenceIndicator#ZERO_OR_MORE}.
*If the variable was implicitly declared by reference (which can happen only when the * allowUndeclaredVariables option is set), the returned type will be * {@link OccurrenceIndicator#ZERO_OR_MORE}.
*If no variable with the specified QName has been declared either explicitly or implicitly, * the method returns null.
* @since 9.2 */ public OccurrenceIndicator getRequiredCardinalityForVariable(QName variableName) { XPathVariable var = env.getExternalVariable(variableName.getStructuredQName()); if (var == null) { return null; } else { return OccurrenceIndicator.getOccurrenceIndicator(var.getRequiredType().getCardinality()); } } /** * Get the underlying implementation object representing the compiled XPath expression. * This method provides access to lower-level Saxon classes and methods which may be subject to change * from one release to the next. * * @return the underlying compiled XPath expression. */ public XPathExpression getUnderlyingExpression() { return exp; } /** * Get the underlying implementation object representing the static context of the compiled * XPath expression. This method provides access to lower-level Saxon classes and methods which may be * subject to change from one release to the next. * * @return the underlying static context. */ public StaticContext getUnderlyingStaticContext() { return env; } }