| 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.om.*; |
| 11 | import net.sf.saxon.s9api.streams.XdmStream; |
| 12 | import net.sf.saxon.sxpath.XPathDynamicContext; |
| 13 | import net.sf.saxon.sxpath.XPathExpression; |
| 14 | import net.sf.saxon.sxpath.XPathVariable; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | |
| 17 | import javax.xml.transform.URIResolver; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | /** |
| 21 | * An XPathSelector represents a compiled and loaded XPath expression ready for execution. |
| 22 | * The XPathSelector holds details of the dynamic evaluation context for the XPath expression. |
| 23 | */ |
| 24 | @SuppressWarnings({"ForeachStatement"}) |
| 25 | public class XPathSelector implements Iterable<XdmItem> { |
| 26 | |
| 27 | private XPathExpression exp; |
| 28 | private XPathDynamicContext dynamicContext; |
| 29 | private Map<StructuredQName, XPathVariable> declaredVariables; |
| 30 | |
| 31 | // protected constructor |
| 32 | |
| 33 | protected XPathSelector(XPathExpression exp, |
| 34 | Map<StructuredQName, XPathVariable> declaredVariables) { |
| 35 | this.exp = exp; |
| 36 | this.declaredVariables = declaredVariables; |
| 37 | dynamicContext = exp.createDynamicContext(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Set the context item for evaluating the XPath expression. |
| 42 | * This may be either a node or an atomic value. Most commonly it will be a document node, |
| 43 | * which might be constructed using the {@link DocumentBuilder#build} method. |
| 44 | * |
| 45 | * @param item The context item for evaluating the expression. Must not be null. |
| 46 | * @throws SaxonApiException if an error occurs, for example because the type |
| 47 | * of item supplied does not match the required item type |
| 48 | */ |
| 49 | |
| 50 | public void setContextItem(XdmItem item) throws SaxonApiException { |
| 51 | if (item == null) { |
| 52 | throw new NullPointerException("contextItem"); |
| 53 | } |
| 54 | if (!exp.getInternalExpression().getPackageData().isSchemaAware()) { |
| 55 | Item it = item.getUnderlyingValue().head(); |
| 56 | if (it instanceof NodeInfo && ((NodeInfo) it).getTreeInfo().isTyped()) { |
| 57 | throw new SaxonApiException( |
| 58 | "The supplied node has been schema-validated, but the XPath expression was compiled without schema-awareness"); |
| 59 | } |
| 60 | } |
| 61 | try { |
| 62 | dynamicContext.setContextItem(item.getUnderlyingValue()); |
| 63 | } catch (XPathException e) { |
| 64 | throw new SaxonApiException(e); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the context item used for evaluating the XPath expression. |
| 70 | * This may be either a node or an atomic value. Most commonly it will be a document node, |
| 71 | * which might be constructed using the Build method of the DocumentBuilder object. |
| 72 | * |
| 73 | * @return The context item for evaluating the expression, or null if no context item |
| 74 | * has been set. |
| 75 | */ |
| 76 | |
| 77 | public XdmItem getContextItem() { |
| 78 | return XdmItem.wrapItem(dynamicContext.getContextItem()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Set the value of a variable |
| 83 | * |
| 84 | * @param name The name of the variable. This must match the name of a variable |
| 85 | * that was declared to the XPathCompiler. No error occurs if the expression does not |
| 86 | * actually reference a variable with this name. |
| 87 | * @param value The value to be given to the variable. |
| 88 | * @throws SaxonApiException if the variable has not been declared or if the type of the value |
| 89 | * supplied does not conform to the required type that was specified when the variable was declared |
| 90 | */ |
| 91 | |
| 92 | public void setVariable(QName name, XdmValue value) throws SaxonApiException { |
| 93 | |
| 94 | StructuredQName qn = name.getStructuredQName(); |
| 95 | XPathVariable var = declaredVariables.get(qn); |
| 96 | if (var == null) { |
| 97 | throw new SaxonApiException( |
| 98 | new XPathException("Variable has not been declared: " + name)); |
| 99 | } |
| 100 | try { |
| 101 | dynamicContext.setVariable(var, value.getUnderlyingValue()); |
| 102 | } catch (XPathException e) { |
| 103 | throw new SaxonApiException(e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Set an object that will be used to resolve URIs used in |
| 109 | * fn:doc() and related functions. |
| 110 | * |
| 111 | * @param resolver An object that implements the URIResolver interface, or null. |
| 112 | * @since 9.4 |
| 113 | */ |
| 114 | |
| 115 | public void setURIResolver(URIResolver resolver) { |
| 116 | dynamicContext.setURIResolver(resolver); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the URI resolver. |
| 121 | * |
| 122 | * @return the user-supplied URI resolver if there is one, or the |
| 123 | * system-defined one otherwise |
| 124 | * @since 9.4 |
| 125 | */ |
| 126 | |
| 127 | public URIResolver getURIResolver() { |
| 128 | return dynamicContext.getURIResolver(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Evaluate the expression, returning the result as an <code>XdmValue</code> (that is, |
| 133 | * a sequence of nodes and/or atomic values). |
| 134 | * <p>Note: Although a singleton result <i>may</i> be represented as an <code>XdmItem</code>, there is |
| 135 | * no guarantee that this will always be the case. If you know that the expression will return at |
| 136 | * most one node or atomic value, it is best to use the <code>evaluateSingle</code> method, which |
| 137 | * does guarantee that an <code>XdmItem</code> (or null) will be returned.</p> |
| 138 | * |
| 139 | * @return An <code>XdmValue</code> representing the results of the expression. |
| 140 | * @throws SaxonApiException if a dynamic error occurs during the expression evaluation. |
| 141 | */ |
| 142 | |
| 143 | public XdmValue evaluate() throws SaxonApiException { |
| 144 | Sequence<?> value; |
| 145 | try { |
| 146 | value = exp.iterate(dynamicContext).materialize(); |
| 147 | } catch (XPathException e) { |
| 148 | throw new SaxonApiException(e); |
| 149 | } |
| 150 | return XdmValue.wrap(value); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Evaluate the XPath expression, returning the result as an <code>XdmItem</code> (that is, |
| 155 | * a single node or atomic value). |
| 156 | * |
| 157 | * @return an <code>XdmItem</code> representing the result of the expression, or null if the expression |
| 158 | * returns an empty sequence. If the expression returns a sequence of more than one item, |
| 159 | * any items after the first are ignored. |
| 160 | * @throws SaxonApiException if a dynamic error occurs during the expression evaluation. |
| 161 | */ |
| 162 | |
| 163 | |
| 164 | public XdmItem evaluateSingle() throws SaxonApiException { |
| 165 | try { |
| 166 | Item<?> i = exp.evaluateSingle(dynamicContext); |
| 167 | if (i == null) { |
| 168 | return null; |
| 169 | } |
| 170 | return (XdmItem) XdmValue.wrap(i); |
| 171 | } catch (XPathException e) { |
| 172 | throw new SaxonApiException(e); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Evaluate the expression, returning the result as an <code>Iterator</code> (that is, |
| 178 | * an iterator over a sequence of nodes and/or atomic values). |
| 179 | * <p>Because an <code>XPathSelector</code> is an {@link Iterable}, it is possible to |
| 180 | * iterate over the result using a Java 5 "for each" expression, for example:</p> |
| 181 | * <pre> |
| 182 | * XPathCompiler compiler = processor.newXPathCompiler(); |
| 183 | * XPathSelector seq = compiler.compile("1 to 20").load(); |
| 184 | * for (XdmItem item : seq) { |
| 185 | * System.err.println(item); |
| 186 | * } |
| 187 | * </pre> |
| 188 | * |
| 189 | * @return An iterator over the sequence that represents the results of the expression. |
| 190 | * Each object in this sequence will be an instance of <code>XdmItem</code>. Note |
| 191 | * that the expression may be evaluated lazily, which means that a successful response |
| 192 | * from this method does not imply that the expression has executed successfully: failures |
| 193 | * may be reported later while retrieving items from the iterator. |
| 194 | * @throws SaxonApiUncheckedException if a dynamic error occurs during XPath evaluation that |
| 195 | * can be detected at this point. It is also possible that an SaxonApiUncheckedException will |
| 196 | * be thrown by the <code>hasNext()</code> method of the returned iterator. |
| 197 | */ |
| 198 | |
| 199 | public XdmSequenceIterator<XdmItem> iterator() throws SaxonApiUncheckedException { |
| 200 | try { |
| 201 | return new XdmSequenceIterator<>(exp.iterate(dynamicContext)); |
| 202 | } catch (XPathException e) { |
| 203 | throw new SaxonApiUncheckedException(e); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Evaluate the expression, returning the result as an <code>Stream</code>. |
| 209 | * |
| 210 | * @return A stream delivering the results of the expression. |
| 211 | * Each object in this stream will be an instance of <code>XdmItem</code>. Note |
| 212 | * that the expression may be evaluated lazily, which means that a successful response |
| 213 | * from this method does not imply that the expression has executed successfully: failures |
| 214 | * may be reported later while retrieving items from the iterator. |
| 215 | * @throws SaxonApiUncheckedException if a dynamic error occurs during XPath evaluation that |
| 216 | * can be detected at this point. |
| 217 | */ |
| 218 | |
| 219 | public XdmStream<XdmItem> stream() throws SaxonApiUncheckedException { |
| 220 | return iterator().stream(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Evaluate the XPath expression, returning the effective boolean value of the result. |
| 225 | * |
| 226 | * @return a <code>boolean</code> representing the effective boolean value of the result of evaluating |
| 227 | * the expression, as defined by the rules for the fn:boolean() function. |
| 228 | * @throws SaxonApiException if a dynamic error occurs during the expression evaluation, or if the result |
| 229 | * of the expression is a value whose effective boolean value is not defined (for example, a date or a |
| 230 | * sequence of three integers) |
| 231 | * @since 9.1 |
| 232 | */ |
| 233 | |
| 234 | |
| 235 | public boolean effectiveBooleanValue() throws SaxonApiException { |
| 236 | try { |
| 237 | return exp.effectiveBooleanValue(dynamicContext); |
| 238 | } catch (XPathException e) { |
| 239 | throw new SaxonApiException(e); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get the underlying dynamic context object. This provides an escape hatch to the underlying |
| 245 | * implementation classes, which contain methods that may change from one release to another. |
| 246 | * |
| 247 | * @return the underlying object representing the dynamic context for query execution |
| 248 | */ |
| 249 | |
| 250 | public XPathDynamicContext getUnderlyingXPathContext() { |
| 251 | return dynamicContext; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | } |
| 256 |