Debian Codegraph @

net/sf/saxon/expr/RootExpression.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.expr;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.parser.*;
12 import net.sf.saxon.om.AxisInfo;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.pattern.AnyNodeTest;
17 import net.sf.saxon.pattern.NodeKindTest;
18 import net.sf.saxon.pattern.NodeTestPattern;
19 import net.sf.saxon.pattern.Pattern;
20 import net.sf.saxon.trace.ExpressionPresenter;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.tree.iter.SingletonIterator;
23 import net.sf.saxon.type.*;
24
25
26 /**
27 * An expression whose value is always a set of nodes containing a single node,
28 * the document root. This corresponds to the XPath Expression "/", including the implicit
29 * "/" at the start of a path expression with a leading "/".
30 */
31
32 public class RootExpression extends Expression {
33
34
35 private boolean contextMaybeUndefined = true;
36
37 public RootExpression() {
38 }
39
40 /**
41 * Type-check the expression.
42 */
43
44 /*@NotNull*/
45 public Expression typeCheck(ExpressionVisitor visitor, /*@Nullable*/ ContextItemStaticInfo contextInfo) throws XPathException {
46 if (contextInfo == null || contextInfo.getItemType() == null || contextInfo.getItemType().equals(ErrorType.getInstance())) {
47 XPathException err = new XPathException(noContextMessage() + ": the context item is absent");
48 err.setErrorCode("XPDY0002");
49 err.setIsTypeError(true);
50 err.setLocation(getLocation());
51 throw err;
52 } else {
53 contextMaybeUndefined = contextInfo.isPossiblyAbsent();
54 }
55 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
56 if (th.isSubType(contextInfo.getItemType(), NodeKindTest.DOCUMENT)) {
57 // this rewrite is important for streamability analysis
58 ContextItemExpression cie = new ContextItemExpression();
59 cie.setStaticInfo(contextInfo);
60 return cie;
61 }
62
63 int relation = th.relationship(contextInfo.getItemType(), AnyNodeTest.getInstance());
64 if (relation == TypeHierarchy.DISJOINT) {
65 XPathException err = new XPathException(noContextMessage() + ": the context item is not a node");
66 err.setErrorCode("XPTY0020");
67 err.setIsTypeError(true);
68 err.setLocation(getLocation());
69 throw err;
70 }
71 return this;
72 }
73
74 /**
75 * Perform optimisation of an expression and its subexpressions.
76 * <p>This method is called after all references to functions and variables have been resolved
77 * to the declaration of the function or variable, and after all type checking has been done.</p>
78 *
79 * @param visitor an expression visitor
80 * @param contextItemType the static type of "." at the point where this expression is invoked.
81 * The parameter is set to null if it is known statically that the context item will be undefined.
82 * If the type of the context item is not known statically, the argument is set to
83 * {@link net.sf.saxon.type.Type#ITEM_TYPE}
84 * @return the original expression, rewritten if appropriate to optimize execution
85 * @throws net.sf.saxon.trans.XPathException if an error is discovered during this phase
86 * (typically a type error)
87 */
88
89 /*@NotNull*/
90 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
91 // repeat the check: in XSLT insufficient information is available the first time
92 return typeCheck(visitor, contextItemType);
93 }
94
95 public int computeSpecialProperties() {
96 return StaticProperty.ORDERED_NODESET |
97 StaticProperty.CONTEXT_DOCUMENT_NODESET |
98 StaticProperty.SINGLE_DOCUMENT_NODESET |
99 StaticProperty.NO_NODES_NEWLY_CREATED;
100 }
101
102 /**
103 * Ask whether there is a possibility that the context item will be undefined
104 *
105 * @return true if this is a possibility
106 */
107
108 public boolean isContextPossiblyUndefined() {
109 return contextMaybeUndefined;
110 }
111
112
113 /**
114 * Customize the error message on type checking
115 */
116
117 protected String noContextMessage() {
118 return "Leading '/' selects nothing";
119 }
120
121 /**
122 * Is this expression the same as another expression?
123 */
124
125 public boolean equals(Object other) {
126 return other instanceof RootExpression;
127 }
128
129 /**
130 * Specify that the expression returns a singleton
131 */
132
133 public final int computeCardinality() {
134 return StaticProperty.EXACTLY_ONE;
135 }
136
137 /**
138 * Determine the data type of the items returned by this expression
139 *
140 * @return Type.NODE
141 */
142
143 /*@NotNull*/
144 public ItemType getItemType() {
145 return NodeKindTest.DOCUMENT;
146 }
147
148
149 /**
150 * Get the static type of the expression as a UType, following precisely the type
151 * inference rules defined in the XSLT 3.0 specification.
152 *
153 * @return the static item type of the expression according to the XSLT 3.0 defined rules
154 * @param contextItemType
155 */
156 @Override
157 public UType getStaticUType(UType contextItemType) {
158 return UType.DOCUMENT;
159 }
160
161
162 /**
163 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
164 * This method indicates which of these methods is provided directly. The other methods will always be available
165 * indirectly, using an implementation that relies on one of the other methods.
166 *
167 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
168 * {@link #PROCESS_METHOD}
169 */
170 @Override
171 public int getImplementationMethod() {
172 return EVALUATE_METHOD;
173 }
174
175 /**
176 * get HashCode for comparing two expressions
177 */
178
179 public int computeHashCode() {
180 return "RootExpression".hashCode();
181 }
182
183 /**
184 * Return the first element selected by this Expression
185 *
186 * @param context The evaluation context
187 * @return the NodeInfo of the first selected element, or null if no element
188 * is selected
189 */
190
191 /*@Nullable*/
192 public NodeInfo getNode(XPathContext context) throws XPathException {
193 Item current = context.getContextItem();
194 if (current == null) {
195 dynamicError("Finding root of tree: the context item is absent", "XPDY0002", context);
196 }
197 if (current instanceof NodeInfo) {
198 NodeInfo doc = ((NodeInfo) current).getRoot();
199 if (doc.getNodeKind() != Type.DOCUMENT) {
200 dynamicError("The root of the tree containing the context item is not a document node", "XPDY0050", context);
201 }
202 return doc;
203 }
204 typeError("Finding root of tree: the context item is not a node", "XPTY0020", context);
205 // dummy return; we never get here
206 return null;
207 }
208
209 /**
210 * Determine which aspects of the context the expression depends on. The result is
211 * a bitwise-or'ed value composed from constants such as StaticProperty.VARIABLES and
212 * StaticProperty.CURRENT_NODE
213 */
214
215 public int getIntrinsicDependencies() {
216 return StaticProperty.DEPENDS_ON_CONTEXT_DOCUMENT;
217 }
218
219 /**
220 * Copy an expression. This makes a deep copy.
221 *
222 * @return the copy of the original expression
223 * @param rebindings
224 */
225
226 /*@NotNull*/
227 public Expression copy(RebindingMap rebindings) {
228 RootExpression exp = new RootExpression();
229 ExpressionTool.copyLocationInfo(this, exp);
230 return exp;
231 }
232
233 /**
234 * Convert this expression to an equivalent XSLT pattern
235 *
236 * @param config the Saxon configuration
237 * @return the equivalent pattern
238 * @throws net.sf.saxon.trans.XPathException
239 * if conversion is not possible
240 */
241 @Override
242 public Pattern toPattern(Configuration config) throws XPathException {
243 return new NodeTestPattern(NodeKindTest.DOCUMENT);
244 }
245
246 /**
247 * Add a representation of this expression to a PathMap. The PathMap captures a map of the nodes visited
248 * by an expression in a source tree.
249 *
250 * @param pathMap the PathMap to which the expression should be added
251 * @param pathMapNodeSet
252 * @return the pathMapNode representing the focus established by this expression, in the case where this
253 * expression is the first operand of a path expression or filter expression
254 */
255
256 public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) {
257 if (pathMapNodeSet == null) {
258 ContextItemExpression cie = new ContextItemExpression();
259 ExpressionTool.copyLocationInfo(this, cie);
260 pathMapNodeSet = new PathMap.PathMapNodeSet(pathMap.makeNewRoot(cie));
261 }
262 return pathMapNodeSet.createArc(AxisInfo.ANCESTOR_OR_SELF, NodeKindTest.DOCUMENT);
263 }
264
265 /**
266 * The toString() method for an expression attempts to give a representation of the expression
267 * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath.
268 * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax
269 */
270
271 public String toString() {
272 return "(/)";
273 }
274
275 @Override
276 public String getExpressionName() {
277 return "root";
278 }
279
280 /**
281 * Diagnostic print of expression structure. The abstract expression tree
282 * is written to the supplied output destination.
283 */
284
285 public void export(ExpressionPresenter destination) throws XPathException {
286 destination.startElement("root", this);
287 destination.endElement();
288 }
289
290
291 /**
292 * Evaluate the expression in a given context to return an iterator
293 *
294 * @param context the evaluation context
295 */
296
297 /*@NotNull*/
298 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
299 return SingletonIterator.makeIterator(getNode(context));
300 }
301
302 public NodeInfo evaluateItem(XPathContext context) throws XPathException {
303 return getNode(context);
304 }
305
306 public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
307 return getNode(context) != null;
308 }
309
310
311 /**
312 * Get the (partial) name of a class that supports streaming of this kind of expression
313 *
314 * @return the partial name of a class that can be instantiated to provide streaming support in Saxon-EE,
315 * or null if there is no such class
316 */
317 @Override
318 public String getStreamerName() {
319 return "RootExpression";
320 }
321 }
322