| 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.sxpath; |
| 9 | |
| 10 | import net.sf.saxon.Controller; |
| 11 | import net.sf.saxon.expr.Expression; |
| 12 | import net.sf.saxon.expr.StaticContext; |
| 13 | import net.sf.saxon.expr.XPathContextMajor; |
| 14 | import net.sf.saxon.expr.instruct.Executable; |
| 15 | import net.sf.saxon.expr.instruct.SlotManager; |
| 16 | import net.sf.saxon.om.Item; |
| 17 | import net.sf.saxon.om.SequenceIterator; |
| 18 | import net.sf.saxon.trans.XPathException; |
| 19 | import net.sf.saxon.type.ItemType; |
| 20 | import net.sf.saxon.type.TypeHierarchy; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | /** |
| 26 | * This class is a representation of an XPath Expression for use with the {@link XPathEvaluator} class. |
| 27 | * It is modelled on the XPath API defined in JAXP 1.3, but is free-standing, and is more strongly typed. |
| 28 | * <p>In Saxon 9.6, the methods that returned Object and did implicit conversion to a native Java object |
| 29 | * have been removed; all conversions must now be done explicitly.</p> |
| 30 | * |
| 31 | * @author Michael H. Kay |
| 32 | */ |
| 33 | |
| 34 | |
| 35 | public class XPathExpression { |
| 36 | |
| 37 | private StaticContext env; |
| 38 | private Expression expression; |
| 39 | private SlotManager stackFrameMap; |
| 40 | private Executable executable; |
| 41 | private int numberOfExternalVariables; |
| 42 | |
| 43 | /** |
| 44 | * The constructor is protected, to ensure that instances can only be |
| 45 | * created using the createExpression() or createPattern() method of XPathEvaluator |
| 46 | * |
| 47 | * @param env the static context of the expression |
| 48 | * @param exp the internal representation of the compiled expression |
| 49 | * @param exec the Executable associated with this expression |
| 50 | */ |
| 51 | |
| 52 | protected XPathExpression(StaticContext env, Expression exp, Executable exec) { |
| 53 | expression = exp; |
| 54 | this.env = env; |
| 55 | this.executable = exec; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the Executable associated with this expression |
| 60 | * @return the associated Executable |
| 61 | */ |
| 62 | |
| 63 | public Executable getExecutable() { |
| 64 | return executable; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Define the number of slots needed for local variables within the expression |
| 69 | * |
| 70 | * @param map the stack frame map identifying all the variables used by the expression, both |
| 71 | * those declared internally, and references to variables declared externally |
| 72 | * @param numberOfExternalVariables the number of slots in the stack frame allocated to |
| 73 | * externally-declared variables. These must be assigned a value before execution starts |
| 74 | */ |
| 75 | |
| 76 | protected void setStackFrameMap(SlotManager map, int numberOfExternalVariables) { |
| 77 | stackFrameMap = map; |
| 78 | this.numberOfExternalVariables = numberOfExternalVariables; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Create a dynamic context suitable for evaluating this expression, without supplying a context |
| 83 | * item |
| 84 | * |
| 85 | * @return an XPathDynamicContext object representing a suitable dynamic context. This will |
| 86 | * be initialized with a stack frame suitable for holding the variables used by the |
| 87 | * expression. |
| 88 | */ |
| 89 | |
| 90 | public XPathDynamicContext createDynamicContext() { |
| 91 | XPathContextMajor context = new XPathContextMajor(null, executable); |
| 92 | context.openStackFrame(stackFrameMap); |
| 93 | return new XPathDynamicContext(env.getRequiredContextItemType(), context, stackFrameMap); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * Create a dynamic context suitable for evaluating this expression |
| 99 | * |
| 100 | * @param contextItem the initial context item, which may be null if no |
| 101 | * context item is required, or if it is to be supplied later |
| 102 | * @return an XPathDynamicContext object representing a suitable dynamic context. This will |
| 103 | * be initialized with a stack frame suitable for holding the variables used by the |
| 104 | * expression. |
| 105 | * @throws XPathException if the context item does not match the required context item type |
| 106 | * set up in the static context |
| 107 | */ |
| 108 | |
| 109 | public XPathDynamicContext createDynamicContext(Item contextItem) throws XPathException { |
| 110 | checkContextItemType(contextItem); |
| 111 | XPathContextMajor context = new XPathContextMajor(contextItem, executable); |
| 112 | context.openStackFrame(stackFrameMap); |
| 113 | return new XPathDynamicContext(env.getRequiredContextItemType(), context, stackFrameMap); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Create a dynamic context suitable for evaluating this expression within an environment |
| 118 | * represented by an existing controller. The dynamic context will inherit properties of the |
| 119 | * Controller such as the current error listener, URI resolver, document pool, and implicit |
| 120 | * timezone. |
| 121 | * |
| 122 | * @param controller an existing controller. May be null, in which case the method is |
| 123 | * equivalent to calling {@link #createDynamicContext(net.sf.saxon.om.Item)} |
| 124 | * @param contextItem the initial context item, which may be null if no |
| 125 | * context item is required, or if it is to be supplied later |
| 126 | * @return an XPathDynamicContext object representing a suitable dynamic context. This will |
| 127 | * be initialized with a stack frame suitable for holding the variables used by the |
| 128 | * expression. |
| 129 | * @throws XPathException if the context item does not match the required context item type |
| 130 | * set up in the static context |
| 131 | * @since 9.2 |
| 132 | */ |
| 133 | |
| 134 | public XPathDynamicContext createDynamicContext(Controller controller, Item contextItem) |
| 135 | throws XPathException { |
| 136 | checkContextItemType(contextItem); |
| 137 | if (controller == null) { |
| 138 | return createDynamicContext(contextItem); |
| 139 | } else { |
| 140 | XPathContextMajor context = controller.newXPathContext(); |
| 141 | context.openStackFrame(stackFrameMap); |
| 142 | XPathDynamicContext dc = new XPathDynamicContext(env.getRequiredContextItemType(), context, stackFrameMap); |
| 143 | if (contextItem != null) { |
| 144 | dc.setContextItem(contextItem); |
| 145 | } |
| 146 | return dc; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private void checkContextItemType(Item contextItem) throws XPathException { |
| 151 | if (contextItem != null) { |
| 152 | ItemType type = env.getRequiredContextItemType(); |
| 153 | TypeHierarchy th = env.getConfiguration().getTypeHierarchy(); |
| 154 | if (!type.matches(contextItem, th)) { |
| 155 | throw new XPathException("Supplied context item does not match required context item type " + |
| 156 | type); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Execute the expression, returning the result as a {@link SequenceIterator}, whose members will be instances |
| 163 | * of the class {@link net.sf.saxon.om.Item} |
| 164 | * <p>Note that if evaluation of the expression fails with a dynamic error, this will generally |
| 165 | * be reported in the form of an exception thrown by the next() method of the {@link SequenceIterator}</p> |
| 166 | * |
| 167 | * @param context the XPath dynamic context |
| 168 | * @return an iterator over the result of the expression |
| 169 | * @throws XPathException if a dynamic error occurs during the expression evaluation (though in many |
| 170 | * cases, the error will only be detected when the iterator is actually used). |
| 171 | */ |
| 172 | |
| 173 | public SequenceIterator<?> iterate(XPathDynamicContext context) throws XPathException { |
| 174 | context.checkExternalVariables(stackFrameMap, numberOfExternalVariables); |
| 175 | return expression.iterate(context.getXPathContextObject()); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Execute the expression, returning the result as a List, whose members will be instances |
| 180 | * of the class {@link net.sf.saxon.om.Item} |
| 181 | * |
| 182 | * @param context the XPath dynamic context |
| 183 | * @return a list of items representing the result of the expression |
| 184 | * @throws XPathException if a dynamic error occurs while evaluation the expression |
| 185 | */ |
| 186 | |
| 187 | public List<Item<?>> evaluate(XPathDynamicContext context) throws XPathException { |
| 188 | List<Item<?>> list = new ArrayList<>(20); |
| 189 | expression.iterate(context.getXPathContextObject()).forEachOrFail(list::add); |
| 190 | return list; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Execute the expression, returning the result as a single {@link net.sf.saxon.om.Item} |
| 195 | * If the result of the expression is a sequence containing more than one item, items after |
| 196 | * the first are discarded. If the result is an empty sequence, the method returns null. |
| 197 | * |
| 198 | * @param context the XPath dynamic context |
| 199 | * @return the first item in the result of the expression, or null to indicate that the result |
| 200 | * is an empty sequence |
| 201 | * @throws XPathException if a dynamic error occurs during the expression evaluation |
| 202 | */ |
| 203 | |
| 204 | |
| 205 | /*@Nullable*/ |
| 206 | public Item evaluateSingle(XPathDynamicContext context) throws XPathException { |
| 207 | SequenceIterator<?> iter = expression.iterate(context.getXPathContextObject()); |
| 208 | Item result = iter.next(); |
| 209 | iter.close(); |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Evaluate the expression, returning its effective boolean value |
| 215 | * |
| 216 | * @param context the XPath dynamic context |
| 217 | * @return the effective boolean value of the result, as defined by the fn:boolean() function |
| 218 | * @throws XPathException if a dynamic error occurs, or if the result is a value for which |
| 219 | * no effective boolean value is defined |
| 220 | */ |
| 221 | |
| 222 | public boolean effectiveBooleanValue(XPathDynamicContext context) throws XPathException { |
| 223 | return expression.effectiveBooleanValue(context.getXPathContextObject()); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Low-level method to get the internal Saxon expression object. This exposes a wide range of |
| 228 | * internal methods that may be needed by specialized applications, and allows greater control |
| 229 | * over the dynamic context for evaluating the expression. |
| 230 | * |
| 231 | * @return the underlying Saxon expression object. |
| 232 | */ |
| 233 | |
| 234 | public Expression getInternalExpression() { |
| 235 | return expression; |
| 236 | } |
| 237 | |
| 238 | } |
| 239 |