Debian Codegraph @

net/sf/saxon/sxpath/XPathDynamicContext.java

raw symbols SCIP
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.expr.XPathContext;
11 import net.sf.saxon.expr.XPathContextMajor;
12 import net.sf.saxon.expr.instruct.SlotManager;
13 import net.sf.saxon.expr.parser.TypeChecker;
14 import net.sf.saxon.lib.CollectionFinder;
15 import net.sf.saxon.lib.CollectionURIResolver;
16 import net.sf.saxon.lib.UnfailingErrorListener;
17 import net.sf.saxon.lib.UnparsedTextURIResolver;
18 import net.sf.saxon.om.*;
19 import net.sf.saxon.trans.SaxonErrorCode;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.ManualIterator;
22 import net.sf.saxon.type.ItemType;
23 import net.sf.saxon.type.TypeHierarchy;
24 import net.sf.saxon.value.SequenceType;
25
26 import javax.xml.transform.ErrorListener;
27 import javax.xml.transform.URIResolver;
28
29 /**
30 * This object represents the dynamic XPath execution context for use in the free-standing Saxon XPath API.
31 * The dynamic context holds the context item and the values of external variables used by the XPath expression.
32 * <p>This object is always created via the method
33 * {@link net.sf.saxon.sxpath.XPathExpression#createDynamicContext(net.sf.saxon.om.Item)}</p>
34 */
35 public class XPathDynamicContext {
36
37 private ItemType contextItemType;
38 private XPathContextMajor contextObject;
39 private SlotManager stackFrameMap;
40
41 protected XPathDynamicContext(ItemType contextItemType, XPathContextMajor contextObject, SlotManager stackFrameMap) {
42 this.contextItemType = contextItemType;
43 this.contextObject = contextObject;
44 this.stackFrameMap = stackFrameMap;
45 }
46
47 /**
48 * Set the context item for evaluation of the XPath Expression
49 *
50 * @param item the context item
51 * @throws XPathException if the node is in a document that was built under the wrong configuration
52 */
53
54 public void setContextItem(Item<?> item) throws XPathException {
55 if (item instanceof NodeInfo) {
56 if (!((NodeInfo) item).getConfiguration().isCompatible(contextObject.getConfiguration())) {
57 throw new XPathException(
58 "Supplied node must be built using the same or a compatible Configuration",
59 SaxonErrorCode.SXXP0004);
60 }
61 }
62 TypeHierarchy th = contextObject.getConfiguration().getTypeHierarchy();
63 if (!contextItemType.matches(item, th)) {
64 throw new XPathException("Supplied context item does not match required context item type " +
65 contextItemType);
66 }
67 ManualIterator<Item<?>> iter = new ManualIterator<>(item);
68 contextObject.setCurrentIterator(iter);
69 }
70
71 /**
72 * Get the context item
73 *
74 * @return the context item if there is one, or null otherwise
75 */
76
77 public Item getContextItem() {
78 return contextObject.getContextItem();
79 }
80
81 /**
82 * Set the value of an external variable used within the XPath expression
83 *
84 * @param variable the object representing the variable.
85 * Note that setting the value of a variable does not modify the {@link XPathVariable}
86 * object itself, which means that this method is thread-safe.
87 * @param value The value of the variable.
88 * @throws XPathException if the supplied value does not conform to the required type of the
89 * variable; or if the supplied value contains a node that does not belong to this Configuration
90 * (or another Configuration that shares the same namePool)
91 */
92
93 public void setVariable(XPathVariable variable, Sequence<?> value) throws XPathException {
94 SequenceType requiredType = variable.getRequiredType();
95 if (requiredType != SequenceType.ANY_SEQUENCE) {
96 XPathException err = TypeChecker.testConformance(value, requiredType, contextObject);
97 if (err != null) {
98 throw err;
99 }
100 }
101 SequenceIterator<?> iter = value.iterate();
102 Item<?> item;
103 while ((item = iter.next()) != null) {
104 if (item instanceof NodeInfo && !((NodeInfo) item).getConfiguration().isCompatible(contextObject.getConfiguration())) {
105 throw new XPathException(
106 "Supplied node must be built using the same or a compatible Configuration",
107 SaxonErrorCode.SXXP0004);
108 }
109 }
110 int slot = variable.getLocalSlotNumber();
111 StructuredQName expectedName = slot >= stackFrameMap.getNumberOfVariables() ? null :
112 stackFrameMap.getVariableMap().get(slot);
113 if (!variable.getVariableQName().equals(expectedName)) {
114 throw new XPathException(
115 "Supplied XPathVariable is bound to the wrong slot: perhaps it was created using a different static context");
116 }
117 contextObject.setLocalVariable(slot, value);
118 }
119
120 /**
121 * Set an object that will be used to resolve URIs used in
122 * document(), etc. The URIResolver is set locally to this XPathContext
123 * object.
124 *
125 * @param resolver An object that implements the URIResolver interface, or
126 * null.
127 * @since 9.2. Changed in 9.6 to set the URIResolver locally.
128 */
129
130 public void setURIResolver(URIResolver resolver) {
131 contextObject.setURIResolver(resolver);
132 }
133
134 /**
135 * Get the URI resolver.
136 *
137 * @return the user-supplied URI resolver if there is one, or null otherwise. If no URIResolver
138 * has been set locally, the URIResolver in the Controller is returned; this in turn defaults
139 * to the URIResolver set in the Configuration.
140 * @since 9.2. Changed in 9.6 to use a local URIResolver.
141 */
142
143 public URIResolver getURIResolver() {
144 return contextObject.getURIResolver();
145 }
146
147 /**
148 * Set the CollectionURIResolver used for resolving collection URIs.
149 * Defaults to the CollectionURIResolver registered with the Configuration
150 *
151 * @param resolver the resolver for references to collections
152 * @since 9.4
153 * @deprecated since 9.7 - use {@link #setCollectionFinder(CollectionFinder)}
154 */
155
156 public void setCollectionURIResolver(CollectionURIResolver resolver) {
157 contextObject.getController().setCollectionURIResolver(resolver);
158 }
159
160 /**
161 * Get the CollectionURIResolver used for resolving references to collections.
162 * If none has been set on the Controller, returns the
163 * CollectionURIResolver registered with the Configuration
164 *
165 * @return the resolver for references to collections
166 * @since 9.4
167 * @deprecated since 9.7 - use {@link #getCollectionFinder()}
168 */
169
170 public CollectionURIResolver getCollectionURIResolver() {
171 return contextObject.getController().getCollectionURIResolver();
172 }
173
174 /**
175 * Get the collection finder associated with this configuration. This is used to dereference
176 * collection URIs used in the fn:collection and fn:uri-collection functions
177 *
178 * @return the CollectionFinder to be used
179 * @since 9.7
180 */
181
182
183 public CollectionFinder getCollectionFinder() {
184 return contextObject.getController().getCollectionFinder();
185 }
186
187 /**
188 * Set the collection finder associated with this configuration. This is used to dereference
189 * collection URIs used in the fn:collection and fn:uri-collection functions
190 *
191 * @param cf the CollectionFinder to be used
192 * @since 9.7
193 */
194
195 public void setCollectionFinder(CollectionFinder cf) {
196 contextObject.getController().setCollectionFinder(cf);
197 }
198
199
200 /**
201 * Set the error listener. The ErrorListener is set locally to this XPathContext
202 * object.
203 *
204 * @param listener the ErrorListener to be used
205 * @since 9.2. Changed in 9.6 to set the ErrorListener locally.
206 */
207
208 public void setErrorListener(ErrorListener listener) {
209 contextObject.setErrorListener(listener);
210 }
211
212 /**
213 * Get the error listener. If no ErrorListener
214 * has been set locally, the ErrorListener in the Controller is returned; this in turn defaults
215 * to the ErrorListener set in the Configuration.
216 *
217 * @return the ErrorListener in use. This will always be an UnfailingErrorListener,
218 * which is a Saxon subclass of ErrorListener that throws no exceptions.
219 * @since 9.2. Changed in 9.6 to use a local ErrorListener.
220 */
221
222 public UnfailingErrorListener getErrorListener() {
223 return contextObject.getErrorListener();
224 }
225
226
227 /**
228 * For system use: get the wrapped XPathContext object
229 *
230 * @return the underlying XPathContext object
231 */
232
233 public XPathContext getXPathContextObject() {
234 return contextObject;
235 }
236
237 /**
238 * Set an object that will be used to resolve URIs used in
239 * fn:unparsed-text() and related functions.
240 *
241 * @param resolver An object that implements the UnparsedTextURIResolver interface, or
242 * null.
243 * @since 9.5
244 */
245
246 public void setUnparsedTextURIResolver(UnparsedTextURIResolver resolver) {
247 contextObject.getController().setUnparsedTextURIResolver(resolver);
248 }
249
250 /**
251 * Get the URI resolver for unparsed text.
252 *
253 * @return the user-supplied unparsed text URI resolver if there is one, or the
254 * system-defined one otherwise
255 * @since 9.5
256 */
257
258 public UnparsedTextURIResolver getUnparsedTextURIResolver() {
259 return contextObject.getController().getUnparsedTextURIResolver();
260 }
261
262 /**
263 * Check that all external variables have been given a value
264 *
265 * @param stackFrameMap describes the stack frame
266 * @param numberOfExternals the number of variables that need to be supplied
267 * @throws XPathException if required variables have not been given a value
268 */
269
270 protected void checkExternalVariables(/*@NotNull*/ SlotManager stackFrameMap, int numberOfExternals) throws XPathException {
271 Sequence[] stack = contextObject.getStackFrame().getStackFrameValues();
272 for (int i = 0; i < numberOfExternals; i++) {
273 if (stack[i] == null) {
274 StructuredQName qname = stackFrameMap.getVariableMap().get(i);
275 throw new XPathException("No value has been supplied for variable $" + qname.getDisplayName());
276 }
277 }
278 }
279 }
280