| 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.xpath; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.expr.instruct.Executable; |
| 13 | import net.sf.saxon.expr.instruct.SlotManager; |
| 14 | import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 15 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 16 | import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 17 | import net.sf.saxon.type.SchemaException; |
| 18 | import net.sf.saxon.type.Type; |
| 19 | import org.xml.sax.InputSource; |
| 20 | |
| 21 | import javax.xml.namespace.NamespaceContext; |
| 22 | import javax.xml.namespace.QName; |
| 23 | import javax.xml.transform.Source; |
| 24 | import javax.xml.xpath.*; |
| 25 | |
| 26 | /** |
| 27 | * <p>XPathEvaluator implements the JAXP API for standalone XPath processing (that is, |
| 28 | * executing XPath expressions in the absence of an XSLT stylesheet). It is an implementation |
| 29 | * of the JAXP 1.3 XPath interface, with additional methods provided (a) for backwards |
| 30 | * compatibility (b) to give extra control over the XPath evaluation, and (c) to support |
| 31 | * later XPath versions (2.0, 3.0, 3.1).</p> |
| 32 | * <p>The JAXP API is designed at one level to be object-model independent, but in other |
| 33 | * respects (especially in Java SE 9) it is designed specifically with DOM in mind. The Saxon |
| 34 | * implementation makes its own decisions about how to handle non-DOM nodes. Specifically, |
| 35 | * when an expression with return type {@link XPathConstants#NODE} is evaluated, Saxon |
| 36 | * returns the underlying node from the native object model; when the return type is given |
| 37 | * as {@link XPathConstants#NODESET}, the nodes are delivered as a DOM {@link org.w3c.dom.NodeList} |
| 38 | * if they are DOM nodes, or as a {@link java.util.List} otherwise.</p> |
| 39 | * <p>For an alternative XPath API, offering more complete access to Saxon capabilities, |
| 40 | * see {@link net.sf.saxon.s9api.XPathCompiler}.</p> |
| 41 | * <p>Note that the <code>XPathEvaluator</code> links to a Saxon {@link Configuration} |
| 42 | * object. By default a new <code>Configuration</code> is created automatically. In many |
| 43 | * applications, however, it is desirable to share a configuration. The default configuration |
| 44 | * is not schema aware. All source documents used by XPath expressions under this evaluator |
| 45 | * must themselves be built using the <code>Configuration</code> used by this evaluator.</p> |
| 46 | */ |
| 47 | |
| 48 | public class XPathEvaluator implements XPath { |
| 49 | |
| 50 | private Configuration config; |
| 51 | private JAXPXPathStaticContext staticContext; |
| 52 | |
| 53 | /** |
| 54 | * Default constructor. Creates an XPathEvaluator with Configuration appropriate |
| 55 | * to the version of the Saxon software being run. |
| 56 | */ |
| 57 | |
| 58 | public XPathEvaluator() { |
| 59 | this(Configuration.newConfiguration()); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Construct an XPathEvaluator with a specified configuration. |
| 64 | * |
| 65 | * @param config the configuration to be used. If schema-aware XPath expressions are to be used, |
| 66 | * this must be an EnterpriseConfiguration. |
| 67 | */ |
| 68 | public XPathEvaluator(/*@NotNull*/ Configuration config) { |
| 69 | this.config = config; |
| 70 | staticContext = new JAXPXPathStaticContext(config); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the Configuration used by this XPathEvaluator |
| 75 | * |
| 76 | * @return the Configuration used by this XPathEvaluator |
| 77 | */ |
| 78 | |
| 79 | public Configuration getConfiguration() { |
| 80 | return config; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the current static context. The caller can set properties of the returned |
| 85 | * static context to influence the way in which XPath expressions are compiled and |
| 86 | * evaluated; for example it is possible to set whether XPath 1.0 backwards compatibility |
| 87 | * is enabled or not. |
| 88 | * |
| 89 | * @return the static context |
| 90 | */ |
| 91 | |
| 92 | public JAXPXPathStaticContext getStaticContext() { |
| 93 | return staticContext; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | public void reset() { |
| 98 | staticContext = new JAXPXPathStaticContext(config); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Set the resolver for XPath variables |
| 103 | * |
| 104 | * @param xPathVariableResolver a resolver for variables |
| 105 | */ |
| 106 | |
| 107 | public void setXPathVariableResolver(XPathVariableResolver xPathVariableResolver) { |
| 108 | staticContext.setXPathVariableResolver(xPathVariableResolver); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the resolver for XPath variables |
| 113 | * |
| 114 | * @return the resolver, if one has been set |
| 115 | */ |
| 116 | public XPathVariableResolver getXPathVariableResolver() { |
| 117 | return staticContext.getXPathVariableResolver(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set the resolver for XPath functions |
| 122 | * |
| 123 | * @param xPathFunctionResolver a resolver for XPath function calls |
| 124 | */ |
| 125 | |
| 126 | public void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver) { |
| 127 | staticContext.setXPathFunctionResolver(xPathFunctionResolver); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the resolver for XPath functions |
| 132 | * |
| 133 | * @return the resolver, if one has been set |
| 134 | */ |
| 135 | |
| 136 | /*@Nullable*/ |
| 137 | public XPathFunctionResolver getXPathFunctionResolver() { |
| 138 | return staticContext.getXPathFunctionResolver(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Set the namespace context to be used. |
| 143 | * |
| 144 | * @param namespaceContext The namespace context |
| 145 | */ |
| 146 | |
| 147 | public void setNamespaceContext(NamespaceContext namespaceContext) { |
| 148 | staticContext.setNamespaceContext(namespaceContext); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get the namespace context, if one has been set using {@link #setNamespaceContext} |
| 153 | * |
| 154 | * @return the namespace context if set, or null otherwise |
| 155 | */ |
| 156 | |
| 157 | public NamespaceContext getNamespaceContext() { |
| 158 | return staticContext.getNamespaceContext(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Import a schema. This is possible only if Saxon-EE is being used, |
| 163 | * and if the Configuration is an EnterpriseConfiguration. Having imported a schema, the types |
| 164 | * defined in that schema become part of the static context. |
| 165 | * |
| 166 | * @param source A Source object identifying the schema document to be loaded |
| 167 | * @throws net.sf.saxon.type.SchemaException |
| 168 | * if the schema contained in this document is invalid |
| 169 | * @throws UnsupportedOperationException if the configuration is not schema-aware |
| 170 | */ |
| 171 | |
| 172 | public void importSchema(Source source) throws SchemaException { |
| 173 | staticContext.importSchema(source); |
| 174 | staticContext.setSchemaAware(true); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Compile an XPath 3.1 expression |
| 179 | * |
| 180 | * @param expr the XPath 3.1 expression to be compiled, as a string |
| 181 | * @return the compiled form of the expression |
| 182 | * @throws XPathExpressionException if there are any static errors in the expression. |
| 183 | * Note that references to undeclared variables are not treated as static errors, because |
| 184 | * variables are not pre-declared using this API. |
| 185 | */ |
| 186 | /*@NotNull*/ |
| 187 | public XPathExpression compile(String expr) throws XPathExpressionException { |
| 188 | if (expr == null) { |
| 189 | throw new NullPointerException("expr"); |
| 190 | } |
| 191 | try { |
| 192 | Executable exec = new Executable(getConfiguration()); |
| 193 | Expression exp = ExpressionTool.make(expr, staticContext, 0, -1, null); |
| 194 | ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); |
| 195 | final ContextItemStaticInfo contextItemType = getConfiguration().makeContextItemStaticInfo(Type.ITEM_TYPE, true); |
| 196 | exp = exp.typeCheck(visitor, contextItemType).optimize(visitor, contextItemType); |
| 197 | SlotManager map = staticContext.getConfiguration().makeSlotManager(); |
| 198 | ExpressionTool.allocateSlots(exp, 0, map); |
| 199 | XPathExpressionImpl xpe = new XPathExpressionImpl(exp, exec); |
| 200 | xpe.setStackFrameMap(map); |
| 201 | return xpe; |
| 202 | } catch (net.sf.saxon.trans.XPathException e) { |
| 203 | throw new XPathExpressionException(e); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Single-shot method to compile and execute an XPath 3.1 expression. |
| 209 | * |
| 210 | * @param expr The XPath 3.1 expression to be compiled and executed |
| 211 | * @param node The context node for evaluation of the expression. |
| 212 | * <p>This may be a NodeInfo object, representing a node in Saxon's native |
| 213 | * implementation of the data model, or it may be a node in any supported |
| 214 | * external object model: DOM, JDOM, DOM4J, or XOM, or any other model for |
| 215 | * which support has been configured in the Configuration. Note that the |
| 216 | * supporting libraries for the chosen model must be on the class path.</p> |
| 217 | * <p><b>Contrary to the interface specification, Saxon does not supply an empty |
| 218 | * document when the value is null. This is because XPath 2.0 allows the context |
| 219 | * item to be "absent" (null). So Saxon executes the XPath expression with the |
| 220 | * context item undefined.</b></p> |
| 221 | * @param qName The type of result required. For details, see |
| 222 | * {@link XPathExpressionImpl#evaluate(Object, javax.xml.namespace.QName)} |
| 223 | * @return the result of evaluating the expression, returned as described in |
| 224 | * {@link XPathExpressionImpl#evaluate(Object, javax.xml.namespace.QName)} |
| 225 | * @throws XPathExpressionException if any static or dynamic error occurs |
| 226 | * in evaluating the expression. |
| 227 | */ |
| 228 | |
| 229 | public Object evaluate(String expr, Object node, QName qName) throws XPathExpressionException { |
| 230 | XPathExpression exp = compile(expr); |
| 231 | return exp.evaluate(node, qName); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Single-shot method to compile an execute an XPath 2.0 expression, returning |
| 236 | * the result as a string. |
| 237 | * |
| 238 | * @param expr The XPath 2.0 expression to be compiled and executed |
| 239 | * @param node The context node for evaluation of the expression |
| 240 | * <p>This may be a NodeInfo object, representing a node in Saxon's native |
| 241 | * implementation of the data model, or it may be a node in any supported |
| 242 | * external object model: DOM, JDOM, DOM4J, or XOM, or any other model for |
| 243 | * which support has been configured in the Configuration. Note that the |
| 244 | * supporting libraries for the chosen model must be on the class path.</p> |
| 245 | * <p><b>Contrary to the interface specification, Saxon does not supply an empty |
| 246 | * document when the value is null. This is because XPath 2.0 allows the context |
| 247 | * item to be "absent" (null). So Saxon executes the XPath expression with the |
| 248 | * context item undefined.</b></p> |
| 249 | * @return the result of evaluating the expression, converted to a string as if |
| 250 | * by calling the XPath string() function |
| 251 | * @throws XPathExpressionException if any static or dynamic error occurs |
| 252 | * in evaluating the expression. |
| 253 | */ |
| 254 | |
| 255 | public String evaluate(String expr, Object node) throws XPathExpressionException { |
| 256 | XPathExpression exp = compile(expr); |
| 257 | return exp.evaluate(node); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Single-shot method to parse and build a source document, and |
| 262 | * compile an execute an XPath 2.0 expression, against that document |
| 263 | * |
| 264 | * @param expr The XPath 2.0 expression to be compiled and executed |
| 265 | * @param inputSource The source document: this will be parsed and built into a tree, |
| 266 | * and the XPath expression will be executed with the root node of the tree as the |
| 267 | * context node. |
| 268 | * @param qName The type of result required. For details, see |
| 269 | * {@link XPathExpressionImpl#evaluate(Object, javax.xml.namespace.QName)} |
| 270 | * @return the result of evaluating the expression, returned as described in |
| 271 | * {@link XPathExpressionImpl#evaluate(Object, javax.xml.namespace.QName)} |
| 272 | * @throws XPathExpressionException if any static or dynamic error occurs |
| 273 | * in evaluating the expression. |
| 274 | * @throws NullPointerException if any of the three arguments is null |
| 275 | */ |
| 276 | |
| 277 | public Object evaluate(/*@Nullable*/ String expr, /*@Nullable*/ InputSource inputSource, /*@Nullable*/ QName qName) throws XPathExpressionException { |
| 278 | if (expr == null) { |
| 279 | throw new NullPointerException("expr"); |
| 280 | } |
| 281 | if (inputSource == null) { |
| 282 | throw new NullPointerException("inputSource"); |
| 283 | } |
| 284 | if (qName == null) { |
| 285 | throw new NullPointerException("qName"); |
| 286 | } |
| 287 | XPathExpression exp = compile(expr); |
| 288 | return exp.evaluate(inputSource, qName); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Single-shot method to parse and build a source document, and |
| 293 | * compile an execute an XPath 2.0 expression, against that document, |
| 294 | * returning the result as a string |
| 295 | * |
| 296 | * @param expr The XPath 2.0 expression to be compiled and executed |
| 297 | * @param inputSource The source document: this will be parsed and built into a tree, |
| 298 | * and the XPath expression will be executed with the root node of the tree as the |
| 299 | * context node |
| 300 | * @return the result of evaluating the expression, converted to a string as |
| 301 | * if by calling the XPath string() function |
| 302 | * @throws XPathExpressionException if any static or dynamic error occurs |
| 303 | * in evaluating the expression. |
| 304 | * @throws NullPointerException if either of the two arguments is null |
| 305 | */ |
| 306 | |
| 307 | public String evaluate(/*@Nullable*/ String expr, /*@Nullable*/ InputSource inputSource) throws XPathExpressionException { |
| 308 | if (expr == null) { |
| 309 | throw new NullPointerException("expr"); |
| 310 | } |
| 311 | if (inputSource == null) { |
| 312 | throw new NullPointerException("inputSource"); |
| 313 | } |
| 314 | XPathExpression exp = compile(expr); |
| 315 | return exp.evaluate(inputSource); |
| 316 | } |
| 317 | |
| 318 | } |
| 319 |