Debian Codegraph @

net/sf/saxon/sxpath/XPathEvaluator.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.Configuration;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.PackageData;
13 import net.sf.saxon.expr.instruct.Executable;
14 import net.sf.saxon.expr.instruct.SlotManager;
15 import net.sf.saxon.expr.parser.*;
16 import net.sf.saxon.functions.FunctionLibrary;
17 import net.sf.saxon.functions.FunctionLibraryList;
18 import net.sf.saxon.functions.registry.ConstructorFunctionLibrary;
19 import net.sf.saxon.pattern.Pattern;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.ItemType;
22 import net.sf.saxon.type.Type;
23
24 /**
25 * This class provides a native Saxon API for free-standing evaluation of XPath expressions. Unlike the
26 * JAXP API offered by {@link net.sf.saxon.xpath.XPathEvaluator} it exposes Saxon classes and interfaces
27 * and thus provides a more strongly-typed interface with greater control over the detailed behaviour.
28 * <p>However, the preferred API for user applications calling XPath is the s9api {@link net.sf.saxon.s9api.XPathCompiler}
29 * interface.</p>
30 *
31 * @author Michael H. Kay
32 * @since 8.4
33 */
34
35 public class XPathEvaluator {
36
37 private XPathStaticContext staticContext;
38
39 /**
40 * Default constructor. Creates an XPathEvaluator with a default configuration and name pool.
41 * Note that any source documents used by an XPath expression under this XPathEvaluator must
42 * be built using the {@link Configuration} that is implicitly created by this constructor,
43 * which is accessible using the {@link #getConfiguration} method.
44 */
45
46 public XPathEvaluator() {
47 this(new Configuration());
48 }
49
50 /**
51 * Construct an XPathEvaluator with a specified configuration.
52 *
53 * @param config the configuration to be used. If the XPathEvaluator is
54 * to be used to run schema-aware XPath expressions this must be an instance
55 * of {@link com.saxonica.config.EnterpriseConfiguration}
56 */
57 public XPathEvaluator(Configuration config) {
58 staticContext = new IndependentContext(config);
59 }
60
61 /**
62 * Get the Configuration in use.
63 *
64 * @return the Saxon configuration
65 */
66
67 public Configuration getConfiguration() {
68 return staticContext.getConfiguration();
69 }
70
71 /**
72 * Set the static context for compiling XPath expressions. This provides more detailed control over the
73 * environment in which the expression is compiled, for example it allows namespace prefixes to
74 * be declared, variables to be bound and functions to be defined. For most purposes, the static
75 * context can be defined by providing and tailoring an instance of the {@link IndependentContext} class.
76 * Until this method is called, a default static context is used, in which no namespaces are defined
77 * other than the standard ones (xml, xslt, and saxon), and no variables or functions (other than the
78 * core XPath functions) are available.
79 *
80 * @param context the XPath static context
81 * <p>Setting a new static context clears any variables and namespaces that have previously been declared.</p>
82 */
83
84 public void setStaticContext(XPathStaticContext context) {
85 staticContext = context;
86 }
87
88 /**
89 * Get the current static context. This will always return a value; if no static context has been
90 * supplied by the user, the system will have created its own. A system-created static context
91 * will always be an instance of {@link IndependentContext}
92 *
93 * @return the static context object
94 */
95
96 public XPathStaticContext getStaticContext() {
97 return staticContext;
98 }
99
100 /**
101 * Prepare (compile) an XPath expression for subsequent evaluation.
102 *
103 * @param expression The XPath expression to be compiled, supplied as a string.
104 * @return an XPathExpression object representing the prepared expression
105 * @throws XPathException if the syntax of the expression is wrong, or if it references namespaces,
106 * variables, or functions that have not been declared.
107 */
108
109 public XPathExpression createExpression(String expression) throws XPathException {
110 Configuration config = getConfiguration();
111 Executable exec = new Executable(config);
112
113 boolean allowXPath30 = staticContext.getXPathVersion() >= 30;
114 exec.setTopLevelPackage(staticContext.getPackageData());
115 exec.setSchemaAware(staticContext.getPackageData().isSchemaAware());
116 exec.setHostLanguage(Configuration.XPATH);
117
118 // Make the function library that's available at run-time (e.g. for saxon:evaluate() and function-lookup()).
119 // This includes all user-defined functions regardless of which module they are in
120
121 FunctionLibrary userlib = exec.getFunctionLibrary();
122 FunctionLibraryList lib = new FunctionLibraryList();
123 lib.addFunctionLibrary(config.getXPath31FunctionSet());
124 lib.addFunctionLibrary(config.getBuiltInExtensionLibraryList());
125 lib.addFunctionLibrary(new ConstructorFunctionLibrary(config));
126 lib.addFunctionLibrary(config.getIntegratedFunctionLibrary());
127 config.addExtensionBinders(lib);
128 if (userlib != null) {
129 lib.addFunctionLibrary(userlib);
130 }
131 exec.setFunctionLibrary(lib);
132
133 Optimizer opt = config.obtainOptimizer();
134 Expression exp = ExpressionTool.make(expression, staticContext, 0, -1, null);
135 RetainedStaticContext rsc = staticContext.makeRetainedStaticContext();
136 exp.setRetainedStaticContext(rsc);
137 ExpressionVisitor visitor = ExpressionVisitor.make(staticContext);
138 ItemType contextItemType = staticContext.getRequiredContextItemType();
139 ContextItemStaticInfo cit = config.makeContextItemStaticInfo(contextItemType, true);
140 exp = exp.typeCheck(visitor, cit);
141 if (opt.isOptionSet(OptimizerOptions.MISCELLANEOUS)) {
142 exp = exp.optimize(visitor, cit);
143 }
144 if (opt.isOptionSet(OptimizerOptions.LOOP_LIFTING)) {
145 exp.setParentExpression(null);
146 exp = LoopLifter.process(exp, visitor, cit);
147 }
148 exp = postProcess(exp, visitor, cit);
149 exp.setRetainedStaticContext(rsc);
150 SlotManager map = staticContext.getStackFrameMap();
151 int numberOfExternalVariables = map.getNumberOfVariables();
152 ExpressionTool.allocateSlots(exp, numberOfExternalVariables, map);
153 XPathExpression xpe = new XPathExpression(staticContext, exp, exec);
154 xpe.setStackFrameMap(map, numberOfExternalVariables);
155 return xpe;
156 }
157
158 /**
159 * Customize the expression post-optimization. This method does nothing,
160 * but can be overridden in a subclass
161 * @param exp the expression after optimization
162 * @param visitor an expression visitor that includes static context information
163 * @param cit information about the context item type
164 */
165
166 protected Expression postProcess(Expression exp, ExpressionVisitor visitor, ContextItemStaticInfo cit) throws XPathException {
167 return exp;
168 }
169
170 /**
171 * Prepare (compile) an XSLT pattern for subsequent evaluation. The result is an XPathExpression
172 * object representing a (pseudo-) expression that when evaluated returns a boolean result: true
173 * if the context node matches the pattern, false if it does not.
174 *
175 * @param pattern the XSLT pattern to be compiled, supplied as a string
176 * @return an XPathExpression object representing the pattern, wrapped as an expression
177 * @throws XPathException if the syntax of the expression is wrong, or if it references namespaces,
178 * variables, or functions that have not been declared.
179 * @since 9.1
180 */
181
182 /*@NotNull*/
183 public XPathExpression createPattern(String pattern) throws XPathException {
184 Configuration config = getConfiguration();
185 Executable exec = new Executable(config);
186 Pattern pat = Pattern.make(pattern, staticContext, new PackageData(config));
187 ExpressionVisitor visitor = ExpressionVisitor.make(staticContext);
188 pat.typeCheck(visitor, config.makeContextItemStaticInfo(Type.NODE_TYPE, true));
189 SlotManager map = staticContext.getStackFrameMap();
190 int slots = map.getNumberOfVariables();
191 slots = pat.allocateSlots(map, slots);
192 //PatternSponsor sponsor = new PatternSponsor(pat);
193 XPathExpression xpe = new XPathExpression(staticContext, pat, exec);
194 xpe.setStackFrameMap(map, slots);
195 return xpe;
196 }
197
198
199 }
200